forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_test.go
106 lines (85 loc) · 2.21 KB
/
execute_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
package grim
// 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.
import (
"os/exec"
"testing"
"time"
)
func TestRunFalse(t *testing.T) {
withTempDir(t, func(path string) {
falsePath, err := exec.LookPath("false")
if err != nil {
t.Fatal(err)
}
result, err := execute(nil, "", falsePath, testBuildtimeout)
if err != nil {
t.Error(err)
}
if result.ExitCode != 1 {
t.Fatal("false should return 1 as its exit code")
}
})
}
func TestRunEcho(t *testing.T) {
t.Skipf("Skipping echo test as they fail sporadically.")
withTempDir(t, func(path string) {
echoPath, err := exec.LookPath("echo")
if err != nil {
t.Fatal(err)
}
result, err := execute(nil, "", echoPath, testBuildtimeout, "test")
if err != nil {
t.Error(err)
}
if result.ExitCode != 0 {
t.Error("echo should return 0 as its exit code")
}
if result.Output != "test\n" {
t.Errorf("only line of output was not 'test' as expected it was '%s'", result.Output)
}
})
}
func TestRunEchoWithChan(t *testing.T) {
t.Skipf("Skipping echo test as they fail sporadically.")
withTempDir(t, func(path string) {
echoPath, err := exec.LookPath("echo")
if err != nil {
t.Fatal(err)
}
outputChan := make(chan string)
result, err := executeWithOutputChan(outputChan, nil, "", echoPath, testBuildtimeout, "test")
if err != nil {
t.Error(err)
}
if result.ExitCode != 0 {
t.Error("false should return 1 as its exit code")
}
select {
case line, ok := <-outputChan:
if !ok {
t.Error("channel closed before output")
} else if line != "test" {
t.Error("only line of output was not 'test' as expected")
}
default:
t.Error("no output ready even though echo terminated")
}
})
}
func TestGetExitCode(t *testing.T) {
timeoutTime := time.Duration(1) * time.Second
cmd := exec.Command("grep", "go")
err := cmd.Start()
if err != nil {
t.Error("can not start the command.")
}
exCode, err := killProcessOnTimeout(cmd, timeoutTime)
if err != nil {
t.Error("process still running")
}
if exCode != 1 {
t.Error("process should return 1 as its exit code")
}
}