-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild_test.go
More file actions
219 lines (189 loc) · 5.93 KB
/
build_test.go
File metadata and controls
219 lines (189 loc) · 5.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package gitreceive
import (
"bytes"
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
builderconf "github.com/drycc/builder/pkg/conf"
"github.com/drycc/builder/pkg/sys"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/pkg/log"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
type testJSONStruct struct {
Foo string `json:"foo,omitempty"`
}
type podSelectorBuildCase struct {
Config string
Output map[string]string
}
func TestBuild(t *testing.T) {
config := &Config{}
env := sys.NewFakeEnv()
// NOTE(bacongobbler): there's a little easter egg here... ;)
sha := "0462cef5812ce31fe12f25596ff68dc614c708af"
tmpDir, err := os.MkdirTemp("", "tmpdir")
if err != nil {
t.Fatalf("error creating temp directory (%s)", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Fatalf("failed to remove tmpdir (%s)", err)
}
}()
config.GitHome = tmpDir
storageDriver, err := factory.Create(context.Background(), "inmemory", nil)
if err != nil {
t.Fatal(err)
}
if err := build(config, storageDriver, nil, env, sha); err == nil {
t.Error("expected running build() without setting config.ImagebuilderImagePullPolicy to fail")
}
config.ImagebuilderImagePullPolicy = "Always"
if err := build(config, storageDriver, nil, env, sha); err == nil {
t.Error("expected running build() without setting config.ImagebuilderImagePullPolicy to fail")
}
err = build(config, storageDriver, nil, env, "abc123")
expected := "git sha abc123 was invalid"
if err.Error() != expected {
t.Errorf("expected '%s', got '%v'", expected, err.Error())
}
if err := build(config, storageDriver, nil, env, sha); err == nil {
t.Error("expected running build() without valid controller client info to fail")
}
config.ControllerHost = "localhost"
config.ControllerPort = "1234"
if err := build(config, storageDriver, nil, env, sha); err == nil {
t.Error("expected running build() without a valid builder key to fail")
}
builderconf.BuilderKeyLocation = filepath.Join(tmpDir, "builder-key")
data := []byte("testbuilderkey")
if err := os.WriteFile(builderconf.BuilderKeyLocation, data, 0644); err != nil {
t.Fatalf("error creating %s (%s)", builderconf.BuilderKeyLocation, err)
}
if err := build(config, storageDriver, nil, env, sha); err == nil {
t.Error("expected running build() without a valid controller connection to fail")
}
}
func TestRepoCmd(t *testing.T) {
cmd := repoCmd("/tmp", "ls")
if cmd.Dir != "/tmp" {
t.Errorf("expected '%s', got '%s'", "/tmp", cmd.Dir)
}
}
func TestGetProcfileFromRepoSuccess(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "tmpdir")
if err != nil {
t.Fatalf("error creating temp directory (%s)", err)
}
data := []byte("web: example-go")
if err := os.WriteFile(tmpDir+"/Procfile", data, 0644); err != nil {
t.Fatalf("error creating %s/Procfile (%s)", tmpDir, err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Fatalf("failed to remove Procfile from %s (%s)", tmpDir, err)
}
}()
procType, err := getProcfile(tmpDir)
actualData := api.ProcessType{}
yaml.Unmarshal(data, &actualData)
assert.Equal(t, err, nil)
assert.Equal(t, procType, actualData, "data")
}
func TestGetProcfileFromRepoFailure(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "tmpdir")
if err != nil {
t.Fatalf("error creating temp directory (%s)", err)
}
data := []byte("web= example-go")
if err := os.WriteFile(tmpDir+"/Procfile", data, 0644); err != nil {
t.Fatalf("error creating %s/Procfile (%s)", tmpDir, err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Fatalf("failed to remove Procfile from %s (%s)", tmpDir, err)
}
}()
_, err = getProcfile(tmpDir)
assert.True(t, err != nil, "no error received when there should have been")
}
func TestGetProcfileFromServerSuccess(t *testing.T) {
data := []byte("")
expect, _ := getProcfile("")
actualData := api.ProcessType{}
yaml.Unmarshal(data, &actualData)
assert.Equal(t, expect, actualData)
}
func TestPrettyPrintJSON(t *testing.T) {
f := testJSONStruct{Foo: "bar"}
output, err := prettyPrintJSON(f)
if err != nil {
t.Errorf("expected error to be nil, got '%v'", err)
}
expected := `{
"foo": "bar"
}
`
if output != expected {
t.Errorf("expected\n%s, got\n%s", expected, output)
}
output, err = prettyPrintJSON(testJSONStruct{})
if err != nil {
t.Errorf("expected error to be nil, got %v", err)
}
expected = "{}\n"
if output != expected {
t.Errorf("expected\n%s, got\n%s", expected, output)
}
}
func captureOutput(f func()) string {
var buf bytes.Buffer
log.DefaultLogger.SetDebug(true)
log.DefaultLogger.SetStdout(&buf)
f()
return buf.String()
}
func TestRunCmd(t *testing.T) {
cmd := exec.Command("ls")
if err := run(cmd); err != nil {
t.Errorf("expected error to be nil, got %v", err)
}
// test log output
output := captureOutput(func() {
run(cmd)
})
expected := "running [ls]\n"
if output != expected {
t.Errorf("expected '%s', got '%s'", expected, output)
}
cmd.Dir = "/"
expected = "running [ls] in directory /\n"
output = captureOutput(func() {
run(cmd)
})
if output != expected {
t.Errorf("expected '%s', got '%s'", expected, output)
}
}
func TestBuildBuilderPodNodeSelector(t *testing.T) {
emptyNodeSelector := make(map[string]string)
cazes := []podSelectorBuildCase{
{"", emptyNodeSelector},
{"pool:worker", map[string]string{"pool": "worker"}},
{"pool:worker,network:fast", map[string]string{"pool": "worker", "network": "fast"}},
{"pool:worker ,network:fast, disk:ssd", map[string]string{"pool": "worker", "network": "fast", "disk": "ssd"}},
}
for _, caze := range cazes {
output, err := buildBuilderPodNodeSelector(caze.Config)
assert.Nil(t, err, "error")
assert.Equal(t, output, caze.Output, "pod selector")
}
_, err := buildBuilderPodNodeSelector("invalidformat")
assert.NotEqual(t, err, nil, "invalid format")
}