Skip to content

Commit

Permalink
Improve vagrant tests (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumo-drosiek committed Sep 24, 2020
1 parent 9837541 commit ad09539
Show file tree
Hide file tree
Showing 31 changed files with 258 additions and 169 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ deploy/helm/sumologic/requirements.lock
# vagrant ignores
.vagrant
ubuntu-bionic-18.04-cloudimg-console.log
.kube-config
.kube-config

# ignore tmp direcotries
tmp
40 changes: 17 additions & 23 deletions ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,16 @@ if [ -n "$GITHUB_TOKEN" ] && [ "$TRAVIS_EVENT_TYPE" == "pull_request" ] && [[ !
fi
fi

bundle_fluentd_plugins "${VERSION}" || (echo "Failed bundling fluentd plugins" && exit 1)

echo "Building docker image with $DOCKER_TAG:local in $(pwd)..."
cd ./deploy/docker || exit 1
docker build . -f ./Dockerfile -t "$DOCKER_TAG:local" --no-cache
rm -f ./gems/*.gem
cd ../.. || exit 1

echo "Test docker image locally..."
ruby deploy/test/test_docker.rb

echo "Test tracing configuration..."
if ./tests/tracing/run.sh; then
echo "Tracing configuration test passed"
else
echo "Tracing configuration test failed"
exit 1
fi

echo "Test terraform configuration..."
if ./tests/terraform/run.sh; then
echo "Terraform configuration test passed"
# Test if template files are generated correctly for various values.yaml
echo "Test helm templates generation"
if ./tests/run.sh; then
echo "Helm templates generation test passed"
else
echo "Terraform configuration test failed"
echo "Tracing templates generation test failed"
exit 1
fi

# Test upgrade script
echo "Test upgrade script..."
if ./tests/upgrade_script/run.sh; then
echo "Upgrade Script test passed"
Expand All @@ -119,6 +102,17 @@ else
exit 1
fi

bundle_fluentd_plugins "${VERSION}" || (echo "Failed bundling fluentd plugins" && exit 1)

echo "Building docker image with $DOCKER_TAG:local in $(pwd)..."
cd ./deploy/docker || exit 1
docker build . -f ./Dockerfile -t "$DOCKER_TAG:local" --no-cache
rm -f ./gems/*.gem
cd ../.. || exit 1

echo "Test docker image locally..."
ruby deploy/test/test_docker.rb

# Check for changes that require re-generating overrides yaml files
if [ -n "$GITHUB_TOKEN" ] && [ "$TRAVIS_EVENT_TYPE" == "pull_request" ]; then
echo "Generating deployment yaml from helm chart..."
Expand Down
37 changes: 37 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Helm template tests

To add test for new template file, please create new directory with given structure:

```text
example_test/ # Test set name
├── config.sh # Configuration file
└── static # Test cases
├── test_name.input.yaml # Input configuration for test_name
└── test_name.output.yaml # Output configuration for test_name
```

## Configuration file

`config.sh` should export `TEST_TEMPLATE` env variable, which should point to the helm template
file name, e.g. for `deploy/helm/sumologic/templates/configmap.yaml` it will be `templates/configmap.yaml`:

```bash
TEST_TEMPLATE="templates/configmap.yaml"
```

## Input file

Input file e.g. `test_name.input.yaml` should be compatible with `values.yaml`

## Output file

Output file is the yaml template which is expected to be output of the following command:

```bash
helm template deploy/helm/sumologic/ \
--namespace sumologic \
--set sumologic.accessId='accessId' \
--set sumologic.accessKey='accessKey' \
-f test_name.input.yaml \
-s values.yaml
```
111 changes: 111 additions & 0 deletions tests/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

function test_start() {
echo -e "[.] $*";
}

function test_passed() {
echo -e "[+] $*";
}

function test_failed() {
echo -e "[-] $*";
}

# Set variables used by the test
function set_variables() {
# Path to the tests directory
TEST_SCRIPT_PATH="${1}"
# Path to the static files (input and output)
TEST_STATICS_PATH="${TEST_SCRIPT_PATH}/static"
# Path to the temporary directory (created by the prepare_tests)
TEST_TMP_PATH="${TEST_SCRIPT_PATH}/tmp"
# List of test input files (names only)
TEST_INPUT_FILES="$(find "${TEST_STATICS_PATH}" -name '*input*' -exec basename {} \;)"
# Path to the temporary output file
TEST_OUT="${TEST_TMP_PATH}/new_values.yaml"
# Current version wchich should override the placeholder in test
CURRENT_CHART_VERSION=$(yq r "${TEST_SCRIPT_PATH}/../../deploy/helm/sumologic/Chart.yaml" version)
}

# Update helm chart and remove the tmpcharts eventually
function prepare_environment() {
local repo_path=${1}
rm -rf "${repo_path}/tmpcharts"
docker run --rm \
-v "${repo_path}":/chart \
sumologic/kubernetes-tools:master \
helm dependency update /chart
}

# Prepare temporary directory for tests
function prepare_tests() {
mkdir -p "${TEST_TMP_PATH}"
}

# Remove temporary directory
function cleanup_tests() {
if [[ -n "${TEST_TMP_PATH}" ]]; then
rm -rf "${TEST_TMP_PATH}"
fi

for env_name in $(env | grep -oE '^TEST_.*?=' | grep -v "TEST_SUCCESS" | sed 's/=//g'); do
unset "${env_name}"
done
}

# Patch tests with current helm chart version
function patch_test() {
local input_file="${1}"
local output_file="${2}"
sed "s/%CURRENT_CHART_VERSION%/${CURRENT_CHART_VERSION}/g" "${input_file}" > "${output_file}"
}

# Generate output file basing on the input values.yaml
function generate_file {
local template_name="${1}"

docker run --rm \
-v "${TEST_SCRIPT_PATH}/../../deploy/helm/sumologic":/chart \
-v "${TEST_STATICS_PATH}/${input_file}":/values.yaml \
sumologic/kubernetes-tools:master \
helm template /chart -f /values.yaml \
--namespace sumologic \
--set sumologic.accessId='accessId' \
--set sumologic.accessKey='accessKey' \
-s "${template_name}" 2>/dev/null 1> "${TEST_OUT}"
}

# Run test
function perform_test {
local input_file="${1}"
local template_name="${2}"

test_name="${input_file//.input.yaml/}"
output_file="${test_name}.output.yaml"

patch_test "${TEST_STATICS_PATH}/${output_file}" "${TEST_TMP_PATH}/${output_file}"

test_start "${test_name}"
generate_file "${template_name}"

test_output=$(diff "${TEST_TMP_PATH}/${output_file}" "${TEST_OUT}" | cat -te)
rm "${TEST_OUT}"

if [[ -n "${test_output}" ]]; then
echo -e "\tOutput diff (${TEST_STATICS_PATH}/${output_file}):\n${test_output}"
test_failed "${test_name}"
# Set all tests as failed
# This env comes from run.sh
export TEST_SUCCESS=false
else
test_passed "${test_name}"
fi
}

# Perform tests from the directory using TEST_INPUT_FILES
function perform_tests {
for input_file in ${TEST_INPUT_FILES}; do
perform_test "${input_file}" "${TEST_TEMPLATE}"
done
}
25 changes: 25 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

readonly SCRIPT_PATH="$( dirname $(realpath ${0}) )"
readonly CONFIG_FILES=$(find "${SCRIPT_PATH}"/* -maxdepth 1 -name 'config.sh')
source "${SCRIPT_PATH}/functions.sh"

export TEST_SUCCESS=true

prepare_environment "${SCRIPT_PATH}/../deploy/helm/sumologic"

for config_file in ${CONFIG_FILES}; do
test_dir="$( dirname $(realpath ${config_file}) )"
echo "Performing tests for $(basename "${test_dir}")"
source "${config_file}"
set_variables "${test_dir}"
prepare_tests
perform_tests
cleanup_tests
done

if [[ "${TEST_SUCCESS}" = "true" ]]; then
exit 0
else
exit 1
fi
3 changes: 3 additions & 0 deletions tests/terraform/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

export TEST_TEMPLATE="templates/setup/setup-configmap.yaml"
47 changes: 0 additions & 47 deletions tests/terraform/run.sh

This file was deleted.

4 changes: 2 additions & 2 deletions tests/terraform/static/all_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ data:
# Kubernetes Secret
terraform import kubernetes_secret.sumologic_collection_secret default/sumologic
terraform import kubernetes_secret.sumologic_collection_secret sumologic/sumologic
terraform apply -auto-approve
variables.tf: |-
Expand All @@ -220,5 +220,5 @@ data:
variable "namespace_name" {
type = string
default = "default"
default = "sumologic"
}
4 changes: 2 additions & 2 deletions tests/terraform/static/collector_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ data:
# Kubernetes Secret
terraform import kubernetes_secret.sumologic_collection_secret default/sumologic
terraform import kubernetes_secret.sumologic_collection_secret sumologic/sumologic
terraform apply -auto-approve
variables.tf: |-
Expand All @@ -174,5 +174,5 @@ data:
variable "namespace_name" {
type = string
default = "default"
default = "sumologic"
}
2 changes: 1 addition & 1 deletion tests/terraform/static/conditional_sources.input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sumologic:
traces:
enabled: false
events:
enabled: false
enabled: false
4 changes: 2 additions & 2 deletions tests/terraform/static/conditional_sources.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ data:
# Kubernetes Secret
terraform import kubernetes_secret.sumologic_collection_secret default/sumologic
terraform import kubernetes_secret.sumologic_collection_secret sumologic/sumologic
terraform apply -auto-approve
variables.tf: |-
Expand All @@ -91,5 +91,5 @@ data:
variable "namespace_name" {
type = string
default = "default"
default = "sumologic"
}
4 changes: 2 additions & 2 deletions tests/terraform/static/default.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ data:
# Kubernetes Secret
terraform import kubernetes_secret.sumologic_collection_secret default/sumologic
terraform import kubernetes_secret.sumologic_collection_secret sumologic/sumologic
terraform apply -auto-approve
variables.tf: |-
Expand All @@ -172,5 +172,5 @@ data:
variable "namespace_name" {
type = string
default = "default"
default = "sumologic"
}
2 changes: 1 addition & 1 deletion tests/terraform/static/strip_extrapolation.input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ sumologic:
cluster_ca_certificate: "${file(\"/var/run/secrets/kubernetes.io/serviceaccount/ca.crt\")}"
token: "${file(\"/var/run/secrets/kubernetes.io/serviceaccount/token\")}"
extrapolation: "${file(\"/var/run/secrets/kubernetes.io/serviceaccount/token\")}/${test}"
load_config_file: false
load_config_file: false
4 changes: 2 additions & 2 deletions tests/terraform/static/strip_extrapolation.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ data:
# Kubernetes Secret
terraform import kubernetes_secret.sumologic_collection_secret default/sumologic
terraform import kubernetes_secret.sumologic_collection_secret sumologic/sumologic
terraform apply -auto-approve
variables.tf: |-
Expand All @@ -173,5 +173,5 @@ data:
variable "namespace_name" {
type = string
default = "default"
default = "sumologic"
}
2 changes: 1 addition & 1 deletion tests/terraform/static/traces.input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sumologic:
traces:
enabled: true
events:
enabled: false
enabled: false
Loading

0 comments on commit ad09539

Please sign in to comment.