-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_test.go
219 lines (164 loc) · 6.21 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2021 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main_test
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
adapter "github.com/newrelic/newrelic-k8s-metrics-adapter"
"github.com/newrelic/newrelic-k8s-metrics-adapter/internal/provider/cache"
)
//nolint:paralleltest // We manipulate environment variables here which are global.
func Test_Run_reads_API_key_and_cluster_name_from_environment_variable(t *testing.T) {
setenv(t, adapter.NewRelicAPIKeyEnv, "foo")
setenv(t, adapter.ClusterNameEnv, "bar")
withoutGlobalMetricsRegistry(t)
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("accountID: 1"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
flags := []string{"--cert-dir=" + t.TempDir(), "--secure-port=0", "--config-file=" + configPath}
err := adapter.Run(testContext(t), flags)
if err == nil {
t.Fatalf("Expected error running adapter")
}
expectedError := "failed to get delegated authentication kubeconfig"
if !strings.Contains(err.Error(), expectedError) {
t.Fatalf("Expected error %q, got: %v", expectedError, err)
}
}
func Test_Run_does_not_return_error_when_help_flag_is_specified(t *testing.T) {
t.Parallel()
if err := adapter.Run(testContext(t), []string{"--help"}); err != nil {
t.Fatalf("Unexpected error running adapter: %v", err)
}
}
//nolint:funlen,cyclop // Just many test cases.
func Test_Run_fails_when(t *testing.T) {
t.Parallel()
t.Run("config_file_is_not_readable", func(t *testing.T) {
t.Parallel()
flags := []string{"--cert-dir=" + t.TempDir(), "--config-file=" + t.TempDir()}
if err := adapter.Run(testContext(t), flags); err == nil {
t.Fatalf("Expected error running adapter")
}
})
t.Run("config_file_is_not_valid_YAML", func(t *testing.T) {
t.Parallel()
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("badKey: 1"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
flags := []string{"--cert-dir=" + t.TempDir(), "--config-file=" + configPath}
if err := adapter.Run(testContext(t), flags); err == nil {
t.Fatalf("Expected error running adapter")
}
})
//nolint:paralleltest // We manipulate environment variables here which are global.
t.Run("initializing_NewRelic_client_fails", func(t *testing.T) {
unsetenv(t, adapter.NewRelicAPIKeyEnv)
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("accountID: 1"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
flags := []string{"--cert-dir=" + t.TempDir(), "--config-file=" + configPath}
if err := adapter.Run(testContext(t), flags); err == nil {
t.Fatalf("Expected error running adapter")
}
})
//nolint:paralleltest // We manipulate environment variables here which are global.
t.Run("initializing_direct_metric_provider_fails", func(t *testing.T) {
setenv(t, adapter.NewRelicAPIKeyEnv, "foo")
unsetenv(t, adapter.ClusterNameEnv)
withoutGlobalMetricsRegistry(t)
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("accountID: 0"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
flags := []string{"--cert-dir=" + t.TempDir(), "--config-file=" + configPath}
if err := adapter.Run(testContext(t), flags); err == nil {
t.Fatalf("Expected error running adapter")
}
})
t.Run("parsing_given_flags_fails", func(t *testing.T) {
t.Parallel()
if err := adapter.Run(testContext(t), []string{"--secure-port=foo"}); err == nil {
t.Fatalf("Expected error running adapter")
}
})
//nolint:paralleltest // We manipulate environment variables here which are global.
t.Run("invalid_region_is_configured", func(t *testing.T) {
setenv(t, adapter.NewRelicAPIKeyEnv, "foo")
setenv(t, adapter.ClusterNameEnv, "bar")
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("accountID: 1\nregion: BAR"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
flags := []string{"--cert-dir=" + t.TempDir(), "--config-file=" + configPath}
if err := adapter.Run(testContext(t), flags); err == nil {
t.Fatalf("Expected error running adapter")
}
})
//nolint:paralleltest // We manipulate environment variables here which are global.
t.Run("initializing_cache_provider_fails", func(t *testing.T) {
setenv(t, adapter.NewRelicAPIKeyEnv, "foo")
setenv(t, adapter.ClusterNameEnv, "bar")
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte("accountID: 1\ncacheTTLSeconds: 10\n"), 0o600); err != nil {
t.Fatalf("Error writing test config file: %v", err)
}
expectedError := "sample error"
reg := legacyregistry.Register
legacyregistry.Register = func(m metrics.Registerable) error {
t.Log(m.FQName())
if strings.Contains(m.FQName(), cache.MetricsSubsystem) {
return fmt.Errorf(expectedError)
}
return nil
}
t.Cleanup(func() {
legacyregistry.Register = reg
})
err := adapter.Run(testContext(t), []string{"--cert-dir=" + t.TempDir(), "--config-file=" + configPath})
if err == nil {
t.Fatalf("Expected error running adapter")
}
if !strings.Contains(err.Error(), expectedError) {
t.Fatalf("Expected error to contain %q, got %q", expectedError, err.Error())
}
})
}
func withoutGlobalMetricsRegistry(t *testing.T) {
t.Helper()
reg := legacyregistry.Register
legacyregistry.Register = nil
t.Cleanup(func() {
legacyregistry.Register = reg
})
}
func testContext(t *testing.T) context.Context {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
t.Cleanup(func() {
cancel()
})
return ctx
}
func setenv(t *testing.T, key, value string) {
t.Helper()
if err := os.Setenv(key, value); err != nil {
t.Fatalf("Setting environment variable %q to %q: %v", key, value, err)
}
}
func unsetenv(t *testing.T, key string) {
t.Helper()
if err := os.Unsetenv(key); err != nil {
t.Fatalf("Unsetting environment variable %q: %v", key, err)
}
}