Skip to content

Commit

Permalink
e2e: Use Must with WatchForAllSyncs (#1417)
Browse files Browse the repository at this point in the history
Simple find/replace to wrap all WatchForAllSyncs with Must.
  • Loading branch information
karlkfi authored Sep 3, 2024
1 parent 01bbfcf commit 8fe46c3
Show file tree
Hide file tree
Showing 44 changed files with 302 additions and 911 deletions.
4 changes: 1 addition & 3 deletions e2e/nomostest/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,7 @@ func setupTestCase(nt *NT, opts *ntopts.New) {
}

// Wait for all RootSyncs and all RepoSyncs to be reconciled
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
// Validate all expected syncs exist (some may have been deployed by others)
if err := validateRootSyncsExist(nt); err != nil {
nt.T.Fatal(err)
Expand Down
12 changes: 3 additions & 9 deletions e2e/testcases/acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func TestAcmeCorpRepo(t *testing.T) {
}
nt.Must(rootSyncGitRepo.Copy("../../examples/acme", "."))
nt.Must(rootSyncGitRepo.CommitAndPush("Initialize the acme directory"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

checkResourceCount(nt, kinds.Namespace(), "", len(nsToFolder), nil, configSyncManagementAnnotations)
for namespace, folder := range nsToFolder {
Expand Down Expand Up @@ -176,9 +174,7 @@ func TestAcmeCorpRepo(t *testing.T) {
// Add back the safety ClusterRole to pass the safety check (KNV2006).
nt.Must(rootSyncGitRepo.AddSafetyClusterRole())
nt.Must(rootSyncGitRepo.CommitAndPush("Reset the acme directory"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
}

// TestObjectInCMSNamespace will test that user can sync object to CMS namespace
Expand All @@ -190,9 +186,7 @@ func TestObjectInCMSNamespace(t *testing.T) {

nt.Must(rootSyncGitRepo.Copy("../testdata/object-in-cms-namespace", "acme"))
nt.Must(rootSyncGitRepo.CommitAndPush("adding resource to config-management-system namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// validate the resources synced successfully in CMS namespace
namespace := configmanagement.ControllerNamespace
Expand Down
12 changes: 3 additions & 9 deletions e2e/testcases/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func TestAdmission(t *testing.T) {
nt.Must(rootSyncGitRepo.Add("acme/namespaces/hello/ns.yaml",
k8sobjects.NamespaceObject("hello", core.Annotation("goodbye", "moon"))))
nt.Must(rootSyncGitRepo.CommitAndPush("add Namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// Ensure we properly forbid changing declared information.

Expand Down Expand Up @@ -161,9 +159,7 @@ func TestDisableWebhookConfigurationUpdateHierarchy(t *testing.T) {

nt.Must(rootSyncGitRepo.Add("acme/namespaces/hello/ns.yaml", k8sobjects.NamespaceObject("hello")))
nt.Must(rootSyncGitRepo.CommitAndPush("add test namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err := nt.Validate("hello", "", &corev1.Namespace{}, testpredicates.HasAnnotationKey(metadata.DeclaredFieldsKey))
if err != nil {
Expand Down Expand Up @@ -233,9 +229,7 @@ func TestDisableWebhookConfigurationUpdateUnstructured(t *testing.T) {
sa := k8sobjects.ServiceAccountObject("store", core.Namespace(namespaceRepo))
nt.Must(repoSyncGitRepo.Add("acme/sa.yaml", sa))
nt.Must(repoSyncGitRepo.CommitAndPush("Adding test service account"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// Test starts with Admission Webhook already installed
nomostest.WaitForWebhookReadiness(nt)
Expand Down
4 changes: 1 addition & 3 deletions e2e/testcases/adopt_client_side_applied_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func TestAdoptClientSideAppliedResource(t *testing.T) {
}}
nt.Must(rootSyncGitRepo.Add("acme/cluster/ns-viewer-cr.yaml", nsViewer))
nt.Must(rootSyncGitRepo.CommitAndPush("add namespace-viewer ClusterRole"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// Validate the ClusterRole exist and the Rules are the same as the one
// in "acme/cluster/ns-viewer-cr.yaml".
Expand Down
20 changes: 5 additions & 15 deletions e2e/testcases/apiservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func TestCreateAPIServiceAndEndpointInTheSameCommit(t *testing.T) {
nt.Must(rootSyncGitRepo.Copy("../testdata/apiservice/apiservice.yaml", "acme/cluster/apiservice.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("adding apiservice resources"))
nt.T.Log("Waiting for nomos to sync new APIService")
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err := validateStackdriverAdapterStatusCurrent(nt)
if err != nil {
Expand All @@ -62,17 +60,13 @@ func TestCreateAPIServiceAndEndpointInTheSameCommit(t *testing.T) {
// the test repo from cleaning up
nt.Must(rootSyncGitRepo.Remove("acme/cluster/apiservice.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Remove custom metric stackdriver adapter API service"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// Remove the backend Deployment of test APIService
nt.Must(rootSyncGitRepo.Remove("acme/namespaces/custom-metrics/namespace-custom-metrics.yaml"))
nt.Must(rootSyncGitRepo.Remove("acme/namespaces/custom-metrics/namespace.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Remove custom metric stackdriver adapter namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
}

func TestReconcilerResilientToFlakyAPIService(t *testing.T) {
Expand Down Expand Up @@ -101,9 +95,7 @@ func TestReconcilerResilientToFlakyAPIService(t *testing.T) {
nt.Must(rootSyncGitRepo.CommitAndPush("add testing resources"))

nt.T.Log("Wait for test resource to have status CURRENT")
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err := nt.Watcher.WatchForCurrentStatus(kinds.Namespace(), "resilient", "")
if err != nil {
Expand All @@ -115,9 +107,7 @@ func TestReconcilerResilientToFlakyAPIService(t *testing.T) {
nt.MustKubectl("apply", "-f", "../testdata/apiservice/rbac.yaml")
nt.MustKubectl("apply", "-f", "../testdata/apiservice/namespace-custom-metrics.yaml")
nt.T.Log("Waiting for nomos to stabilize")
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err = validateStackdriverAdapterStatusCurrent(nt)
if err != nil {
Expand Down
48 changes: 12 additions & 36 deletions e2e/testcases/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,15 @@ func TestNamespaceGarbageCollection(t *testing.T) {

nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/accounting-namespace.yaml", yamlDir), "acme/namespaces/accounting/namespace.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Add accounting namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.Validate("accounting", "", &corev1.Namespace{}); err != nil {
nt.T.Fatal(err)
}

nt.Must(rootSyncGitRepo.Remove("acme/namespaces/accounting/namespace.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Remove accounting namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.ValidateNotFound("accounting", "", &corev1.Namespace{}); err != nil {
nt.T.Fatal("Namespace still exist when it should have been garbage collected")
Expand All @@ -64,9 +60,7 @@ func TestNamespacePolicyspaceConversion(t *testing.T) {

nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/dir-namespace.yaml", yamlDir), "acme/namespaces/dir/namespace.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Add dir namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.Validate("dir", "", &corev1.Namespace{}); err != nil {
nt.T.Fatal(err)
Expand All @@ -75,9 +69,7 @@ func TestNamespacePolicyspaceConversion(t *testing.T) {
nt.Must(rootSyncGitRepo.Remove("acme/namespaces/dir/namespace.yaml"))
nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/subdir-namespace.yaml", yamlDir), "acme/namespaces/dir/subdir/namespace.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Remove dir namespace, add subdir namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.Validate("subdir", "", &corev1.Namespace{}); err != nil {
nt.T.Fatal(err)
Expand All @@ -104,9 +96,7 @@ func TestSyncDeploymentAndReplicaSet(t *testing.T) {
// This sync may block until reconcile timeout is reached,
// because ReplicaSet or Deployment may never reconcile.
// So this wait timeout must be longer than the reconcile timeout (5m).
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
nt.T.Log("check that the replicaset was created")
if err := nt.Validate("hello-world", "dir", &appsv1.ReplicaSet{}, testpredicates.HasLabel("app", "hello-world")); err != nil {
nt.T.Fatal(err)
Expand All @@ -115,9 +105,7 @@ func TestSyncDeploymentAndReplicaSet(t *testing.T) {
nt.T.Log("Add a corresponding deployment")
nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/deployment-helloworld.yaml", yamlDir), "acme/namespaces/dir/deployment.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Add corresponding deployment"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

nt.T.Log("check that the deployment was created")
if err := nt.Validate("hello-world", "dir", &appsv1.Deployment{}); err != nil {
Expand All @@ -130,9 +118,7 @@ func TestSyncDeploymentAndReplicaSet(t *testing.T) {
// This sync may block until reconcile timeout is reached,
// because the ReplicaSet is re-applied before deleting the Deployment.
// So this wait timeout must be longer than the reconcile timeout (5m).
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

nt.T.Log("check that the deployment was removed and replicaset remains")
if err := nt.ValidateNotFound("hello-world", "dir", &appsv1.Deployment{}); err != nil {
Expand All @@ -150,18 +136,14 @@ func TestRolebindingsUpdated(t *testing.T) {
nt.Must(rootSyncGitRepo.Copy("../../examples/acme/namespaces/eng/backend/namespace.yaml", "acme/namespaces/eng/backend/namespace.yaml"))
nt.Must(rootSyncGitRepo.Copy("../../examples/acme/namespaces/eng/backend/bob-rolebinding.yaml", "acme/namespaces/eng/backend/br.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Add bob rolebinding"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
if err := nt.Validate("bob-rolebinding", "backend", &rbacv1.RoleBinding{}, testpredicates.RoleBindingHasName("acme-admin")); err != nil {
nt.T.Fatal("bob-rolebinding not found")
}

nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/robert-rolebinding.yaml", yamlDir), "acme/namespaces/eng/backend/br.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("Replace bob with robert rolebinding"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.ValidateNotFound("bob-rolebinding", "backend", &rbacv1.RoleBinding{}); err != nil {
nt.T.Fatal("bob-rolebinding is not deleted")
Expand Down Expand Up @@ -193,9 +175,7 @@ func manageNamespace(nt *nomostest.NT, namespace string) {
nt.T.Log("Start managing the namespace")
nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/reserved_namespaces/namespace.%s.yaml", yamlDir, namespace), fmt.Sprintf("acme/namespaces/%s/namespace.yaml", namespace)))
nt.Must(rootSyncGitRepo.CommitAndPush("Start managing the namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

nt.T.Log("Validate managed service appears on the cluster")
if err := nt.Validate("some-service", namespace, &corev1.Service{}); err != nil {
Expand All @@ -205,9 +185,7 @@ func manageNamespace(nt *nomostest.NT, namespace string) {
nt.T.Log("Remove the namespace directory from the repo")
nt.Must(rootSyncGitRepo.Remove(fmt.Sprintf("acme/namespaces/%s", namespace)))
nt.Must(rootSyncGitRepo.CommitAndPush("Remove the namespace from the managed set of namespaces"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

nt.T.Log("Validate managed service disappears from the cluster")
if err := nt.ValidateNotFound("some-service", namespace, &corev1.Service{}); err != nil {
Expand All @@ -225,9 +203,7 @@ func unmanageNamespace(nt *nomostest.NT, namespace string) {
nt.T.Log("stop managing the system namespace")
nt.Must(rootSyncGitRepo.Copy(fmt.Sprintf("%s/reserved_namespaces/unmanaged-namespace.%s.yaml", yamlDir, namespace), fmt.Sprintf("acme/namespaces/%s/namespace.yaml", namespace)))
nt.Must(rootSyncGitRepo.CommitAndPush("Stop managing the namespace"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
}

func TestNamespaceDefaultCanBeManaged(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions e2e/testcases/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,7 @@ func testSyncFromNomosHydrateOutput(nt *nomostest.NT, config string) {

nt.Must(rootSyncGitRepo.Copy(config, "acme"))
nt.Must(rootSyncGitRepo.CommitAndPush("Add cluster-dev configs"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

if err := nt.Validate("bookstore1", "", &corev1.Namespace{}); err != nil {
nt.T.Fatal(err)
Expand Down
16 changes: 4 additions & 12 deletions e2e/testcases/cluster_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ func TestRevertClusterRole(t *testing.T) {
declaredCr.Rules = declaredRules
nt.Must(rootSyncGitRepo.Add("acme/cluster/clusterrole.yaml", declaredCr))
nt.Must(rootSyncGitRepo.CommitAndPush("add get/list/create ClusterRole"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err = nt.Validate(crName, "", &rbacv1.ClusterRole{},
clusterRoleHasRules(declaredRules))
Expand Down Expand Up @@ -165,9 +163,7 @@ func TestClusterRoleLifecycle(t *testing.T) {
declaredCr.Rules = declaredRules
nt.Must(rootSyncGitRepo.Add("acme/cluster/clusterrole.yaml", declaredCr))
nt.Must(rootSyncGitRepo.CommitAndPush("add get/list/create ClusterRole"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err = nt.Validate(crName, "", &rbacv1.ClusterRole{},
clusterRoleHasRules(declaredRules), managerFieldsNonEmpty())
Expand Down Expand Up @@ -198,9 +194,7 @@ func TestClusterRoleLifecycle(t *testing.T) {
updatedCr.Rules = updatedRules
nt.Must(rootSyncGitRepo.Add("acme/cluster/clusterrole.yaml", updatedCr))
nt.Must(rootSyncGitRepo.CommitAndPush("update ClusterRole to get/list"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

// Ensure the resource is updated.
if err = nt.Validate(crName, "", &rbacv1.ClusterRole{}, clusterRoleHasRules(updatedRules), managerFieldsNonEmpty()); err != nil {
Expand All @@ -220,9 +214,7 @@ func TestClusterRoleLifecycle(t *testing.T) {
// Delete the ClusterRole from the SOT.
nt.Must(rootSyncGitRepo.Remove("acme/cluster/clusterrole.yaml"))
nt.Must(rootSyncGitRepo.CommitAndPush("deleting ClusterRole"))
if err := nt.WatchForAllSyncs(); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())

err = nt.ValidateNotFound(crName, "", &rbacv1.ClusterRole{})
if err != nil {
Expand Down
Loading

0 comments on commit 8fe46c3

Please sign in to comment.