-
Notifications
You must be signed in to change notification settings - Fork 331
/
opencensus_exporter_test.go
190 lines (175 loc) · 4.7 KB
/
opencensus_exporter_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
/*
Copyright 2020 The Knative Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"crypto/tls"
"fmt"
"net"
"os"
"path/filepath"
"testing"
"contrib.go.opencensus.io/exporter/ocagent"
"github.com/google/go-cmp/cmp"
"go.opencensus.io/stats/view"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
logtesting "knative.dev/pkg/logging/testing"
)
func TestOpenCensusConfig(t *testing.T) {
cert, err := os.ReadFile(filepath.Join("testdata", "client-cert.pem"))
if err != nil {
t.Fatal("Couldn't find testdata/client-cert.pem:", err)
}
key, err := os.ReadFile(filepath.Join("testdata", "client-key.pem"))
if err != nil {
t.Fatal("Couldn't find testdata/client-key.pem:", err)
}
cases := []struct {
desc string
config metricsConfig
tls *tls.Config
err error
wantFunc func(*testing.T, view.Exporter)
}{{
desc: "No TLS mostly default",
config: metricsConfig{
domain: "test",
component: "test",
backendDestination: openCensus,
},
wantFunc: func(t *testing.T, v view.Exporter) {
if v == nil {
t.Error("Expected view to be non-nil")
}
},
}, {
desc: "With TLS",
config: metricsConfig{
domain: "secure",
component: "test",
backendDestination: openCensus,
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test-opencensus",
},
Data: map[string][]byte{
"client-cert.pem": cert,
"client-key.pem": key,
},
},
requireSecure: true,
},
tls: &tls.Config{},
wantFunc: func(t *testing.T, v view.Exporter) {
if v == nil {
t.Error("Expected view to be non-nil")
}
oc, ok := v.(*ocagent.Exporter)
if !ok {
t.Errorf("Did not get an OpenCensus exporter: %+v", v)
}
oc.Flush()
},
}}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
var server net.Listener
var shutdown chan error
var err error
if c.err == nil {
server, shutdown, err = GetServer(c.tls)
if err != nil {
t.Fatal("Failed to start server:", err)
}
c.config.collectorAddress = server.Addr().String()
}
got, _, gotErr := newOpenCensusExporter(&c.config, logtesting.TestLogger(t))
if c.err != nil {
if diff := cmp.Diff(c.err, gotErr); diff != "" {
t.Error("wrong err (-want +got) =", diff)
}
return
}
if gotErr != nil {
t.Error("unexpected err:", gotErr)
return
}
if c.wantFunc != nil {
c.wantFunc(t, got)
}
t.Log("Awaiting channel shutdown at", server.Addr().String())
err = <-shutdown
if err != nil {
t.Error("Error from server:", err)
}
err = server.Close()
if err != nil {
t.Error("Failed to shut down server:", err)
}
})
}
}
type fakeSecrets struct {
secrets []corev1.Secret
}
func fakeSecretList(s ...corev1.Secret) *fakeSecrets {
return &fakeSecrets{secrets: s}
}
func (f *fakeSecrets) Get(name string) (*corev1.Secret, error) {
for _, s := range f.secrets {
if fmt.Sprintf("%s/%s", s.Namespace, s.Name) == name {
return &s, nil
}
if s.Name == name {
return &s, nil
}
}
return nil, errors.NewNotFound(schema.GroupResource{Resource: "secrets"}, name)
}
func GetServer(config *tls.Config) (net.Listener, chan error, error) {
var server net.Listener
var err error
if config == nil {
server, err = net.Listen("tcp", "localhost:0")
} else {
if config.Certificates == nil {
serverCert, err := tls.LoadX509KeyPair(
filepath.Join("testdata", "server-cert.pem"), filepath.Join("testdata", "server-key.pem"))
if err != nil {
return nil, nil, fmt.Errorf("Unable to load server cert from testadata: %w", err)
}
config.Certificates = []tls.Certificate{serverCert}
}
server, err = tls.Listen("tcp", "localhost:0", config)
}
if err != nil {
return nil, nil, fmt.Errorf("Unable to create listen server: %w", err)
}
shutdown := make(chan error)
go func() {
c, err := server.Accept()
if err != nil {
shutdown <- fmt.Errorf("Failed to accept connection: %w", err)
return
}
err = c.Close()
if err != nil {
shutdown <- fmt.Errorf("Failed to close server connection: %w", err)
return
}
shutdown <- nil
}()
return server, shutdown, err
}