-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathk8s_util_test.go
More file actions
181 lines (158 loc) · 5.95 KB
/
k8s_util_test.go
File metadata and controls
181 lines (158 loc) · 5.95 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
slugBuilderImagePullPolicy api.PullPolicy
storageType string
}
type dockerBuildCase struct {
debug bool
name string
namespace string
env map[string]interface{}
tarKey string
imgName string
dockerBuilderImage string
dockerBuilderImagePullPolicy api.PullPolicy
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", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullNever, ""},
}
for _, build := range slugBuilds {
pod = slugbuilderPod(
build.debug,
build.name,
build.namespace,
build.env,
build.tarKey,
build.putKey,
build.buildPack,
build.storageType,
build.slugBuilderImage,
build.slugBuilderImagePullPolicy,
)
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)
}
}
if build.slugBuilderImagePullPolicy != "" {
if pod.Spec.Containers[0].ImagePullPolicy != build.slugBuilderImagePullPolicy {
t.Errorf("expected %v but returned %v", build.slugBuilderImagePullPolicy, pod.Spec.Containers[0].ImagePullPolicy)
}
}
}
dockerBuilds := []dockerBuildCase{
{true, "test", "default", emptyEnv, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "img", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullNever, ""},
}
for _, build := range dockerBuilds {
pod = dockerBuilderPod(
build.debug,
build.name,
build.namespace,
build.env,
build.tarKey,
build.imgName,
build.storageType,
build.dockerBuilderImage,
"5555",
build.dockerBuilderImagePullPolicy,
)
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)
}
}
if build.dockerBuilderImagePullPolicy != "" {
if pod.Spec.Containers[0].ImagePullPolicy != "" {
if pod.Spec.Containers[0].ImagePullPolicy != build.dockerBuilderImagePullPolicy {
t.Errorf("expected %v but returned %v", build.dockerBuilderImagePullPolicy, pod.Spec.Containers[0].ImagePullPolicy)
}
}
}
}
}
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)
}