This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathnode_test.go
More file actions
188 lines (164 loc) · 6.69 KB
/
node_test.go
File metadata and controls
188 lines (164 loc) · 6.69 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
// Copyright 2014 The fleet Authors
//
// 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 functional
import (
"fmt"
"strings"
"testing"
"github.com/coreos/fleet/functional/platform"
"github.com/coreos/fleet/functional/util"
)
// Simulate the shutdown of a single fleet node
func TestNodeShutdown(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy(t)
// Start with a single node and wait for it to come up
m0, err := cluster.CreateMember()
if err != nil {
t.Fatal(err)
}
machines, err := cluster.WaitForNMachines(m0, 1)
if err != nil {
t.Fatal(err)
}
// Start a unit and ensure it comes up quickly
unit := fmt.Sprintf("fixtures/units/pin@%s.service", machines[0])
stdout, stderr, err := cluster.Fleetctl(m0, "start", unit)
if err != nil {
t.Errorf("Failed starting unit: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
_, err = cluster.WaitForNActiveUnits(m0, 1)
if err != nil {
t.Fatal(err)
}
// Create a second node, waiting for it
m1, err := cluster.CreateMember()
if err != nil {
t.Fatal(err)
}
if _, err = cluster.WaitForNMachines(m0, 2); err != nil {
t.Fatal(err)
}
// Stop the fleet process on the first member
if stdout, stderr, err = cluster.MemberCommand(m0, "sudo", "systemctl", "stop", "fleet"); err != nil {
t.Fatalf("Failed stopping fleet service:\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
// The first member should quickly remove itself from the published
// list of cluster members
if _, err = cluster.WaitForNMachines(m1, 1); err != nil {
t.Fatal(err)
}
// State for the member's unit should be purged from the Registry
if _, err = cluster.WaitForNActiveUnits(m1, 0); err != nil {
t.Fatal(err)
}
// The member's unit should actually stop running, too.
// NOTE: In case of no units, systemd v230 or older prints out
// "Active: inactive" to stdout, while systemd v231 or newer prints out
// "Unit NAME could not be found" to stderr. So we need to check for
// both cases.
stdout, stderr, err = cluster.MemberCommand(m0, "systemctl", "status", "hello.service")
if !strings.Contains(stdout, "Active: inactive") && !strings.Contains(stderr, "could not be found") {
t.Fatalf("Unit hello.service not reported as inactive:\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
}
// TestDetectMachineId checks for etcd registration failing on a duplicated
// machine-id on different machines.
// First it creates a cluster with 2 members, m0 and m1. Then make their
// machine IDs the same as each other, by explicitly setting the m1's ID to
// the same as m0's. Test succeeds when an error returns, while test fails
// when nothing happens.
func TestDetectMachineId(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy(t)
members, err := platform.CreateNClusterMembers(cluster, 2)
if err != nil {
t.Fatal(err)
}
m0 := members[0]
m1 := members[1]
_, err = cluster.WaitForNMachines(m0, 2)
if err != nil {
t.Fatal(err)
}
machineIdFile := "/etc/machine-id"
// Restart fleet service, and check if its systemd status is still active.
restartFleetService := func(m platform.Member) error {
stdout, stderr, err := cluster.MemberCommand(m, "sudo", "systemctl", "restart", "fleet.service")
if err != nil {
return fmt.Errorf("failed to restart fleet service\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
stdout, stderr, err = cluster.MemberCommand(m, "systemctl", "show", "--property=ActiveState", "fleet")
if strings.TrimSpace(stdout) != "ActiveState=active" {
return fmt.Errorf("fleet unit not reported as active:\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
stdout, stderr, err = cluster.MemberCommand(m, "systemctl", "show", "--property=Result", "fleet")
if strings.TrimSpace(stdout) != "Result=success" {
return fmt.Errorf("result for fleet unit not reported as success:\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
return nil
}
stdout, stderr, err := cluster.MemberCommand(m0, "cat", machineIdFile)
if err != nil {
t.Fatalf("Failed to get machine-id\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
m0_machine_id := strings.TrimSpace(stdout)
// If the two machine IDs are different with each other,
// set the m1's ID to the same one as m0, to intentionally
// trigger an error case of duplication of machine ID.
stdout, stderr, err = cluster.MemberCommand(m1,
"echo", m0_machine_id, "|", "sudo", "tee", machineIdFile)
if err != nil {
t.Fatalf("Failed to replace machine-id\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
if err := restartFleetService(m1); err != nil {
t.Fatal(err)
}
// fleetd should actually be running, but failing to list machines.
// So we should expect a specific error after running fleetctl list-machines,
// like "googlapi: Error 503: fleet server unable to communicate with etcd".
stdout, stderr, err = cluster.Fleetctl(m1, "list-machines", "--no-legend")
if err != nil {
if !strings.Contains(err.Error(), "exit status 1") ||
!strings.Contains(stderr, "fleet server unable to communicate with etcd") {
t.Fatalf("m1: Failed to get list of machines. err: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
}
// If both conditions are satisfied, "exit status 1" and
// "...unable to communicate...", then it's an expected error. PASS.
} else {
t.Fatalf("m1: should get an error, but got success.\nstdout: %s\nstderr: %s", stdout, stderr)
}
// Trigger another test case of m0's ID getting different from m1's.
// Then it's expected that m0 and m1 would be working properly with distinct
// machine IDs, after having restarted fleet.service both on m0 and m1.
stdout, stderr, err = cluster.MemberCommand(m0,
"echo", util.NewMachineID(), "|", "sudo", "tee", machineIdFile)
if err != nil {
t.Fatalf("m0: Failed to replace machine-id\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
// Restart fleet service on m0, and see that it's still working.
if err := restartFleetService(m0); err != nil {
t.Fatal(err)
}
stdout, stderr, err = cluster.Fleetctl(m0, "list-machines", "--no-legend")
if err != nil {
t.Fatalf("m0: error: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
}
}