Skip to content

Commit

Permalink
Add test for namespace sync
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Kleeman <jackkleeman@gmail.com>
  • Loading branch information
jackkleeman committed Dec 30, 2019
1 parent bc2ad08 commit 1c2eed6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/overlays/dev/manager_image_patch.yaml-e
@@ -0,0 +1,11 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- image: quay.io/open-policy-agent/gatekeeper:latest
name: manager
9 changes: 9 additions & 0 deletions pkg/controller/sync/sync_controller.go
Expand Up @@ -99,6 +99,8 @@ func (r *ReconcileSync) Reconcile(request reconcile.Request) (reconcile.Result,
if err != nil {
if errors.IsNotFound(err) {
// This is a deletion; remove the data
instance.SetNamespace(request.Namespace)
instance.SetName(request.Name)
if _, err := r.opa.RemoveData(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
Expand All @@ -113,6 +115,13 @@ func (r *ReconcileSync) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, nil
}

if !instance.GetDeletionTimestamp().IsZero() {
if _, err := r.opa.RemoveData(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}

log.Info("data will be added", "data", instance)
if _, err := r.opa.AddData(context.Background(), instance); err != nil {
return reconcile.Result{}, err
Expand Down
10 changes: 10 additions & 0 deletions test/bats/test.bats
Expand Up @@ -78,6 +78,16 @@ SLEEP_TIME=1
assert_success
}

@test "waiting for namespaces to be synced" {
run kubectl apply -f ${BATS_TESTS_DIR}/constraints/ns_must_sync.yaml
assert_success

wait_for_process $((2 * $WAIT_TIME)) $SLEEP_TIME "kubectl get k8srequiredlabels.constraints.gatekeeper.sh ns-must-sync -o json | jq -e '.status.totalViolations == 1'"

run kubectl delete -f ${BATS_TESTS_DIR}/constraints/ns_must_sync.yaml
assert_success
}

@test "unique labels test" {
run kubectl apply -f ${BATS_TESTS_DIR}/templates/k8suniquelabel_template.yaml
assert_success
Expand Down
15 changes: 15 additions & 0 deletions test/bats/tests/constraints/ns_must_sync.yaml
@@ -0,0 +1,15 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: ns-must-sync
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Namespace"]
labelSelector:
matchLabels:
name: no-dupes

parameters:
labels: ["it-wont-have-this-label"]
1 change: 1 addition & 0 deletions test/bats/tests/good/no_dupe_ns.yaml
Expand Up @@ -4,3 +4,4 @@ metadata:
name: no-dupes
labels:
"gatekeeper": "not_duplicated"
name: no-dupes

0 comments on commit 1c2eed6

Please sign in to comment.