Skip to content

Commit 6eeac6d

Browse files
committed
Add tests
1 parent 8fd1280 commit 6eeac6d

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

pkg/kubelet/server/auth_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func AuthzTestCases(fineGrained bool) []AuthzTestCase {
137137
"/logs/": {"log"},
138138
"/logs/{logpath:*}": {"log"},
139139
"/metrics": {"metrics"},
140+
"/metrics/slis": {"metrics"},
140141
"/metrics/cadvisor": {"metrics"},
141142
"/metrics/probes": {"metrics"},
142143
"/metrics/resource": {"metrics"},

pkg/kubelet/server/server_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,3 +1641,19 @@ func TestFineGrainedAuthz(t *testing.T) {
16411641
})
16421642
}
16431643
}
1644+
1645+
func TestNewServerRegistersMetricsSLIsEndpointTwice(t *testing.T) {
1646+
host := &fakeKubelet{
1647+
hostnameFunc: func() string {
1648+
return "127.0.0.1"
1649+
},
1650+
}
1651+
resourceAnalyzer := stats.NewResourceAnalyzer(nil, time.Minute, &record.FakeRecorder{})
1652+
1653+
server1 := NewServer(host, resourceAnalyzer, nil, nil)
1654+
server2 := NewServer(host, resourceAnalyzer, nil, nil)
1655+
1656+
// Check if both servers registered the /metrics/slis endpoint
1657+
assert.Contains(t, server1.restfulCont.RegisteredHandlePaths(), "/metrics/slis", "First server should register /metrics/slis")
1658+
assert.Contains(t, server2.restfulCont.RegisteredHandlePaths(), "/metrics/slis", "Second server should register /metrics/slis")
1659+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2024 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 slis
18+
19+
import (
20+
"net/http"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
type mockMux struct {
27+
handledPaths []string
28+
}
29+
30+
func (m *mockMux) Handle(path string, handler http.Handler) {
31+
m.handledPaths = append(m.handledPaths, path)
32+
}
33+
34+
func TestSLIMetrics_Install(t *testing.T) {
35+
m := &mockMux{}
36+
s := SLIMetrics{}
37+
38+
s.Install(m)
39+
assert.Equal(t, []string{"/metrics/slis"}, m.handledPaths)
40+
41+
s.Install(m)
42+
// Assert that the path is registered twice for the 2 calls made to Install().
43+
assert.Equal(t, []string{"/metrics/slis", "/metrics/slis"}, m.handledPaths, "Should handle the path twice.")
44+
}

0 commit comments

Comments
 (0)