Merge pull request #1111 from captainroy-hy/chp-1041

cherry-pick(1041&1036) fix unit test
This commit is contained in:
Jianbo Sun
2021-02-26 18:57:10 +08:00
committed by GitHub
6 changed files with 150 additions and 100 deletions
+47 -34
View File
@@ -34,7 +34,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/pkg/oam/util"
@@ -426,37 +425,33 @@ var _ = Describe("Test appFile parser", func() {
Name: "myweb",
Namespace: "default",
Labels: map[string]string{"application.oam.dev": "test"},
}, Spec: v1alpha2.ComponentSpec{
Workload: runtime.RawExtension{
Object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
"workload.oam.dev/type": "worker",
"app.oam.dev/component": "myweb",
"app.oam.dev/name": "test",
},
},
"spec": map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"app.oam.dev/component": "myweb"}},
"template": map[string]interface{}{
"metadata": map[string]interface{}{"labels": map[string]interface{}{"app.oam.dev/component": "myweb"}},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"command": []interface{}{"sleep", "1000"},
"image": "busybox",
"name": "myweb",
"env": []interface{}{
map[string]interface{}{"name": "c1", "value": "v1"},
map[string]interface{}{"name": "c2", "value": "v2"},
},
},
},
}}
expectWorkload := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
"workload.oam.dev/type": "worker",
"app.oam.dev/component": "myweb",
"app.oam.dev/name": "test",
},
},
"spec": map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"app.oam.dev/component": "myweb"}},
"template": map[string]interface{}{
"metadata": map[string]interface{}{"labels": map[string]interface{}{"app.oam.dev/component": "myweb"}},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"command": []interface{}{"sleep", "1000"},
"image": "busybox",
"name": "myweb",
"env": []interface{}{
map[string]interface{}{"name": "c1", "value": "v1"},
map[string]interface{}{"name": "c2", "value": "v2"},
},
},
},
@@ -465,12 +460,30 @@ var _ = Describe("Test appFile parser", func() {
},
},
}
// assertion util cannot compare slices embedded in map correctly while slice order is not required
// e.g., .containers[0].env in this case
// as a workaround, prepare two expected targets covering all possible slice order
// if any one is satisfied, the equal assertion pass
expectWorkloadOptional := expectWorkload.DeepCopy()
unstructured.SetNestedSlice(expectWorkloadOptional.Object, []interface{}{
map[string]interface{}{
"command": []interface{}{"sleep", "1000"},
"image": "busybox",
"name": "myweb",
"env": []interface{}{
map[string]interface{}{"name": "c2", "value": "v2"},
map[string]interface{}{"name": "c1", "value": "v1"},
},
},
}, "spec", "template", "spec", "containers")
By(" built components' length must be 1")
Expect(len(components)).To(BeEquivalentTo(1))
Expect(components[0].ObjectMeta).To(BeEquivalentTo(expectComponent.ObjectMeta))
Expect(components[0].TypeMeta).To(BeEquivalentTo(expectComponent.TypeMeta))
logf.Log.Info(cmp.Diff(components[0].Spec.Workload.Object, expectComponent.Spec.Workload.Object))
Expect(assert.ObjectsAreEqual(components[0].Spec.Workload.Object, expectComponent.Spec.Workload.Object)).To(BeTrue())
Expect(components[0].Spec.Workload.Object).Should(SatisfyAny(
BeEquivalentTo(expectWorkload),
BeEquivalentTo(expectWorkloadOptional)))
})
})
@@ -136,13 +136,12 @@ spec:
return k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: appName}, ac)
}, 3*time.Second, 300*time.Millisecond).Should(BeNil())
By("Reconcile")
reconcileRetry(reconciler, req)
By("Check workload created successfully")
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
return k8sClient.Get(ctx, workloadKey, &workload)
}, 5*time.Second, 300*time.Millisecond).Should(BeNil())
}, 5*time.Second, time.Second).Should(BeNil())
By("Check reconcile again and no error will happen")
reconcileRetry(reconciler, req)
@@ -223,9 +222,14 @@ spec:
By("Check new trait CR is applied")
scale := v1alpha2.ManualScalerTrait{}
scaleKey := client.ObjectKey{Name: scaleName, Namespace: namespace}
err = k8sClient.Get(ctx, scaleKey, &scale)
Expect(err).Should(BeNil())
Expect(scale.Spec.ReplicaCount).Should(Equal(int32(3)))
Eventually(func() int32 {
By("Reconcile")
reconcileRetry(reconciler, req)
if err := k8sClient.Get(ctx, scaleKey, &scale); err != nil {
return 0
}
return scale.Spec.ReplicaCount
}, 5*time.Second, time.Second).Should(Equal(int32(3)))
})
AfterEach(func() {
@@ -143,8 +143,7 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
}, time.Second, 300*time.Millisecond).Should(BeNil())
By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())
time.Sleep(3 * time.Second)
reconcileRetry(reconciler, req)
})
AfterEach(func() {
@@ -168,6 +167,8 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
By("Get workload instance & Check workload spec")
cwObj := v1alpha2.ContainerizedWorkload{}
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
return k8sClient.Get(ctx, cwObjKey, &cwObj)
}, 5*time.Second, time.Second).Should(BeNil())
Expect(cwObj.Spec.Containers[0].Image).Should(Equal(image1))
@@ -269,6 +270,8 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
By("Get workload instance & Check workload spec")
cwObj := v1alpha2.ContainerizedWorkload{}
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
return k8sClient.Get(ctx, cwObjKey, &cwObj)
}, 5*time.Second, time.Second).Should(BeNil())
@@ -490,6 +493,8 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
By("Get workload instance & Check workload spec")
cwObj := v1alpha2.ContainerizedWorkload{}
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
return k8sClient.Get(ctx, cwObjKey, &cwObj)
}, 5*time.Second, time.Second).Should(BeNil())
Expect(cwObj.Spec.Containers[0].Image).Should(Equal(image1))
@@ -591,8 +596,10 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
By("Get workload instance")
cwObj := v1alpha2.ContainerizedWorkload{}
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
return k8sClient.Get(ctx, cwObjKey, &cwObj)
}, 3*time.Second, time.Second).Should(BeNil())
}, 5*time.Second, time.Second).Should(BeNil())
By("Get trait instance & Check trait spec")
fooObj := unstructured.Unstructured{}
@@ -645,13 +652,22 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
Expect(k8sClient.Patch(ctx, &appConfig, client.Merge)).Should(Succeed())
time.Sleep(1 * time.Second)
By("Reconcile")
reconcileRetry(reconciler, req)
time.Sleep(2 * time.Second)
By("Check AppConfig is updated successfully")
updateAC := v1alpha2.ApplicationConfiguration{}
Eventually(func() int64 {
if err := k8sClient.Get(ctx, appConfigKey, &updateAC); err != nil {
return 0
}
return updateAC.GetGeneration()
}, 3*time.Second, time.Second).Should(Equal(int64(2)))
By("Check workload is re-created by reconciliation")
recreatedCwObj = v1alpha2.ContainerizedWorkload{}
Expect(k8sClient.Get(ctx, cwObjKey, &recreatedCwObj)).Should(Succeed())
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
recreatedCwObj = v1alpha2.ContainerizedWorkload{}
return k8sClient.Get(ctx, cwObjKey, &recreatedCwObj)
}, 5*time.Second, time.Second).Should(Succeed())
By("Check trait is re-created by reconciliation")
recreatedFooObj = unstructured.Unstructured{}
@@ -236,31 +236,26 @@ spec:
return appConfig.GetGeneration()
}, time.Second, 300*time.Millisecond).Should(Equal(int64(2)))
By("Reconcile")
reconcileRetry(reconciler, req)
Eventually(func() string {
By("Reconcile & check updated trait")
var traitObj unstructured.Unstructured
Eventually(func() int64 {
reconcileRetry(reconciler, req)
if err := k8sClient.Get(ctx, appConfigKey, &appConfig); err != nil {
return ""
return 0
}
if appConfig.Status.Workloads == nil {
reconcileRetry(reconciler, req)
return ""
return 0
}
return appConfig.Status.Workloads[0].Traits[0].Reference.Name
}, 3*time.Second, time.Second).ShouldNot(BeEmpty())
By("Get changed trait object")
traitName := appConfig.Status.Workloads[0].Traits[0].Reference.Name
var traitObj unstructured.Unstructured
traitObj.SetAPIVersion("example.com/v1")
traitObj.SetKind("Bar")
Eventually(func() int64 {
traitName := appConfig.Status.Workloads[0].Traits[0].Reference.Name
traitObj.SetAPIVersion("example.com/v1")
traitObj.SetKind("Bar")
if err := k8sClient.Get(ctx,
client.ObjectKey{Namespace: namespace, Name: traitName}, &traitObj); err != nil {
return 0
}
return traitObj.GetGeneration()
}, 3*time.Second, time.Second).Should(Equal(int64(2)))
}, 5*time.Second, time.Second).Should(Equal(int64(2)))
By("Check labels are removed")
_, found, _ := unstructured.NestedString(traitObj.UnstructuredContent(), "metadata", "labels", "test.label")
@@ -355,13 +350,19 @@ spec:
}
return appConfig.GetGeneration()
}, time.Second, 300*time.Millisecond).Should(Equal(int64(2)))
By("Reconcile")
reconcileRetry(reconciler, req)
changedTrait = unstructured.Unstructured{}
changedTrait.SetAPIVersion("example.com/v1")
changedTrait.SetKind("Bar")
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: traitName}, &changedTrait)).Should(Succeed())
Eventually(func() int64 {
By("Reconcile")
reconcileRetry(reconciler, req)
changedTrait = unstructured.Unstructured{}
changedTrait.SetAPIVersion("example.com/v1")
changedTrait.SetKind("Bar")
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: traitName}, &changedTrait); err != nil {
return 0
}
return changedTrait.GetGeneration()
}, 5*time.Second, time.Second).Should(Equal(int64(3)))
By("Check AppConfig's change works")
// changed a field
v, _, _ = unstructured.NestedString(changedTrait.UnstructuredContent(), "spec", "valueChanged")
@@ -80,8 +80,23 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
AfterEach(func() {
logf.Log.Info("Clean up resources")
// delete the namespace with all its resources
Expect(k8sClient.Delete(ctx, in)).Should(SatisfyAny(BeNil(), &util.NotFoundMatcher{}))
Expect(k8sClient.Delete(ctx, out)).Should(BeNil())
ac := &v1alpha2.ApplicationConfiguration{}
Expect(k8sClient.DeleteAllOf(ctx, ac, client.InNamespace(namespace))).Should(Succeed())
cm := &corev1.ConfigMap{}
Expect(k8sClient.DeleteAllOf(ctx, cm, client.InNamespace(namespace))).Should(Succeed())
foo := &unstructured.Unstructured{}
foo.SetAPIVersion("example.com/v1")
foo.SetKind("Foo")
Expect(k8sClient.DeleteAllOf(ctx, foo, client.InNamespace(namespace))).Should(Succeed())
Eventually(func() bool {
l := &unstructured.UnstructuredList{}
l.SetAPIVersion("example.com/v1")
l.SetKind("Foo")
if err := k8sClient.List(ctx, l, client.InNamespace(namespace)); err != nil {
return false
}
return len(l.Items) == 0
}, 3*time.Second, time.Second).Should(BeTrue())
})
// common function for verification
@@ -114,11 +129,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Eventually(func() error {
err := k8sClient.Get(ctx, outFooKey, outFoo)
if err != nil {
// Try 3 (= 1s/300ms) times
// Try 3 (= 3s/1s) times
reconciler.Reconcile(req)
}
return err
}, time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
By("Reconcile")
reconcileRetry(reconciler, req)
@@ -159,7 +174,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
k8sClient.Get(ctx, outFooKey, outFoo)
data, _, _ := unstructured.NestedString(outFoo.Object, "status", "key")
return data
}, time.Second, 300*time.Millisecond).Should(Equal("test"))
}, 3*time.Second, time.Second).Should(Equal("test"))
By("Reconcile")
reconcileRetry(reconciler, req)
@@ -175,7 +190,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
reconciler.Reconcile(req)
k8sClient.Get(ctx, appconfigKey, appconfig)
return appconfig.Status.Dependency.Unsatisfied
}, 2*time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
}
It("trait depends on another trait", func() {
@@ -372,7 +387,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Expect(k8sClient.Create(ctx, &appConfig)).Should(Succeed())
Eventually(func() error {
return k8sClient.Get(ctx, appconfigKey, &appConfig)
}, time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
By("Reconcile")
reconcileRetry(reconciler, req)
@@ -399,11 +414,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Eventually(func() error {
err := k8sClient.Get(ctx, outFooKey, outFoo)
if err != nil {
// Try 3 (= 1s/300ms) times
// Try 3 (= 3s/1s) times
reconciler.Reconcile(req)
}
return err
}, time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
err := unstructured.SetNestedField(outFoo.Object, "test", "status", "key")
Expect(err).Should(BeNil())
@@ -423,11 +438,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
err = k8sClient.Get(ctx, appconfigKey, tempApp)
tempApp.DeepCopyInto(newAppConfig)
if err != nil || tempApp.Status.Dependency.Unsatisfied != nil {
// Try 3 (= 1s/300ms) times
// Try 3 (= 3s/1s) times
reconciler.Reconcile(req)
}
return tempApp.Status.Dependency.Unsatisfied
}(), time.Second, 300*time.Millisecond).Should(BeNil())
}(), 3*time.Second, time.Second).Should(BeNil())
By("Checking that resource which accepts data is created now")
logf.Log.Info("Checking on resource that inputs data", "Key", inFooKey)
@@ -436,11 +451,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Eventually(func() error {
err := k8sClient.Get(ctx, outFooKey, outFoo)
if err != nil {
// Try 3 (= 1s/300ms) times
// Try 3 (= 3s/1s) times
reconciler.Reconcile(req)
}
return err
}, time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
err = unstructured.SetNestedField(outFoo.Object, "test", "status", "key")
Expect(err).Should(BeNil())
err = unstructured.SetNestedField(outFoo.Object, "hash-v1", "status", "app-hash")
@@ -453,7 +468,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
}
s, _, _ := unstructured.NestedString(outFoo.Object, "status", "key")
return s == "test"
}, time.Second, 300*time.Millisecond).Should(BeTrue())
}, 3*time.Second, time.Second).Should(BeTrue())
newAppConfig.Labels["app-hash"] = "hash-v2"
By("Update newAppConfig & check successfully")
@@ -464,10 +479,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
return false
}
return newAppConfig.Labels["app-hash"] == "hash-v2"
}, time.Second, 300*time.Millisecond).Should(BeTrue())
By("Reconcile")
reconcileRetry(reconciler, req)
}, 3*time.Second, time.Second).Should(BeTrue())
By("Verify the appconfig's dependency should be unsatisfied, because requirementCondition valueFrom not match")
depStatus := v1alpha2.DependencyStatus{
@@ -492,9 +504,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
}}}},
}
Eventually(func() v1alpha2.DependencyStatus {
By("Reconcile")
reconcileRetry(reconciler, req)
k8sClient.Get(ctx, appconfigKey, newAppConfig)
return newAppConfig.Status.Dependency
}, time.Second, 300*time.Millisecond).Should(Equal(depStatus))
}, 3*time.Second, time.Second).Should(Equal(depStatus))
By("Update trait resource to meet the requirement")
Expect(k8sClient.Get(ctx, outFooKey, outFoo)).Should(BeNil()) // Get the latest before update
@@ -508,7 +522,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
}
s, _, _ := unstructured.NestedString(outFoo.Object, "status", "key")
return s == "test-new"
}, time.Second, 300*time.Millisecond).Should(BeTrue())
}, 3*time.Second, time.Second).Should(BeTrue())
By("Reconcile")
reconcileRetry(reconciler, req)
@@ -519,11 +533,11 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
tempAppConfig := &v1alpha2.ApplicationConfiguration{}
err := k8sClient.Get(ctx, appconfigKey, tempAppConfig)
if err != nil || tempAppConfig.Status.Dependency.Unsatisfied != nil {
// Try 3 (= 1s/300ms) times
// Try 3 (= 3s/1s) times
reconciler.Reconcile(req)
}
return tempAppConfig.Status.Dependency.Unsatisfied
}(), time.Second, 300*time.Millisecond).Should(BeNil())
}(), 3*time.Second, time.Second).Should(BeNil())
By("Checking that resource which accepts data is updated")
Expect(func() string {
@@ -625,6 +639,9 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Name: appConfigName,
Namespace: namespace,
}
Eventually(func() error {
return k8sClient.Get(ctx, appconfigKey, &v1alpha2.ApplicationConfiguration{})
}, 3*time.Second, time.Second).Should(Succeed())
By("Reconcile")
req := reconcile.Request{NamespacedName: appconfigKey}
reconcileRetry(reconciler, req)
@@ -641,7 +658,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
reconciler.Reconcile(req)
}
return err
}, time.Second, 300*time.Millisecond).Should(BeNil())
}, 3*time.Second, time.Second).Should(BeNil())
By("Get reconciled AppConfig the first time")
appconfig := &v1alpha2.ApplicationConfiguration{}
@@ -669,7 +686,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
k8sClient.Get(ctx, outFooKey, outFoo)
data, _, _ := unstructured.NestedSlice(outFoo.Object, "status", "complex2")
return data
}, time.Second, 300*time.Millisecond).Should(BeEquivalentTo(complex2))
}, 3*time.Second, time.Second).Should(BeEquivalentTo(complex2))
By("Reconcile")
reconcileRetry(reconciler, req)
@@ -680,7 +697,7 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
reconciler.Reconcile(req)
k8sClient.Get(ctx, appconfigKey, appconfig)
return appconfig.Status.Dependency.Unsatisfied
}, 2*time.Second, 300*time.Millisecond).Should(BeNil())
}, 2*3*time.Second, time.Second).Should(BeNil())
// Verification after satisfying dependency
By("Checking that resource which accepts data is created now")
inFooKey := client.ObjectKey{
@@ -202,11 +202,10 @@ var _ = Describe("Test ApplicationConfiguration Component Revision Enabled trait
return k8sClient.Get(ctx, appConfigKey, &appConfig)
}, time.Second, 300*time.Millisecond).Should(BeNil())
By("Reconcile")
reconcileRetry(reconciler, req)
By("Check workload created successfully")
Eventually(func() error {
By("Reconcile")
reconcileRetry(reconciler, req)
var workloadKey = client.ObjectKey{Namespace: namespace, Name: compName + "-v1"}
return k8sClient.Get(ctx, workloadKey, &wr)
}, 3*time.Second, 300*time.Millisecond).Should(BeNil())
@@ -297,11 +296,11 @@ var _ = Describe("Test ApplicationConfiguration Component Revision Enabled trait
Eventually(func() error {
return k8sClient.Get(ctx, appConfigKey, &appConfig)
}, time.Second, 300*time.Millisecond).Should(BeNil())
By("Reconcile for new revision")
reconcileRetry(reconciler, req)
By("Check new revision workload created successfully")
Eventually(func() error {
By("Reconcile for new revision")
reconcileRetry(reconciler, req)
var workloadKey = client.ObjectKey{Namespace: namespace, Name: compName + "-v2"}
return k8sClient.Get(ctx, workloadKey, &wr)
}, time.Second, 300*time.Millisecond).Should(BeNil())