This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build_test.go
206 lines (165 loc) · 5.66 KB
/
build_test.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
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
package grim
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
const testBuildtimeout = time.Second * time.Duration(10)
func TestPreparePublicRepo(t *testing.T) {
if testing.Short() {
t.Skipf("Skipping prepare test in short mode.")
}
f, err := ioutil.TempDir("", "prepare-public-repo-test")
if err != nil {
t.Errorf("|%v|", err)
}
basename := getTimeStamp()
clonePath := filepath.Join("foo", "bar", "baz")
builder := &workspaceBuilder{
workspaceRoot: f,
clonePath: clonePath,
//public repo shouldnt require token
token: "",
configRoot: "",
owner: "MediaMath",
repo: "part",
ref: "eb78552e86dfead7f6506e6d35ae5db9fc078403",
extraEnv: []string{},
timeout: testBuildtimeout,
}
ws, err := builder.PrepareWorkspace(basename)
if err != nil {
t.Errorf("|%v|", err)
t.FailNow()
}
files, err := ioutil.ReadDir(filepath.Join(ws, clonePath))
if err != nil {
t.Errorf("|%v|", err)
}
if len(files) != 16 {
t.Errorf("Directory %s had %v files.", ws, len(files))
}
if !t.Failed() {
//remove directory only on success so you can diagnose failure
os.RemoveAll(f)
}
}
type testBuilder struct {
workspaceErr error
workspaceResult string
buildScriptErr error
buildScriptPath string
buildErr error
buildResult *executeResult
}
func (tb *testBuilder) PrepareWorkspace(basename string) (string, error) {
return tb.workspaceResult, tb.workspaceErr
}
func (tb *testBuilder) FindBuildScript(workspacePath string) (string, error) {
return tb.buildScriptPath, tb.buildScriptErr
}
func (tb *testBuilder) RunBuildScript(workspacePath, buildScript string, outputChan chan string) (*executeResult, error) {
return tb.buildResult, tb.buildErr
}
func TestOnBuildStatusFileError(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "build-status-file-error")
defer os.RemoveAll(resultPath)
tb := &testBuilder{workspaceErr: errors.New(""), workspaceResult: "@$@$"}
grimBuild(tb, resultPath, "")
_, err := ioutil.ReadFile(resultPath + "/build.txt")
if err != nil {
t.Errorf(fmt.Sprintf("Error in building status file: %v", err))
}
}
func TestOnPrepareWorkspaceFailure(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "prepare-workspace-failure")
defer os.RemoveAll(resultPath)
tb := &testBuilder{workspaceErr: errors.New(""), workspaceResult: "@$@$"}
grimBuild(tb, resultPath, "")
buildFile, _ := ioutil.ReadFile(resultPath + "/build.txt")
if !strings.Contains(string(buildFile), "failed to prepare workspace @$@$") {
t.Errorf("Failed to log error in preparing workspace")
}
}
func TestOnBuildScriptFailure(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "builds-script-failure")
defer os.RemoveAll(resultPath)
tb := &testBuilder{buildScriptErr: errors.New("&^&^")}
grimBuild(tb, resultPath, "")
buildFile, _ := ioutil.ReadFile(resultPath + "/build.txt")
buildText := string(buildFile)
if !strings.Contains(buildText, "workspace created") {
t.Errorf("Failed to log sucessful workspace creation")
}
if !strings.Contains(buildText, "&^&^") {
t.Errorf("Failed to log error in FindBuildScript")
}
}
func TestOnRunBuildScriptError(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "builds-script-error")
defer os.RemoveAll(resultPath)
tb := &testBuilder{buildScriptPath: "!@#", buildErr: errors.New("^%$")} //buildResult: &executeResult{ExitCode: 0}}
grimBuild(tb, resultPath, "")
buildFile, _ := ioutil.ReadFile(resultPath + "/build.txt")
buildText := string(buildFile)
if !strings.Contains(buildText, "workspace created") {
t.Errorf("Failed to log successful workspace creation")
}
if !strings.Contains(buildText, "build script found !@#") {
t.Errorf("Failed to log successful build start")
}
if !strings.Contains(buildText, "build started ...") {
t.Errorf("Failed to log build start")
}
if !strings.Contains(buildText, "build error ^%$") {
t.Errorf("Failed to log build error")
}
}
func TestOnRunBuildScriptSuccess(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "build-script-success")
defer os.RemoveAll(resultPath)
tb := &testBuilder{buildScriptPath: "!@#", buildResult: &executeResult{ExitCode: 0}}
grimBuild(tb, resultPath, "")
buildFile, _ := ioutil.ReadFile(resultPath + "/build.txt")
buildText := string(buildFile)
if !strings.Contains(buildText, "workspace created") {
t.Errorf("Failed to log successful workspace creation")
}
if !strings.Contains(buildText, "build script found !@#") {
t.Errorf("Failed to log successful build start")
}
if !strings.Contains(buildText, "build started ...") {
t.Errorf("Failed to log build start")
}
if !strings.Contains(buildText, "build success") {
t.Errorf("Failed to log build success")
}
}
func TestOnRunBuildScriptFailure(t *testing.T) {
resultPath, _ := ioutil.TempDir("", "build-script-error")
defer os.RemoveAll(resultPath)
tb := &testBuilder{buildScriptPath: "!@#", buildResult: &executeResult{ExitCode: 123123123}}
grimBuild(tb, resultPath, "")
buildFile, _ := ioutil.ReadFile(resultPath + "/build.txt")
buildText := string(buildFile)
if !strings.Contains(buildText, "workspace created") {
t.Errorf("Failed to log successful workspace creation")
}
if !strings.Contains(buildText, "build script found !@#") {
t.Errorf("Failed to log successful build start")
}
if !strings.Contains(buildText, "build started ...") {
t.Errorf("Failed to log build start")
}
if !strings.Contains(buildText, "build failed") || !strings.Contains(buildText, "123123123") {
t.Errorf("Failed to log build failure")
}
}