Skip to content

Commit

Permalink
add configmap support
Browse files Browse the repository at this point in the history
Signed-off-by: Phanindra Padala <phanindra_padala@intuit.com>
  • Loading branch information
Phanindra Padala committed Jun 14, 2024
1 parent 5b5a76b commit 8317d13
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
- `<GK> [I] (create|submit|update) [the] secret <non-whitespace-characters> in namespace <non-whitespace-characters> from [environment variable] <non-whitespace-characters>` kdt.KubeClientSet.SecretOperationFromEnvironmentVariable
- `<GK> [I] delete [the] secret <non-whitespace-characters> in namespace <non-whitespace-characters>` kdt.KubeClientSet.SecretDelete
- `<GK> <digits> node[s] with selector <non-whitespace-characters> should be (found|ready)` kdt.KubeClientSet.NodesWithSelectorShouldBe
- `<GK> [the] (deployment|hpa|horizontalpodautoscaler|service|pdb|poddisruptionbudget|sa|serviceaccount) <any-characters-except-(")> (is|is not) in namespace <any-characters-except-(")>` kdt.KubeClientSet.ResourceInNamespace
- `<GK> [the] (deployment|hpa|horizontalpodautoscaler|service|pdb|poddisruptionbudget|sa|serviceaccount|configmap) <any-characters-except-(")> (is|is not) in namespace <any-characters-except-(")>` kdt.KubeClientSet.ResourceInNamespace
- `<GK> [I] scale [the] deployment <any-characters-except-(")> in namespace <any-characters-except-(")> to <digits>` kdt.KubeClientSet.ScaleDeployment
- `<GK> [I] validate Prometheus Statefulset <any-characters-except-(")> in namespace <any-characters-except-(")> has volumeClaimTemplates name <any-characters-except-(")>` kdt.KubeClientSet.ValidatePrometheusVolumeClaimTemplatesName
- `<GK> [I] get [the] nodes list` kdt.KubeClientSet.ListNodes
Expand All @@ -52,7 +52,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
- `<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
- `<GK> [I] send <digits> tps to ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [available] on port <digits> and path <any-characters-except-(")> for <digits> (minutes|seconds) expecting up to <digits> error[s]` kdt.KubeClientSet.SendTrafficToIngress

- `<GK> configmap <any-characters-except-(")> exists in namespace <any-characters-except-(")>` kdt.KubeClientSet.ConfigMapExists)
## AWS steps
- `<GK> [there are] [valid] AWS Credentials` kdt.AwsClientSet.DiscoverClients
- `<GK> an Auto Scaling Group named <any-characters-except-(")>` kdt.AwsClientSet.AnASGNamed
Expand Down
3 changes: 2 additions & 1 deletion kubedog.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ func (kdt *Test) SetScenario(scenario *godog.ScenarioContext) {
kdt.scenario.Step(`^(?:I )?(create|submit|update) (?:the )?secret (\S+) in namespace (\S+) from (?:environment variable )?(\S+)$`, kdt.KubeClientSet.SecretOperationFromEnvironmentVariable)
kdt.scenario.Step(`^(?:I )?delete (?:the )?secret (\S+) in namespace (\S+)$`, kdt.KubeClientSet.SecretDelete)
kdt.scenario.Step(`^(\d+) node(?:s)? with selector (\S+) should be (found|ready)$`, kdt.KubeClientSet.NodesWithSelectorShouldBe)
kdt.scenario.Step(`^(?:the )?(deployment|hpa|horizontalpodautoscaler|service|pdb|poddisruptionbudget|sa|serviceaccount) ([^"]*) (is|is not) in namespace ([^"]*)$`, kdt.KubeClientSet.ResourceInNamespace)
kdt.scenario.Step(`^(?:the )?(deployment|hpa|horizontalpodautoscaler|service|pdb|poddisruptionbudget|sa|serviceaccount|configmap) ([^"]*) (is|is not) in namespace ([^"]*)$`, kdt.KubeClientSet.ResourceInNamespace)

Check warning on line 75 in kubedog.go

View check run for this annotation

Codecov / codecov/patch

kubedog.go#L75

Added line #L75 was not covered by tests
kdt.scenario.Step(`^(?:I )?scale (?:the )?deployment ([^"]*) in namespace ([^"]*) to (\d+)$`, kdt.KubeClientSet.ScaleDeployment)
kdt.scenario.Step(`^(?:I )?validate Prometheus Statefulset ([^"]*) in namespace ([^"]*) has volumeClaimTemplates name ([^"]*)$`, kdt.KubeClientSet.ValidatePrometheusVolumeClaimTemplatesName)
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(`^configmap ([^"]*) exists in namespace ([^"]*)$`, kdt.KubeClientSet.ConfigMapExists)

Check warning on line 81 in kubedog.go

View check run for this annotation

Codecov / codecov/patch

kubedog.go#L81

Added line #L81 was not covered by tests
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) ConfigMapExists(name, namespace string) error {
return structured.ConfigMapExists(kc.KubeInterface, name, namespace)

Check warning on line 316 in pkg/kube/kube.go

View check run for this annotation

Codecov / codecov/patch

pkg/kube/kube.go#L315-L316

Added lines #L315 - L316 were not covered by tests
}

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

