Skip to content

Commit

Permalink
refactor: remove gomega on top of syncset changes
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <[email protected]>
  • Loading branch information
acpana committed Nov 23, 2023
1 parent 6276cb2 commit 9224e64
Show file tree
Hide file tree
Showing 151 changed files with 220 additions and 181,366 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.1
github.com/onsi/gomega v1.27.10
github.com/open-policy-agent/cert-controller v0.8.0
github.com/open-policy-agent/frameworks/constraint v0.0.0-20231030230613-2e0cb3d68575
github.com/pkg/errors v0.9.1
Expand Down
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1ls
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand All @@ -963,8 +963,7 @@ github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU=
github.com/open-policy-agent/cert-controller v0.8.0 h1:pao3WCLsKGz5dSWSlNUFrNFQdXtVTQ3lVDgk2IelH34=
github.com/open-policy-agent/cert-controller v0.8.0/go.mod h1:alotCQRwX4M6VEwEgO53FB6nGLSlvah6L0pWxSRslIk=
github.com/open-policy-agent/frameworks/constraint v0.0.0-20231030230613-2e0cb3d68575 h1:rhln22JjTgsJGL8gDK4qEM372Ei1PPQk4ZTIOKM9WvY=
Expand Down Expand Up @@ -1730,7 +1729,7 @@ golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyj
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
78 changes: 36 additions & 42 deletions pkg/controller/config/config_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/onsi/gomega"
constraintclient "github.com/open-policy-agent/frameworks/constraint/pkg/client"
"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/rego"
configv1alpha1 "github.com/open-policy-agent/gatekeeper/v3/apis/config/v1alpha1"
Expand Down Expand Up @@ -58,7 +57,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const timeout = time.Second * 20
const (
timeout = time.Second * 10
tick = time.Second * 1
)

// setupManager sets up a controller-runtime manager with registered watch manager.
func setupManager(t *testing.T) (manager.Manager, *watch.Manager) {
Expand Down Expand Up @@ -92,8 +94,6 @@ func TestReconcile(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

g := gomega.NewGomegaWithT(t)

instance := &configv1alpha1.Config{
ObjectMeta: metav1.ObjectMeta{
Name: "config",
Expand Down Expand Up @@ -173,21 +173,20 @@ func TestReconcile(t *testing.T) {
t.Fatal(err)
}
}()
g.Eventually(func() bool {
expectedReq := reconcile.Request{NamespacedName: types.NamespacedName{
Name: "config",
Namespace: "gatekeeper-system",
}}
_, ok := requests.Load(expectedReq)

expectedReq := reconcile.Request{NamespacedName: types.NamespacedName{
Name: "config",
Namespace: "gatekeeper-system",
}}
require.Eventually(t, func() bool {
_, ok := requests.Load(expectedReq)
return ok
}).WithTimeout(timeout).Should(gomega.BeTrue())
}, timeout, tick, "waiting to receive request")
require.Eventually(t, func() bool {
return len(wm.GetManagedGVK()) != 0
}, timeout, tick)

g.Eventually(func() int {
return len(wm.GetManagedGVK())
}).WithTimeout(timeout).ShouldNot(gomega.Equal(0))
gvks := wm.GetManagedGVK()

wantGVKs := []schema.GroupVersionKind{
{Group: "", Version: "v1", Kind: "Namespace"},
{Group: "", Version: "v1", Kind: "Pod"},
Expand Down Expand Up @@ -291,8 +290,6 @@ func TestReconcile(t *testing.T) {
func TestConfig_DeleteSyncResources(t *testing.T) {
log.Info("Running test: Cancel the expectations when sync only resource gets deleted")

g := gomega.NewGomegaWithT(t)

// setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
// channel when it is finished.
mgr, wm := setupManager(t)
Expand Down Expand Up @@ -373,9 +370,9 @@ func TestConfig_DeleteSyncResources(t *testing.T) {
}

// ensure that expectations are set for the constraint gvk
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return tr.IsExpecting(gvk, types.NamespacedName{Name: "testpod", Namespace: "default"})
}, timeout).Should(gomega.BeTrue())
}, timeout, tick)

// delete the pod , the delete event will be reconciled by sync controller
// to cancel the expectation set for it by tracker
Expand All @@ -395,9 +392,9 @@ func TestConfig_DeleteSyncResources(t *testing.T) {
}

// check readiness tracker is satisfied post-reconcile
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return tracker.ForData(gvk).Satisfied()
}, timeout).Should(gomega.BeTrue())
}, timeout, tick)
}

