Skip to content

Commit

Permalink
Add a unit test for config_controller.go
Browse files Browse the repository at this point in the history
Add a unit test for config_controller.go to test the case that when a new type is added to HNCConfiguration singleton, the corresponding object reconciler can be created correctly.

This PR also moves shared helper functions from each controller test files to a common file.

Design doc: http://bit.ly/hnc-type-configuration
Issue: kubernetes-retired#411
  • Loading branch information
sophieliu15 committed Feb 20, 2020
1 parent 483fdfa commit 3bee2dd
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 352 deletions.
2 changes: 0 additions & 2 deletions incubator/hnc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/kubernetes-sigs/multi-tenancy/incubator/hnc
go 1.12

require (
contrib.go.opencensus.io/exporter/stackdriver v0.13.0
github.com/Azure/go-autorest/autorest v0.9.1 // indirect
github.com/Azure/go-autorest/autorest/adal v0.6.0 // indirect
github.com/emicklei/go-restful v2.10.0+incompatible // indirect
Expand All @@ -17,7 +16,6 @@ require (
github.com/onsi/gomega v1.7.0
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/spf13/cobra v0.0.5
go.opencensus.io v0.22.3
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/cli-runtime v0.0.0-20190314001948-2899ed30580f
Expand Down
130 changes: 0 additions & 130 deletions incubator/hnc/go.sum

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions incubator/hnc/pkg/controllers/config_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package controllers_test

import (
"context"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("HNCConfiguration", func() {
ctx := context.Background()

var (
fooName string
barName string
)

BeforeEach(func() {
fooName = createNS(ctx, "foo")
barName = createNS(ctx, "bar")
})

AfterEach(func() {
// Change current singleton back to the default value.
Eventually(func() error {
c := getHNCConfig(ctx)
return resetHNCConfigToDefault(ctx, c)
}).Should(Succeed())
})

It("should propagate objects whose types have been added to HNCConfiguration", func() {
setParent(ctx, barName, fooName)
makeSecret(ctx, fooName, "foo-sec")

// Wait 1 second to give "foo-sec" a chance to be propagated to bar, if it can be propagated.
time.Sleep(1 * time.Second)
// Foo should have "foo-sec" since we created it there.
Eventually(hasSecret(ctx, fooName, "foo-sec")).Should(BeTrue())
// "foo-sec" is not propagated to bar because Secret hasn't been configured in HNCConfiguration.
Eventually(hasSecret(ctx, barName, "foo-sec")).Should(BeFalse())

Eventually(func() error {
c := getHNCConfig(ctx)
return addSecretToHNCConfig(ctx, c)
}).Should(Succeed())

// "foo-sec" should now be propagated from foo to bar.
Eventually(hasSecret(ctx, barName, "foo-sec")).Should(BeTrue())
Expect(secretInheritedFrom(ctx, barName, "foo-sec")).Should(Equal(fooName))
})
})
36 changes: 0 additions & 36 deletions incubator/hnc/pkg/controllers/hierarchy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers_test
import (
"context"
"fmt"
"math/rand"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -386,41 +385,6 @@ func updateHierarchy(ctx context.Context, h *api.HierarchyConfiguration) {
}
}

// createNSName generates random namespace names. Namespaces are never deleted in test-env because
// the building Namespace controller (which finalizes namespaces) doesn't run; I searched Github and
// found that everyone who was deleting namespaces was *also* very intentionally generating random
// names, so I guess this problem is widespread.
func createNSName(prefix string) string {
suffix := make([]byte, 10)
rand.Read(suffix)
return fmt.Sprintf("%s-%x", prefix, suffix)
}

// createNSWithLabel has similar function to createNS with label as additional parameter
func createNSWithLabel(ctx context.Context, prefix string, label map[string]string) string {
nm := createNSName(prefix)

// Create the namespace
ns := &corev1.Namespace{}
ns.SetLabels(label)
ns.Name = nm
Expect(k8sClient.Create(ctx, ns)).Should(Succeed())
return nm
}

// createNS is a convenience function to create a namespace and wait for its singleton to be
// created. It's used in other tests in this package, but basically duplicates the code in this test
// (it didn't originally). TODO: refactor.
func createNS(ctx context.Context, prefix string) string {
nm := createNSName(prefix)

// Create the namespace
ns := &corev1.Namespace{}
ns.Name = nm
Expect(k8sClient.Create(ctx, ns)).Should(Succeed())
return nm
}

func getLabel(ctx context.Context, from, label string) func() string {
return func() string {
ns := getNamespace(ctx, from)
Expand Down
Loading

0 comments on commit 3bee2dd

Please sign in to comment.