Debian Codegraph @

pkg/server/options/tracing_test.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package options
18
19 import (
20 "fmt"
21 "io/ioutil"
22 "os"
23 "reflect"
24 "strings"
25 "testing"
26
27 tracingapi "k8s.io/component-base/tracing/api/v1"
28 )
29
30 var (
31 localhost = "localhost:4317"
32 ipAddress = "127.0.0.1:4317"
33 samplingRate = int32(12345)
34 )
35
36 func strptr(s string) *string {
37 return &s
38 }
39
40 func TestValidateTracingOptions(t *testing.T) {
41 testcases := []struct {
42 name string
43 expectError bool
44 contents *TracingOptions
45 }{
46 {
47 name: "nil-valid",
48 expectError: false,
49 },
50 {
51 name: "empty-valid",
52 expectError: false,
53 contents: &TracingOptions{},
54 },
55 {
56 name: "path-valid",
57 expectError: false,
58 contents: &TracingOptions{ConfigFile: "/"},
59 },
60 {
61 name: "path-invalid",
62 expectError: true,
63 contents: &TracingOptions{ConfigFile: "/path/doesnt/exist"},
64 },
65 }
66
67 for _, tc := range testcases {
68 t.Run(tc.name, func(t *testing.T) {
69 errs := tc.contents.Validate()
70 if !tc.expectError && len(errs) != 0 {
71 t.Errorf("Calling Validate expected no error, got %v", errs)
72 } else if tc.expectError && len(errs) == 0 {
73 t.Errorf("Calling Validate expected error, got no error")
74 }
75 })
76 }
77 }
78
79 func TestReadTracingConfiguration(t *testing.T) {
80 testcases := []struct {
81 name string
82 contents string
83 createFile bool
84 expectedResult *tracingapi.TracingConfiguration
85 expectedError *string
86 }{
87 {
88 name: "empty",
89 createFile: true,
90 contents: ``,
91 expectedResult: &tracingapi.TracingConfiguration{},
92 expectedError: nil,
93 },
94 {
95 name: "absent",
96 createFile: false,
97 contents: ``,
98 expectedResult: nil,
99 expectedError: strptr("unable to read tracing configuration from \"test-tracing-config-absent\": open test-tracing-config-absent: no such file or directory"),
100 },
101 {
102 name: "duplicate field error; strict validation",
103 createFile: true,
104 contents: `
105 apiVersion: apiserver.config.k8s.io/v1alpha1
106 kind: TracingConfiguration
107 endpoint: localhost:4317
108 endpoint: localhost:4318
109 samplingRatePerMillion: 12345
110 `,
111 expectedResult: nil,
112 expectedError: strptr("unable to decode tracing configuration data: strict decoding error"),
113 },
114 {
115 name: "unknown field error; strict validation",
116 createFile: true,
117 contents: `
118 apiVersion: apiserver.config.k8s.io/v1alpha1
119 kind: TracingConfiguration
120 foo: bar
121 endpoint: localhost:4318
122 samplingRatePerMillion: 12345
123 `,
124 expectedResult: nil,
125 expectedError: strptr("unable to decode tracing configuration data: strict decoding error"),
126 },
127 {
128 name: "v1alpha1",
129 createFile: true,
130 contents: `
131 apiVersion: apiserver.config.k8s.io/v1alpha1
132 kind: TracingConfiguration
133 endpoint: localhost:4317
134 samplingRatePerMillion: 12345
135 `,
136 expectedResult: &tracingapi.TracingConfiguration{
137 Endpoint: &localhost,
138 SamplingRatePerMillion: &samplingRate,
139 },
140 expectedError: nil,
141 },
142 {
143 name: "ip address",
144 createFile: true,
145 contents: `
146 apiVersion: apiserver.config.k8s.io/v1alpha1
147 kind: TracingConfiguration
148 endpoint: 127.0.0.1:4317
149 `,
150 expectedResult: &tracingapi.TracingConfiguration{
151 Endpoint: &ipAddress,
152 },
153 expectedError: nil,
154 },
155 {
156 name: "wrong_type",
157 createFile: true,
158 contents: `
159 apiVersion: apps/v1
160 kind: DaemonSet
161 metadata:
162 name: agent
163 spec:
164 selector:
165 matchLabels:
166 k8s-app: agent
167 template:
168 metadata:
169 labels:
170 k8s-app: agent
171 spec:
172 containers:
173 - image: registry.k8s.io/busybox
174 name: agent
175 `,
176 expectedResult: nil,
177 expectedError: strptr("unable to decode tracing configuration data: no kind \"DaemonSet\" is registered for version \"apps/v1\" in scheme"),
178 },
179 }
180
181 for _, tc := range testcases {
182 t.Run(tc.name, func(t *testing.T) {
183 proxyConfig := fmt.Sprintf("test-tracing-config-%s", tc.name)
184 if tc.createFile {
185 f, err := ioutil.TempFile("", proxyConfig)
186 if err != nil {
187 t.Fatal(err)
188 }
189 defer os.Remove(f.Name())
190 if err := ioutil.WriteFile(f.Name(), []byte(tc.contents), os.FileMode(0755)); err != nil {
191 t.Fatal(err)
192 }
193 proxyConfig = f.Name()
194 }
195 config, err := ReadTracingConfiguration(proxyConfig)
196 if err == nil && tc.expectedError != nil {
197 t.Errorf("calling ReadTracingConfiguration expected error: %s, did not get it", *tc.expectedError)
198 }
199 if err != nil && tc.expectedError == nil {
200 t.Errorf("unexpected error calling ReadTracingConfiguration got: %#v", err)
201 }
202 if err != nil && tc.expectedError != nil && !strings.HasPrefix(err.Error(), *tc.expectedError) {
203 t.Errorf("calling ReadTracingConfiguration expected error: %s, got %#v", *tc.expectedError, err)
204 }
205 if !reflect.DeepEqual(config, tc.expectedResult) {
206 t.Errorf("problem with configuration returned from ReadTracingConfiguration expected: %#v, got: %#v", tc.expectedResult, config)
207 }
208 })
209 }
210 }