From 9d1f6ccf8ec2e63e066ef789e0bb38b6525bf507 Mon Sep 17 00:00:00 2001 From: roy wang Date: Fri, 28 Aug 2020 23:30:03 +0900 Subject: [PATCH] fix issue#187 add e2e-test & modify unit test Signed-off-by: roy wang --- .../applicationconfiguration/apply.go | 4 +- .../applicationconfiguration/apply_test.go | 6 ++- test/e2e-test/appconfig_finalizer_test.go | 2 +- test/e2e-test/health_scope_test.go | 47 +++++++++++++++++-- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/pkg/controller/v1alpha2/applicationconfiguration/apply.go b/pkg/controller/v1alpha2/applicationconfiguration/apply.go index 1ba6c43e..564e0256 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/apply.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/apply.go @@ -118,7 +118,9 @@ func (a *workloads) Apply(ctx context.Context, status []v1alpha2.WorkloadStatus, } for _, s := range wl.Scopes { - return a.applyScope(ctx, wl, s, workloadRef) + if err := a.applyScope(ctx, wl, s, workloadRef); err != nil { + return err + } } } diff --git a/pkg/controller/v1alpha2/applicationconfiguration/apply_test.go b/pkg/controller/v1alpha2/applicationconfiguration/apply_test.go index 3ebb5150..3e246240 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/apply_test.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/apply_test.go @@ -86,7 +86,7 @@ func TestApplyWorkloads(t *testing.T) { Namespace: namespace, }, TypeMeta: metav1.TypeMeta{ - APIVersion: "scope.oam.dev", + APIVersion: "scope.oam.dev/v1alpha2", Kind: "scopeKind", }, Spec: v1alpha2.HealthScopeSpec{ @@ -272,6 +272,10 @@ func TestApplyWorkloads(t *testing.T) { client: resource.ApplyFn(func(_ context.Context, o runtime.Object, _ ...resource.ApplyOption) error { return nil }), rawClient: &test.MockClient{ MockGet: func(_ context.Context, key client.ObjectKey, obj runtime.Object) error { + if scopeDef, ok := obj.(*v1alpha2.ScopeDefinition); ok { + *scopeDef = scopeDefinition + return nil + } return nil }, MockUpdate: func(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error { diff --git a/test/e2e-test/appconfig_finalizer_test.go b/test/e2e-test/appconfig_finalizer_test.go index f218265d..e37fb017 100644 --- a/test/e2e-test/appconfig_finalizer_test.go +++ b/test/e2e-test/appconfig_finalizer_test.go @@ -22,7 +22,7 @@ var ( workloadScopeFinalizer = "scope.finalizer.core.oam.dev" ) -var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() { +var _ = Describe("Finalizer for HealthScope in ApplicationConfiguration", func() { ctx := context.Background() namespace := "finalizer-test" ns := corev1.Namespace{ diff --git a/test/e2e-test/health_scope_test.go b/test/e2e-test/health_scope_test.go index 73845cb7..62a805d4 100644 --- a/test/e2e-test/health_scope_test.go +++ b/test/e2e-test/health_scope_test.go @@ -176,7 +176,8 @@ var _ = Describe("HealthScope", func() { logf.Log.Info("Creating component", "Name", comp.Name, "Namespace", comp.Namespace) Expect(k8sClient.Create(ctx, &comp)).Should(BeNil()) // Create application configuration - workloadInstanceName := "example-appconfig-healthscope" + workloadInstanceName1 := "example-appconfig-healthscope-a" + workloadInstanceName2 := "example-appconfig-healthscope-b" imageName := "wordpress:php7.2" appConfig := v1alpha2.ApplicationConfiguration{ ObjectMeta: metav1.ObjectMeta{ @@ -191,7 +192,29 @@ var _ = Describe("HealthScope", func() { ParameterValues: []v1alpha2.ComponentParameterValue{ { Name: "instance-name", - Value: intstr.IntOrString{StrVal: workloadInstanceName, Type: intstr.String}, + Value: intstr.IntOrString{StrVal: workloadInstanceName1, Type: intstr.String}, + }, + { + Name: "image", + Value: intstr.IntOrString{StrVal: imageName, Type: intstr.String}, + }, + }, + Scopes: []v1alpha2.ComponentScope{ + { + ScopeReference: v1alpha1.TypedReference{ + APIVersion: gvks[0].GroupVersion().String(), + Kind: v1alpha2.HealthScopeGroupVersionKind.Kind, + Name: healthScopeName, + }, + }, + }, + }, + { + ComponentName: componentName, + ParameterValues: []v1alpha2.ComponentParameterValue{ + { + Name: "instance-name", + Value: intstr.IntOrString{StrVal: workloadInstanceName2, Type: intstr.String}, }, { Name: "image", @@ -214,9 +237,9 @@ var _ = Describe("HealthScope", func() { logf.Log.Info("Creating application config", "Name", appConfig.Name, "Namespace", appConfig.Namespace) Expect(k8sClient.Create(ctx, &appConfig)).Should(BeNil()) // Verification - By("Checking deployment is created") + By("Checking deployment-a is created") objectKey := client.ObjectKey{ - Name: workloadInstanceName, + Name: workloadInstanceName1, Namespace: namespace, } deploy := &appsv1.Deployment{} @@ -227,6 +250,20 @@ var _ = Describe("HealthScope", func() { }, time.Second*15, time.Millisecond*500).Should(BeNil()) + // Verify all components declared in AppConfig are created + By("Checking deployment-b is created") + objectKey2 := client.ObjectKey{ + Name: workloadInstanceName2, + Namespace: namespace, + } + deploy2 := &appsv1.Deployment{} + logf.Log.Info("Checking on deployment", "Key", objectKey2) + Eventually( + func() error { + return k8sClient.Get(ctx, objectKey2, deploy2) + }, + time.Second*15, time.Millisecond*500).Should(BeNil()) + By("Verify that the parameter substitute works") Expect(deploy.Spec.Template.Spec.Containers[0].Image).Should(Equal(imageName)) @@ -261,6 +298,6 @@ var _ = Describe("HealthScope", func() { return healthScope.Status.Health == "healthy" }, - time.Second*60, time.Second*5).Should(BeEquivalentTo(true)) + time.Second*120, time.Second*5).Should(BeEquivalentTo(true)) }) })