Skip to content

Commit

Permalink
feat: improve tests over namespace controller
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Jul 31, 2024
1 parent 1f90b43 commit c86dd7b
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions controllers/namespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ var ctx = context.Background()
var _ = Describe("namespace controller", func() {
// Define utility constants for object names and testing timeouts/durations and intervals.
const (
timeout = time.Second * 10
// duration = time.Second * 10
interval = time.Millisecond * 250
timeout = time.Second * 10
interval = time.Second * 1
teamLabel = "argocd.snappcloud.io/appproj"
)
// Creating user-argocd namespace
Expand All @@ -54,52 +53,69 @@ var _ = Describe("namespace controller", func() {
})
})
})
// Creating AppPRoj as soon as we create a test namespace

// Creating AppProj as soon as we create a test namespace
Context("when creating namespace", func() {
It("Should create appProject", func() {
By("Creating test namespace")
testNs := &corev1.Namespace{
// create test namespace with test-team label.
testNS := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns",
Labels: map[string]string{teamLabel: "test-team"},
},
}
Expect(k8sClient.Create(ctx, testNs)).Should(Succeed())
lookupTestns := types.NamespacedName{Name: "test-ns"}
Expect(k8sClient.Get(ctx, lookupTestns, testNs)).Should(Succeed())
Expect(k8sClient.Create(ctx, testNS)).Should(Succeed())

// make sure test namespace is created.
testNSLookup := types.NamespacedName{Name: "test-ns"}
Expect(k8sClient.Get(ctx, testNSLookup, testNS)).Should(Succeed())

testAppProj := &argov1alpha1.AppProject{}
appProjLookupKey := types.NamespacedName{Name: "test-team", Namespace: "user-argocd"}
testAppProjLookup := types.NamespacedName{Name: "test-team", Namespace: "user-argocd"}
Eventually(func() bool {
err := k8sClient.Get(ctx, appProjLookupKey, testAppProj)
err := k8sClient.Get(ctx, testAppProjLookup, testAppProj)
return err == nil
}, timeout, interval).Should(BeTrue())
Expect(testAppProj.Name).Should(Equal(appProjLookupKey.Name))
Expect(testAppProj.Namespace).Should(Equal(appProjLookupKey.Namespace))
Expect(testAppProj.Spec.Destinations[0].Namespace).Should(Equal(testNs.ObjectMeta.Name))

// make sure appproject has the correct fields.
Expect(testAppProj.Name).Should(Equal(testAppProjLookup.Name))
Expect(testAppProj.Namespace).Should(Equal(testAppProjLookup.Namespace))
Expect(testAppProj.Spec.Destinations[0].Namespace).Should(Equal(testNS.Name))
})
})

// Changing the namespace label and checking if the appProjects are updated
Context("when changing namespace team label", func() {
It("Should update appProject", func() {
By("Removing from AppProject and creating new AppProject", func() {
testNs := &corev1.Namespace{
By("Removing from AppProject and creating new AppProject", func() {
// update test namespace with cloudy-team label.
testNS := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns",
Labels: map[string]string{teamLabel: "cloudy-team"},
},
}
Expect(k8sClient.Update(ctx, testNs)).Should(Succeed())
lookupTestns := types.NamespacedName{Name: "test-ns"}
Expect(k8sClient.Get(ctx, lookupTestns, testNs)).Should(Succeed())
newAppProj := &argov1alpha1.AppProject{}
newAppProjLookup := types.NamespacedName{Name: "cloudy-team", Namespace: "user-argocd"}
Expect(k8sClient.Update(ctx, testNS)).Should(Succeed())

// make sure test namespace is updated.
testNSLookup := types.NamespacedName{Name: "test-ns"}
Expect(k8sClient.Get(ctx, testNSLookup, testNS)).Should(Succeed())

// appproject should be created in user-argocd for cloudy-team because of having
// namespace.
cloudyAppProj := &argov1alpha1.AppProject{}
cloudyAppProjLookup := types.NamespacedName{Name: "cloudy-team", Namespace: "user-argocd"}
Eventually(func() bool {
err := k8sClient.Get(ctx, newAppProjLookup, newAppProj)
err := k8sClient.Get(ctx, cloudyAppProjLookup, cloudyAppProj)
return err == nil
}, timeout, interval).Should(BeTrue())
Expect(newAppProj.Name).Should(Equal(newAppProjLookup.Name))
Expect(newAppProj.Namespace).Should(Equal(newAppProjLookup.Namespace))
Expect(newAppProj.Spec.Destinations[0].Namespace).Should(Equal(testNs.ObjectMeta.Name))

// make sure appproject has the correct fields.
Expect(cloudyAppProj.Name).Should(Equal(cloudyAppProjLookup.Name))
Expect(cloudyAppProj.Namespace).Should(Equal(cloudyAppProjLookup.Namespace))
Expect(cloudyAppProj.Spec.Destinations[0].Namespace).Should(Equal(testNS.Name))

testAppProj := &argov1alpha1.AppProject{}
appProjLookupKey := types.NamespacedName{Name: "test-team", Namespace: "user-argocd"}
Eventually(func() bool {
Expand Down

0 comments on commit c86dd7b

Please sign in to comment.