Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #291 from captainroy-hy/fix-unstable-ut
Browse files Browse the repository at this point in the history
fix unstable unit tests of AppConfig controller
  • Loading branch information
hongchaodeng authored Nov 10, 2020
2 parents 65e7fb3 + 2dfb242 commit 11fab2b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,19 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
FieldPath: "status.app-hash",
}}}}}}}}},
}
logf.Log.Info("Creating application config", "Name", appConfig.Name, "Namespace", appConfig.Namespace)
Expect(k8sClient.Create(ctx, &appConfig)).Should(BeNil())

By("Reconcile")
appconfigKey := client.ObjectKey{
Name: appConfigName,
Namespace: namespace,
}
req := reconcile.Request{NamespacedName: appconfigKey}
logf.Log.Info("Creating application config", "Name", appConfig.Name, "Namespace", appConfig.Namespace)
By("Create appConfig & check successfully")
Expect(k8sClient.Create(ctx, &appConfig)).Should(Succeed())
Eventually(func() error {
return k8sClient.Get(ctx, appconfigKey, &appConfig)
}, time.Second, 300*time.Millisecond).Should(BeNil())

By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())

By("Checking that resource which accepts data isn't created yet")
Expand Down Expand Up @@ -447,12 +451,27 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Expect(err).Should(BeNil())
err = unstructured.SetNestedField(outFoo.Object, "hash-v1", "status", "app-hash")
Expect(err).Should(BeNil())
By("Update outFoo & check successfully")
Expect(k8sClient.Update(ctx, outFoo)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.Get(ctx, outFooKey, outFoo); err != nil {
return false
}
s, _, _ := unstructured.NestedString(outFoo.Object, "status", "key")
return s == "test"
}, time.Second, 300*time.Millisecond).Should(BeTrue())

By("Update AppConfig with new version")
newAppConfig.Labels["app-hash"] = "hash-v2"
Expect(k8sClient.Update(ctx, newAppConfig)).Should(BeNil())
time.Sleep(time.Second)
By("Update newAppConfig & check successfully")
Expect(k8sClient.Update(ctx, newAppConfig)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.Get(ctx, appconfigKey, newAppConfig); err != nil {
logf.Log.Error(err, "failed get AppConfig")
return false
}
return newAppConfig.Labels["app-hash"] == "hash-v2"
}, time.Second, 300*time.Millisecond).Should(BeTrue())

By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())

Expand Down Expand Up @@ -487,7 +506,15 @@ var _ = Describe("Resource Dependency in an ApplicationConfiguration", func() {
Expect(k8sClient.Get(ctx, outFooKey, outFoo)).Should(BeNil()) // Get the latest before update
Expect(unstructured.SetNestedField(outFoo.Object, "test-new", "status", "key")).Should(BeNil())
Expect(unstructured.SetNestedField(outFoo.Object, "hash-v2", "status", "app-hash")).Should(BeNil())
By("Update outFoo & check successfully")
Expect(k8sClient.Update(ctx, outFoo)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.Get(ctx, outFooKey, outFoo); err != nil {
return false
}
s, _, _ := unstructured.NestedString(outFoo.Object, "status", "key")
return s == "test-new"
}, time.Second, 300*time.Millisecond).Should(BeTrue())

By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())
Expand Down
13 changes: 11 additions & 2 deletions pkg/controller/v1alpha2/applicationconfiguration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/onsi/gomega"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/resource"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -37,7 +38,7 @@ var reconciler *OAMApplicationReconciler
var mgrclose chan struct{}
var testEnv *envtest.Environment
var cfg *rest.Config
var k8sClient client.Client
var k8sClient resource.ClientApplicator
var scheme = runtime.NewScheme()
var crd crdv1.CustomResourceDefinition

Expand Down Expand Up @@ -77,7 +78,15 @@ var _ = BeforeSuite(func(done Done) {
Expect(depSchemeBuilder.AddToScheme(scheme)).Should(BeNil())

By("Setting up kubernetes client")
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
c, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
logf.Log.Error(err, "failed to create a client")
Fail("setup failed")
}
k8sClient = resource.ClientApplicator{
Client: c,
Applicator: resource.NewAPIUpdatingApplicator(c),
}
if err != nil {
logf.Log.Error(err, "failed to create k8sClient")
Fail("setup failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ spec:
removed: bar
valueChanged: bar`
Expect(yaml.Unmarshal([]byte(appConfigYAML), &appConfig)).Should(BeNil())
By("Creat appConfig & check successfully")
Expect(k8sClient.Create(ctx, &appConfig)).Should(Succeed())
Eventually(func() error {
return k8sClient.Get(ctx, appConfigKey, &appConfig)
}, time.Second, 300*time.Millisecond).Should(BeNil())

By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())
Expand Down Expand Up @@ -226,20 +230,22 @@ spec:
unchanged: bar
valueChanged: foo
added: bar`

Expect(k8sClient.Get(ctx, appConfigKey, &appConfig)).Should(Succeed())
appConfigUpdated := v1alpha2.ApplicationConfiguration{}
Expect(yaml.Unmarshal([]byte(appConfigYAMLUpdated), &appConfigUpdated)).Should(BeNil())
appConfigUpdated.SetResourceVersion(appConfig.GetResourceVersion())
appConfigUpdated.SetNamespace(namespace)
Expect(k8sClient.Update(ctx, &appConfigUpdated)).Should(Succeed())

By("Apply appConfig & check successfully")
Expect(k8sClient.Apply(ctx, &appConfigUpdated)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.Get(ctx, appConfigKey, &appConfig); err != nil {
return false
}
return appConfig.GetGeneration() == 2
}, time.Second, 300*time.Millisecond).Should(BeTrue())

By("Reconcile")
Expect(func() error { _, err := reconciler.Reconcile(req); return err }()).Should(BeNil())

By("Get updated AppConfig")
Expect(k8sClient.Get(ctx, appConfigKey, &appConfig)).Should(Succeed())

By("Get updated trait object")
var traitObj unstructured.Unstructured
traitObj.SetAPIVersion("example.com/v1")
Expand Down

0 comments on commit 11fab2b

Please sign in to comment.