-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathk8s_util_test.go
More file actions
141 lines (118 loc) · 4.37 KB
/
k8s_util_test.go
File metadata and controls
141 lines (118 loc) · 4.37 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package gitreceive
import (
"fmt"
"strings"
"testing"
"k8s.io/kubernetes/pkg/api"
)
func TestDockerBuilderPodName(t *testing.T) {
name := dockerBuilderPodName("demo", "12345678")
if !strings.HasPrefix(name, "dockerbuild-demo-12345678-") {
t.Fatalf("expected pod name dockerbuild-demo-12345678-*, got %s", name)
}
}
func TestSlugBuilderPodName(t *testing.T) {
name := slugBuilderPodName("demo", "12345678")
if !strings.HasPrefix(name, "slugbuild-demo-12345678-") {
t.Fatalf("expected pod name slugbuild-demo-12345678-*, got %s", name)
}
}
type slugBuildCase struct {
debug bool
name string
namespace string
env map[string]interface{}
tarKey string
putKey string
buildPack string
slugBuilderImage string
storageType string
}
type dockerBuildCase struct {
debug bool
name string
namespace string
env map[string]interface{}
tarKey string
imgName string
dockerBuilderImage string
storageType string
}
func TestBuildPod(t *testing.T) {
emptyEnv := make(map[string]interface{})
env := make(map[string]interface{})
env["KEY"] = "VALUE"
var pod *api.Pod
slugBuilds := []slugBuildCase{
{true, "test", "default", emptyEnv, "tar", "put-url", "", "", ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "", "", ""},
{true, "test", "default", env, "tar", "put-url", "", "", ""},
{true, "test", "default", env, "tar", "put-url", "", "", ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "", ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", ""},
}
for _, build := range slugBuilds {
pod = slugbuilderPod(build.debug, build.name, build.namespace, build.env, build.tarKey, build.putKey, build.buildPack, build.slugBuilderImage, build.storageType)
if pod.ObjectMeta.Name != build.name {
t.Errorf("expected %v but returned %v ", build.name, pod.ObjectMeta.Name)
}
if pod.ObjectMeta.Namespace != build.namespace {
t.Errorf("expected %v but returned %v ", build.namespace, pod.ObjectMeta.Namespace)
}
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
checkForEnv(t, pod, "PUT_PATH", build.putKey)
if build.buildPack != "" {
checkForEnv(t, pod, "BUILDPACK_URL", build.buildPack)
}
if build.slugBuilderImage != "" {
if pod.Spec.Containers[0].Image != build.slugBuilderImage {
t.Errorf("expected %v but returned %v ", build.slugBuilderImage, pod.Spec.Containers[0].Image)
}
}
}
dockerBuilds := []dockerBuildCase{
{true, "test", "default", emptyEnv, "tar", "", "", ""},
{true, "test", "default", emptyEnv, "tar", "", "", ""},
{true, "test", "default", env, "tar", "", "", ""},
{true, "test", "default", env, "tar", "", "", ""},
{true, "test", "default", emptyEnv, "tar", "img", "", ""},
{true, "test", "default", emptyEnv, "tar", "img", "", ""},
{true, "test", "default", env, "tar", "img", "", ""},
{true, "test", "default", env, "tar", "img", "customimage", ""},
}
for _, build := range dockerBuilds {
pod = dockerBuilderPod(build.debug, build.name, build.namespace, build.env, build.tarKey, build.imgName, build.dockerBuilderImage, build.storageType)
if pod.ObjectMeta.Name != build.name {
t.Errorf("expected %v but returned %v ", build.name, pod.ObjectMeta.Name)
}
if pod.ObjectMeta.Namespace != build.namespace {
t.Errorf("expected %v but returned %v ", build.namespace, pod.ObjectMeta.Namespace)
}
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
checkForEnv(t, pod, "IMG_NAME", build.imgName)
if build.dockerBuilderImage != "" {
if pod.Spec.Containers[0].Image != build.dockerBuilderImage {
t.Errorf("expected %v but returned %v ", build.dockerBuilderImage, pod.Spec.Containers[0].Image)
}
}
}
}
func checkForEnv(t *testing.T, pod *api.Pod, key, expVal string) {
val, err := envValueFromKey(pod, key)
if err != nil {
t.Errorf("%v", err)
}
if val != val {
t.Errorf("expected %v but returned %v ", expVal, val)
}
}
func envValueFromKey(pod *api.Pod, key string) (string, error) {
for _, env := range pod.Spec.Containers[0].Env {
if env.Name == key {
return env.Value, nil
}
}
return "", fmt.Errorf("no key with name %v found in pod env", key)
}