forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
259 lines (196 loc) · 6.34 KB
/
node_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
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
// Copyright 2022 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package database
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"path"
"strconv"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/agent"
"github.com/juju/juju/controller"
"github.com/juju/juju/database/app"
jujutesting "github.com/juju/juju/testing"
)
type nodeManagerSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&nodeManagerSuite{})
func (s *nodeManagerSuite) TestEnsureDataDirSuccess(c *gc.C) {
subDir := strconv.Itoa(rand.Intn(10))
cfg := fakeAgentConfig{dataDir: "/tmp/" + subDir}
m := NewNodeManager(cfg, stubLogger{})
expected := fmt.Sprintf("/tmp/%s/%s", subDir, dqliteDataDir)
s.AddCleanup(func(*gc.C) { _ = os.RemoveAll(cfg.DataDir()) })
// Call twice to check both the creation and extant scenarios.
dir, err := m.EnsureDataDir()
c.Assert(err, jc.ErrorIsNil)
c.Check(dir, gc.Equals, expected)
_, err = os.Stat(expected)
c.Assert(err, jc.ErrorIsNil)
dir, err = m.EnsureDataDir()
c.Assert(err, jc.ErrorIsNil)
c.Check(dir, gc.Equals, expected)
_, err = os.Stat(expected)
c.Assert(err, jc.ErrorIsNil)
}
func (s *nodeManagerSuite) TestIsExistingNode(c *gc.C) {
subDir := strconv.Itoa(rand.Intn(10))
cfg := fakeAgentConfig{dataDir: "/tmp/" + subDir}
s.AddCleanup(func(*gc.C) { _ = os.RemoveAll(cfg.DataDir()) })
m := NewNodeManager(cfg, stubLogger{})
// Empty directory indicates we've never started.
extant, err := m.IsExistingNode()
c.Assert(err, jc.ErrorIsNil)
c.Check(extant, jc.IsFalse)
// Non-empty indicates we've come up before.
dataDir, err := m.EnsureDataDir()
c.Assert(err, jc.ErrorIsNil)
someFile := path.Join(dataDir, "a-file.txt")
err = os.WriteFile(someFile, nil, 06000)
c.Assert(err, jc.ErrorIsNil)
extant, err = m.IsExistingNode()
c.Assert(err, jc.ErrorIsNil)
c.Check(extant, jc.IsTrue)
}
func (s *nodeManagerSuite) TestIsBootstrappedNode(c *gc.C) {
subDir := strconv.Itoa(rand.Intn(10))
cfg := fakeAgentConfig{dataDir: "/tmp/" + subDir}
s.AddCleanup(func(*gc.C) { _ = os.RemoveAll(cfg.DataDir()) })
m := NewNodeManager(cfg, stubLogger{})
ctx := context.TODO()
// Empty directory indicates we are not the bootstrapped node.
asBootstrapped, err := m.IsBootstrappedNode(ctx)
c.Assert(err, jc.ErrorIsNil)
c.Check(asBootstrapped, jc.IsFalse)
dataDir, err := m.EnsureDataDir()
c.Assert(err, jc.ErrorIsNil)
clusterFile := path.Join(dataDir, "cluster.yaml")
// Multiple nodes indicates the cluster has mutated since bootstrap.
data := `
- Address: 10.246.27.114:17666
ID: 3297041220608546238
Role: 0
- Address: 10.246.27.115:17666
ID: 123456789
Role: 0
`[1:]
err = os.WriteFile(clusterFile, []byte(data), 0600)
c.Assert(err, jc.ErrorIsNil)
asBootstrapped, err = m.IsBootstrappedNode(ctx)
c.Assert(err, jc.ErrorIsNil)
c.Check(asBootstrapped, jc.IsFalse)
// Non-loopback address indicates node was reconfigured since bootstrap.
data = `
- Address: 10.246.27.114:17666
ID: 3297041220608546238
Role: 0
`[1:]
err = os.WriteFile(clusterFile, []byte(data), 0600)
c.Assert(err, jc.ErrorIsNil)
asBootstrapped, err = m.IsBootstrappedNode(ctx)
c.Assert(err, jc.ErrorIsNil)
c.Check(asBootstrapped, jc.IsFalse)
// Loopback IP address indicates the node is as we bootstrapped it.
data = `
- Address: 127.0.0.1:17666
ID: 3297041220608546238
Role: 0
`[1:]
err = os.WriteFile(clusterFile, []byte(data), 0600)
c.Assert(err, jc.ErrorIsNil)
asBootstrapped, err = m.IsBootstrappedNode(ctx)
c.Assert(err, jc.ErrorIsNil)
c.Check(asBootstrapped, jc.IsTrue)
}
func (s *nodeManagerSuite) TestWithAddressOptionSuccess(c *gc.C) {
m := NewNodeManager(nil, stubLogger{})
withAddress, err := m.WithAddressOption()
c.Assert(err, jc.ErrorIsNil)
dqlite, err := app.New(c.MkDir(), withAddress)
c.Assert(err, jc.ErrorIsNil)
_ = dqlite.Close()
}
func (s *nodeManagerSuite) TestWithTLSOptionSuccess(c *gc.C) {
cfg := fakeAgentConfig{}
m := NewNodeManager(cfg, stubLogger{})
withTLS, err := m.WithTLSOption()
c.Assert(err, jc.ErrorIsNil)
dqlite, err := app.New(c.MkDir(), withTLS)
c.Assert(err, jc.ErrorIsNil)
_ = dqlite.Close()
}
func (s *nodeManagerSuite) TestWithClusterOptionSuccess(c *gc.C) {
// Hack to get a bind address to add to config.
h := NewNodeManager(fakeAgentConfig{}, stubLogger{})
err := h.ensureBindAddress()
c.Assert(err, jc.ErrorIsNil)
cfg := fakeAgentConfig{
apiAddrs: []string{
"10.0.0.5:17070",
h.bindAddress, // Filtered out as not being us.
"127.0.0.1:17070", // Filtered out as a non-local-cloud address.
},
}
m := NewNodeManager(cfg, stubLogger{})
withCluster, err := m.WithClusterOption()
c.Assert(err, jc.ErrorIsNil)
dqlite, err := app.New(c.MkDir(), withCluster)
c.Assert(err, jc.ErrorIsNil)
_ = dqlite.Close()
}
func (s *nodeManagerSuite) TestWithClusterNotHASuccess(c *gc.C) {
// Hack to get a bind address to add to config.
h := NewNodeManager(fakeAgentConfig{}, stubLogger{})
err := h.ensureBindAddress()
c.Assert(err, jc.ErrorIsNil)
cfg := fakeAgentConfig{apiAddrs: []string{h.bindAddress}}
m := NewNodeManager(cfg, stubLogger{})
withCluster, err := m.WithClusterOption()
c.Assert(err, jc.ErrorIsNil)
dqlite, err := app.New(c.MkDir(), withCluster)
c.Assert(err, jc.ErrorIsNil)
_ = dqlite.Close()
}
func (s *nodeManagerSuite) TestIgnoreInterface(c *gc.C) {
shouldIgnore := []string{
"lxdbr0",
"virbr0",
"docker0",
}
for _, devName := range shouldIgnore {
c.Check(ignoreInterface(net.Interface{Name: devName}), jc.IsTrue)
}
c.Check(ignoreInterface(net.Interface{Flags: net.FlagLoopback}), jc.IsTrue)
c.Check(ignoreInterface(net.Interface{Name: "enp5s0"}), jc.IsFalse)
}
type fakeAgentConfig struct {
agent.Config
dataDir string
apiAddrs []string
}
// DataDir implements agent.Config.
func (cfg fakeAgentConfig) DataDir() string {
return cfg.dataDir
}
// CACert implements agent.Config.
func (cfg fakeAgentConfig) CACert() string {
return jujutesting.CACert
}
// StateServingInfo implements agent.AgentConfig.
func (cfg fakeAgentConfig) StateServingInfo() (controller.StateServingInfo, bool) {
return controller.StateServingInfo{
CAPrivateKey: jujutesting.CAKey,
Cert: jujutesting.ServerCert,
PrivateKey: jujutesting.ServerKey,
}, true
}
// APIAddresses implements agent.Config.
func (cfg fakeAgentConfig) APIAddresses() ([]string, error) {
return cfg.apiAddrs, nil
}