Skip to content

Commit

Permalink
Add CircleCI config (hashicorp#254)
Browse files Browse the repository at this point in the history
* Upgrade to terraform 0.12

* Add terraform to the Test.dockerfile

* Add CircleCI config

* Rename .circleci/config.yaml to .circleci/config.yml

* Use env variable for the terraform version

* Switch to hashicorpdev/consul-helm-test Docker image

* Specify directory for terraform commands

Also, add a clarifying comment describing which image is being used

* Update .circleci/config.yml

Co-Authored-By: Luke Kysow <1034429+lkysow@users.noreply.github.com>

* Only run acceptance tests on the changes to master

Plus, minor changes to terraform commands.

* Split out acceptance tests into multiple steps for clarity

* use multiple steps rather than a single step
* 'terraform destroy' step runs always, even if previous steps fail
* decrease GKE cluster node pool from 5 to 3 to make spin ups faster
* wait for Helm to initialize to make sure tiller is ready
* use the latest consul-helm-test docker image
  • Loading branch information
ishustava authored Oct 17, 2019
1 parent c3c8694 commit 3835b22
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
51 changes: 51 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: 2
jobs:
unit:
docker:
# This image is built from test/docker/Test.dockerfile
- image: hashicorpdev/consul-helm-test:0.3.0

steps:
- checkout

- run:
name: Run Unit Tests
command: bats ./test/unit

acceptance:
docker:
# This image is build from test/docker/Test.dockerfile
- image: hashicorpdev/consul-helm-test:0.3.0

steps:
- checkout

- run:
name: terraform init & apply
command: |
terraform init test/terraform
echo "${GOOGLE_CREDENTIALS}" | gcloud auth activate-service-account --key-file=-
terraform apply -var project=${CLOUDSDK_CORE_PROJECT} -var init_cli=true -auto-approve test/terraform
- run:
name: Run acceptance tests
command: bats ./test/acceptance

- run:
name: terraform destroy
command: |
terraform destroy -auto-approve
when: always

workflows:
version: 2
test:
jobs:
- unit
- acceptance:
requires:
- unit
filters:
branches:
only: master
8 changes: 7 additions & 1 deletion test/docker/Test.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FROM alpine:latest
WORKDIR /root

ENV BATS_VERSION "1.1.0"
ENV TERRAFORM_VERSION "0.12.10"

# base packages
RUN apk update && apk add --no-cache --virtual .build-deps \
Expand All @@ -31,6 +32,11 @@ RUN curl -OL https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_clo
bash install_google_cloud_sdk.bash --disable-prompts --install-dir='/root/' && \
ln -s /root/google-cloud-sdk/bin/gcloud /usr/local/bin/gcloud

# terraform
RUN curl -sSL https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -o /tmp/tf.zip \
&& unzip /tmp/tf.zip \
&& ln -s /root/terraform /usr/local/bin/terraform

# kubectl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && \
chmod +x ./kubectl && \
Expand All @@ -42,4 +48,4 @@ RUN curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get |
# bats
RUN curl -sSL https://github.com/bats-core/bats-core/archive/v${BATS_VERSION}.tar.gz -o /tmp/bats.tgz \
&& tar -zxf /tmp/bats.tgz -C /tmp \
&& /bin/bash /tmp/bats-core-$BATS_VERSION/install.sh /usr/local
&& /bin/bash /tmp/bats-core-${BATS_VERSION}/install.sh /usr/local
28 changes: 14 additions & 14 deletions test/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ locals {
}

provider "google" {
project = "${var.project}"
project = var.project
}

resource "random_id" "suffix" {
byte_length = 4
}

data "google_container_engine_versions" "main" {
zone = "${var.zone}"
location = var.zone
}

resource "google_container_cluster" "cluster" {
name = "consul-k8s-${random_id.suffix.dec}"
project = "${var.project}"
project = var.project
enable_legacy_abac = true
initial_node_count = 5
zone = "${var.zone}"
min_master_version = "${data.google_container_engine_versions.main.latest_master_version}"
node_version = "${data.google_container_engine_versions.main.latest_node_version}"
initial_node_count = 3
location = var.zone
min_master_version = data.google_container_engine_versions.main.latest_master_version
node_version = data.google_container_engine_versions.main.latest_node_version
}

resource "null_resource" "kubectl" {
count = "${var.init_cli ? 1 : 0 }"
count = var.init_cli ? 1 : 0

triggers {
cluster = "${google_container_cluster.cluster.id}"
triggers = {
cluster = google_container_cluster.cluster.id
}

# On creation, we want to setup the kubectl credentials. The easiest way
Expand All @@ -55,17 +55,17 @@ resource "null_resource" "kubectl" {
}

resource "null_resource" "helm" {
count = "${var.init_cli ? 1 : 0 }"
count = var.init_cli ? 1 : 0
depends_on = ["null_resource.kubectl"]

triggers {
cluster = "${google_container_cluster.cluster.id}"
triggers = {
cluster = google_container_cluster.cluster.id
}

provisioner "local-exec" {
command = <<EOF
kubectl apply -f '${local.service_account_path}'
helm init --service-account helm
helm init --service-account helm --wait
EOF
}
}
4 changes: 2 additions & 2 deletions test/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
output "cluster_id" {
value = "${google_container_cluster.cluster.id}"
value = google_container_cluster.cluster.id
}

output "cluster_name" {
value = "${google_container_cluster.cluster.name}"
value = google_container_cluster.cluster.name
}

0 comments on commit 3835b22

Please sign in to comment.