Skip to content

Commit

Permalink
Add e2e tests for neg naming and neg crd
Browse files Browse the repository at this point in the history
  • Loading branch information
swetharepakula committed Aug 5, 2020
1 parent 311b19c commit 6662cd4
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 3 deletions.
238 changes: 238 additions & 0 deletions cmd/e2e-test/neg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,241 @@ func TestReadinessReflector(t *testing.T) {
}
})
}

func TestEnableNegCRD(t *testing.T) {
t.Parallel()
port80 := intstr.FromInt(80)
port443 := intstr.FromInt(443)
serviceName := "neg-service"
expectedNEGName := "neg-name"
replicas := int32(2)
ctx := context.Background()

Framework.RunWithSandbox("NEGs with custom names", t, func(t *testing.T, s *e2e.Sandbox) {
var previousZones []string
var previousNegs map[string]string

for _, tc := range []struct {
desc string
annotations annotations.NegAnnotation
expectedNegAttrs map[string]string
gcNegAttrs map[string]string
}{
{desc: "one NEG with custom name, one neg with generated name",
annotations: annotations.NegAnnotation{
Ingress: false,
ExposedPorts: map[int32]annotations.NegAttributes{
int32(port80.IntValue()): annotations.NegAttributes{Name: expectedNEGName},
int32(port443.IntValue()): annotations.NegAttributes{},
}},
expectedNegAttrs: map[string]string{port80.String(): expectedNEGName, port443.String(): ""},
},
{desc: "remove custom name",
annotations: annotations.NegAnnotation{
Ingress: false,
ExposedPorts: map[int32]annotations.NegAttributes{
int32(port80.IntValue()): annotations.NegAttributes{},
int32(port443.IntValue()): annotations.NegAttributes{},
}},
expectedNegAttrs: map[string]string{port80.String(): "", port443.String(): ""},
gcNegAttrs: map[string]string{port80.String(): expectedNEGName},
},
{desc: "add custom name",
annotations: annotations.NegAnnotation{
Ingress: false,
ExposedPorts: map[int32]annotations.NegAttributes{
int32(port80.IntValue()): annotations.NegAttributes{},
int32(port443.IntValue()): annotations.NegAttributes{Name: expectedNEGName},
}},
expectedNegAttrs: map[string]string{port80.String(): "", port443.String(): expectedNEGName},
gcNegAttrs: map[string]string{port443.String(): ""},
},
{desc: "no NEGs",
annotations: annotations.NegAnnotation{
Ingress: false,
ExposedPorts: map[int32]annotations.NegAttributes{}},
gcNegAttrs: map[string]string{port80.String(): "", port443.String(): expectedNEGName},
},
} {
_, err := e2e.EnsureEchoService(s, serviceName, map[string]string{
annotations.NEGAnnotationKey: tc.annotations.String()}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}
t.Logf("Echo service ensured (%s/%s)", s.Namespace, serviceName)

if len(tc.gcNegAttrs) > 0 {
if err = e2e.WaitForStandaloneNegDeletion(ctx, s.ValidatorEnv.Cloud(), s, tc.gcNegAttrs, previousNegs, previousZones, true); err != nil {
t.Errorf("Error waiting for NEGDeletion: %v", err)
}
}

svc, err := e2e.WaitForNegCRs(s, serviceName, tc.expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.WaitForNegCRs(%s,%+v) = %s, want nil", serviceName, tc.expectedNegAttrs, err)
}

negStatus, err := e2e.CheckCustomNegNameStatus(svc, tc.expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.CheckCustomNegName(%s,%+v) = %s, want nil", serviceName, tc.expectedNegAttrs, err)
}

for port, negName := range negStatus.NetworkEndpointGroups {
err := e2e.WaitForNegs(ctx, Framework.Cloud, negName, negStatus.Zones, false, int(replicas))
if err != nil {
t.Fatalf("Error: e2e.WaitForNegs service %s/%s neg port/name %s/%s", serviceName, s.Namespace, port, negName)
}
}
previousZones = negStatus.Zones
previousNegs = negStatus.NetworkEndpointGroups
}
})
}

