-
Notifications
You must be signed in to change notification settings - Fork 25
/
aliases_test.go
179 lines (147 loc) · 5.07 KB
/
aliases_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
package rig_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/k0sproject/rig/v2"
"github.com/k0sproject/rig/v2/initsystem"
"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/k0sproject/rig/v2/sudo"
"github.com/stretchr/testify/require"
)
func TestGetRemoteFS(t *testing.T) {
mr := rigtest.NewMockRunner()
t.Run("posix", func(t *testing.T) {
fs, err := rig.GetRemoteFS(mr)
// the current implementation never returns an error, the result
// will be either posixfs or winfs.
require.NoError(t, err)
require.IsType(t, &remotefs.PosixFS{}, fs)
})
t.Run("windows", func(t *testing.T) {
mr.Windows = true
fs, err := rig.GetRemoteFS(mr)
// the current implementation never returns an error, the result
// will be either posixfs or winfs.
require.NoError(t, err)
require.IsType(t, &remotefs.WinFS{}, fs)
})
}
func TestGetServiceManager(t *testing.T) {
t.Run("systemd", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.Equal("stat /run/systemd/system"), func(a *rigtest.A) error { return nil })
sm, err := rig.GetServiceManager(mr)
require.NoError(t, err)
require.IsType(t, initsystem.Systemd{}, sm)
})
t.Run("upstart", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.HasPrefix("command -v initctl"), func(a *rigtest.A) error { return nil })
sm, err := rig.GetServiceManager(mr)
require.NoError(t, err)
require.IsType(t, initsystem.Upstart{}, sm)
})
t.Run("error", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
_, err := rig.GetServiceManager(mr)
require.ErrorIs(t, err, initsystem.ErrNoInitSystem)
})
}
func TestGetPackageManager(t *testing.T) {
t.Run("apk", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.Equal("command -v apk"), func(a *rigtest.A) error { return nil })
pm, err := rig.GetPackageManager(mr)
require.NoError(t, err)
require.NotNil(t, pm)
_ = pm.Install(context.Background(), "package")
rigtest.ReceivedContains(t, mr, "apk add package")
})
t.Run("yum", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.Equal("command -v yum"), func(a *rigtest.A) error { return nil })
pm, err := rig.GetPackageManager(mr)
require.NoError(t, err)
require.NotNil(t, pm)
_ = pm.Install(context.Background(), "package")
rigtest.ReceivedContains(t, mr, "yum install -y package")
})
t.Run("error", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
_, err := rig.GetPackageManager(mr)
require.ErrorIs(t, err, packagemanager.ErrNoPackageManager)
})
}
func TestGetSudoRunner(t *testing.T) {
t.Run("sudo", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.Match("sudo.*true"), func(a *rigtest.A) error { return nil })
sudoRunner, err := rig.GetSudoRunner(mr)
require.NoError(t, err)
require.NotNil(t, sudoRunner)
_ = sudoRunner.Exec("hello")
rigtest.ReceivedMatch(t, mr, "^sudo.*hello$")
})
t.Run("doas", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommand(rigtest.Match("doas.*true"), func(a *rigtest.A) error { return nil })
sudoRunner, err := rig.GetSudoRunner(mr)
require.NoError(t, err)
require.NotNil(t, sudoRunner)
_ = sudoRunner.Exec("hello")
rigtest.ReceivedMatch(t, mr, `^doas.*hello$`)
})
t.Run("error", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
_, err := rig.GetSudoRunner(mr)
require.ErrorIs(t, err, sudo.ErrNoSudo)
})
}
func TestGetOSRelease(t *testing.T) {
t.Run("linux", func(t *testing.T) {
builder := strings.Builder{}
// this is easier than trying to keep format when gofmt will mess it up
fmt.Fprintln(&builder, `PRETTY_NAME="Foo 1.2.3"`)
fmt.Fprintln(&builder, `NAME="Foo"`)
fmt.Fprintln(&builder, `ID="foo"`)
fmt.Fprintln(&builder, `VERSION_ID="1.0"`)
fmt.Fprintln(&builder, `FOO="BAR"`)
osRelease := builder.String()
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
mr.AddCommandSuccess(rigtest.Match("uname.*Linux"))
mr.AddCommand(rigtest.HasPrefix("cat /etc/os-release"), func(a *rigtest.A) error {
t.Log("executing", a.Command)
_, _ = fmt.Fprint(a.Stdout, osRelease)
return nil
})
os, err := rig.GetOSRelease(mr)
require.NoError(t, err)
require.Equal(t, "Foo", os.Name)
require.Equal(t, "1.0", os.Version)
require.Equal(t, "foo", os.ID)
require.Equal(t, "BAR", os.ExtraFields["FOO"])
require.Equal(t, "Foo 1.2.3", os.ExtraFields["PRETTY_NAME"])
})
t.Run("error", func(t *testing.T) {
mr := rigtest.NewMockRunner()
mr.ErrDefault = errors.New("mock error")
_, err := rig.GetOSRelease(mr)
require.ErrorIs(t, err, os.ErrNotRecognized)
})
}