Skip to content

Commit cffd5db

Browse files
committed
Create terratests and CICD workflow
Signed-off-by: Sameer Kulkarni <[email protected]>
1 parent 374d2a8 commit cffd5db

File tree

11 files changed

+1036
-75
lines changed

11 files changed

+1036
-75
lines changed

.github/workflows/build.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Repository Dispatch
2+
on:
3+
push:
4+
pull_request:
5+
repository_dispatch:
6+
types: build-on-release
7+
jobs:
8+
build:
9+
name: Build
10+
runs-on: ubuntu-latest
11+
if: github.event_name != 'repository_dispatch' || github.event.client_payload.prerelease == 'false'
12+
env:
13+
GCP_KEY: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
14+
GOOGLE_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
15+
steps:
16+
- name: Tag for Repository Dispatch event
17+
if: github.event_name == 'repository_dispatch'
18+
run: |
19+
tag_name="${{ github.event.client_payload.tag }}"
20+
version_number="$(echo "$tag_name" | sed 's/v//')"
21+
count="$(echo "$version_number" | awk -F"." '{print NF-1}')"
22+
if [[ $count -eq 2 ]]; then
23+
version_number="$(echo "$version_number" | sed 's/$/.0/')"
24+
fi
25+
if [[ "$(echo "$version_number" | awk -F"." '{print NF-1}')" -ne 3 ]]; then
26+
echo "The tag '$tag_name' is invalid" 1>&2
27+
exit 1
28+
fi
29+
echo "::set-env name=TAG_NAME::$version_number"
30+
- name: Google Cloud Platform (GCP) CLI - gcloud
31+
uses: actions-hub/[email protected]
32+
env:
33+
# Project id
34+
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
35+
# GCP authorization credentials
36+
APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
37+
with:
38+
args: auth list
39+
40+
- name: Set up Go 1.13
41+
uses: actions/setup-go@v1
42+
with:
43+
go-version: 1.13.5
44+
id: go
45+
46+
- name: Setup Terraform environment
47+
uses: marocchino/[email protected]
48+
with:
49+
# The terraform version to download (if necessary) and use. Example: 0.12.13
50+
version: 0.12.13
51+
52+
- name: Check out code into the Go module directory
53+
uses: actions/checkout@v1
54+
with:
55+
submodules: true
56+
57+
- name: Get dependencies
58+
run: |
59+
cd ./test
60+
go mod download
61+
62+
- name: Build
63+
run: |
64+
echo "$GCP_KEY" | base64 -d > /tmp/account.json
65+
export GOOGLE_APPLICATION_CREDENTIALS="/tmp/account.json"
66+
cd ./test
67+
go test -v -timeout 90m yugabyte_test.go
68+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,23 @@
22
A Terraform module to deploy and run YugaByte on Google Cloud.
33

44
## Config
5-
* First create a terraform file with provider details
6-
```
7-
provider "google"
8-
{
9-
# Provide your GCP Creadentilals
10-
credentials = "${file("yugabyte-pcf-bc8114281026.json")}"
11-
12-
# The name of your GCP project
13-
project = "yugabyte-pcf"
14-
}
15-
```
16-
Note :- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started)
17-
18-
* Now add the yugabyte terraform module to your file
5+
* First create a terraform file and add the yugabyte terraform module to your file
196
```
207
module "yugabyte-db-cluster" {
218
source = "github.com/YugaByte/terraform-gcp-yugabyte.git"
229
10+
# Your GCP project id
11+
project_id = "<YOUR-GCP-PROJECT-ID>"
12+
13+
# Your GCP credentials file path
14+
credentials = "<PATH-OF-YOUR-GCP-CREDENTIAL-FILE>"
15+
16+
Note:- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started)
17+
2318
# The name of the cluster to be created.
2419
cluster_name = "test-yugabyte"
2520
26-
# key pair.
27-
ssh_private_key = "SSH_PRIVATE_KEY_HERE"
28-
ssh_public_key = "SSH_PUBLIC_KEY_HERE"
21+
# User name for ssh connection
2922
ssh_user = "SSH_USER_NAME_HERE"
3023
3124
# The region name where the nodes should be spawned.
@@ -38,7 +31,7 @@ A Terraform module to deploy and run YugaByte on Google Cloud.
3831
node_count = "3"
3932
}
4033
```
41-
34+
4235

4336
## Usage
4437

@@ -81,3 +74,36 @@ To destroy what we just created, you can run the following command.
8174
$ terraform destroy
8275
```
8376
`Note:- To make any changes in the created cluster you will need the terraform state files. So don't delete state files of Terraform.`
77+
78+
## Test
79+
80+
### Configurations
81+
82+
#### Prerequisites
83+
84+
- [Terraform **(~> 0.12.5)**](https://www.terraform.io/downloads.html)
85+
- [Golang **(~> 1.12.10)**](https://golang.org/dl/)
86+
- [dep **(~> 0.5.4)**](https://github.com/golang/dep)
87+
88+
#### Environment setup
89+
90+
* First install `dep` dependency management tool for Go.
91+
```sh
92+
$ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
93+
```
94+
* Change your working directory to the `test` folder.
95+
* Run `dep` command to get required modules
96+
```sh
97+
$ dep ensure
98+
```
99+
100+
#### Run test
101+
102+
Then simply run it in the local shell:
103+
104+
```sh
105+
$ go test -v -timeout 15m yugabyte_test.go
106+
```
107+
* 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.
108+
* 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.
109+

main.tf

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11

2+
terraform {
3+
required_version = ">= 0.12"
4+
}
5+
6+
provider "google" {
7+
credentials = "${file(var.credentials)}"
8+
project = var.project_id
9+
}
10+
211
data "google_compute_image" "YugaByte_DB_Image" {
3-
family = "centos-6"
12+
family = "centos-7"
413
project = "centos-cloud"
514
}
615
data "google_compute_zones" "available" {
716
region = "${var.region_name}"
817
}
918

19+
resource "tls_private_key" "example" {
20+
algorithm = "RSA"
21+
}
22+
23+
resource "local_file" "foo" {
24+
content = "${tls_private_key.example.private_key_pem}"
25+
filename = "${path.module}/foo"
26+
file_permission = "0600"
27+
}
1028
resource "google_compute_firewall" "YugaByte-Firewall" {
1129
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-firewall"
12-
network = var.vpc_network
30+
network = "${var.vpc_network}"
1331
allow {
1432
protocol = "tcp"
1533
ports = ["9000","7000","6379","9042","5433","22"]
@@ -18,7 +36,7 @@ resource "google_compute_firewall" "YugaByte-Firewall" {
1836
}
1937
resource "google_compute_firewall" "YugaByte-Intra-Firewall" {
2038
name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-intra-firewall"
21-
network = var.vpc_network
39+
network = "${var.vpc_network}"
2240
allow {
2341
protocol = "tcp"
2442
ports = ["7100", "9100"]
@@ -27,24 +45,24 @@ resource "google_compute_firewall" "YugaByte-Intra-Firewall" {
2745
}
2846

2947
resource "google_compute_instance" "yugabyte_node" {
30-
count = var.node_count
48+
count = "${var.node_count}"
3149
name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}"
32-
machine_type = var.node_type
33-
zone = element(data.google_compute_zones.available.names, count.index)
50+
machine_type = "${var.node_type}"
51+
zone = "${element(data.google_compute_zones.available.names, count.index)}"
3452
tags=["${var.prefix}${var.cluster_name}"]
3553

3654
boot_disk{
3755
initialize_params {
38-
image = data.google_compute_image.YugaByte_DB_Image.self_link
39-
size = var.disk_size
56+
image = "${data.google_compute_image.YugaByte_DB_Image.self_link}"
57+
size = "${var.disk_size}"
4058
}
4159
}
4260
metadata = {
43-
sshKeys = "${var.ssh_user}:${file(var.ssh_public_key)}"
61+
sshKeys = "${var.ssh_user}:${tls_private_key.example.public_key_openssh}"
4462
}
4563

4664
network_interface{
47-
network = var.vpc_network
65+
network = "${var.vpc_network}"
4866
access_config {
4967
// external ip to instance
5068
}
@@ -54,41 +72,41 @@ resource "google_compute_instance" "yugabyte_node" {
5472
source = "${path.module}/utilities/scripts/install_software.sh"
5573
destination = "/home/${var.ssh_user}/install_software.sh"
5674
connection {
57-
host = self.network_interface.0.access_config.0.nat_ip
75+
host = "${self.network_interface.0.access_config.0.nat_ip}"
5876
type = "ssh"
59-
user = var.ssh_user
60-
private_key = file(var.ssh_private_key)
77+
user = "${var.ssh_user}"
78+
private_key = "${tls_private_key.example.private_key_pem}"
6179
}
6280
}
6381

6482
provisioner "file" {
6583
source = "${path.module}/utilities/scripts/create_universe.sh"
6684
destination ="/home/${var.ssh_user}/create_universe.sh"
6785
connection {
68-
host = self.network_interface.0.access_config.0.nat_ip
86+
host = "${self.network_interface.0.access_config.0.nat_ip}"
6987
type = "ssh"
70-
user = var.ssh_user
71-
private_key = file(var.ssh_private_key)
88+
user = "${var.ssh_user}"
89+
private_key = "${tls_private_key.example.private_key_pem}"
7290
}
7391
}
7492
provisioner "file" {
7593
source = "${path.module}/utilities/scripts/start_master.sh"
7694
destination ="/home/${var.ssh_user}/start_master.sh"
7795
connection {
78-
host = self.network_interface.0.access_config.0.nat_ip
96+
host = "${self.network_interface.0.access_config.0.nat_ip}"
7997
type = "ssh"
80-
user = var.ssh_user
81-
private_key = file(var.ssh_private_key)
98+
user = "${var.ssh_user}"
99+
private_key = "${tls_private_key.example.private_key_pem}"
82100
}
83101
}
84102
provisioner "file" {
85103
source = "${path.module}/utilities/scripts/start_tserver.sh"
86104
destination ="/home/${var.ssh_user}/start_tserver.sh"
87105
connection {
88-
host = self.network_interface.0.access_config.0.nat_ip
106+
host = "${self.network_interface.0.access_config.0.nat_ip}"
89107
type = "ssh"
90-
user = var.ssh_user
91-
private_key = file(var.ssh_private_key)
108+
user = "${var.ssh_user}"
109+
private_key = "${tls_private_key.example.private_key_pem}"
92110
}
93111
}
94112
provisioner "remote-exec" {
@@ -100,10 +118,10 @@ resource "google_compute_instance" "yugabyte_node" {
100118
"/home/${var.ssh_user}/install_software.sh '${var.yb_version}'"
101119
]
102120
connection {
103-
host = self.network_interface.0.access_config.0.nat_ip
121+
host = "${self.network_interface.0.access_config.0.nat_ip}"
104122
type = "ssh"
105-
user = var.ssh_user
106-
private_key = file(var.ssh_private_key)
123+
user = "${var.ssh_user}"
124+
private_key = "${tls_private_key.example.private_key_pem}"
107125
}
108126
}
109127
}
@@ -116,10 +134,10 @@ locals {
116134
}
117135

118136
resource "null_resource" "create_yugabyte_universe" {
119-
depends_on = [google_compute_instance.yugabyte_node]
137+
depends_on = ["google_compute_instance.yugabyte_node"]
120138

121139
provisioner "local-exec" {
122-
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}"
140+
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}"
123141
}
124142
}
125143

outputs.tf

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
output "ui" {
1+
output "master-ui" {
22
sensitive = false
33
value = "http://${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip}:7000"
44
}
5-
output "ssh_user" {
5+
output "tserver-ui" {
66
sensitive = false
7-
value = "${var.ssh_user}"
8-
}
9-
output "ssh_key" {
10-
sensitive = false
11-
value = "${var.ssh_private_key}"
7+
value = "http://${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip}:9000"
128
}
9+
#output "ssh_user" {
10+
# sensitive = false
11+
# value = "${var.ssh_user}"
12+
#}
13+
#output "ssh_key" {
14+
# sensitive = false
15+
# value = "${var.ssh_private_key}"
16+
#}
1317

1418
output "JDBC" {
1519
sensitive =false
@@ -30,3 +34,11 @@ output "YEDIS"{
3034
sensitive = false
3135
value = "redis-cli -h ${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip} -p 6379"
3236
}
37+
38+
output "instance-ip"{
39+
value = "${google_compute_instance.yugabyte_node.*.network_interface.0.access_config.0.nat_ip}"
40+
}
41+
42+
output "hosts"{
43+
value = "${google_compute_instance.yugabyte_node.*.name}"
44+
}

test/DirectoryList

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
create_universe.sh
2+
install_software.sh
3+
start_master.sh
4+
start_tserver.sh
5+
yugabyte-db

test/go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module terraform-gcp-yugabyte/test
2+
3+
go 1.13
4+
5+
require (
6+
github.com/Pallinder/go-randomdata v1.2.0
7+
github.com/gruntwork-io/terratest v0.23.4
8+
github.com/stretchr/testify v1.4.0
9+
)

0 commit comments

Comments
 (0)