Skip to content

Commit

Permalink
feat: Add new step to validate data in configmap (#159)
Browse files Browse the repository at this point in the history
* feat: Add new step to validate data in configmap

* address review

* address review

* sign

* sign commit

Signed-off-by: Phanindra Padala <phanindra_padala@intuit.com>

---------

Signed-off-by: Phanindra Padala <phanindra_padala@intuit.com>
Co-authored-by: Phanindra Padala <phanindra_padala@intuit.com>
  • Loading branch information
phanipadala and Phanindra Padala committed Jul 1, 2024
1 parent 46a43ba commit 2a2fbc2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
- `<GK> [I] get [the] nodes list` kdt.KubeClientSet.ListNodes
- `<GK> [the] daemonset <any-characters-except-(")> is running in namespace <any-characters-except-(")>` kdt.KubeClientSet.DaemonSetIsRunning
- `<GK> [the] deployment <any-characters-except-(")> is running in namespace <any-characters-except-(")>` kdt.KubeClientSet.DeploymentIsRunning
- `<GK> [the] data in [the] ConfigMap "<any-characters-except-(")>" in namespace "<any-characters-except-(")>" has key "<any-characters-except-(")>" with value "<any-characters-except-(")>"` kdt.KubeClientSet.ConfigMapDataHasKeyAndValue
- `<GK> [the] persistentvolume <any-characters-except-(")> exists with status (Available|Bound|Released|Failed|Pending)` kdt.KubeClientSet.PersistentVolExists
- `<GK> [the] (clusterrole|clusterrolebinding) with name <any-characters-except-(")> should be found` kdt.KubeClientSet.ClusterRbacIsFound
- `<GK> [the] ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [is] [available] on port <digits> and path <any-characters-except-(")>` kdt.KubeClientSet.IngressAvailable
Expand Down
1 change: 1 addition & 0 deletions kubedog.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (kdt *Test) SetScenario(scenario *godog.ScenarioContext) {
kdt.scenario.Step(`^(?:I )?get (?:the )?nodes list$`, kdt.KubeClientSet.ListNodes)
kdt.scenario.Step(`^(?:the )?daemonset ([^"]*) is running in namespace ([^"]*)$`, kdt.KubeClientSet.DaemonSetIsRunning)
kdt.scenario.Step(`^(?:the )?deployment ([^"]*) is running in namespace ([^"]*)$`, kdt.KubeClientSet.DeploymentIsRunning)
kdt.scenario.Step(`^(?:the )?data in (?:the )?ConfigMap "([^"]*)" in namespace "([^"]*)" has key "([^"]*)" with value "([^"]*)"$`, kdt.KubeClientSet.ConfigMapDataHasKeyAndValue)
kdt.scenario.Step(`^(?:the )?persistentvolume ([^"]*) exists with status (Available|Bound|Released|Failed|Pending)$`, kdt.KubeClientSet.PersistentVolExists)
kdt.scenario.Step(`^(?:the )?(clusterrole|clusterrolebinding) with name ([^"]*) should be found$`, kdt.KubeClientSet.ClusterRbacIsFound)
kdt.scenario.Step(`^(?:the )?ingress (\S+) in (?:the )?namespace (\S+) (?:is )?(?:available )?on port (\d+) and path ([^"]*)$`, kdt.KubeClientSet.IngressAvailable)
Expand Down
4 changes: 4 additions & 0 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ func (kc *ClientSet) DeploymentIsRunning(name, namespace string) error {
return structured.DeploymentIsRunning(kc.KubeInterface, name, namespace)
}

func (kc *ClientSet) ConfigMapDataHasKeyAndValue(name, namespace, key, value string) error {
return structured.ConfigMapDataHasKeyAndValue(kc.KubeInterface, name, namespace, key, value)
}

func (kc *ClientSet) PersistentVolExists(name, expectedPhase string) error {
return structured.PersistentVolExists(kc.KubeInterface, name, expectedPhase)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/kube/structured/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ func DeploymentIsRunning(kubeClientset kubernetes.Interface, name, namespace str
return nil
}

func ConfigMapDataHasKeyAndValue(kubeClientset kubernetes.Interface, configMapName, namespace, key, value string) error {

currentData, err := GetConfigMap(kubeClientset, configMapName, namespace)
if err != nil {
return err
}
if currentData.Data[key] != value {
return fmt.Errorf("configMap %s/%s does not have the expected data", namespace, configMapName)
}

return nil
}

func PersistentVolExists(kubeClientset kubernetes.Interface, name, expectedPhase string) error {
vol, err := GetPersistentVolume(kubeClientset, name)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/kube/structured/structured_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ func GetDeployment(kubeClientset kubernetes.Interface, name, namespace string) (
return deploy.(*appsv1.Deployment), nil
}

func GetConfigMap(kubeClientset kubernetes.Interface, name, namespace string) (*corev1.ConfigMap, error) {
configmaps, err := kubeClientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil || configmaps.Name != name {
return nil, errors.Wrap(err, "failed to get configmap")
}

return configmaps, nil
}

func GetPersistentVolume(kubeClientset kubernetes.Interface, name string) (*corev1.PersistentVolume, error) {
if err := common.ValidateClientset(kubeClientset); err != nil {
return nil, err
Expand Down
50 changes: 50 additions & 0 deletions pkg/kube/structured/structured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,53 @@ func TestDeploymentIsRunning(t *testing.T) {
}
}

func TestConfigMapExists(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
name string
namespace string
key string
value string
}
configMapName := "configmap1"
namespace := "namespace1"
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Positive Test",
args: args{
kubeClientset: fake.NewSimpleClientset(getResourceWithNamespace(t, configMapType, configMapName, namespace)),
name: configMapName,
namespace: namespace,
key: "key1",
value: "value1",
},
wantErr: false,
},
{
name: "Negative Test",
args: args{
kubeClientset: fake.NewSimpleClientset(getResourceWithNamespace(t, configMapType, configMapName, namespace)),
name: configMapName,
namespace: namespace,
key: "key1",
value: "value2",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ConfigMapDataHasKeyAndValue(tt.args.kubeClientset, tt.args.name, tt.args.namespace, tt.args.key, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("ConfigMapDataHasKeyAndValue() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestPersistentVolExists(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
Expand Down Expand Up @@ -712,6 +759,9 @@ func getResourceWithAll(t *testing.T, resourceType, name, namespace, label strin
Namespace: namespace,
Labels: labels,
},
Data: map[string]string{
"key1": "value1",
},
}
case serviceType:
return &corev1.Service{
Expand Down

0 comments on commit 2a2fbc2

Please sign in to comment.