forked from gruntwork-io/helm-kubernetes-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_app_test_helpers.go
69 lines (61 loc) · 2.12 KB
/
sample_app_test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// +build all integration
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
// run just the template tests. See the test README for more information.
package test
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/shell"
)
// Expected response from the sample app is a json
type SampleAppResponse struct {
Text string `json:"text"`
}
// createSampleAppDockerImage builds the sample app docker image into the minikube environment, tagging it using the
// unique ID.
func createSampleAppDockerImage(t *testing.T, uniqueID string, examplePath string) {
dockerWorkingDir := filepath.Join(examplePath, "docker")
cmdsToRun := []string{}
// Build the docker environment to talk to minikube daemon
// In CircleCI, we have to run minikube in none driver mode. In this mode, minikube runs directly on the machine
// using the existing docker, so no environment prep is necessary. For all other environments, this is necessary.
isInCircle := os.Getenv("CIRCLECI") == "true"
if !isInCircle {
cmdsToRun = append(cmdsToRun, "eval $(minikube docker-env)")
}
cmdsToRun = append(
cmdsToRun,
// Build the image and tag using the unique ID
fmt.Sprintf("docker build -t gruntwork-io/sample-sinatra-app:%s .", uniqueID),
)
cmd := shell.Command{
Command: "sh",
Args: []string{
"-c",
strings.Join(cmdsToRun, " && "),
},
WorkingDir: dockerWorkingDir,
}
shell.RunCommand(t, cmd)
}
// sampleAppValidationFunctionGenerator will output a validation function that can be used with the pod verification
// code in k8s_service_example_test_helpers.go.
func sampleAppValidationFunctionGenerator(t *testing.T, expectedText string) func(int, string) bool {
return func(statusCode int, body string) bool {
if statusCode != 200 {
return false
}
var resp SampleAppResponse
err := json.Unmarshal([]byte(body), &resp)
if err != nil {
logger.Logf(t, "Error unmarshalling sample app response: %s", err)
return false
}
return resp.Text == expectedText
}
}