Skip to content

Commit

Permalink
Unit test ValidateNodeTokenAccessHandler (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello authored Dec 11, 2024
1 parent 4abeba0 commit 378d6cd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/k8s/pkg/k8sd/api/node_access_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package api

import (
"context"
"net/http"
"os"
"path"
"testing"

"github.com/canonical/k8s/pkg/snap"
"github.com/canonical/k8s/pkg/snap/mock"
. "github.com/onsi/gomega"
)

func TestValidateNodeTokenAccessHandler(t *testing.T) {
for _, tc := range []struct {
name string
tokenHeaderContent string
tokenFileContent string
expectErr bool
createFile bool
}{
{
name: "header and file token match",
tokenHeaderContent: "node-token",
tokenFileContent: "node-token",
createFile: true,
},
{
name: "header and file token differ",
tokenHeaderContent: "node-token",
tokenFileContent: "different-node-token",
expectErr: true,
createFile: true,
},
{
name: "missing header token",
tokenFileContent: "node-token",
expectErr: true,
createFile: true,
},
{
name: "missing token file",
tokenHeaderContent: "node-token",
tokenFileContent: "node-token",
expectErr: true,
},
{
name: "missing token in token file",
tokenHeaderContent: "node-token",
expectErr: true,
createFile: true,
},
{
name: "missing header and token file",
expectErr: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

dir := t.TempDir()

var err error
if tc.createFile {
err = os.WriteFile(path.Join(dir, "token-file"), []byte(tc.tokenFileContent), 0o644)
g.Expect(err).ToNot(HaveOccurred())
}

e := &Endpoints{
context: context.Background(),
provider: &mock.Provider{
SnapFn: func() snap.Snap {
return &mock.Snap{
Mock: mock.Mock{
NodeTokenFile: path.Join(dir, "token-file"),
},
}
},
},
}

req := &http.Request{
Header: make(http.Header),
}
req.Header.Set("Node-Token", tc.tokenHeaderContent)

handler := e.ValidateNodeTokenAccessHandler("Node-Token")
valid, resp := handler(nil, req)

if tc.expectErr {
g.Expect(valid).To(BeFalse())
g.Expect(resp).NotTo(BeNil())
} else {
g.Expect(valid).To(BeTrue())
g.Expect(resp).To(BeNil())
}
})
}
}
39 changes: 39 additions & 0 deletions src/k8s/pkg/snap/mock/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mock

import (
"github.com/canonical/k8s/pkg/snap"
"github.com/canonical/microcluster/v2/microcluster"
)

type Provider struct {
MicroClusterFn func() *microcluster.MicroCluster
SnapFn func() snap.Snap
NotifyUpdateNodeConfigControllerFn func()
NotifyFeatureControllerFn func(network, gateway, ingress, loadBalancer, localStorage, metricsServer, dns bool)
}

func (p *Provider) MicroCluster() *microcluster.MicroCluster {
if p.MicroClusterFn != nil {
return p.MicroClusterFn()
}
return nil
}

func (p *Provider) Snap() snap.Snap {
if p.SnapFn != nil {
return p.SnapFn()
}
return nil
}

func (p *Provider) NotifyUpdateNodeConfigController() {
if p.NotifyUpdateNodeConfigControllerFn != nil {
p.NotifyUpdateNodeConfigControllerFn()
}
}

func (p *Provider) NotifyFeatureController(network, gateway, ingress, loadBalancer, localStorage, metricsServer, dns bool) {
if p.NotifyFeatureControllerFn != nil {
p.NotifyFeatureControllerFn(network, gateway, ingress, loadBalancer, localStorage, metricsServer, dns)
}
}

0 comments on commit 378d6cd

Please sign in to comment.