func setupController(ctx context.Context, mgr manager.Manager, wm *watch.Manager, tracker *readiness.Tracker, events chan event.GenericEvent, reader client.Reader, useFakeClient bool) (cachemanager.CFDataClient, error) {
Expand Down Expand Up @@ -469,7 +466,6 @@ func TestConfig_CacheContents(t *testing.T) {
// Setup the Manager and Controller.
mgr, wm := setupManager(t)
c := testclient.NewRetryClient(mgr.GetClient())
g := gomega.NewGomegaWithT(t)
nsGVK := schema.GroupVersionKind{
Group: "",
Version: "v1",
Expand Down Expand Up @@ -520,9 +516,9 @@ func TestConfig_CacheContents(t *testing.T) {
cmKey: nil,
// kube-system namespace is being excluded, it should not be in the cache
}
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return fakeClient.Contains(expected)
}, 10*time.Second).Should(gomega.BeTrue(), "checking initial cache contents")
}, timeout, tick, "checking initial cache contents")
require.True(t, fakeClient.HasGVK(nsGVK), "want fakeClient.HasGVK(nsGVK) to be true but got false")

// Reconfigure to drop the namespace watches
Expand All @@ -534,25 +530,24 @@ func TestConfig_CacheContents(t *testing.T) {
require.NoError(t, c.Update(ctx, configUpdate), "updating Config config")

// Expect namespaces to go away from cache
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return fakeClient.HasGVK(nsGVK)
}, 10*time.Second).Should(gomega.BeFalse())
}, timeout, tick)

// Expect our configMap to return at some point
// TODO: In the future it will remain instead of having to repopulate.
expected = map[fakes.CfDataKey]interface{}{
cmKey: nil,
}
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return fakeClient.Contains(expected)
}, 10*time.Second).Should(gomega.BeTrue(), "waiting for ConfigMap to repopulate in cache")

}, timeout, tick, "waiting for ConfigMap to repopulate in cache")
expected = map[fakes.CfDataKey]interface{}{
cm2Key: nil,
}
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return !fakeClient.Contains(expected)
}, 10*time.Second).Should(gomega.BeTrue(), "kube-system namespace is excluded. kube-system/config-test-2 should not be in the cache")
}, timeout, tick, "kube-system namespace is excluded. kube-system/config-test-2 should not be in the cache")

// Delete the config resource - expect cache to empty out.
if fakeClient.Len() == 0 {
Expand All @@ -561,16 +556,15 @@ func TestConfig_CacheContents(t *testing.T) {
require.NoError(t, c.Delete(ctx, config), "deleting Config resource")

// The cache will be cleared out.
g.Eventually(func() int {
return fakeClient.Len()
}, 10*time.Second).Should(gomega.BeZero(), "waiting for cache to empty")
require.Eventually(t, func() bool {
return fakeClient.Len() == 0
}, timeout, tick, "waiting for cache to empty")
}

func TestConfig_Retries(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

g := gomega.NewGomegaWithT(t)
nsGVK := schema.GroupVersionKind{
Group: "",
Version: "v1",
Expand Down Expand Up @@ -643,9 +637,9 @@ func TestConfig_Retries(t *testing.T) {
testutils.StartManager(ctx, t, mgr)

// Create the Config object and expect the Reconcile to be created
g.Eventually(func() error {
return c.Create(ctx, instance.DeepCopy())
}, timeout).Should(gomega.BeNil())
require.Eventually(t, func() bool {
return c.Create(ctx, instance.DeepCopy()) == nil
}, timeout, tick)

defer func() {
ctx := context.Background()
Expand Down Expand Up @@ -675,9 +669,9 @@ func TestConfig_Retries(t *testing.T) {
expected := map[fakes.CfDataKey]interface{}{
cmKey: nil,
}
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return dataClient.Contains(expected)
}, 10*time.Second).Should(gomega.BeTrue(), "checking initial cache contents")
}, timeout, tick, "checking initial cache contents")

fi.SetFailures("ConfigMapList", 2)

Expand All @@ -693,9 +687,9 @@ func TestConfig_Retries(t *testing.T) {
}

// Despite the transient error, we expect the cache to eventually be repopulated.
g.Eventually(func() bool {
require.Eventually(t, func() bool {
return dataClient.Contains(expected)
}, 10*time.Second).Should(gomega.BeTrue(), "checking final cache contents")
}, timeout, tick, "checking final cache contents")
}

// configFor returns a config resource that watches the requested set of resources.
Expand Down
Loading

0 comments on commit 9224e64

Please sign in to comment.