Skip to content

Commit db818cd

Browse files
committed
Added more tests for copying
1 parent 40262c8 commit db818cd

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

Diff for: pipeline/build_golang_test.go

+41-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pipeline
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -36,7 +37,8 @@ func fakeExecCommandContext(ctx context.Context, name string, args ...string) *e
3637
envArgs = arg
3738
}
3839
os.Setenv("CMD_ARGS", envArgs)
39-
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
40+
es := strconv.Itoa(mockedStatus)
41+
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", "EXIT_STATUS=" + es}
4042
return cmd
4143
}
4244

@@ -45,7 +47,8 @@ func TestExecCommandContextHelper(t *testing.T) {
4547
return
4648
}
4749
fmt.Fprintln(os.Stdout, mockedOutput)
48-
os.Exit(mockedStatus)
50+
i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
51+
os.Exit(i)
4952
}
5053

5154
func TestPrepareEnvironment(t *testing.T) {
@@ -87,26 +90,15 @@ func TestExecuteBuild(t *testing.T) {
8790
if err != nil {
8891
t.Fatal("error while running executebuild. none was expected")
8992
}
90-
expectedOut := ""
91-
actualOut := os.Getenv("STDOUT")
92-
expectedStatus := 0
93-
actualStatus, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
9493
expectedArgs := "-test.run=TestExecCommandContextHelper,--,/usr/local/bin/go,get,-d,./...:-test.run=TestExecCommandContextHelper,--,/usr/local/bin/go,build,-o,_"
9594
actualArgs := os.Getenv("CMD_ARGS")
96-
if expectedOut != actualOut {
97-
t.Fatalf("expected out '%s' actual out '%s'", expectedOut, actualOut)
98-
}
99-
if expectedStatus != actualStatus {
100-
t.Fatalf("expected status '%d' actual status '%d'", expectedStatus, actualStatus)
101-
}
10295
if expectedArgs != actualArgs {
10396
t.Fatalf("expected args '%s' actual args '%s'", expectedArgs, actualArgs)
10497
}
10598
}
10699

107100
func TestExecuteBuildContextTimeout(t *testing.T) {
108101
execCommandContext = fakeExecCommandContext
109-
mockedOutput = "mocked output\n"
110102
killContext = true
111103
defer func() { execCommandContext = exec.CommandContext }()
112104
defer func() { killContext = false }()
@@ -145,11 +137,45 @@ func TestCopyBinary(t *testing.T) {
145137
p.Pipeline.Name = "main"
146138
p.Pipeline.Type = "go"
147139
p.Pipeline.Repo.LocalDest = tmp
148-
f, _ := os.Create(filepath.Join(tmp, appendTypeToName(p.Pipeline.Name, p.Pipeline.Type)))
140+
src := filepath.Join(tmp, appendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
141+
dst := appendTypeToName(p.Pipeline.Name, p.Pipeline.Type)
142+
f, _ := os.Create(src)
149143
defer f.Close()
150-
defer os.Remove(appendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
144+
defer os.Remove(dst)
145+
ioutil.WriteFile(src, []byte("testcontent"), 0666)
151146
err := b.CopyBinary(p)
152147
if err != nil {
153148
t.Fatal("error was not expected when copying binary: ", err)
154149
}
150+
content, err := ioutil.ReadFile(dst)
151+
if err != nil {
152+
t.Fatal("error encountered while reading destination file: ", err)
153+
}
154+
if string(content) != "testcontent" {
155+
t.Fatal("file content did not equal src content. was: ", string(content))
156+
}
157+
}
158+
159+
func TestCopyBinarySrcDoesNotExist(t *testing.T) {
160+
tmp := os.TempDir()
161+
gaia.Cfg = new(gaia.Config)
162+
gaia.Cfg.HomePath = tmp
163+
// Initialize shared logger
164+
gaia.Cfg.Logger = hclog.New(&hclog.LoggerOptions{
165+
Level: hclog.Trace,
166+
Output: hclog.DefaultOutput,
167+
Name: "Gaia",
168+
})
169+
b := new(BuildPipelineGolang)
170+
p := new(gaia.CreatePipeline)
171+
p.Pipeline.Name = "main"
172+
p.Pipeline.Type = "go"
173+
p.Pipeline.Repo.LocalDest = "/noneexistent"
174+
err := b.CopyBinary(p)
175+
if err == nil {
176+
t.Fatal("error was expected when copying binary but none occurred ")
177+
}
178+
if err.Error() != "open /noneexistent/main_go: no such file or directory" {
179+
t.Fatal("a different error occurred then expected: ", err)
180+
}
155181
}

Diff for: pipeline/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestGitCloneRepo(t *testing.T) {
1111
repo := &gaia.GitRepo{
12-
URL: "https://github.com/gaia-pipeline/gaia",
12+
URL: "https://github.com/gaia-pipeline/go-test-example",
1313
LocalDest: "tmp",
1414
}
1515
// always ensure that tmp folder is cleaned up

0 commit comments

Comments
 (0)