Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add configmap support #156

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ 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
- `<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> configmap <any-characters-except-(")> exists in namespace <any-characters-except-(")>` kdt.KubeClientSet.ConfigMapExists
- `<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
3 changes: 2 additions & 1 deletion kubedog.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@
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
phanipadala marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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 @@
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
Loading