|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package googlecloudmonitoringreceiver |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.opentelemetry.io/collector/component" |
| 14 | + "go.opentelemetry.io/collector/confmap/confmaptest" |
| 15 | + "go.opentelemetry.io/collector/receiver/scraperhelper" |
| 16 | + |
| 17 | + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudmonitoringreceiver/internal/metadata" |
| 18 | +) |
| 19 | + |
| 20 | +func TestLoadConfig(t *testing.T) { |
| 21 | + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 22 | + require.NoError(t, err) |
| 23 | + factory := NewFactory() |
| 24 | + cfg := factory.CreateDefaultConfig() |
| 25 | + |
| 26 | + sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String()) |
| 27 | + require.NoError(t, err) |
| 28 | + require.NoError(t, sub.Unmarshal(cfg)) |
| 29 | + |
| 30 | + assert.Equal(t, |
| 31 | + &Config{ |
| 32 | + ControllerConfig: scraperhelper.ControllerConfig{ |
| 33 | + CollectionInterval: 120 * time.Second, |
| 34 | + InitialDelay: 1 * time.Second, |
| 35 | + }, |
| 36 | + Region: "us-central1", |
| 37 | + ProjectID: "my-project-id", |
| 38 | + ServiceAccountKey: "path/to/service_account.json", |
| 39 | + Services: []Service{ |
| 40 | + { |
| 41 | + ServiceName: "compute", |
| 42 | + Delay: 60 * time.Second, |
| 43 | + MetricName: "compute.googleapis.com/instance/cpu/usage_time", |
| 44 | + }, |
| 45 | + { |
| 46 | + ServiceName: "connectors", |
| 47 | + Delay: 60 * time.Second, |
| 48 | + MetricName: "connectors.googleapis.com/flex/instance/cpu/usage_time", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + cfg, |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +func TestValidateService(t *testing.T) { |
| 57 | + testCases := map[string]struct { |
| 58 | + service Service |
| 59 | + requireError bool |
| 60 | + }{ |
| 61 | + "Valid Service": { |
| 62 | + Service{ |
| 63 | + ServiceName: "service_name", |
| 64 | + Delay: 0, |
| 65 | + }, false}, |
| 66 | + "Empty ServiceName": { |
| 67 | + Service{ |
| 68 | + ServiceName: "", |
| 69 | + Delay: 0, |
| 70 | + }, true}, |
| 71 | + "Negative Delay": { |
| 72 | + Service{ |
| 73 | + ServiceName: "service_name", |
| 74 | + Delay: -1, |
| 75 | + }, true}, |
| 76 | + } |
| 77 | + |
| 78 | + for name, testCase := range testCases { |
| 79 | + t.Run(name, func(t *testing.T) { |
| 80 | + err := testCase.service.Validate() |
| 81 | + if testCase.requireError { |
| 82 | + require.Error(t, err) |
| 83 | + } else { |
| 84 | + require.NoError(t, err) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestValidateConfig(t *testing.T) { |
| 91 | + validService := Service{ |
| 92 | + ServiceName: "service_name", |
| 93 | + Delay: 0 * time.Second, |
| 94 | + } |
| 95 | + |
| 96 | + testCases := map[string]struct { |
| 97 | + services []Service |
| 98 | + collectionInterval time.Duration |
| 99 | + requireError bool |
| 100 | + }{ |
| 101 | + "Valid Config": {[]Service{validService}, 60 * time.Second, false}, |
| 102 | + "Empty Services": {nil, 60 * time.Second, true}, |
| 103 | + "Invalid Service in Services": {[]Service{{}}, 60 * time.Second, true}, |
| 104 | + "Invalid Collection Interval": {[]Service{validService}, 0 * time.Second, true}, |
| 105 | + } |
| 106 | + |
| 107 | + for name, testCase := range testCases { |
| 108 | + t.Run(name, func(t *testing.T) { |
| 109 | + cfg := &Config{ |
| 110 | + ControllerConfig: scraperhelper.ControllerConfig{ |
| 111 | + CollectionInterval: testCase.collectionInterval, |
| 112 | + }, |
| 113 | + Services: testCase.services, |
| 114 | + } |
| 115 | + |
| 116 | + err := cfg.Validate() |
| 117 | + if testCase.requireError { |
| 118 | + require.Error(t, err) |
| 119 | + } else { |
| 120 | + require.NoError(t, err) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments