-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdiscovery.go
200 lines (167 loc) · 5.82 KB
/
discovery.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
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"encoding/json"
"fmt"
"github.com/coreos/pkg/capnslog"
"github.com/pborman/uuid"
"github.com/flatcar/mantle/kola/cluster"
"github.com/flatcar/mantle/kola/register"
"github.com/flatcar/mantle/platform/conf"
)
var plog = capnslog.NewPackageLogger("github.com/flatcar/mantle", "kola/tests/etcd")
func init() {
register.Register(®ister.Test{
Run: Discovery,
ClusterSize: 3,
Name: "cl.etcd-member.discovery",
UserData: conf.ContainerLinuxConfig(`etcd:
listen_client_urls: http://0.0.0.0:2379
advertise_client_urls: http://{PRIVATE_IPV4}:2379
listen_peer_urls: http://{PRIVATE_IPV4}:2380
initial_advertise_peer_urls: http://{PRIVATE_IPV4}:2380
discovery: $discovery`),
Distros: []string{"cl"},
// Should run on all cloud environments to test CLC IP addr templating
})
register.Register(®ister.Test{
Run: etcdMemberV2BackupRestore,
ClusterSize: 1,
Name: "cl.etcd-member.v2-backup-restore",
UserData: conf.ContainerLinuxConfig(`
etcd:
version: 3.5.0
listen_client_urls: http://0.0.0.0:4001,http://0.0.0.0:2379
advertise_client_urls: http://{PRIVATE_IPV4}:2379
listen_peer_urls: http://0.0.0.0:2380
initial_advertise_peer_urls: http://{PRIVATE_IPV4}:2380
discovery: $discovery
enable_v2: true`),
Distros: []string{"cl"},
// This test is normally not related to the cloud environment
Platforms: []string{"qemu", "qemu-unpriv"},
})
register.Register(®ister.Test{
Run: etcdmemberEtcdctlV3,
// Clustersize of 1 to avoid needing private ips everywhere for clustering;
// this lets it run on more platforms, and also faster
ClusterSize: 1,
Name: "cl.etcd-member.etcdctlv3",
UserData: conf.ContainerLinuxConfig(fmt.Sprintf(`
etcd:
name: kola-etcd-member-%s
listen_client_urls: http://0.0.0.0:2379
advertise_client_urls: http://127.0.0.1:2379
listen_peer_urls: http://0.0.0.0:2380
initial_advertise_peer_urls: http://127.0.0.1:2380
`, uuid.New())),
Distros: []string{"cl"},
// This test is normally not related to the cloud environment
Platforms: []string{"qemu", "qemu-unpriv"},
})
}
func Discovery(c cluster.TestCluster) {
var err error
if err = GetClusterHealth(c, c.Machines()[0], len(c.Machines())); err != nil {
c.Fatalf("discovery failed cluster-health check: %v", err)
}
}
// etcdMemberV2BackupRestore tests that the basic etcdctl v2 operations (get,
// put, rm) work. It verifies that a backup and restore, similar to the one
// documented in
// https://coreos.com/etcd/docs/latest/v2/admin_guide.html#disaster-recovery
// works.
// Note, this is a v2 backup/restore being performed against the current v3
// etcd
func etcdMemberV2BackupRestore(c cluster.TestCluster) {
m := c.Machines()[0]
if err := GetClusterHealth(c, c.Machines()[0], len(c.Machines())); err != nil {
c.Fatalf("failed cluster-health check: %v", err)
}
c.MustSSH(m, `
set -e
export ETCDCTL_API=2
prefix=$RANDOM
etcdctl set /$prefix/test magic
res="$(etcdctl get /$prefix/test)"
if [[ "$res" != "magic" ]]; then
echo "Expected magic, got $res"
exit 1
fi
backup_to="$(mktemp -d)"
sudo --preserve-env=ETCDCTL_API etcdctl backup --data-dir=/var/lib/etcd \
--backup-dir "${backup_to}"
etcdctl rm /$prefix/test
if etcdctl get /$prefix/test 2>&1; then
echo "Expected rm'd key to error on get, didn't"
exit 1
fi
sudo systemctl stop etcd-member
# Note: this means we're now a new cluster of size 1 because of how etcd2
# backup/restore works.
sudo rm -rf /var/lib/etcd
sudo mv "${backup_to}" /var/lib/etcd/
sudo chown -R etcd:etcd /var/lib/etcd
sudo mkdir -p /run/systemd/system/etcd-member.service.d/
sudo tee /run/systemd/system/etcd-member.service.d/10-force-new.conf <<EOF
[Service]
Environment=ETCD_FORCE_NEW_CLUSTER=true
EOF
sudo systemctl daemon-reload
sudo systemctl start etcd-member
res="$(etcdctl get /$prefix/test)"
if [[ "$res" != "magic" ]]; then
echo "Expected magic after backup-restore, got $res"
exit 1
fi
`)
}
// etcdmemberEtcdctlV3 tests the basic operation of the ETCDCTL_API=3 behavior
// of the etcdctl we ship.
func etcdmemberEtcdctlV3(c cluster.TestCluster) {
m := c.Machines()[0]
type etcdMemberOutput struct {
Members []struct {
ID uint64
Name string
PeerURLs []string
ClientURLs []string
}
}
memberJson := c.MustSSH(m, `ETCDCTL_API=3 etcdctl member list --write-out=json`)
members := etcdMemberOutput{}
if err := json.Unmarshal(memberJson, &members); err != nil {
c.Fatalf("could not unmarshal %s: %s", memberJson, err)
}
if len(members.Members) != len(c.Machines()) {
c.Fatalf("expected %v members; only got %v", len(c.Machines()), len(members.Members))
}
c.MustSSH(m, `
set -e
export ETCDCTL_API=3
if [[ "$(etcdctl put foo bar)" != "OK" ]]; then
echo "Put failed"
exit 1
fi
value="$(etcdctl get foo -w json | jq '.kvs[].value' -r | base64 -d)"
if [[ "${value}" != "bar" ]]; then
echo "Reading our put failed: expected bar, got ${value}"
exit 1
fi
backup_to="$(mktemp -d)"
sudo -E etcdctl snapshot save "${backup_to}/snapshot.db"
sudo -E etcdctl snapshot status "${backup_to}/snapshot.db"
`)
}