Skip to content

Commit

Permalink
Create terratests and CICD workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Sameer Kulkarni <[email protected]>
  • Loading branch information
samkulkarni20 committed Apr 23, 2020
1 parent 374d2a8 commit 5d5dfe5
Show file tree
Hide file tree
Showing 11 changed files with 1,036 additions and 75 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Repository Dispatch
on:
push:
pull_request:
repository_dispatch:
types: build-on-release
jobs:
build:
name: Build
runs-on: ubuntu-latest
if: github.event.client_payload.prerelease == 'false'
env:
GCP_KEY: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
GOOGLE_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
steps:
- name: Tag for Repository Dispatch event
if: github.event_name == 'repository_dispatch'
run: |
tag_name="${{ github.event.client_payload.tag }}"
version_number="$(echo "$tag_name" | sed 's/v//')"
count="$(echo "$version_number" | awk -F"." '{print NF-1}')"
if [[ $count -eq 2 ]]; then
version_number="$(echo "$version_number" | sed 's/$/.0/')"
fi
if [[ "$(echo "$version_number" | awk -F"." '{print NF-1}')" -ne 3 ]]; then
echo "The tag '$tag_name' is invalid" 1>&2
exit 1
fi
echo "::set-env name=TAG_NAME::$version_number"
- name: Google Cloud Platform (GCP) CLI - gcloud
uses: actions-hub/[email protected]
env:
# Project id
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
# GCP authorization credentials
APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
with:
args: auth list

- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13.5
id: go

- name: Setup Terraform environment
uses: marocchino/[email protected]
with:
# The terraform version to download (if necessary) and use. Example: 0.12.13
version: 0.12.13

- name: Check out code into the Go module directory
uses: actions/checkout@v1
with:
submodules: true

- name: Get dependencies
run: |
cd ./test
go mod download
- name: Build
run: |
echo "$GCP_KEY" | base64 -d > /tmp/account.json
export GOOGLE_APPLICATION_CREDENTIALS="/tmp/account.json"
cd ./test
go test -v -timeout 90m yugabyte_test.go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@
A Terraform module to deploy and run YugaByte on Google Cloud.

## Config
* First create a terraform file with provider details
```
provider "google"
{
# Provide your GCP Creadentilals
credentials = "${file("yugabyte-pcf-bc8114281026.json")}"
# The name of your GCP project
project = "yugabyte-pcf"
}
```
Note :- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started)

* Now add the yugabyte terraform module to your file
* First create a terraform file and add the yugabyte terraform module to your file
```
module "yugabyte-db-cluster" {
source = "github.com/YugaByte/terraform-gcp-yugabyte.git"
# Your GCP project id
project_id = "<YOUR-GCP-PROJECT-ID>"
# Your GCP credentials file path
credentials = "<PATH-OF-YOUR-GCP-CREDENTIAL-FILE>"
Note:- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started)
# The name of the cluster to be created.
cluster_name = "test-yugabyte"
# key pair.
ssh_private_key = "SSH_PRIVATE_KEY_HERE"
ssh_public_key = "SSH_PUBLIC_KEY_HERE"
# User name for ssh connection
ssh_user = "SSH_USER_NAME_HERE"
# The region name where the nodes should be spawned.
Expand All @@ -38,7 +31,7 @@ A Terraform module to deploy and run YugaByte on Google Cloud.
node_count = "3"
}
```


## Usage

Expand Down Expand Up @@ -81,3 +74,36 @@ To destroy what we just created, you can run the following command.
$ terraform destroy
```
`Note:- To make any changes in the created cluster you will need the terraform state files. So don't delete state files of Terraform.`

## Test

### Configurations

#### Prerequisites

