Skip to content

Commit

Permalink
Add unit tests for fg method
Browse files Browse the repository at this point in the history
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
  • Loading branch information
bpradipt committed Apr 29, 2024
1 parent 9a30d74 commit 5f47dc5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
k8s.io/cri-api v0.23.1
k8s.io/kubectl v0.22.1
sigs.k8s.io/controller-runtime v0.15.0
sigs.k8s.io/controller-tools v0.11.3
)
Expand Down Expand Up @@ -61,6 +62,7 @@ require (
github.com/coreos/vcontext v0.0.0-20201120045928-b0e13dab675c // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.11.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.5.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
Expand Down Expand Up @@ -3067,6 +3068,7 @@ k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2R
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/kubectl v0.22.1 h1:kpXO+ajPNTzAVLDM9pAzCsWH9MtCMr92zpcvXMt7P6E=
k8s.io/kubectl v0.22.1/go.mod h1:mjAOgEbMNMtZWxnfM6jd+nPjPsaoLqO5xanc78WcSbw=
k8s.io/kubelet v0.22.1/go.mod h1:rZuP1msr5NH7IGApW60DYFR3Cs3On4ftWLMJRfg+iU4=
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
Expand Down
98 changes: 98 additions & 0 deletions internal/featuregates/featuregates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package featuregates

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestFeatureGates_GetFeatureGateParams(t *testing.T) {
fg := &FeatureGates{
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Namespace: "test-namespace",
}

ctx := context.TODO()

t.Run("FeatureGate not found", func(t *testing.T) {
feature := "non-existent-feature"

params := fg.GetFeatureGateParams(ctx, feature)

if len(params) != 0 {
t.Errorf("Expected empty params, got: %v", params)
}
})

t.Run("FeatureGate found", func(t *testing.T) {
feature := "AdditionalRuntimeClasses"

cfgMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: AdditionalRuntimeClassesConfig,
Namespace: fg.Namespace,
},
Data: map[string]string{
"runtimeClassConfig": "name1,name2",
},
}

err := fg.Client.Create(ctx, cfgMap)
if err != nil {
t.Fatalf("Failed to create ConfigMap: %v", err)
}

params := fg.GetFeatureGateParams(ctx, feature)

expectedParams := map[string]string{
"runtimeClassConfig": "name1,name2",
}

if len(params) != len(expectedParams) {
t.Errorf("Expected %d params, got: %d", len(expectedParams), len(params))
}

for key, value := range expectedParams {
if params[key] != value {
t.Errorf("Expected param %s to have value %s, got: %s", key, value, params[key])
}
}

// Clean up the created ConfigMap
err = fg.Client.Delete(ctx, cfgMap)
if err != nil {
t.Fatalf("Failed to delete ConfigMap: %v", err)
}
})

t.Run("FeatureGate configmap not found", func(t *testing.T) {
feature := "LayeredImageDeployment"

params := fg.GetFeatureGateParams(ctx, feature)

if len(params) != 0 {
t.Errorf("Expected empty params, got: %v", params)
}

})

t.Run("FeatureGate configmap not found", func(t *testing.T) {
feature := "LayeredImageDeployment"

params := fg.GetFeatureGateParams(ctx, feature)

if len(params) != 0 {
t.Errorf("Expected empty params, got: %v", params)
}

// Check existence of params["kernelArguments"] in the returned map
if _, ok := params["kernelArguments"]; ok {
t.Errorf("Expected empty params to not contain key 'kernelArguments'")
}

})
}

0 comments on commit 5f47dc5

Please sign in to comment.