func ConfigMapExists(kubeClientset kubernetes.Interface, name, namespace string) error {
configMap, err := GetConfigMap(kubeClientset, name, namespace)
if err != nil || configMap.Name != name {
return fmt.Errorf("configMap %s/%s does not exist", namespace, name)
}

return nil
}

func PersistentVolExists(kubeClientset kubernetes.Interface, name, expectedPhase string) error {
vol, err := GetPersistentVolume(kubeClientset, name)
if err != nil {
Expand Down Expand Up @@ -407,6 +416,8 @@ func ResourceInNamespace(kubeClientset kubernetes.Interface, resourceType, name,
_, err = kubeClientset.PolicyV1().PodDisruptionBudgets(namespace).Get(context.Background(), name, metav1.GetOptions{})
case "sa", "serviceaccount":
_, err = kubeClientset.CoreV1().ServiceAccounts(namespace).Get(context.Background(), name, metav1.GetOptions{})
case "configmap":
_, err = kubeClientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
default:
return errors.Errorf("Invalid resource type")
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/kube/structured/structured_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ func GetDeployment(kubeClientset kubernetes.Interface, name, namespace string) (
return deploy.(*appsv1.Deployment), nil
}

func GetConfigMap(kubeClientset kubernetes.Interface, name, namespace string) (*corev1.ConfigMap, error) {
if err := common.ValidateClientset(kubeClientset); err != nil {
return nil, err

Check warning on line 78 in pkg/kube/structured/structured_helper.go

View check run for this annotation

Codecov / codecov/patch

pkg/kube/structured/structured_helper.go#L78

Added line #L78 was not covered by tests
}

configMap, err := util.RetryOnError(&util.DefaultRetry, util.IsRetriable, func() (interface{}, error) {
return kubeClientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
})
if err != nil {
return nil, errors.Wrap(err, "failed to get configMap")
}
return configMap.(*corev1.ConfigMap), nil
}

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

const (
configMapType = "configmap"
deploymentType = "deployment"
serviceType = "service"
hpaType = "horizontalpodautoscaler"
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestResourceInNamespace(t *testing.T) {
hpaName := "horizontalpodautoscaler1"
pdbName := "poddisruptionbudget1"
saName := "serviceaccount1"
configMapName := "configmap1"

namespace := "namespace1"
tests := []struct {
Expand All @@ -125,6 +127,25 @@ func TestResourceInNamespace(t *testing.T) {
namespace: namespace,
},
},
{
name: "Positive Test: configmap",
args: args{
kubeClientset: fake.NewSimpleClientset(getResourceWithNamespace(t, configMapType, configMapName, namespace)),
resourceType: configMapType,
name: configMapName,
namespace: namespace,
},
},
{
name: "Negative Test: Invalid resource type",
args: args{
kubeClientset: fake.NewSimpleClientset(getResourceWithNamespace(t, configMapType, configMapName, namespace)),
resourceType: "configmaps",
name: configMapName,
namespace: namespace,
},
wantErr: true,
},
{
name: "Positive Test: service",
args: args{
Expand Down Expand Up @@ -380,6 +401,47 @@ func TestDeploymentIsRunning(t *testing.T) {
}
}

func TestConfigMapExists(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
name string
namespace 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,
},
wantErr: false,
},
{
name: "Negative Test",
args: args{
kubeClientset: fake.NewSimpleClientset(getResourceWithNamespace(t, configMapType, "configMapName", namespace)),
name: configMapName,
namespace: namespace,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ConfigMapExists(tt.args.kubeClientset, tt.args.name, tt.args.namespace); (err != nil) != tt.wantErr {
t.Errorf("ConfigMapExists() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestPersistentVolExists(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
Expand Down Expand Up @@ -684,6 +746,14 @@ func getResourceWithAll(t *testing.T, resourceType, name, namespace, label strin
Labels: labels,
},
}
case configMapType:
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
}
case serviceType:
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 8317d13

Please sign in to comment.