- [Terraform **(~> 0.12.5)**](https://www.terraform.io/downloads.html)
- [Golang **(~> 1.12.10)**](https://golang.org/dl/)
- [dep **(~> 0.5.4)**](https://github.com/golang/dep)

#### Environment setup

* First install `dep` dependency management tool for Go.
```sh
$ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
```
* Change your working directory to the `test` folder.
* Run `dep` command to get required modules
```sh
$ dep ensure
```

#### Run test

Then simply run it in the local shell:

```sh
$ go test -v -timeout 15m yugabyte_test.go
```
* Note that go has a default test timeout of 10 minutes. With infrastructure testing, your tests will surpass the 10 minutes very easily. To extend the timeout, you can pass in the -timeout option, which takes a go duration string (e.g 10m for 10 minutes or 1h for 1 hour). In the above command, we use the -timeout option to override to a 90 minute timeout.
* When you hit the timeout, Go automatically exits the test, skipping all cleanup routines. This is problematic for infrastructure testing because it will skip your deferred infrastructure cleanup steps (i.e terraform destroy), leaving behind the infrastructure that was spun up. So it is important to use a longer timeout every time you run the tests.

72 changes: 45 additions & 27 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@

terraform {
required_version = ">= 0.12"
}

provider "google" {
credentials = "${file(var.credentials)}"
project = var.project_id
}

data "google_compute_image" "YugaByte_DB_Image" {
family = "centos-6"
family = "centos-7"
project = "centos-cloud"
}
data "google_compute_zones" "available" {
region = "${var.region_name}"
}

resource "tls_private_key" "example" {
algorithm = "RSA"
}

resource "local_file" "foo" {
content = "${tls_private_key.example.private_key_pem}"
filename = "${path.module}/foo"
file_permission = "0600"
}
resource "google_compute_firewall" "YugaByte-Firewall" {
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-firewall"
network = var.vpc_network
network = "${var.vpc_network}"
allow {
protocol = "tcp"
ports = ["9000","7000","6379","9042","5433","22"]
Expand All @@ -18,7 +36,7 @@ resource "google_compute_firewall" "YugaByte-Firewall" {
}
resource "google_compute_firewall" "YugaByte-Intra-Firewall" {
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-intra-firewall"
network = var.vpc_network
network = "${var.vpc_network}"
allow {
protocol = "tcp"
ports = ["7100", "9100"]
Expand All @@ -27,24 +45,24 @@ resource "google_compute_firewall" "YugaByte-Intra-Firewall" {
}

resource "google_compute_instance" "yugabyte_node" {
count = var.node_count
count = "${var.node_count}"
name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}"
machine_type = var.node_type
zone = element(data.google_compute_zones.available.names, count.index)
machine_type = "${var.node_type}"
zone = "${element(data.google_compute_zones.available.names, count.index)}"
tags=["${var.prefix}${var.cluster_name}"]

boot_disk{
initialize_params {
image = data.google_compute_image.YugaByte_DB_Image.self_link
size = var.disk_size
image = "${data.google_compute_image.YugaByte_DB_Image.self_link}"
size = "${var.disk_size}"
}
}
metadata = {
sshKeys = "${var.ssh_user}:${file(var.ssh_public_key)}"
sshKeys = "${var.ssh_user}:${tls_private_key.example.public_key_openssh}"
}

network_interface{
network = var.vpc_network
network = "${var.vpc_network}"
access_config {
// external ip to instance
}
Expand All @@ -54,41 +72,41 @@ resource "google_compute_instance" "yugabyte_node" {
source = "${path.module}/utilities/scripts/install_software.sh"
destination = "/home/${var.ssh_user}/install_software.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
user = "${var.ssh_user}"
private_key = "${tls_private_key.example.private_key_pem}"
}
}

provisioner "file" {
source = "${path.module}/utilities/scripts/create_universe.sh"
destination ="/home/${var.ssh_user}/create_universe.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
user = "${var.ssh_user}"
private_key = "${tls_private_key.example.private_key_pem}"
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_master.sh"
destination ="/home/${var.ssh_user}/start_master.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
user = "${var.ssh_user}"
private_key = "${tls_private_key.example.private_key_pem}"
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_tserver.sh"
destination ="/home/${var.ssh_user}/start_tserver.sh"
connection {
host = self.network_interface.0.access_config.0.nat_ip
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
user = "${var.ssh_user}"
private_key = "${tls_private_key.example.private_key_pem}"
}
}
provisioner "remote-exec" {
Expand All @@ -100,10 +118,10 @@ resource "google_compute_instance" "yugabyte_node" {
"/home/${var.ssh_user}/install_software.sh '${var.yb_version}'"
]
connection {
host = self.network_interface.0.access_config.0.nat_ip
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
user = "${var.ssh_user}"
private_key = "${tls_private_key.example.private_key_pem}"
}
}
}
Expand All @@ -116,10 +134,10 @@ locals {
}

resource "null_resource" "create_yugabyte_universe" {
depends_on = [google_compute_instance.yugabyte_node]
depends_on = ["google_compute_instance.yugabyte_node"]

provisioner "local-exec" {
command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${var.ssh_private_key}"
command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${local_file.foo.filename}"
}
}

26 changes: 19 additions & 7 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
output "ui" {
output "master-ui" {
sensitive = false
value = "http://${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip}:7000"
}
output "ssh_user" {
output "tserver-ui" {
sensitive = false
value = "${var.ssh_user}"
}
output "ssh_key" {
sensitive = false
value = "${var.ssh_private_key}"
value = "http://${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip}:9000"
}
#output "ssh_user" {
# sensitive = false
# value = "${var.ssh_user}"
#}
#output "ssh_key" {
# sensitive = false
# value = "${var.ssh_private_key}"
#}

output "JDBC" {
sensitive =false
Expand All @@ -30,3 +34,11 @@ output "YEDIS"{
sensitive = false
value = "redis-cli -h ${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip} -p 6379"
}

output "instance-ip"{
value = "${google_compute_instance.yugabyte_node.*.network_interface.0.access_config.0.nat_ip}"
}

output "hosts"{
value = "${google_compute_instance.yugabyte_node.*.name}"
}
5 changes: 5 additions & 0 deletions test/DirectoryList
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create_universe.sh
install_software.sh
start_master.sh
start_tserver.sh
yugabyte-db
9 changes: 9 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module terraform-gcp-yugabyte/test

go 1.13

require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/gruntwork-io/terratest v0.23.4
github.com/stretchr/testify v1.4.0
)
Loading

0 comments on commit 5d5dfe5

Please sign in to comment.