@@ -3,6 +3,7 @@ package pipeline
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"os"
7
8
"os/exec"
8
9
"path/filepath"
@@ -36,7 +37,8 @@ func fakeExecCommandContext(ctx context.Context, name string, args ...string) *e
36
37
envArgs = arg
37
38
}
38
39
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 }
40
42
return cmd
41
43
}
42
44
@@ -45,7 +47,8 @@ func TestExecCommandContextHelper(t *testing.T) {
45
47
return
46
48
}
47
49
fmt .Fprintln (os .Stdout , mockedOutput )
48
- os .Exit (mockedStatus )
50
+ i , _ := strconv .Atoi (os .Getenv ("EXIT_STATUS" ))
51
+ os .Exit (i )
49
52
}
50
53
51
54
func TestPrepareEnvironment (t * testing.T ) {
@@ -87,26 +90,15 @@ func TestExecuteBuild(t *testing.T) {
87
90
if err != nil {
88
91
t .Fatal ("error while running executebuild. none was expected" )
89
92
}
90
- expectedOut := ""
91
- actualOut := os .Getenv ("STDOUT" )
92
- expectedStatus := 0
93
- actualStatus , _ := strconv .Atoi (os .Getenv ("EXIT_STATUS" ))
94
93
expectedArgs := "-test.run=TestExecCommandContextHelper,--,/usr/local/bin/go,get,-d,./...:-test.run=TestExecCommandContextHelper,--,/usr/local/bin/go,build,-o,_"
95
94
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
- }
102
95
if expectedArgs != actualArgs {
103
96
t .Fatalf ("expected args '%s' actual args '%s'" , expectedArgs , actualArgs )
104
97
}
105
98
}
106
99
107
100
func TestExecuteBuildContextTimeout (t * testing.T ) {
108
101
execCommandContext = fakeExecCommandContext
109
- mockedOutput = "mocked output\n "
110
102
killContext = true
111
103
defer func () { execCommandContext = exec .CommandContext }()
112
104
defer func () { killContext = false }()
@@ -145,11 +137,45 @@ func TestCopyBinary(t *testing.T) {
145
137
p .Pipeline .Name = "main"
146
138
p .Pipeline .Type = "go"
147
139
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 )
149
143
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 )
151
146
err := b .CopyBinary (p )
152
147
if err != nil {
153
148
t .Fatal ("error was not expected when copying binary: " , err )
154
149
}
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
+ }
155
181
}
0 commit comments