-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclient_test.go
More file actions
313 lines (251 loc) · 8.24 KB
/
client_test.go
File metadata and controls
313 lines (251 loc) · 8.24 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package rig_test
import (
"context"
"errors"
"testing"
"github.com/k0sproject/rig/v2"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/os"
"github.com/k0sproject/rig/v2/packagemanager"
"github.com/k0sproject/rig/v2/remotefs"
"github.com/k0sproject/rig/v2/rigtest"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestClientWithConnectionFactory(t *testing.T) {
cc := &rig.CompositeConfig{
Localhost: true,
}
conn, err := rig.NewClient(
rig.WithConnectionFactory(cc),
)
require.NoError(t, err)
require.NotNil(t, conn)
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClient(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
out, err := client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClientLogging(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
logger := &rigtest.MockLogger{}
client, err := rig.NewClient(rig.WithConnection(conn), rig.WithLogger(logger))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
_, _ = client.ExecOutput("echo hello")
t.Log(logger.Messages())
}
func TestClientFSErrorFallback(t *testing.T) {
conn := rigtest.NewMockConnection()
mockErr := errors.New("mock fs error")
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithRemoteFSProvider(func(_ cmd.Runner) (remotefs.FS, error) {
return nil, mockErr
}),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
fs := client.FS()
require.NotNil(t, fs)
_, err = fs.Open("test")
require.Error(t, err)
_, err = client.RemoteFSProvider.FS()
require.ErrorIs(t, err, mockErr)
}
func TestClientPackageManagerErrorFallback(t *testing.T) {
conn := rigtest.NewMockConnection()
mockErr := errors.New("mock pm error")
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithPackageManagerProvider(func(_ cmd.ContextRunner) (packagemanager.PackageManager, error) {
return nil, mockErr
}),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
pm := client.PackageManager()
require.NotNil(t, pm)
err = pm.Install(context.Background(), "test-package")
require.ErrorIs(t, err, mockErr)
}
func TestClientReconnect(t *testing.T) {
conn := rigtest.NewMockConnection()
conn.AddCommandOutput(rigtest.Match("echo hello"), "hello")
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
require.True(t, client.IsConnected())
out, err := client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
client.Disconnect()
require.False(t, client.IsConnected())
require.NoError(t, client.Connect(context.Background()))
require.True(t, client.IsConnected())
out, err = client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestClientProtocolName(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(rig.WithConnection(conn))
require.NoError(t, err)
require.Equal(t, "mock", client.Protocol())
require.Equal(t, "mock", client.ProtocolName())
}
type testConfig struct {
Hosts []*testHost `yaml:"hosts"`
}
type testHost struct {
ClientConfig rig.CompositeConfig `yaml:"-,inline"`
*rig.Client
}
func (th *testHost) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawTestHost testHost
h := (*rawTestHost)(th)
if err := unmarshal(h); err != nil {
return err
}
conn, err := rig.NewClient(rig.WithConnectionFactory(&h.ClientConfig))
if err != nil {
return err
}
h.Client = conn
return nil
}
func TestConnectionUnmarshal(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfig{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
require.NoError(t, conn.Connect(context.Background()))
require.Equal(t, "Local", conn.Protocol())
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
type testConfigConfigured struct {
Hosts []*testHostConfigured `yaml:"hosts"`
}
type testHostConfigured struct {
rig.ClientWithConfig `yaml:"-,inline"`
}
func TestConfiguredConnectionUnmarshal(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfigConfigured{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
require.NoError(t, conn.Connect(context.Background()))
require.Equal(t, "Local", conn.Protocol())
require.NoError(t, conn.Connect(context.Background()))
out, err := conn.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
func TestWithOSIDOverride(t *testing.T) {
detectedRelease := &os.Release{
ID: "detected-os",
Name: "Detected OS",
IDLike: []string{"family"},
}
releaseProvider := func(_ cmd.SimpleRunner) (*os.Release, error) {
return detectedRelease, nil
}
t.Run("override after provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSReleaseProvider(releaseProvider),
rig.WithOSIDOverride("override-id"),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
release, err := client.OS()
require.NoError(t, err)
require.Equal(t, "override-id", release.ID)
require.Equal(t, "Detected OS", release.Name)
require.Equal(t, []string{"family"}, release.IDLike)
})
t.Run("override before provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSIDOverride("override-id"),
rig.WithOSReleaseProvider(releaseProvider),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
release, err := client.OS()
require.NoError(t, err)
require.Equal(t, "override-id", release.ID)
require.Equal(t, "Detected OS", release.Name)
require.Equal(t, []string{"family"}, release.IDLike)
})
t.Run("nil release from provider", func(t *testing.T) {
conn := rigtest.NewMockConnection()
client, err := rig.NewClient(
rig.WithConnection(conn),
rig.WithOSReleaseProvider(func(_ cmd.SimpleRunner) (*os.Release, error) {
return nil, nil
}),
rig.WithOSIDOverride("override-id"),
)
require.NoError(t, err)
require.NoError(t, client.Connect(context.Background()))
_, err = client.OS()
require.Error(t, err)
})
}
// TestConfiguredConnectionConnectOptsApplied is a regression test verifying that
// options passed to Connect() after YAML unmarshal are actually applied.
// Previously, UnmarshalYAML called Setup() eagerly, causing a subsequent
// Connect(ctx, opts...) to skip Setup() and silently ignore those options.
func TestConfiguredConnectionConnectOptsApplied(t *testing.T) {
hostConfig := map[string]any{
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
}
yamlContent, err := yaml.Marshal(mainConfig)
require.NoError(t, err)
testConfig := &testConfigConfigured{}
require.NoError(t, yaml.Unmarshal(yamlContent, testConfig))
require.Len(t, testConfig.Hosts, 1)
conn := testConfig.Hosts[0]
mock := rigtest.NewMockRunner()
mock.AddCommand(rigtest.Equal("echo hello"), func(a *rigtest.A) error { return nil })
require.NoError(t, conn.Connect(context.Background(), rig.WithRunner(mock)))
_, err = conn.ExecOutput("echo hello")
require.NoError(t, err)
rigtest.ReceivedEqual(t, mock, "echo hello", "WithRunner option passed to Connect was not applied")
}