-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript_test.go
More file actions
153 lines (137 loc) · 3.57 KB
/
Copy pathscript_test.go
File metadata and controls
153 lines (137 loc) · 3.57 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package hive_test
import (
"bufio"
"bytes"
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/hivetest"
"github.com/cilium/hive/script"
"github.com/stretchr/testify/require"
)
func exampleCmd() hive.ScriptCmdOut {
return hive.NewScriptCmd(
"example1",
script.Command(
script.CmdUsage{
Summary: "Example command",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
s.Logf("hello1")
return nil, nil
},
),
)
}
func example2Cmd() hive.ScriptCmdsOut {
return hive.NewScriptCmds(
map[string]script.Cmd{
"example2": script.Command(
script.CmdUsage{
Summary: "Second example command",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
s.Logf("hello2")
return nil, nil
},
),
},
)
}
func TestScriptCommands(t *testing.T) {
h := hive.New(
cell.Provide(exampleCmd, example2Cmd),
)
cmds, err := h.ScriptCommands(hivetest.Logger(t))
require.NoError(t, err, "ScriptCommands")
e := script.Engine{
Cmds: cmds,
}
s, err := script.NewState(context.TODO(), "/tmp", nil)
require.NoError(t, err, "NewState")
script := `
hive/start
example1
example2
hive/stop
`
bio := bufio.NewReader(bytes.NewBufferString(script))
var stdout bytes.Buffer
err = e.Execute(s, "", bio, &stdout)
require.NoError(t, err, "Execute")
expected := `> hive/start.*> example1.*hello1.*> example2.*hello2.*> hive/stop`
require.Regexp(t, expected, strings.ReplaceAll(stdout.String(), "\n", " "))
}
func TestScriptCommandRetries(t *testing.T) {
var tests = []struct {
name string
succeedAfter uint
cancelAfter uint
maxRetries uint
assert require.ErrorAssertionFunc
}{
{
name: "max two retries, succeed after two",
succeedAfter: 2,
maxRetries: 2,
assert: require.NoError,
},
{
name: "max two retries, succeed after three",
succeedAfter: 3,
maxRetries: 2,
assert: func(tt require.TestingT, err error, args ...any) {
require.ErrorContains(t, err, "expected to succeed after 3 times, current: 2", args...)
},
},
{
name: "no limit, context cancellation only",
succeedAfter: 1000,
cancelAfter: 10,
assert: func(tt require.TestingT, err error, args ...any) {
require.ErrorContains(t, err, "expected to succeed after 1000 times, current: 10", args...)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
counter uint
engine = script.Engine{
Cmds: map[string]script.Cmd{
"test": script.Command(
script.CmdUsage{},
func(s *script.State, args ...string) (script.WaitFunc, error) {
defer func() { counter++ }()
s.Logf("test command called %d times", counter)
if tt.cancelAfter != 0 && counter == tt.cancelAfter {
cancel()
}
if counter != tt.succeedAfter {
return nil, fmt.Errorf("expected to succeed after %d times, current: %d", tt.succeedAfter, counter)
}
return nil, nil
},
),
},
RetryInterval: 10 * time.Millisecond,
MaxRetryInterval: 10 * time.Millisecond,
MaxRetries: tt.maxRetries,
}
)
s, err := script.NewState(ctx, t.TempDir(), nil)
require.NoError(t, err, "NewState")
var stdout bytes.Buffer
err = engine.Execute(s, "", bufio.NewReader(strings.NewReader("* test")), &stdout)
tt.assert(t, err)
})
}
}