-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit test ValidateNodeTokenAccessHandler (#880)
- Loading branch information
1 parent
4abeba0
commit 378d6cd
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |