Skip to content

Commit

Permalink
Put terraform tests into packages and cleanup upgrade test
Browse files Browse the repository at this point in the history
Signed-off-by: rancher-max <max.ross@suse.com>
  • Loading branch information
rancher-max authored and brandond committed Jul 21, 2022
1 parent 989f3b3 commit ca15e0c
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 345 deletions.
8 changes: 5 additions & 3 deletions tests/terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ go test -timeout=60m ./tests/terrfaorm/... -run TF
```
Tests can be run individually with:
```bash
go test -timeout=30m ./tests/terraform/createcluster.go ./tests/terraform/createcluster_test.go ./tests/terraform/testutils.go
go test -timeout=30m ./tests/terraform/createcluster/createcluster.go ./tests/terraform/createcluster/createcluster_test.go
# OR
go test -v -timeout=30m ./tests/terraform/... -run TFClusterCreateValidation
# example with vars:
go test -timeout=30m -v ./tests/terraform/createcluster.go ./tests/terraform/createcluster_test.go ./tests/terraform/testutils.go -node_os=ubuntu -aws_ami=ami-02f3416038bdb17fb -cluster_type=etcd -resource_name=localrun1 -sshuser=ubuntu -sshkey="key-name" -destroy=false
go test -timeout=30m -v ./tests/terraform/createcluster.go ./tests/terraform/createcluster_test.go -node_os=ubuntu -aws_ami=ami-02f3416038bdb17fb -cluster_type=etcd -resource_name=localrun1 -sshuser=ubuntu -sshkey="key-name" -destroy=false
```

In between tests, if the cluster is not destroyed, then make sure to delete the ./tests/terraform/terraform.tfstate file if you want to create a new cluster.
Expand All @@ -40,4 +42,4 @@ Note: The `go test` default timeout is 10 minutes, thus the `-timeout` flag shou

# Debugging
The cluster and VMs can be retained after a test by passing `-destroy=false`.
To focus individual runs on specific test clauses, you can prefix with `F`. For example, in the [create cluster test](../tests/terraform/createcluster_test.go), you can upate the initial creation to be: `FIt("Starts up with no issues", func() {` in order to focus the run on only that clause.
To focus individual runs on specific test clauses, you can prefix with `F`. For example, in the [create cluster test](../tests/terraform/createcluster_test.go), you can update the initial creation to be: `FIt("Starts up with no issues", func() {` in order to focus the run on only that clause.
74 changes: 0 additions & 74 deletions tests/terraform/createcluster.go

This file was deleted.

142 changes: 142 additions & 0 deletions tests/terraform/createcluster/createcluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package createcluster

import (
"fmt"

"path/filepath"
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
tf "github.com/k3s-io/k3s/tests/terraform"
)

var (
KubeConfigFile string
MasterIPs string
WorkerIPs string
)

type options struct {
nodeOs string
awsAmi string
clusterType string
resourceName string
externalDb string
sshuser string
sshkey string
accessKey string
serverNodes int
workerNodes int
}

func ClusterOptions(os ...ClusterOption) map[string]interface{} {
opts := options{}
for _, o := range os {
opts = o(opts)
}
return map[string]interface{}{
"node_os": opts.nodeOs,
"aws_ami": opts.awsAmi,
"cluster_type": opts.clusterType,
"resource_name": opts.resourceName,
"external_db": opts.externalDb,
"aws_user": opts.sshuser,
"key_name": opts.sshkey,
"access_key": opts.accessKey,
"no_of_server_nodes": opts.serverNodes,
"no_of_worker_nodes": opts.workerNodes,
}
}

func BuildCluster(t *testing.T, tfVarsPath string, destroy bool, terraformVars map[string]interface{}) (string, error) {
basepath := tf.GetBasepath()
tfDir, err := filepath.Abs(basepath + "/tests/terraform/modules/k3scluster")
if err != nil {
return "", err
}
varDir, err := filepath.Abs(basepath + tfVarsPath)
if err != nil {
return "", err
}
TerraformOptions := &terraform.Options{
TerraformDir: tfDir,
VarFiles: []string{varDir},
Vars: terraformVars,
}

if destroy {
fmt.Printf("Cluster is being deleted")
terraform.Destroy(t, TerraformOptions)
return "cluster destroyed", err
}

fmt.Printf("Creating Cluster")
terraform.InitAndApply(t, TerraformOptions)
KubeConfigFile = "/tmp/" + terraform.Output(t, TerraformOptions, "kubeconfig") + "_kubeconfig"
MasterIPs = terraform.Output(t, TerraformOptions, "master_ips")
WorkerIPs = terraform.Output(t, TerraformOptions, "worker_ips")
return "cluster created", err
}

type ClusterOption func(o options) options

func NodeOs(n string) ClusterOption {
return func(o options) options {
o.nodeOs = n
return o
}
}
func AwsAmi(n string) ClusterOption {
return func(o options) options {
o.awsAmi = n
return o
}
}
func ClusterType(n string) ClusterOption {
return func(o options) options {
o.clusterType = n
return o
}
}
func ResourceName(n string) ClusterOption {
return func(o options) options {
o.resourceName = n
return o
}
}
func ExternalDb(n string) ClusterOption {
return func(o options) options {
o.externalDb = n
return o
}
}
func Sshuser(n string) ClusterOption {
return func(o options) options {
o.sshuser = n
return o
}
}
func Sshkey(n string) ClusterOption {
return func(o options) options {
o.sshkey = n
return o
}
}
func AccessKey(n string) ClusterOption {
return func(o options) options {
o.accessKey = n
return o
}
}
func ServerNodes(n int) ClusterOption {
return func(o options) options {
o.serverNodes = n
return o
}
}
func WorkerNodes(n int) ClusterOption {
return func(o options) options {
o.workerNodes = n
return o
}
}
Loading

0 comments on commit ca15e0c

Please sign in to comment.