Skip to content

Commit

Permalink
Adds a base admission acceptance test.
Browse files Browse the repository at this point in the history
Test checks that creating external resources outside of juju are tagged
correctly with the app name.
  • Loading branch information
tlm committed Apr 27, 2020
1 parent b6ee699 commit c8cab77
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cmd/charmd/charmd
cmd/charmload/charmload
provider/ec2/internal/ec2instancetypes/index.json
provider/ec2/internal/ec2instancetypes/data.yaml
tests/test-*
tags
!tags/
TAGS
Expand Down
6 changes: 4 additions & 2 deletions tests/includes/juju.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ ensure() {
# ```
bootstrap() {
local provider name output model bootstrapped_name

case "${BOOTSTRAP_PROVIDER:-}" in
"aws")
provider="aws"
Expand All @@ -59,6 +58,9 @@ bootstrap() {

provider="${manual_name}"
;;
"microk8s")
provider="microk8s"
;;
*)
echo "Unexpected bootstrap provider (${BOOTSTRAP_PROVIDER})."
exit 1
Expand Down Expand Up @@ -305,4 +307,4 @@ remove_controller_offers() {
done
done
fi
}
}
1 change: 1 addition & 0 deletions tests/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import_subdir_files includes
TEST_NAMES="static_analysis \
appdata \
branches \
caasadmission \
cli \
controller \
deploy \
Expand Down
76 changes: 76 additions & 0 deletions tests/suites/caasadmission/admission.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

run_deploy_microk8s() {
echo

name=${1}
export BOOTSTRAP_PROVIDER=microk8s
bootstrap microk8s "${name}"

microk8s.config > "${TEST_DIR}"/kube.conf
}

test_deploy_admission() {
name=test-$(petname)

run_deploy_microk8s "${name}"

namespace=controller-"${BOOTSTRAPPED_JUJU_CTRL_NAME}"

kubectl --kubeconfig "${TEST_DIR}"/kube.conf apply -f - <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: test
namespace: $namespace
labels:
juju-app: test-app
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: $namespace
name: test
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["create", "get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: role-grantor-binding
namespace: $namespace
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: test
subjects:
- kind: ServiceAccount
name: test
namespace: $namespace
EOF

sa_secret=$(kubectl --kubeconfig "${TEST_DIR}"/kube.conf get sa -o json test -n "$namespace" | jq -r '.secrets[0].name')
bearer_token=$(kubectl --kubeconfig "${TEST_DIR}"/kube.conf get secret -o json "$sa_secret" -n "$namespace" | jq -r '.data.token' | base64 -d)

kubectl --kubeconfig "${TEST_DIR}"/kube.conf config view --raw -o json | jq "del(.users[0]) | .contexts[0].context.user = \"test\" | .users[0] = {\"name\": \"test\", \"user\": {\"token\": \"$bearer_token\"}}" > "${TEST_DIR}"/kube-sa.json


# Short sleep to let juju controller watchers catch up.
sleep 3

kubectl --kubeconfig "${TEST_DIR}"/kube-sa.json apply -f - <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: test
namespace: $namespace
data:
test: test
EOF

juju_app=$(kubectl --kubeconfig "${TEST_DIR}"/kube-sa.json get cm -n "${namespace}" test -o=jsonpath='{.metadata.labels.juju-app}')
check_contains "${juju_app}" "test-app"

echo "$juju_app" | check test-app
}
13 changes: 13 additions & 0 deletions tests/suites/caasadmission/task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_caasadmission() {
if [ "$(skip 'test_caasadmission')" ]; then
echo "==> TEST SKIPPED: caas admission tests"
return
fi

set_verbosity

echo "==> Checking for dependencies"
check_dependencies juju jq petname microk8s

test_deploy_admission
}

0 comments on commit c8cab77

Please sign in to comment.