Skip to content

Commit

Permalink
Add e2e tests for v2 frontend namer
Browse files Browse the repository at this point in the history
  • Loading branch information
skmatti committed Nov 19, 2019
1 parent a2e96e0 commit 7488552
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 53 deletions.
57 changes: 28 additions & 29 deletions cmd/e2e-test/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package main

import (
"context"
"k8s.io/api/networking/v1beta1"
"k8s.io/ingress-gce/pkg/fuzz/features"
"fmt"
"testing"

"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress-gce/pkg/e2e"
"k8s.io/ingress-gce/pkg/fuzz"
Expand Down Expand Up @@ -55,9 +55,8 @@ func TestFinalizer(t *testing.T) {
t.Logf("Ingress created (%s/%s)", s.Namespace, ing.Name)
ing = waitForStableIngress(true, ing, s, t)

ingFinalizers := ing.GetFinalizers()
if len(ingFinalizers) != 1 || ingFinalizers[0] != common.FinalizerKey {
t.Fatalf("GetFinalizers() = %+v, want [%q]", ingFinalizers, common.FinalizerKey)
if err := checkFinalizer(ing); err != nil {
t.Fatalf("checkFinalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}

// Perform whitebox testing.
Expand Down Expand Up @@ -99,9 +98,8 @@ func TestFinalizerIngressClassChange(t *testing.T) {
t.Logf("Ingress created (%s/%s)", s.Namespace, ing.Name)
ing = waitForStableIngress(true, ing, s, t)

ingFinalizers := ing.GetFinalizers()
if len(ingFinalizers) != 1 || ingFinalizers[0] != common.FinalizerKey {
t.Fatalf("GetFinalizers() = %+v, want [%q]", ingFinalizers, common.FinalizerKey)
if err := checkFinalizer(ing); err != nil {
t.Fatalf("checkFinalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}

// Perform whitebox testing.
Expand Down Expand Up @@ -165,13 +163,11 @@ func TestFinalizerIngressesWithSharedResources(t *testing.T) {
ing = waitForStableIngress(true, ing, s, t)
otherIng = waitForStableIngress(true, otherIng, s, t)

ingFinalizers := ing.GetFinalizers()
if len(ingFinalizers) != 1 || ingFinalizers[0] != common.FinalizerKey {
t.Fatalf("GetFinalizers() = %+v, want [%q]", ingFinalizers, common.FinalizerKey)
if err := checkFinalizer(ing); err != nil {
t.Fatalf("checkFinalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}
otherIngFinalizers := otherIng.GetFinalizers()
if len(otherIngFinalizers) != 1 || otherIngFinalizers[0] != common.FinalizerKey {
t.Fatalf("GetFinalizers() = %+v, want [%q]", otherIngFinalizers, common.FinalizerKey)
if err := checkFinalizer(otherIng); err != nil {
t.Fatalf("checkFinalizer(%s/%s) = %v, want nil", otherIng.Namespace, otherIng.Name, err)
}

// Perform whitebox testing.
Expand Down Expand Up @@ -250,6 +246,16 @@ func TestUpdateTo1dot7(t *testing.T) {
t.Errorf("e2e.WaitForFinalizer(_, %q) = %v, want nil", ing.Name, err)
}

// Assert that the ingress has v1 finalizer.
if updatedIng, err := crud.Get(ing.Namespace, ing.Name); err != nil {
t.Fatalf("create(%s/%s) = %v, want nil; Ingress: %v", ing.Namespace, ing.Name, err, ing)
} else {
ing = updatedIng
}
if err := checkV1Finalizer(ing); err != nil {
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}

// Perform whitebox testing.
gclb := whiteboxTest(ing, s, t, "")

Expand All @@ -265,21 +271,14 @@ func TestUpdateTo1dot7(t *testing.T) {
})
}

func checkGCLB(t *testing.T, s *e2e.Sandbox, ing *v1beta1.Ingress, numForwardingRules, numBackendServices int) *fuzz.GCLB {
// Perform whitebox testing.
if len(ing.Status.LoadBalancer.Ingress) < 1 {
t.Fatalf("Ingress does not have an IP: %+v", ing.Status)
// checkFinalizer asserts that an ingress finalizer exists on Ingress.
func checkFinalizer(ing *v1beta1.Ingress) error {
ingFinalizers := ing.GetFinalizers()
if l := len(ingFinalizers); l != 1 {
return fmt.Errorf("expected 1 Finalizer but got %d", l)
}
vip := ing.Status.LoadBalancer.Ingress[0].IP
t.Logf("Ingress %s/%s VIP = %s", s.Namespace, ing.Name, vip)
params := &fuzz.GCLBForVIPParams{VIP: vip, Validators: fuzz.FeatureValidators(features.All)}
gclb, err := fuzz.GCLBForVIP(context.Background(), Framework.Cloud, params)
if err != nil {
t.Fatalf("GCLBForVIP(..., %q, _) = %v, want nil; error getting GCP resources for LB with IP", vip, err)
}

if err = e2e.CheckGCLB(gclb, numForwardingRules, numBackendServices); err != nil {
t.Error(err)
if ingFinalizers[0] != common.FinalizerKey && ingFinalizers[0] != common.FinalizerKeyV2 {
return fmt.Errorf("unexpected finalizer %q found", ingFinalizers[0])
}
return gclb
return nil
}
235 changes: 235 additions & 0 deletions cmd/e2e-test/v2frontendnamer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"fmt"
"testing"

"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress-gce/pkg/e2e"
"k8s.io/ingress-gce/pkg/fuzz"
"k8s.io/ingress-gce/pkg/utils/common"
)

// TestV2FrontendNamer tests ingress basic lifecycle with v2 frontend naming scheme.
// This also tests the lifecycle an ingress with v1 naming scheme with v2 namer enabled.
func TestV2FrontendNamer(t *testing.T) {
t.Parallel()
port80 := intstr.FromInt(80)
svcName := "service-1"
v1Ing := fuzz.NewIngressBuilder("", "ing-v1", "").
AddPath("foo.com", "/", svcName, port80).
Build()
v1Ing.SetFinalizers([]string{common.FinalizerKey})
v2Ing := fuzz.NewIngressBuilder("", "ing-v2", "").
AddPath("foo.com", "/", svcName, port80).
Build()

for _, tc := range []struct {
desc string
ings []*v1beta1.Ingress
}{
{"v2 only", []*v1beta1.Ingress{v2Ing}},
{"v1 only", []*v1beta1.Ingress{v1Ing}},
{"both v1 and v2, delete v1 first", []*v1beta1.Ingress{v1Ing, v2Ing}},
{"both v1 and v2, delete v2 first", []*v1beta1.Ingress{v2Ing, v1Ing}},
} {
tc := tc
desc := fmt.Sprintf("v2 frontend namer %s", tc.desc)
Framework.RunWithSandbox(desc, t, func(t *testing.T, s *e2e.Sandbox) {
t.Parallel()
ctx := context.Background()

_, err := e2e.CreateEchoService(s, svcName, nil)
if err != nil {
t.Fatalf("CreateEchoService(_, %q, nil): %v, want nil", svcName, err)
}
t.Logf("Echo service created (%s/%s)", s.Namespace, svcName)

crud := e2e.IngressCRUD{C: Framework.Clientset}
var gclbs []*fuzz.GCLB
var updatedIngs []*v1beta1.Ingress

// Create Ingresses.
for _, ing := range tc.ings {
ing = ing.DeepCopy()
ing.Namespace = s.Namespace
if _, err := crud.Create(ing); err != nil {
t.Fatalf("create(%s/%s) = %v, want nil; Ingress: %v", ing.Namespace, ing.Name, err, ing)
}
t.Logf("Ingress created (%s/%s)", ing.Namespace, ing.Name)
}
// Perform whitebox testing.
for _, ing := range tc.ings {
// Determine the expected finalizer after ingress creation.
isV1Finalizer := false
if len(ing.GetFinalizers()) > 0 {
isV1Finalizer = true
}
ing = waitForStableIngress(true, ing, s, t)
if isV1Finalizer {
// Assert that v1 finalizer is added.
if err := checkV1Finalizer(ing); err != nil {
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}
} else {
// Assert that v2 finalizer is added.
if err := checkV2Finalizer(ing); err != nil {
t.Fatalf("checkV2Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}
}
// Perform whitebox testing. This also tests naming scheme.
gclbs = append(gclbs, whiteboxTest(ing, s, t, ""))
updatedIngs = append(updatedIngs, ing)
}

// Delete the first ingress.
deleteOptions := &fuzz.GCLBDeleteOptions{
SkipDefaultBackend: true,
}
// Determine if we need to skip backends.
if len(updatedIngs) > 1 {
deleteOptions.SkipBackends = true
}
if err := e2e.WaitForIngressDeletion(ctx, gclbs[0], s, updatedIngs[0], deleteOptions); err != nil {
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", tc.ings[0].Name, err)
}

if len(updatedIngs) > 1 {
// Verify that GCE frontend resources of second ingress are intact.
gclb := whiteboxTest(updatedIngs[1], s, t, "")
// Delete the second ingress.
deleteOptions.SkipBackends = false
if err := e2e.WaitForIngressDeletion(ctx, gclb, s, updatedIngs[1], deleteOptions); err != nil {
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", tc.ings[1].Name, err)
}
}
})
}
}

// TestUpdateTo1dot8 asserts that v1 finalizer is retained/attached and ingress
// continues to use v1 naming naming scheme when master is upgraded to a version that use v1.8.
// Note: The test is named in such a way that it does run as a workflow test or an
// upgrade test for other ingress versions.
func TestUpdateTo1dot8(t *testing.T) {
port80 := intstr.FromInt(80)
svcName := "service-1"
ing := fuzz.NewIngressBuilder("", "ingress-1", "").
AddPath("foo.com", "/", svcName, port80).
SetIngressClass("gce").
Build()
Framework.RunWithSandbox("v2namer-master-upgrade", t, func(t *testing.T, s *e2e.Sandbox) {
t.Parallel()

_, err := e2e.CreateEchoService(s, svcName, nil)
if err != nil {
t.Fatalf("CreateEchoService(_, %q, nil): %v, want nil", svcName, err)
}
t.Logf("Echo service created (%s/%s)", s.Namespace, svcName)

crud := e2e.IngressCRUD{C: Framework.Clientset}
ing.Namespace = s.Namespace
if _, err := crud.Create(ing); err != nil {
t.Fatalf("create(%s/%s) = %v, want nil; Ingress: %v", ing.Namespace, ing.Name, err, ing)
}
t.Logf("Ingress created (%s/%s)", s.Namespace, ing.Name)
ing = waitForStableIngress(true, ing, s, t)

// Assert that v1 finalizer is added.
if err := checkV1Finalizer(ing); err != nil {
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}

// Perform whitebox testing.
whiteboxTest(ing, s, t, "")

for {
// While k8s master is upgrading, it will return a connection refused
// error for any k8s resource we try to hit. We loop until the
// master upgrade has finished.
if s.MasterUpgrading() {
continue
}

if s.MasterUpgraded() {
t.Logf("Detected master upgrade, adding a path to Ingress to force Ingress update")
newIng := fuzz.NewIngressBuilderFromExisting(ing).
AddPath("bar.com", "/", "service-1", port80).
Build()
newIng.Namespace = s.Namespace
if _, err := crud.Update(newIng); err != nil {
t.Fatalf("error creating Ingress spec: %v", err)
} else {
// If Ingress upgrade succeeds, we update the status on this Ingress
// to Unstable. It is set back to Stable after WaitForIngress below
// finishes successfully.
s.PutStatus(e2e.Unstable)
}
break
}
}

// Wait for ingress to stabilize after the upgrade.
ing = waitForStableIngress(false, ing, s, t)

// Assert that v1 finalizer is retained.
if err := checkV1Finalizer(ing); err != nil {
t.Fatalf("checkV1Finalizer(%s/%s) = %v, want nil", ing.Namespace, ing.Name, err)
}

// Perform whitebox testing.
gclb := whiteboxTest(ing, s, t, "")

// If the Master has upgraded and the Ingress is stable,
// we delete the Ingress and exit out of the loop to indicate that
// the test is done.
deleteOptions := &fuzz.GCLBDeleteOptions{
SkipDefaultBackend: true,
}
if err := e2e.WaitForIngressDeletion(context.Background(), gclb, s, ing, deleteOptions); err != nil {
t.Errorf("e2e.WaitForIngressDeletion(..., %q, nil) = %v, want nil", ing.Name, err)
}
})
}

// checkV1Finalizer asserts that v1 finalizer exists on Ingress.
func checkV1Finalizer(ing *v1beta1.Ingress) error {
ingFinalizers := ing.GetFinalizers()
if l := len(ingFinalizers); l != 1 {
return fmt.Errorf("expected 1 Finalizer but got %d", l)
}
if ingFinalizers[0] != common.FinalizerKey {
return fmt.Errorf("expected Finalizer %q but got %q", common.FinalizerKey, ingFinalizers[0])
}
return nil
}

// checkV2Finalizer asserts that v2 finalizer exists on Ingress.
func checkV2Finalizer(ing *v1beta1.Ingress) error {
ingFinalizers := ing.GetFinalizers()
if l := len(ingFinalizers); l != 1 {
return fmt.Errorf("expected 1 Finalizer but got %d", l)
}
if ingFinalizers[0] != common.FinalizerKeyV2 {
return fmt.Errorf("expected Finalizer %q but got %q", common.FinalizerKeyV2, ingFinalizers[0])
}
return nil
}
4 changes: 4 additions & 0 deletions cmd/fuzzer/app/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func Validate() {
if err := iv.PerformWhiteboxTests(gclb); err != nil {
panic(err)
}

if err := iv.FrontendNamingSchemeTest(gclb); err != nil {
panic(err)
}
}

func homeDir() string {
Expand Down
10 changes: 7 additions & 3 deletions pkg/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"k8s.io/ingress-gce/pkg/fuzz"
"k8s.io/ingress-gce/pkg/fuzz/features"
"k8s.io/ingress-gce/pkg/fuzz/whitebox"
"k8s.io/ingress-gce/pkg/utils/common"
"k8s.io/klog"
)

Expand Down Expand Up @@ -132,7 +131,9 @@ func WaitForFinalizer(s *Sandbox, ingName string) error {
return false, nil
}
ingFinalizers := ing.GetFinalizers()
if len(ingFinalizers) != 1 || ingFinalizers[0] != common.FinalizerKey {
if l := len(ingFinalizers); l > 1 {
return true, fmt.Errorf("unexpected number of finalizers for ingress %v, got %d", ing, l)
} else if l != 1 {
klog.Infof("WaitForFinalizer(%s/%s) = %v, finalizer not added for Ingress %v", s.Namespace, ingName, ingFinalizers, ing)
return false, nil
}
Expand All @@ -146,7 +147,10 @@ func PerformWhiteboxTests(s *Sandbox, ing *v1beta1.Ingress, gclb *fuzz.GCLB) err
if err != nil {
return err
}
return validator.PerformWhiteboxTests(gclb)
if err := validator.PerformWhiteboxTests(gclb); err != nil {
return err
}
return validator.FrontendNamingSchemeTest(gclb)
}

// WaitForIngressDeletion deletes the given ingress and waits for the
Expand Down
Loading

0 comments on commit 7488552

Please sign in to comment.