func TestNegCRDErrorEvents(t *testing.T) {
t.Parallel()
port80 := intstr.FromInt(80)
svc1 := "svc1"
svc2 := "svc2"
expectedNEGName := "test-neg-name"
replicas := int32(2)
ctx := context.Background()
annotation := annotations.NegAnnotation{
Ingress: true,
ExposedPorts: map[int32]annotations.NegAttributes{
int32(port80.IntValue()): annotations.NegAttributes{Name: expectedNEGName},
},
}

Framework.RunWithSandbox("two services, same neg name", t, func(t *testing.T, s *e2e.Sandbox) {
_, err := e2e.EnsureEchoService(s, svc1, map[string]string{
annotations.NEGAnnotationKey: annotation.String()}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}

// Ingress true with a custom name should cause an event
if err = e2e.WaitForSvcNegErrorEvents(s, svc1, 1); err != nil {
t.Errorf("error waiting for error events: %s", err)
}

// Ensure service with ingress true and wait for neg to be created
annotation.Ingress = false
_, err = e2e.EnsureEchoService(s, svc1, map[string]string{
annotations.NEGAnnotationKey: annotation.String()}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}
t.Logf("Echo service ensured (%s/%s)", s.Namespace, svc1)

expectedNegAttrs := map[string]string{port80.String(): expectedNEGName}
svc, err := e2e.WaitForNegCRs(s, svc1, expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.WaitForNegCRs(%s,%+v) = %s, want nil", svc1, expectedNegAttrs, err)
}

negStatus, err := e2e.CheckCustomNegNameStatus(svc, expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.CheckCustomNegName(%s,%+v) = %s, want nil", svc1, expectedNegAttrs, err)
}

for port, negName := range negStatus.NetworkEndpointGroups {
err := e2e.WaitForNegs(ctx, Framework.Cloud, negName, negStatus.Zones, false, int(replicas))
if err != nil {
t.Fatalf("Error: e2e.WaitForNegs service %s/%s neg port/name %s/%s", svc1, s.Namespace, port, negName)
}
}

// Ensure a second service requesting the same neg name
_, err = e2e.EnsureEchoService(s, svc2, map[string]string{
annotations.NEGAnnotationKey: annotation.String()}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}

// Requesting the same neg name should cause an error event on the second service
if err = e2e.WaitForSvcNegErrorEvents(s, svc2, 1); err != nil {
t.Errorf("error waiting for error events: %s", err)
}

// GC existing negs
_, err = e2e.EnsureEchoService(s, svc1, map[string]string{}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}

_, err = e2e.EnsureEchoService(s, svc2, map[string]string{}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}

e2e.WaitForStandaloneNegDeletion(ctx, Framework.Cloud, s, map[string]string{port80.String(): expectedNEGName}, negStatus.NetworkEndpointGroups, negStatus.Zones, true)
})
}

