1
16
17 package v1
18
19 import (
20 "testing"
21
22 "k8s.io/apimachinery/pkg/util/validation/field"
23 )
24
25 func TestValidateTracingConfiguration(t *testing.T) {
26 samplingRate := int32(12378)
27 negativeRate := int32(-1)
28 tooHighRate := int32(1000001)
29 validEndpoint := "localhost:4317"
30 dnsEndpoint := "dns://google.com:4317"
31 unixEndpoint := "unix://path/to/socket"
32 invalidURL := "dn%2s://localhost:4317"
33 httpEndpoint := "http://localhost:4317"
34 testcases := []struct {
35 name string
36 expectError bool
37 contents *TracingConfiguration
38 }{
39 {
40 name: "sampling-rate-valid",
41 expectError: false,
42 contents: &TracingConfiguration{
43 SamplingRatePerMillion: &samplingRate,
44 },
45 },
46 {
47 name: "sampling-rate-negative",
48 expectError: true,
49 contents: &TracingConfiguration{
50 SamplingRatePerMillion: &negativeRate,
51 },
52 },
53 {
54 name: "sampling-rate-negative",
55 expectError: true,
56 contents: &TracingConfiguration{
57 SamplingRatePerMillion: &tooHighRate,
58 },
59 },
60 {
61 name: "default Endpoint",
62 expectError: false,
63 contents: &TracingConfiguration{
64 Endpoint: &validEndpoint,
65 },
66 },
67 {
68 name: "dns Endpoint",
69 expectError: false,
70 contents: &TracingConfiguration{
71 Endpoint: &dnsEndpoint,
72 },
73 },
74 {
75 name: "unix Endpoint",
76 expectError: false,
77 contents: &TracingConfiguration{
78 Endpoint: &unixEndpoint,
79 },
80 },
81 {
82 name: "invalid Endpoint",
83 expectError: true,
84 contents: &TracingConfiguration{
85 Endpoint: &httpEndpoint,
86 },
87 },
88 {
89 name: "invalid url",
90 expectError: true,
91 contents: &TracingConfiguration{
92 Endpoint: &invalidURL,
93 },
94 },
95 }
96
97 for _, tc := range testcases {
98 t.Run(tc.name, func(t *testing.T) {
99 errs := ValidateTracingConfiguration(tc.contents, nil, field.NewPath("tracing"))
100 if !tc.expectError && len(errs) != 0 {
101 t.Errorf("Calling ValidateTracingConfiguration expected no error, got %v", errs)
102 } else if tc.expectError && len(errs) == 0 {
103 t.Errorf("Calling ValidateTracingConfiguration expected error, got no error")
104 }
105 })
106 }
107 }
108
View as plain text