Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 2193e1d

Browse files
author
Dongsu Park
committed
functional: add a new test TestUnitDestroyFromRegistry
A new test TestUnitDestroyFromRegistry() checks for a submitted unit being actually deleted from the etcd registry. To compare the old unit body with the one registered in the etcd registry, we need to go through several steps for queries to etcd.
1 parent c804c37 commit 2193e1d

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

functional/unit_action_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
package functional
1616

1717
import (
18+
"encoding/json"
19+
"fmt"
20+
"path"
1821
"strings"
1922
"testing"
2023

2124
"github.com/coreos/fleet/functional/platform"
25+
"github.com/coreos/fleet/functional/util"
26+
"github.com/coreos/fleet/unit"
2227
)
2328

2429
// TestUnitRunnable is the simplest test possible, deplying a single-node
@@ -224,3 +229,149 @@ func TestUnitSSHActions(t *testing.T) {
224229
t.Errorf("Could not find expected string in journal output:\n%s", stdout)
225230
}
226231
}
232+
233+
// TestUnitDestroyFromRegistry() checks for a submitted unit being removed
234+
// from the etcd registry. It compares a local unit body with the unit in
235+
// the etcd registry, to verify the body is identical.
236+
func TestUnitDestroyFromRegistry(t *testing.T) {
237+
cluster, err := platform.NewNspawnCluster("smoke")
238+
if err != nil {
239+
t.Fatal(err)
240+
}
241+
defer cluster.Destroy()
242+
243+
m, err := cluster.CreateMember()
244+
if err != nil {
245+
t.Fatal(err)
246+
}
247+
_, err = cluster.WaitForNMachines(m, 1)
248+
if err != nil {
249+
t.Fatal(err)
250+
}
251+
252+
// submit a unit and assert it shows up
253+
if _, _, err := cluster.Fleetctl(m, "submit", "fixtures/units/hello.service"); err != nil {
254+
t.Fatalf("Unable to submit fleet unit: %v", err)
255+
}
256+
stdout, _, err := cluster.Fleetctl(m, "list-units", "--no-legend")
257+
if err != nil {
258+
t.Fatalf("Failed to run list-units: %v", err)
259+
}
260+
units := strings.Split(strings.TrimSpace(stdout), "\n")
261+
if len(units) != 1 {
262+
t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout)
263+
}
264+
265+
// cat the unit and compare it with the value in etcd registry
266+
unitBody, _, err := cluster.Fleetctl(m, "cat", "hello.service")
267+
if err != nil {
268+
t.Fatalf("Unable to retrieve the fleet unit: %v", err)
269+
}
270+
271+
var hashUnit string
272+
if hashUnit, err = retrieveJobObjectHash(cluster, "hello.service"); err != nil {
273+
t.Fatalf("Failed to retrieve hash of job object hello.service: %v", err)
274+
}
275+
276+
var regBody string
277+
if regBody, err = retrieveUnitBody(cluster, hashUnit); err != nil {
278+
t.Fatalf("Failed to retrieve unit body for hello.service: %v", err)
279+
}
280+
281+
// compare it with unitBody
282+
if regBody != unitBody {
283+
t.Fatalf("Failed to verify fleet unit: %v", err)
284+
}
285+
286+
// destroy the unit again
287+
if _, _, err := cluster.Fleetctl(m, "destroy", "hello.service"); err != nil {
288+
t.Fatalf("Failed to destroy unit: %v", err)
289+
}
290+
291+
stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend")
292+
if err != nil {
293+
t.Fatalf("Failed to run list-units: %v", err)
294+
}
295+
units = strings.Split(strings.TrimSpace(stdout), "\n")
296+
if len(stdout) != 0 && len(units) != 1 {
297+
t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout)
298+
}
299+
300+
// check for the unit being destroyed from the etcd registry,
301+
// /fleet_functional/smoke/unit/.
302+
// NOTE: do not check error of etcdctl, as it returns 4 on an empty list.
303+
etcdUnitPrefix := path.Join(cluster.Keyspace(), "unit")
304+
etcdUnitPath := path.Join(etcdUnitPrefix, hashUnit)
305+
stdout, _, _ = util.RunEtcdctl("ls", etcdUnitPath)
306+
units = strings.Split(strings.TrimSpace(stdout), "\n")
307+
if len(stdout) != 0 && len(units) != 1 {
308+
t.Fatalf("The unit still remains in the registry: %v")
309+
}
310+
}
311+
312+
// retrieveJobObjectHash fetches the job hash value from
313+
// /fleet_functional/smoke/job/<jobName>/object in the etcd registry.
314+
func retrieveJobObjectHash(cluster platform.Cluster, jobName string) (hash string, err error) {
315+
etcdJobPrefix := path.Join(cluster.Keyspace(), "job")
316+
etcdJobPath := path.Join(etcdJobPrefix, jobName, "object")
317+
318+
var stdout string
319+
if stdout, _, err = util.RunEtcdctl("ls", etcdJobPath); err != nil {
320+
return "", fmt.Errorf("Failed to list a unit from the registry: %v", err)
321+
}
322+
units := strings.Split(strings.TrimSpace(stdout), "\n")
323+
if len(stdout) == 0 || len(units) == 0 {
324+
return "", fmt.Errorf("No such unit in the registry: %v", err)
325+
}
326+
327+
stdout, _, err = util.RunEtcdctl("get", etcdJobPath)
328+
stdout = strings.TrimSpace(stdout)
329+
objectBody := strings.Split(stdout, "\n")
330+
if err != nil || len(stdout) == 0 || len(objectBody) == 0 {
331+
return "", fmt.Errorf("Failed to get unit from the registry: %v", err)
332+
}
333+
334+
type jobModel struct {
335+
Name string
336+
UnitHash unit.Hash
337+
}
338+
var jm jobModel
339+
if err = json.Unmarshal([]byte(stdout), &jm); err != nil {
340+
return "", fmt.Errorf("Failed to unmarshal fleet unit in the registry: %v", err)
341+
}
342+
343+
return jm.UnitHash.String(), nil
344+
}
345+
346+
// retrieveUnitBody fetches unit body from /fleet_functional/smoke/unit/<hash>
347+
// in the etcd registry.
348+
func retrieveUnitBody(cluster platform.Cluster, hashUnit string) (regBody string, err error) {
349+
etcdUnitPrefix := path.Join(cluster.Keyspace(), "unit")
350+
etcdUnitPath := path.Join(etcdUnitPrefix, hashUnit)
351+
352+
var stdout string
353+
if stdout, _, err = util.RunEtcdctl("ls", etcdUnitPath); err != nil {
354+
return "", fmt.Errorf("Failed to list a unit from the registry: %v", err)
355+
}
356+
357+
units := strings.Split(strings.TrimSpace(stdout), "\n")
358+
if len(stdout) == 0 || len(units) == 0 {
359+
return "", fmt.Errorf("No such unit in the registry: %v", err)
360+
}
361+
stdout, _, err = util.RunEtcdctl("get", etcdUnitPath)
362+
stdout = strings.TrimSpace(stdout)
363+
unitBody := strings.Split(stdout, "\n")
364+
if err != nil || len(stdout) == 0 || len(unitBody) == 0 {
365+
return "", fmt.Errorf("Failed to get unit from the registry: %v", err)
366+
}
367+
368+
type rawModel struct {
369+
Raw string
370+
}
371+
372+
var rm rawModel
373+
if err = json.Unmarshal([]byte(stdout), &rm); err != nil {
374+
return "", fmt.Errorf("Failed to unmarshal fleet unit in the registry: %v", err)
375+
}
376+
return rm.Raw, nil
377+
}

0 commit comments

Comments
 (0)