Skip to content

Commit

Permalink
Enhance ResourceInNamespace Step (#103)
Browse files Browse the repository at this point in the history
* added logic and unit test to check that resource is NOT in ns

* combine positive and negative test cases into one

---------

Co-authored-by: eramirez8 <estela_ramirez@intuit.com>
  • Loading branch information
estela-ramirez and eramirez8 committed Jun 24, 2023
1 parent 2841b1d commit 440bef1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 50 deletions.
2 changes: 1 addition & 1 deletion docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 in namespace <any-characters-except-(")>` kdt.KubeClientSet.ResourceInNamespace
- `<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> [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.GetNodes
Expand Down
2 changes: 1 addition & 1 deletion kubedog.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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 in namespace ([^"]*)$`, kdt.KubeClientSet.ResourceInNamespace)
kdt.scenario.Step(`^(?:the )?(deployment|hpa|horizontalpodautoscaler|service|pdb|poddisruptionbudget|sa|serviceaccount) ([^"]*) (is|is not) in namespace ([^"]*)$`, kdt.KubeClientSet.ResourceInNamespace)
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.GetNodes)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ func (kc *ClientSet) NodesWithSelectorShouldBe(expectedNodes int, selector, stat
return structured.NodesWithSelectorShouldBe(kc.KubeInterface, kc.getWaiterConfig(), expectedNodes, selector, state)
}

func (kc *ClientSet) ResourceInNamespace(resourceType, name, namespace string) error {
return structured.ResourceInNamespace(kc.KubeInterface, resourceType, name, namespace)
func (kc *ClientSet) ResourceInNamespace(resourceType, name, namespace, isOrIsNot string) error {
return structured.ResourceInNamespace(kc.KubeInterface, resourceType, name, namespace, isOrIsNot)
}

func (kc *ClientSet) ScaleDeployment(name, namespace string, replicas int32) error {
Expand Down
24 changes: 19 additions & 5 deletions pkg/kube/structured/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func SendTrafficToIngress(kubeClientset kubernetes.Interface, w common.WaiterCon
return nil
}

func ResourceInNamespace(kubeClientset kubernetes.Interface, resourceType, name, namespace string) error {
func ResourceInNamespace(kubeClientset kubernetes.Interface, resourceType, name, namespace, isOrIsNot string) error {
var err error

if err := common.ValidateClientset(kubeClientset); err != nil {
Expand All @@ -398,9 +398,23 @@ func ResourceInNamespace(kubeClientset kubernetes.Interface, resourceType, name,
default:
return errors.Errorf("Invalid resource type")
}

if err != nil {
return err
if isOrIsNot == "is not" {
if kerrors.IsNotFound(err) {
return nil
} else if err == nil {
return errors.Errorf("expected resource '%s/%s' to not be found in ns '%s'", resourceType, name, namespace)
} else {
return err
}
} else if isOrIsNot == "is" {
if kerrors.IsNotFound(err) {
return errors.Errorf("expected resource '%s/%s' to be found in ns '%s'", resourceType, name, namespace)
} else if err == nil {
return nil
} else {
return err
}
} else {
return errors.Errorf("paramter isOrIsNot can only be 'is' or 'is not'")
}
return nil
}
98 changes: 57 additions & 41 deletions pkg/kube/structured/structured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,49 @@ func TestResourceInNamespace(t *testing.T) {
)

tests := []struct {
resource string
name string
resource string
name string
isOrIsNot string
}{
{
resource: "deployment",
name: "test_deploy",
resource: "deployment",
name: "test_deploy",
isOrIsNot: "is",
},
{
resource: "service",
name: "test_service",
resource: "service",
name: "test_service",
isOrIsNot: "is",
},
{
resource: "hpa",
name: "test_hpa",
resource: "hpa",
name: "test_hpa",
isOrIsNot: "is",
},
{
resource: "horizontalpodautoscaler",
name: "test_hpa",
resource: "horizontalpodautoscaler",
name: "test_hpa",
isOrIsNot: "is",
},
{
resource: "pdb",
name: "test_pdb",
resource: "pdb",
name: "test_pdb",
isOrIsNot: "is",
},
{
resource: "poddisruptionbudget",
name: "test_pdb",
resource: "poddisruptionbudget",
name: "test_pdb",
isOrIsNot: "is",
},
{
resource: "serviceaccount",
name: "mock_service_account",
resource: "serviceaccount",
name: "mock_service_account",
isOrIsNot: "is",
},
{
resource: "deployment",
name: "test_deploy_not_present",
isOrIsNot: "is not",
},
}

Expand All @@ -117,33 +130,36 @@ func TestResourceInNamespace(t *testing.T) {

for _, tt := range tests {
t.Run(tt.resource, func(t *testing.T) {
meta := metav1.ObjectMeta{
Name: tt.name,
if tt.isOrIsNot == "is" {
meta := metav1.ObjectMeta{
Name: tt.name,
}

switch tt.resource {
case "deployment":
_, _ = fakeClient.AppsV1().Deployments(namespace).Create(context.Background(), &appsv1.Deployment{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "service":
_, _ = fakeClient.CoreV1().Services(namespace).Create(context.Background(), &v1.Service{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "hpa":
_, _ = fakeClient.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Create(context.Background(), &hpa.HorizontalPodAutoscaler{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "pdb":
_, _ = fakeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(context.Background(), &policy.PodDisruptionBudget{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "serviceaccount":
_, _ = fakeClient.CoreV1().ServiceAccounts(namespace).Create(context.Background(), &v1.ServiceAccount{
ObjectMeta: meta,
}, metav1.CreateOptions{})
}
}

switch tt.resource {
case "deployment":
_, _ = fakeClient.AppsV1().Deployments(namespace).Create(context.Background(), &appsv1.Deployment{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "service":
_, _ = fakeClient.CoreV1().Services(namespace).Create(context.Background(), &v1.Service{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "hpa":
_, _ = fakeClient.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Create(context.Background(), &hpa.HorizontalPodAutoscaler{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "pdb":
_, _ = fakeClient.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(context.Background(), &policy.PodDisruptionBudget{
ObjectMeta: meta,
}, metav1.CreateOptions{})
case "serviceaccount":
_, _ = fakeClient.CoreV1().ServiceAccounts(namespace).Create(context.Background(), &v1.ServiceAccount{
ObjectMeta: meta,
}, metav1.CreateOptions{})
}
err = ResourceInNamespace(fakeClient, tt.resource, tt.name, namespace)
err = ResourceInNamespace(fakeClient, tt.resource, tt.name, namespace, tt.isOrIsNot)
g.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}
Expand Down

0 comments on commit 440bef1

Please sign in to comment.