This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nocs_test.go
208 lines (157 loc) · 4.98 KB
/
nocs_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
207
208
package main_test
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"syscall"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("nOCS", func() {
var (
configFileDir string
nocsProcessWD string
cmd *exec.Cmd
stdout, stderr string
exitCode int
)
BeforeEach(func() {
dirtyPath, err := ioutil.TempDir("", "nOCSTest")
Expect(err).NotTo(HaveOccurred())
configFileDir, err = filepath.EvalSymlinks(dirtyPath)
Expect(err).NotTo(HaveOccurred())
nocsProcessWD, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
})
JustBeforeEach(func() {
cmd.Dir = nocsProcessWD
outBuffer, errBuffer := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
cmd.Stdout, cmd.Stderr = outBuffer, errBuffer
Expect(cmd.Start()).To(Succeed())
exitCode = getExitCode(cmd.Wait())
stdout, stderr = outBuffer.String(), errBuffer.String()
})
AfterEach(func() {
os.RemoveAll(configFileDir)
})
Context("Running a simple command", func() {
BeforeEach(func() {
commandArgs := []string{"echo", "Hello OCF!"}
configFilePath := createConfigFile("nocs-simple.json", configFileDir, commandArgs)
cmd = exec.Command(nocsBin, "exec", configFilePath)
})
It("produces the appropriate stdout", func() {
Expect(stdout).To(Equal("Hello OCF!\n"))
})
It("produces the appropriate stderr", func() {
Expect(stderr).To(BeEmpty())
})
It("returns the appropriate exit status code", func() {
Expect(exitCode).To(Equal(0))
})
})
Context("Running a command which produces stderr", func() {
BeforeEach(func() {
commandArgs := []string{"/bin/sh", "-c", "echo Hello OCF! 1>&2"}
configFilePath := createConfigFile("nocs-stderr.json", configFileDir, commandArgs)
cmd = exec.Command(nocsBin, "exec", configFilePath)
})
It("produces the appropriate stdout", func() {
Expect(stdout).To(BeEmpty())
})
It("produces the appropriate stderr", func() {
Expect(stderr).To(Equal("Hello OCF!\n"))
})
It("returns the appropriate exit status code", func() {
Expect(exitCode).To(Equal(0))
})
})
Context("Running a command which exits with non-zero status", func() {
BeforeEach(func() {
commandArgs := []string{"/usr/bin/false"}
configFilePath := createConfigFile("nocs-errcode.json", configFileDir, commandArgs)
cmd = exec.Command(nocsBin, "exec", configFilePath)
})
It("returns the appropriate exit status code", func() {
Expect(exitCode).To(Equal(1))
})
})
Context("Running without passing a configuration filepath", func() {
BeforeEach(func() {
commandArgs := []string{"echo", "Hello default OCF!"}
createConfigFile("config.json", configFileDir, commandArgs)
cmd = exec.Command(nocsBin, "exec")
nocsProcessWD = configFileDir
})
It("produces the appropriate stdout", func() {
Expect(stdout).To(Equal("Hello default OCF!\n"))
})
})
Context("Running when passing the empty configuration filepath", func() {
BeforeEach(func() {
commandArgs := []string{"echo", "Hello empty OCF!"}
createConfigFile("config.json", configFileDir, commandArgs)
cmd = exec.Command(nocsBin, "exec", "")
nocsProcessWD = configFileDir
})
It("produces the appropriate stdout", func() {
Expect(stdout).To(Equal("Hello empty OCF!\n"))
})
})
Context("Running with a missing configuration file", func() {
BeforeEach(func() {
cmd = exec.Command(nocsBin, "exec")
nocsProcessWD = configFileDir
})
It("produces the appropriate stderr", func() {
Expect(stderr).To(Equal(filepath.Base(nocsBin) + ": reading config file: open " + filepath.Join(configFileDir, "config.json: no such file or directory\n")))
})
It("returns the appropriate exit status code", func() {
Expect(exitCode).To(Equal(95))
})
})
Context("Running when passing a relative configuration filepath", func() {
BeforeEach(func() {
commandArgs := []string{"echo", "Hello relative OCF!"}
createConfigFile("relative.json", configFileDir, commandArgs)
subdirPath := filepath.Join(configFileDir, "some-subdir")
err := os.Mkdir(subdirPath, 0777)
Expect(err).NotTo(HaveOccurred())
cmd = exec.Command(nocsBin, "exec", "../relative.json")
nocsProcessWD = subdirPath
})
It("produces the appropriate stdout", func() {
Expect(stdout).To(Equal("Hello relative OCF!\n"))
})
})
})
func createConfigFile(configFileName, configFileDir string, args []string) string {
configFilePath := path.Join(configFileDir, configFileName)
err := ioutil.WriteFile(configFilePath, []byte(
`{"process": {
"user": {
"uid": 1,
"gid": 1
},
"args": `+argsToJSON(args)+`
}}`), 0777)
Expect(err).NotTo(HaveOccurred())
return configFilePath
}
func argsToJSON(args []string) string {
return `["` + strings.Join(args, `","`) + `"]`
}
func getExitCode(err error) int {
if err != nil {
exitErr, ok := err.(*exec.ExitError)
Expect(ok).To(BeTrue())
waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
Expect(ok).To(BeTrue())
return waitStatus.ExitStatus()
}
return 0
}