Skip to content

Commit

Permalink
Unit tests for Metrics Server k8sd feature (#691)
Browse files Browse the repository at this point in the history
Metrics Server feature lacks unit test this PR implements tests for Metrics Server functionality.
KU-1515
  • Loading branch information
maci3jka committed Sep 23, 2024
1 parent 6b15893 commit 02f369b
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/k8s/pkg/k8sd/features/metrics-server/metrics_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics_server_test

import (
"context"
"errors"
"testing"

"github.com/canonical/k8s/pkg/client/helm"
Expand All @@ -14,44 +15,73 @@ import (
)

func TestApplyMetricsServer(t *testing.T) {

helmErr := errors.New("failed to apply")
for _, tc := range []struct {
name string
config types.MetricsServer
expectState helm.State
helmError error
}{
{
name: "Enable",
name: "EnableWithoutHelmError",
config: types.MetricsServer{
Enabled: utils.Pointer(true),
},
expectState: helm.StatePresent,
helmError: nil,
},
{
name: "DisableWithoutHelmError",
config: types.MetricsServer{
Enabled: utils.Pointer(false),
},
expectState: helm.StateDeleted,
helmError: nil,
},
{
name: "EnableWithHelmError",
config: types.MetricsServer{
Enabled: utils.Pointer(true),
},
expectState: helm.StatePresent,
helmError: helmErr,
},
{
name: "Disable",
name: "DisableWithHelmError",
config: types.MetricsServer{
Enabled: utils.Pointer(false),
},
expectState: helm.StateDeleted,
helmError: helmErr,
},
} {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
h := &helmmock.Mock{}
h := &helmmock.Mock{
ApplyErr: tc.helmError,
}
s := &snapmock.Snap{
Mock: snapmock.Mock{
HelmClient: h,
},
}

status, err := metrics_server.ApplyMetricsServer(context.Background(), s, tc.config, nil)
g.Expect(err).ToNot(HaveOccurred())
if tc.helmError == nil {
g.Expect(err).ToNot(HaveOccurred())
} else {
g.Expect(err).To(HaveOccurred())
}

g.Expect(h.ApplyCalledWith).To(ConsistOf(SatisfyAll(
HaveField("Chart.Name", Equal("metrics-server")),
HaveField("Chart.Namespace", Equal("kube-system")),
HaveField("State", Equal(tc.expectState)),
)))
if tc.config.GetEnabled() {
if errors.Is(tc.helmError, helmErr) {
g.Expect(status.Message).To(ContainSubstring(helmErr.Error()))
} else if tc.config.GetEnabled() {
g.Expect(status.Message).To(Equal("enabled"))
} else {
g.Expect(status.Message).To(Equal("disabled"))
Expand Down

0 comments on commit 02f369b

Please sign in to comment.