Skip to content

Commit dfddf23

Browse files
authored
test(kubernetes): add unit tests for ProviderKubeconfig functionality (#375)
Signed-off-by: Marc Nuri <[email protected]>
1 parent f3a4466 commit dfddf23

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package kubernetes
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/containers/kubernetes-mcp-server/internal/test"
9+
"github.com/containers/kubernetes-mcp-server/pkg/config"
10+
"github.com/stretchr/testify/suite"
11+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
12+
)
13+
14+
type ProviderKubeconfigTestSuite struct {
15+
BaseProviderSuite
16+
mockServer *test.MockServer
17+
provider Provider
18+
}
19+
20+
func (s *ProviderKubeconfigTestSuite) SetupTest() {
21+
s.mockServer = test.NewMockServer()
22+
kubeconfig := s.mockServer.Kubeconfig()
23+
for i := 0; i < 10; i++ {
24+
// Add multiple fake contexts to force multi-cluster behavior
25+
kubeconfig.Contexts[fmt.Sprintf("context-%d", i)] = clientcmdapi.NewContext()
26+
}
27+
provider, err := NewProvider(&config.StaticConfig{KubeConfig: test.KubeconfigFile(s.T(), kubeconfig)})
28+
s.Require().NoError(err, "Expected no error creating provider with kubeconfig")
29+
s.provider = provider
30+
}
31+
32+
func (s *ProviderKubeconfigTestSuite) TearDownTest() {
33+
if s.mockServer != nil {
34+
s.mockServer.Close()
35+
}
36+
}
37+
38+
func (s *ProviderKubeconfigTestSuite) TestType() {
39+
s.IsType(&kubeConfigClusterProvider{}, s.provider)
40+
}
41+
42+
func (s *ProviderKubeconfigTestSuite) TestWithNonOpenShiftCluster() {
43+
s.Run("IsOpenShift returns false", func() {
44+
inOpenShift := s.provider.IsOpenShift(s.T().Context())
45+
s.False(inOpenShift, "Expected InOpenShift to return false")
46+
})
47+
}
48+
49+
func (s *ProviderKubeconfigTestSuite) TestWithOpenShiftCluster() {
50+
s.mockServer.Handle(&test.InOpenShiftHandler{})
51+
s.Run("IsOpenShift returns true", func() {
52+
inOpenShift := s.provider.IsOpenShift(s.T().Context())
53+
s.True(inOpenShift, "Expected InOpenShift to return true")
54+
})
55+
}
56+
57+
func (s *ProviderKubeconfigTestSuite) TestVerifyToken() {
58+
s.mockServer.Handle(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
59+
if req.URL.EscapedPath() == "/apis/authentication.k8s.io/v1/tokenreviews" {
60+
w.Header().Set("Content-Type", "application/json")
61+
_, _ = w.Write([]byte(`
62+
{
63+
"kind": "TokenReview",
64+
"apiVersion": "authentication.k8s.io/v1",
65+
"spec": {"token": "the-token"},
66+
"status": {
67+
"authenticated": true,
68+
"user": {
69+
"username": "test-user",
70+
"groups": ["system:authenticated"]
71+
},
72+
"audiences": ["the-audience"]
73+
}
74+
}`))
75+
}
76+
}))
77+
s.Run("VerifyToken returns UserInfo for non-empty context", func() {
78+
userInfo, audiences, err := s.provider.VerifyToken(s.T().Context(), "fake-context", "some-token", "the-audience")
79+
s.Require().NoError(err, "Expected no error from VerifyToken with empty target")
80+
s.Require().NotNil(userInfo, "Expected UserInfo from VerifyToken with empty target")
81+
s.Equalf(userInfo.Username, "test-user", "Expected username test-user, got: %s", userInfo.Username)
82+
s.Containsf(userInfo.Groups, "system:authenticated", "Expected group system:authenticated in %v", userInfo.Groups)
83+
s.Require().NotNil(audiences, "Expected audiences from VerifyToken with empty target")
84+
s.Len(audiences, 1, "Expected audiences from VerifyToken with empty target")
85+
s.Containsf(audiences, "the-audience", "Expected audience the-audience in %v", audiences)
86+
})
87+
s.Run("VerifyToken returns UserInfo for empty context (default context", func() {
88+
userInfo, audiences, err := s.provider.VerifyToken(s.T().Context(), "", "the-token", "the-audience")
89+
s.Require().NoError(err, "Expected no error from VerifyToken with empty target")
90+
s.Require().NotNil(userInfo, "Expected UserInfo from VerifyToken with empty target")
91+
s.Equalf(userInfo.Username, "test-user", "Expected username test-user, got: %s", userInfo.Username)
92+
s.Containsf(userInfo.Groups, "system:authenticated", "Expected group system:authenticated in %v", userInfo.Groups)
93+
s.Require().NotNil(audiences, "Expected audiences from VerifyToken with empty target")
94+
s.Len(audiences, 1, "Expected audiences from VerifyToken with empty target")
95+
s.Containsf(audiences, "the-audience", "Expected audience the-audience in %v", audiences)
96+
})
97+
}
98+
99+
func (s *ProviderKubeconfigTestSuite) TestGetTargets() {
100+
s.Run("GetTargets returns all contexts defined in kubeconfig", func() {
101+
targets, err := s.provider.GetTargets(s.T().Context())
102+
s.Require().NoError(err, "Expected no error from GetTargets")
103+
s.Len(targets, 11, "Expected 11 targets from GetTargets")
104+
s.Contains(targets, "fake-context", "Expected fake-context in targets from GetTargets")
105+
for i := 0; i < 10; i++ {
106+
s.Contains(targets, fmt.Sprintf("context-%d", i), "Expected context-%d in targets from GetTargets", i)
107+
}
108+
})
109+
}
110+
111+
func (s *ProviderKubeconfigTestSuite) TestGetDerivedKubernetes() {
112+
s.Run("GetDerivedKubernetes returns Kubernetes for valid context", func() {
113+
k8s, err := s.provider.GetDerivedKubernetes(s.T().Context(), "fake-context")
114+
s.Require().NoError(err, "Expected no error from GetDerivedKubernetes with valid context")
115+
s.NotNil(k8s, "Expected Kubernetes from GetDerivedKubernetes with valid context")
116+
})
117+
s.Run("GetDerivedKubernetes returns Kubernetes for empty context (default)", func() {
118+
k8s, err := s.provider.GetDerivedKubernetes(s.T().Context(), "")
119+
s.Require().NoError(err, "Expected no error from GetDerivedKubernetes with empty context")
120+
s.NotNil(k8s, "Expected Kubernetes from GetDerivedKubernetes with empty context")
121+
})
122+
s.Run("GetDerivedKubernetes returns error for invalid context", func() {
123+
k8s, err := s.provider.GetDerivedKubernetes(s.T().Context(), "invalid-context")
124+
s.Require().Error(err, "Expected error from GetDerivedKubernetes with invalid context")
125+
s.ErrorContainsf(err, `context "invalid-context" does not exist`, "Expected context does not exist error, got: %v", err)
126+
s.Nil(k8s, "Expected no Kubernetes from GetDerivedKubernetes with invalid context")
127+
})
128+
}
129+
130+
func (s *ProviderKubeconfigTestSuite) TestGetDefaultTarget() {
131+
s.Run("GetDefaultTarget returns current-context defined in kubeconfig", func() {
132+
s.Equal("fake-context", s.provider.GetDefaultTarget(), "Expected fake-context as default target")
133+
})
134+
}
135+
136+
func (s *ProviderKubeconfigTestSuite) TestGetTargetParameterName() {
137+
s.Equal("context", s.provider.GetTargetParameterName(), "Expected context as target parameter name")
138+
}
139+
140+
func TestProviderKubeconfig(t *testing.T) {
141+
suite.Run(t, new(ProviderKubeconfigTestSuite))
142+
}

0 commit comments

Comments
 (0)