func TestNegCRDUserActions(t *testing.T) {
t.Parallel()
port80 := intstr.FromInt(80)
svc1 := "svc1"
expectedNEGName := "test-neg-name"
replicas := int32(2)
ctx := context.Background()
annotation := annotations.NegAnnotation{
Ingress: false,
ExposedPorts: map[int32]annotations.NegAttributes{
int32(port80.IntValue()): annotations.NegAttributes{Name: expectedNEGName},
},
}

Framework.RunWithSandbox("user interventions", t, func(t *testing.T, s *e2e.Sandbox) {
// Create a neg cr on the cluster
err := e2e.CreateNegCR(s, expectedNEGName, port80.String())
if err != nil {
t.Fatalf("error creating neg cr")
}

// Expect that the neg cr is eventually GC'ed
expectedDeletedNegs := map[string]string{port80.String(): expectedNEGName}
err = e2e.WaitForStandaloneNegDeletion(ctx, Framework.Cloud, s, expectedDeletedNegs, expectedDeletedNegs, []string{}, true)

// Ensure a sample service and wait for negs to be created
_, err = e2e.EnsureEchoService(s, svc1, map[string]string{
annotations.NEGAnnotationKey: annotation.String()}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}
t.Logf("Echo service ensured (%s/%s)", s.Namespace, svc1)

expectedNegAttrs := map[string]string{port80.String(): expectedNEGName}
svc, err := e2e.WaitForNegCRs(s, svc1, expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.WaitForNegCRs(%s,%+v) = %s, want nil", svc1, expectedNegAttrs, err)
}

negStatus, err := e2e.CheckCustomNegNameStatus(svc, expectedNegAttrs)
if err != nil {
t.Fatalf("Error: e2e.CheckCustomNegName(%s,%+v) = %s, want nil", svc1, expectedNegAttrs, err)
}

for port, negName := range negStatus.NetworkEndpointGroups {
err := e2e.WaitForNegs(ctx, Framework.Cloud, negName, negStatus.Zones, false, int(replicas))
if err != nil {
t.Fatalf("Error: e2e.WaitForNegs service %s/%s neg port/name %s/%s", svc1, s.Namespace, port, negName)
}
}

// Delete the corresponding neg cr and expect that the corresponding negs are not GC'ed
err = e2e.DeleteNegCR(s, expectedNEGName)
if err != nil {
t.Errorf("error while deleting the neg cr %s/%s", s.Namespace, expectedNEGName)
}
e2e.WaitForStandaloneNegDeletion(ctx, Framework.Cloud, s, map[string]string{port80.String(): expectedNEGName}, negStatus.NetworkEndpointGroups, negStatus.Zones, false)

// GC existing negs
_, err = e2e.EnsureEchoService(s, svc1, map[string]string{}, v1.ServiceTypeClusterIP, replicas)
if err != nil {
t.Fatalf("error ensuring echo service: %v", err)
}
e2e.WaitForStandaloneNegDeletion(ctx, Framework.Cloud, s, map[string]string{port80.String(): expectedNEGName}, negStatus.NetworkEndpointGroups, negStatus.Zones, true)
})
}
10 changes: 9 additions & 1 deletion pkg/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"k8s.io/client-go/rest"
backendconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned"
frontendconfigclient "k8s.io/ingress-gce/pkg/frontendconfig/client/clientset/versioned"
svcnegclient "k8s.io/ingress-gce/pkg/svcneg/client/clientset/versioned"
"k8s.io/klog"
)

Expand Down Expand Up @@ -76,7 +77,12 @@ func NewFramework(config *rest.Config, options Options) *Framework {

frontendConfigClient, err := frontendconfigclient.NewForConfig(config)
if err != nil {
klog.Fatalf("Failed to create BackendConfig client: %v", err)
klog.Fatalf("Failed to create FrontendConfig client: %v", err)
}

svcNegClient, err := svcnegclient.NewForConfig(config)
if err != nil {
klog.Fatalf("Failed to create SvcNeg client: %v", err)
}

f := &Framework{
Expand All @@ -85,6 +91,7 @@ func NewFramework(config *rest.Config, options Options) *Framework {
crdClient: apiextensionsclient.NewForConfigOrDie(config),
FrontendConfigClient: frontendConfigClient,
BackendConfigClient: backendConfigClient,
SvcNegClient: svcNegClient,
Project: options.Project,
Region: options.Region,
Network: options.Network,
Expand Down Expand Up @@ -127,6 +134,7 @@ type Framework struct {
crdClient *apiextensionsclient.Clientset
BackendConfigClient *backendconfigclient.Clientset
FrontendConfigClient *frontendconfigclient.Clientset
SvcNegClient *svcnegclient.Clientset
Project string
Region string
Network string
Expand Down
Loading

0 comments on commit 6662cd4

Please sign in to comment.