-
Notifications
You must be signed in to change notification settings - Fork 0
/
configured_oper_test.go
256 lines (232 loc) · 7.63 KB
/
configured_oper_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/baalimago/go_away_boilerplate/pkg/testboil"
"github.com/baalimago/repeater/internal/output"
)
func checkReportFileContent(reportFile string) (string, error) {
testFileRead, err := os.Open(reportFile)
if err != nil {
return "", err
}
b, err := io.ReadAll(testFileRead)
if err != nil {
return "", err
}
return string(b), nil
}
func Test_configuredOper(t *testing.T) {
t.Run("it should print to file when flagged to do so", func(t *testing.T) {
runTest := func(outputMode output.Mode, wantOutputString bool) {
testFile := testboil.CreateTestFile(t, "tFile")
outputString := "test"
co := configuredOper{
am: 1,
args: []string{"printf", fmt.Sprintf("%v", outputString)},
progress: output.HIDDEN,
output: outputMode,
outputFile: testFile,
amIdleWorkers: 1,
workPlanMu: &sync.Mutex{},
workerWg: &sync.WaitGroup{},
}
co.workerWg.Add(1)
co.run(context.Background())
testFileName := testFile.Name()
testFile.Close()
got, err := checkReportFileContent(testFileName)
if err != nil {
t.Fatalf("failed to get report file: %v", err)
}
if got != outputString && wantOutputString {
t.Fatalf("for: %v, expected: %v, got: %v", outputMode, outputString, got)
} else if got == outputString && !wantOutputString {
t.Fatalf("for: %v, expected empty string, got: %v", outputMode, got)
}
}
runTest(output.STDOUT, false)
runTest(output.HIDDEN, false)
runTest(output.BOTH, true)
runTest(output.FILE, true)
})
t.Run("it should print progress to report file when flagged to do so", func(t *testing.T) {
runTest := func(outputMode output.Mode, wantProgress bool) {
testFile := testboil.CreateTestFile(t, "tFile")
outputString := "something"
progFormat := "%v/%v/%v"
co := configuredOper{
am: 1,
args: []string{"printf", fmt.Sprintf("%v", outputString)},
progressFormat: progFormat,
progress: outputMode,
amIdleWorkers: 1,
output: output.HIDDEN,
outputFile: testFile,
workPlanMu: &sync.Mutex{},
workerWg: &sync.WaitGroup{},
}
co.workerWg.Add(1)
co.run(context.Background())
testFileName := testFile.Name()
testFile.Close()
got, err := checkReportFileContent(testFileName)
if err != nil {
t.Fatalf("failed to get report file: %v", err)
}
wantStr := fmt.Sprintf("%v", fmt.Sprintf(progFormat, 1, 0, 1))
if got != wantStr && wantProgress {
t.Fatalf("for: %s, expected: %v, got: %v", outputMode, wantStr, got)
} else if got == wantStr && !wantProgress {
t.Fatalf("for: %s, expected empty string, got: %v", outputMode, got)
}
}
runTest(output.STDOUT, false)
runTest(output.HIDDEN, false)
runTest(output.BOTH, true)
runTest(output.FILE, true)
})
t.Run("it should follow format set in progressFormat", func(t *testing.T) {
wantFormat := "lol%vtest%vhere%v"
testFile := testboil.CreateTestFile(t, "testFile")
c := configuredOper{
am: 1,
args: []string{"true"},
amIdleWorkers: 1,
progress: output.FILE,
progressFormat: wantFormat,
output: output.HIDDEN,
outputFile: testFile,
workPlanMu: &sync.Mutex{},
workerWg: &sync.WaitGroup{},
}
c.workerWg.Add(1)
c.run(context.Background())
testFileName := testFile.Name()
testFile.Close()
got, err := checkReportFileContent(testFileName)
if err != nil {
t.Fatalf("failed to get report file: %v", err)
}
want := fmt.Sprintf("%v", fmt.Sprintf(wantFormat, 1, 0, 1))
if got != want {
t.Fatalf("expected: %v, got: %v", want, got)
}
})
}
func Test_results(t *testing.T) {
t.Run("it should report output into results", func(t *testing.T) {
// This should ouput "test"
want := "test"
c := configuredOper{
am: 1,
args: []string{"printf", want},
workPlanMu: &sync.Mutex{},
amIdleWorkers: 1,
workerWg: &sync.WaitGroup{},
}
c.workerWg.Add(1)
c.run(context.Background())
gotLen := len(c.results)
if gotLen != 1 {
t.Fatalf("expected: 1, got: %v", gotLen)
}
got := c.results[0].Output
if got != want {
t.Fatalf("expected: %v, got: %v", want, got)
}
})
t.Run("it should report all output into results", func(t *testing.T) {
wantAm := 10
c := configuredOper{
am: wantAm,
// Date is most likely to exist in most OS's running this test
args: []string{"date"},
workerWg: &sync.WaitGroup{},
workPlanMu: &sync.Mutex{},
amIdleWorkers: 1,
}
c.workerWg.Add(1)
c.run(context.Background())
time.Sleep(time.Millisecond)
gotLen := len(c.results)
// ensure that the correc amount is output
if gotLen != wantAm {
t.Fatalf("expected: %v, got: %v", wantAm, gotLen)
}
uniqueSet := make(map[string]struct{})
for _, k := range c.results {
_, exists := uniqueSet[k.Output]
// Ensure that the output isn't copied for each one
if exists {
t.Fatalf("expected output to be different, this has shown twice: %v", exists)
}
}
})
}
func Test_configuredOper_New(t *testing.T) {
t.Run("it should return incrementConfigError if increment is true and no args contains 'INC'", func(t *testing.T) {
args := []string{"test", "abc"}
_, gotErr := New(0, 0, args, output.HIDDEN, "testing", output.HIDDEN, "", "", true, "", false)
if gotErr == nil {
t.Fatal("expected to get error, got nil")
}
var got incrementConfigError
if !errors.As(gotErr, &got) {
t.Fatalf("expected to get incrementConfigError, got: %v", gotErr)
}
for _, want := range args {
if !strings.Contains(got.Error(), want) {
t.Fatalf("error: %v, does not contain: %v", got, want)
}
}
})
t.Run("it should not return an error if increment is true and one argument is 'INC'", func(t *testing.T) {
args := []string{"test", "abc", "INC"}
_, gotErr := New(0, 0, args, output.HIDDEN, "testing", output.HIDDEN, "", "", true, "", false)
if gotErr != nil {
t.Fatalf("expected nil, got: %v", gotErr)
}
})
t.Run("it should not return an error if increment is true and one argument contains 'INC'", func(t *testing.T) {
args := []string{"test", "abc", "another-argument/INC"}
_, gotErr := New(0, 0, args, output.HIDDEN, "testing", output.HIDDEN, "", "", true, "", false)
if gotErr != nil {
t.Fatalf("expected nil, got: %v", gotErr)
}
})
t.Run("it should return incrementConfigError if the number of workers is greater than the number of times to repeat the command", func(t *testing.T) {
am := 1
workers := 2
args := []string{"test", "abc"}
_, gotErr := New(am, workers, args, output.HIDDEN, "testing", output.HIDDEN, "", "", false, "", false)
if gotErr == nil {
t.Fatal("expected to get error, got nil")
}
want := fmt.Errorf("please use less workers than repetitions. Am workers: %v, am repetitions: %v", workers, am)
if gotErr.Error() != want.Error() {
t.Fatalf("got: %v, want: %v", gotErr, want)
}
})
t.Run("it should not return an error if the number of workers is lower than the number of times to repeat the command", func(t *testing.T) {
args := []string{"test", "abc"}
_, gotErr := New(2, 1, args, output.HIDDEN, "testing", output.HIDDEN, "", "", false, "", false)
if gotErr != nil {
t.Fatalf("expected nil, got: %v", gotErr)
}
})
t.Run("it should not return an error if the number of workers is equal to the number of times to repeat the command", func(t *testing.T) {
args := []string{"test", "abc"}
_, gotErr := New(2, 2, args, output.HIDDEN, "testing", output.HIDDEN, "", "", false, "", false)
if gotErr != nil {
t.Fatalf("expected nil, got: %v", gotErr)
}
})
}