-
Notifications
You must be signed in to change notification settings - Fork 86
/
step_write_worker_info_test.go
83 lines (63 loc) · 2.04 KB
/
step_write_worker_info_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
package worker
import (
"bytes"
"testing"
"time"
gocontext "context"
"github.com/mitchellh/multistep"
"github.com/stretchr/testify/assert"
"github.com/travis-ci/worker/backend"
"github.com/travis-ci/worker/config"
)
type byteBufferLogWriter struct {
*bytes.Buffer
}
func (w *byteBufferLogWriter) Close() error {
return nil
}
func (w *byteBufferLogWriter) WriteAndClose(p []byte) (int, error) {
return w.Write(p)
}
func (w *byteBufferLogWriter) Timeout() <-chan time.Time {
return make(<-chan time.Time)
}
func (w *byteBufferLogWriter) SetMaxLogLength(m int) {
}
func (w *byteBufferLogWriter) SetJobStarted(meta *JobStartedMeta) {
}
func (w *byteBufferLogWriter) SetCancelFunc(_ gocontext.CancelFunc) {
}
func (w *byteBufferLogWriter) MaxLengthReached() bool {
return false
}
func setupStepWriteWorkerInfo() (*stepWriteWorkerInfo, *byteBufferLogWriter, multistep.StateBag) {
s := &stepWriteWorkerInfo{}
bp, _ := backend.NewBackendProvider("fake",
config.ProviderConfigFromMap(map[string]string{
"STARTUP_DURATION": "42.17s",
}))
ctx := gocontext.TODO()
instance, _ := bp.Start(ctx, nil)
logWriter := &byteBufferLogWriter{
bytes.NewBufferString(""),
}
state := &multistep.BasicStateBag{}
state.Put("ctx", ctx)
state.Put("logWriter", logWriter)
state.Put("instance", instance)
state.Put("hostname", "frizzlefry.example.local")
state.Put("buildJob", &fakeJob{payload: &JobPayload{Job: JobJobPayload{ID: 4}}})
return s, logWriter, state
}
func TestStepWriteWorkerInfo_Run(t *testing.T) {
s, logWriter, state := setupStepWriteWorkerInfo()
s.Run(state)
out := logWriter.String()
assert.Contains(t, out, "travis_fold:start:worker_info\r\033[0K")
assert.Contains(t, out, "\033[33;1mWorker information\033[0m\n")
assert.Contains(t, out, "\nhostname: frizzlefry.example.local\n")
assert.Contains(t, out, "\nversion: "+VersionString+" "+RevisionURLString+"\n")
assert.Contains(t, out, "\ninstance: fake fake (via fake)\n")
assert.Contains(t, out, "\nstartup: 42.17s\n")
assert.Contains(t, out, "\ntravis_fold:end:worker_info\r\033[0K")
}