Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Add initial test structure. (#3)
Browse files Browse the repository at this point in the history
* Create initial testing structure and tests.

* Build docker image.

* Format with `goimports -w .`.

* Get operator name from environment variable.

* Script improvements.

* Document command for building Docker images.

* Add usage to uninstall_operator.sh script.

* Add tooling to check formatting and format go code.

* Update data-services-kudo module to the latest master.
  • Loading branch information
mpereira committed Oct 29, 2019
1 parent 054b21d commit 8e709e5
Show file tree
Hide file tree
Showing 26 changed files with 1,346 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*-junit.xml
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
- [Docker daemon running under a non-root
user](https://docs.docker.com/install/linux/linux-postinstall/) (only for
Linux)
- [KUDO](https://github.com/kudobuilder/kudo/releases) (last tested on 0.7.2)
- Kubernetes cluster (last tested on [Konvoy
v1.1.5](https://github.com/mesosphere/konvoy/releases))
- [KUDO](https://github.com/kudobuilder/kudo/releases) (check `KUDO_VERSION` in
`metadata.sh` to see the last tested version)
- Kubernetes cluster (last tested on
[Konvoy](https://github.com/mesosphere/konvoy/releases)) (check
`KUBERNETES_VERSION` in `metadata.sh` to see the last tested version)

## Installing

Expand Down Expand Up @@ -96,16 +98,19 @@ kubectl exec "${kudo_cassandra_pod_0}" \
### Uninstalling the KUDO Cassandra operator

```bash
./scripts/uninstall_kudo_cassandra.sh \
./scripts/uninstall_operator.sh \
--instance "${kudo_cassandra_instance_name}" \
--namespace "${kudo_cassandra_instance_namespace}"
```

## Development

### Requirements
### Additional requirements

- [bash 4](https://www.tldp.org/LDP/abs/html/bashver4.html)
- bash 4+ ([macOS](https://formulae.brew.sh/formula/bash))
- envsubst ([macOS](https://formulae.brew.sh/formula/gettext))
- [shellcheck](https://www.shellcheck.net/)
- [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports)

### Compiling templates

Expand All @@ -127,9 +132,10 @@ kubectl exec "${kudo_cassandra_pod_0}" \
./tools/check_files.sh
```

### Building Docker image
### Building Docker images

```bash
./images/build.sh
```

### Running tests
Expand Down
26 changes: 26 additions & 0 deletions images/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# shellcheck disable=SC2039

set -euxo pipefail

readonly script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly project_directory="$(readlink -f "${script_directory}/..")"

# shellcheck source=../metadata.sh
source "${project_directory}/metadata.sh"

readonly cassandra_docker_image="${CASSANDRA_DOCKER_IMAGE:-}"

if [[ -z ${cassandra_docker_image} ]]; then
echo "Missing CASSANDRA_DOCKER_IMAGE" >&2
exit 1
fi

docker image build \
-t "${cassandra_docker_image}" \
-f "${project_directory}/images/Dockerfile" \
"${project_directory}/images"

if [[ "${1:-}" == "push" ]]; then
docker push "${cassandra_docker_image}"
fi
49 changes: 45 additions & 4 deletions metadata.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
#!/usr/bin/env bash

# https://github.com/kudobuilder/kudo/releases/tag/v0.6.0
export KUDO_VERSION="0.7.2"
# This script contains metadata that is either used in other scripts or expanded
# into templates via `tools/compile_templates.sh`.

# "Shadowing" these two environment variables so that they don't affect
# similarly named environment variables in other scripts loading this script.
_script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
_project_directory="$(readlink -f "${_script_directory}")"

################################################################################
################################# Operator #####################################
################################################################################

# https://github.com/mesosphere/kudo-cassandra-operator

export PROJECT_NAME="kudo-cassandra-operator"
export OPERATOR_NAME="cassandra"

# KUDO still doesn't support snapshots, or compound versions yet. Check out:
# - https://github.com/kudobuilder/kudo/pull/889
# - https://github.com/kudobuilder/kudo/issues/163
export OPERATOR_VERSION="0.1.0"

export OPERATOR_DIRECTORY="${_project_directory}/operator"
export VENDOR_DIRECTORY="${_project_directory}/shared/vendor"

################################################################################
############################### Dependencies ###################################
################################################################################

# http://www.apache.org/dyn/closer.lua/cassandra/3.11.4
# https://hub.docker.com/_/cassandra
# https://github.com/docker-library/cassandra/blob/master/3.11/Dockerfile
export CASSANDRA_VERSION="3.11.4"

# https://github.com/mesosphere/kudo-cassandra-operator/releases
export KUDO_CASSANDRA_VERSION="0.1.0"
# https://github.com/kudobuilder/kudo/releases/tag/v0.7.4
export KUDO_VERSION="0.7.4"

export KUBERNETES_VERSION="1.15.0"

################################################################################
############################## Docker images ###################################
################################################################################

export CASSANDRA_DOCKER_IMAGE_NAMESPACE="mesosphere"
export CASSANDRA_DOCKER_IMAGE_NAME="${OPERATOR_NAME}"
export CASSANDRA_DOCKER_IMAGE_TAG="${OPERATOR_VERSION}-${CASSANDRA_VERSION}"
export CASSANDRA_DOCKER_IMAGE="${CASSANDRA_DOCKER_IMAGE_NAMESPACE}/${CASSANDRA_DOCKER_IMAGE_NAME}:${CASSANDRA_DOCKER_IMAGE_TAG}"

################################################################################
################################# Testing ######################################
################################################################################

export INTEGRATION_TESTS_DOCKER_IMAGE="golang:1.13.1-stretch"
2 changes: 1 addition & 1 deletion operator/operator.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "cassandra"
version: "0.1.0"
kudoVersion: "0.7.2"
kudoVersion: "0.7.4"
kubernetesVersion: "1.15.0"
appVersion: "3.11.4"
maintainers:
Expand Down
34 changes: 34 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# shellcheck disable=SC2039

set -euxo pipefail

readonly script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly project_directory="$(readlink -f "${script_directory}")"

# shellcheck source=metadata.sh
source "${project_directory}/metadata.sh"

readonly kubeconfig="${KUBECONFIG:-${HOME}/.kube/config}"

readonly container_kubeconfig="/root/.kube/config"
readonly container_project_directory="/${PROJECT_NAME}"
readonly container_operator_directory="${container_project_directory}/operator"
readonly container_vendor_directory="${container_project_directory}/shared/vendor"

# Note: DS_KUDO_VERSION is used by the shared data-services-kudo tooling.

docker run \
--rm \
-e "KUBECONFIG=${container_kubeconfig}" \
-e "KUBECTL_PATH=${container_vendor_directory}/kubectl.sh" \
-e "DS_KUDO_VERSION=v${KUDO_VERSION}" \
-e "OPERATOR_DIRECTORY=${container_operator_directory}" \
-e "VENDOR_DIRECTORY=${container_vendor_directory}" \
-v "${kubeconfig}:${container_kubeconfig}:ro" \
-v "${OPERATOR_DIRECTORY}:${container_operator_directory}:ro" \
-v "${project_directory}:${container_project_directory}" \
-v "${VENDOR_DIRECTORY}:${container_vendor_directory}" \
-w "${container_project_directory}" \
"${INTEGRATION_TESTS_DOCKER_IMAGE}" \
bash -c "${container_project_directory}/tests/run.sh"
7 changes: 7 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Scripts

Scripts to be used by operators.

## `uninstall_operator.sh`

TODO(mpereira) description.
63 changes: 0 additions & 63 deletions scripts/uninstall_kudo_cassandra.sh

This file was deleted.

102 changes: 102 additions & 0 deletions scripts/uninstall_operator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
# shellcheck disable=SC2039

readonly script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly project_directory="$(readlink -f "${script_directory}/..")"

# shellcheck source=../metadata.sh
source "${project_directory}/metadata.sh"

readonly kubectl="${KUBECTL_PATH:-kubectl}"

operator_name=
operator_version=
operator_instance_name=
operator_instance_namespace=

usage() {
echo -n "Usage: ${0} " >&2
echo -n "--operator OPERATOR_NAME " >&2
echo -n "--version OPERATOR_VERSION " >&2
echo -n "--instance OPERATOR_INSTANCE_NAME " >&2
echo -n "--namespace OPERATOR_INSTANCE_NAMESPACE" >&2
echo >&2
}

while [[ ${#} -gt 0 ]]; do
# TODO(mpereira): handle parameters passed in as "parameter=value";
parameter="${1}"

case "${parameter}" in
--operator|-o)
operator_name="${2}"
shift
;;
--version|-v)
operator_version="${2}"
shift
;;
--instance|-i)
operator_instance_name="${2}"
shift
;;
--namespace|-n)
operator_instance_namespace="${2}"
shift
;;
--help|-h)
usage
exit 0
;;
*)
echo "Invalid parameter: ${parameter}" >&2
exit 1
;;
esac

shift
done

operator_name="${operator_name:-${OPERATOR_NAME}}"
operator_version="${operator_version:-${OPERATOR_VERSION}}"
operator_instance_name="${operator_instance_name:-${OPERATOR_INSTANCE_NAME}}"
operator_instance_namespace="${operator_instance_namespace:-${OPERATOR_INSTANCE_NAMESPACE}}"

for parameter in operator_name \
operator_version \
operator_instance_name \
operator_instance_namespace; do
if [[ -z ${!parameter} ]]; then
echo "Missing ${parameter}" >&2
echo >&2
usage
exit 1
fi
done

${kubectl} delete instance \
"${operator_instance_name}" \
-n "${operator_instance_namespace}"

# TODO(mpereira): add a flag to skip operatorversion deletion?
${kubectl} delete operatorversion \
"${operator_name}-${operator_version}" \
-n "${operator_instance_namespace}"

# TODO(mpereira): add a flag to skip operator deletion?
${kubectl} delete operator \
"${operator_name}" \
-n "${operator_instance_namespace}"

# TODO(mpereira): add a flag to skip pvc deletion?
declare -a PVCS
mapfile -t PVCS < <(
${kubectl} get pvc \
-n "${operator_instance_namespace}" \
-o 'jsonpath={.items[*].metadata.name}' \
| tr ' ' '\n'
)

for pvc in "${PVCS[@]}"; do
${kubectl} delete "pvc/${pvc}" -n "${operator_instance_namespace}"
done
2 changes: 1 addition & 1 deletion shared
2 changes: 1 addition & 1 deletion templates/operator/operator.yaml.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "cassandra"
version: "${KUDO_CASSANDRA_VERSION}"
version: "${OPERATOR_VERSION}"
kudoVersion: "${KUDO_VERSION}"
kubernetesVersion: "${KUBERNETES_VERSION}"
appVersion: "${CASSANDRA_VERSION}"
Expand Down
20 changes: 20 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/mesosphere/kudo-cassandra-operator/tests

go 1.13

require (
github.com/avast/retry-go v2.4.1+incompatible
github.com/gogo/protobuf v1.3.1 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/kudobuilder/kudo v0.7.4
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8
k8s.io/apimachinery v0.0.0-20190704094520-6f131bee5e2c
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
)
Loading

0 comments on commit 8e709e5

Please sign in to comment.