forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
140 lines (109 loc) · 3.25 KB
/
client_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
package rig_test
import (
"context"
"testing"
"github.com/k0sproject/rig/v2"
"github.com/k0sproject/rig/v2/rigtest"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestClientWithConfigurer(t *testing.T) {
cc := &rig.CompositeConfig{
Localhost: true,
}
conn, err := rig.NewClient(
rig.WithConnectionConfigurer(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())
}
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.WithConnectionConfigurer(&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)
}