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

Add gatekeeper testing #384

Merged
merged 1 commit into from Jan 21, 2021
Merged

Add gatekeeper testing #384

merged 1 commit into from Jan 21, 2021

Conversation

marzianor9
Copy link
Contributor

No description provided.

@openshift-ci-robot openshift-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 8, 2020
@openshift-ci-robot
Copy link
Collaborator

Hi @marzianor9. Thanks for your PR.

I'm waiting for a openshift-kni member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@marzianor9 marzianor9 marked this pull request as draft December 8, 2020 12:56
@openshift-ci-robot openshift-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 8, 2020
functests/utils/dynamic/dynamic.go Outdated Show resolved Hide resolved
functests/utils/dynamic/dynamic.go Outdated Show resolved Hide resolved
functests/utils/dynamic/dynamic.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
Copy link
Member

@fedepaol fedepaol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good start!
Added a few comments

functests/gatekeeper/resources.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
@marzianor9 marzianor9 marked this pull request as ready for review December 23, 2020 11:49
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 23, 2020

// create the dynamic client
client, err := dynamicInterface.New("", gvrList...)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets put this in a BeforeAll and use Expect so we failed the test

Copy link
Contributor Author

@marzianor9 marzianor9 Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't create a variable that will be used in execute.BeforeAll, i will need to make it global, should i do it?

log.Fatalf("Unable to create the dynamic client: %v", err)
}

DescribeTable("should be able to add metadata info(labels/annotations)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the should is not part of a DescribeTable can you rephrase this please

return entries
}

func assignMetadataDefinition(amInfo *assignMetadataInfo) *unstructured.Unstructured {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you use an unstructured.Unstructured object when you always use the AssignMetadata kind?

can't you just use the API struct from the gatekeeper project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we created the dynamic client because gatekeeper didn't have the API struct we needed in the repo, and we were not sure if they want to add them.

functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
func(amInfo assignMetadataInfo, object TestObject) {
amObject := assignMetadataDefinition(&amInfo)

amObject, err := client.Resource("assignmetadata").Create(context.Background(), amObject, metav1.CreateOptions{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this reasource why we don't add the schema into a client just like all the other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't want to use 2 clients in the same test suite, and the dynamic client allows for more generic tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and i was unable to add the schema for this object to the client

}()

By("Creating test-pod-b")
testPodB := GetTestPod(TestingNamespace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already have a create pod function no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i needed a customized pod for the tests, also i needed it to be unstructured so that tests can be generic.

Expect(value).To(Equal("0"))

By("Updating assignMetadata to mutation-version: 1")
err = unstructured.SetNestedField(am.Object, "1", "spec", "parameters", "assign", "value")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you know the object type you are working with here so lets use the object and not the unstructured

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the gatekeeper repo does not support this.

Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeFalse())

By("Deleting test-pod-after-delete")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not part of the test should be in the afterEach clean


It("should be able to match by any match category", func() {
By("Creating the test namespaces")
includedNamepsace := &unstructured.Unstructured{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a namespace object just use the namespace schema

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to use 2 clients?

functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
@openshift-ci-robot openshift-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 10, 2021
@marzianor9 marzianor9 changed the title Add gatekeeper testing and a dynamic client Add gatekeeper testing Jan 10, 2021
defer func() {
err := client.Resource("assignmetadata").Delete(context.Background(), amDefinition.GetName(), metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
}()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have quite a lot of these defers, so maybe we can do:

func deleteResource(resource string, name string) func() {
   return func() {
	err := client.Resource(resource).Delete(context.Background(), name, metav1.DeleteOptions{})
	Expect(err).ToNot(HaveOccurred())
  }
}

and then use it like:
defer deleteResource("assignmetadata", amDefinition.GetName())()()

With 19 defer statements we should save more then 50 lines of code.

defer func() {
err := client.Resource("assignmetadata").Delete(context.Background(), namespaceSelected.GetName(), metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
}()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be more readable if we defined the Assigns in one place and then created them in a loop. Something like:

allScopeSelected = {...}
namespaceScopeIncluded = {...}
namespaceScopeIncluded= {...}
...

mutations = { allScopeSelected,  namespaceScopeIncluded, namespaceScopeIncluded}

for mutation ;= range mutatinos {
    client.Resource("assignmetadata").Create(context.Background(), namespaceSelected, metav1.CreateOptions{})
    defer func(){}
}

functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
functests/gatekeeper/gatekeeper.go Outdated Show resolved Hide resolved
Copy link
Member

@oribon oribon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be more readable not assigning value if it is not used

@openshift-ci-robot openshift-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 13, 2021
Expect(err).ToNot(HaveOccurred())
namespaces.WaitForDeletion(client, namespace, time.Minute)
}
}
Copy link

@mmirecki mmirecki Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you care about test time, then maybe move the WaitForDeletion after all namespaces are deleted:

		for _, namespace := range namespacesUsed {
			if namespaces.Exists(namespace, client) {
				err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
				Expect(err).ToNot(HaveOccurred())
			}
		}

		for _, namespace := range namespacesUsed {
			namespaces.WaitForDeletion(client, namespace, time.Minute)
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the namespaces i delete are used only on the last test, all other times we only delete the pods from the TestingNamespace.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If time is not of essence we can skip this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@mmirecki
Copy link

/lgtm

@openshift-ci-robot openshift-ci-robot added lgtm Indicates that a PR is ready to be merged. and removed lgtm Indicates that a PR is ready to be merged. labels Jan 19, 2021
}

for _, namespace := range namespacesUsed {
if namespaces.Exists(namespace, client) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip if namespaces.Exists(namespace, client), this is done in WaitForDeletion

@mmirecki
Copy link

/lgtm

@openshift-ci-robot openshift-ci-robot added lgtm Indicates that a PR is ready to be merged. and removed lgtm Indicates that a PR is ready to be merged. labels Jan 20, 2021
@fedepaol
Copy link
Member

/lgtm
/approve

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 21, 2021
@openshift-ci-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fedepaol, marzianor9

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 21, 2021
@openshift-merge-robot openshift-merge-robot merged commit d7a0b95 into openshift-kni:master Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants