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

Implement e2e test for security policy #360

Merged
merged 5 commits into from
Jun 27, 2018

Conversation

MrHohn
Copy link
Member

@MrHohn MrHohn commented Jun 22, 2018

This e2e test mostly checks if security policy is properly set on the relevant backend service resource upon ingress creation/update.

/assign @rramkumar1 @bowei

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 22, 2018
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 22, 2018
Name: name,
Rules: []*computebeta.SecurityPolicyRule{
&computebeta.SecurityPolicyRule{
Action: "deny(403)",
Copy link
Member Author

Choose a reason for hiding this comment

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

Also, turned out it's possible to add check on the response code (can be explicitly configured on policy). I will add that to validator later.

Copy link
Member

Choose a reason for hiding this comment

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

in a follow on PR?

Copy link
Member Author

@MrHohn MrHohn Jun 25, 2018

Choose a reason for hiding this comment

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

Will put that in a separate PR.

if err := Framework.Clientset.Extensions().Ingresses(s.Namespace).Delete(tc.ing.Name, &metav1.DeleteOptions{}); err != nil {
t.Errorf("Delete(%q) = %v, want nil", tc.ing.Name, err)
if err := e2e.WaitForIngressDeletion(ctx, Framework.Cloud, gclb, s, ing, false); err != nil {
t.Errorf("Failed to wait for ingress deletion: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

e2e.WaitForIngressDeletion(..., %q, false) = %v, want nil", ing.Name, err

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Name: name,
Rules: []*computebeta.SecurityPolicyRule{
&computebeta.SecurityPolicyRule{
Action: "deny(403)",
Copy link
Member

Choose a reason for hiding this comment

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

in a follow on PR?

t.Parallel()

Framework.RunWithSandbox("Security Policy Enable", t, func(t *testing.T, s *e2e.Sandbox) {
// ------ Step: Preparing test ------
Copy link
Member

Choose a reason for hiding this comment

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

replace this with t.Logf("creating security policy") or something similar

"preparing test" doesn't tell us anything about what is going on here

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed comment since it isn't useful. Things like "creating security policy" are already logged below.

t.Fatalf("Failed to prepare k8s resources: %v", err)
}

// ------ Step: Executing test ------
Copy link
Member

Choose a reason for hiding this comment

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

log instead of writing a comment

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}
defer func() {
if err := cleanupSecurityPolicies(ctx, Framework.Cloud, policies); err != nil {
t.Errorf("Failed to cleanup policies: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

don't capitalize t.Errorf strings

}()
policies, err := prepareSecurityPolicies(ctx, Framework.Cloud, policies)
if err != nil {
t.Fatalf("Failed to prepare policies: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

prepareSecurityPolicies(ctx, Framework.Cloud, %+v) = _, %v; want _, nil, policies, err

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Framework.RunWithSandbox("Security Policy Transition", t, func(t *testing.T, s *e2e.Sandbox) {
// ------ Step: Preparing test ------

ctx := context.Background()
Copy link
Member

Choose a reason for hiding this comment

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

move this to the top

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}
}

// ------ Step: Cleaning up test ------
Copy link
Member

Choose a reason for hiding this comment

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

log etc

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

glog.Infof("Deleting security policies...")
for _, policy := range policies {
if err := c.BetaSecurityPolicies().Delete(ctx, meta.GlobalKey(policy.Name)); err != nil {
return fmt.Errorf("failed to delete security policy %q: %v", policy.Name, err)
Copy link
Member

Choose a reason for hiding this comment

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

glog.Info("Security policy %q deleted

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

glog.Infof("Deleting security policies...")
for _, policy := range policies {
if err := c.BetaSecurityPolicies().Delete(ctx, meta.GlobalKey(policy.Name)); err != nil {
return fmt.Errorf("failed to delete security policy %q: %v", policy.Name, err)
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this should just try to delete all policies and then return the first error

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, changed to delete all policies and capture all errors..

}
glog.Infof("Backend config %s/%s created", testBackendConfig.Name, s.Namespace)

_, testSvc, err := e2e.CreateEchoService(s, "service-1", testBackendConfigAnnotation)
Copy link
Member

Choose a reason for hiding this comment

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

Why not just put this outside at the top of the test instead of doing it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, moved it out.

@MrHohn MrHohn force-pushed the e2e-security-policy branch 2 times, most recently from bb0527d to cc2b099 Compare June 25, 2018 22:45
@@ -58,11 +59,25 @@ func WaitForIngress(s *Sandbox, ing *v1beta1.Ingress) (*v1beta1.Ingress, error)
return ing, err
}

// WaitForIngressDeletion deletes the given ingress and waits for the
// resources associated with it to be deleted.
func WaitForIngressDeletion(ctx context.Context, c cloud.Cloud, g *fuzz.GCLB, s *Sandbox, ing *v1beta1.Ingress, omitSystemDefaultBackend bool) error {
Copy link
Member

Choose a reason for hiding this comment

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

do you need to pass in cloud separately if you pass in sandbox?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, remove cloud.

@@ -58,11 +59,25 @@ func WaitForIngress(s *Sandbox, ing *v1beta1.Ingress) (*v1beta1.Ingress, error)
return ing, err
}

// WaitForIngressDeletion deletes the given ingress and waits for the
// resources associated with it to be deleted.
func WaitForIngressDeletion(ctx context.Context, c cloud.Cloud, g *fuzz.GCLB, s *Sandbox, ing *v1beta1.Ingress, omitSystemDefaultBackend bool) error {
Copy link
Member

Choose a reason for hiding this comment

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

Create a struct

type IngressDeletionOptions struct {
  skipDefaultBackend bool
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, done.

Copy link
Member

@bowei bowei left a comment

Choose a reason for hiding this comment

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

small changes, otherwise lgtm

@bowei bowei merged commit 06648eb into kubernetes:master Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants