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

Commit 0ef648c

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 cd81091 commit 0ef648c

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

functional/unit_action_test.go

Lines changed: 153 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,151 @@ 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+
var stdout string
257+
stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend")
258+
if err != nil {
259+
t.Fatalf("Failed to run list-units: %v", err)
260+
}
261+
units := strings.Split(strings.TrimSpace(stdout), "\n")
262+
if len(units) != 1 {
263+
t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout)
264+
}
265+
266+
// cat the unit and compare it with the value in etcd registry
267+
if stdout, _, err = cluster.Fleetctl(m, "cat", "hello.service"); err != nil {
268+
t.Fatalf("Unable to retrieve the fleet unit: %v", err)
269+
}
270+
unitBody := stdout
271+
272+
var hashUnit string
273+
if hashUnit, err = retrieveJobObjectHash("hello.service"); err != nil {
274+
t.Fatalf("Failed to retrieve hash of job object hello.service: %v", err)
275+
}
276+
277+
var regBody string
278+
if regBody, err = retrieveUnitBody(hashUnit); err != nil {
279+
t.Fatalf("Failed to retrieve unit body for hello.service: %v", err)
280+
}
281+
282+
// compare it with unitBody
283+
if regBody != unitBody {
284+
//t.Fatalf("Failed to verify fleet unit: %v", err)
285+
t.Fatalf("Failed to verify fleet unit: %v\nregBody = %s\nunitBody = %s", err, regBody, unitBody)
286+
}
287+
288+
// destroy the unit again
289+
if _, _, err := cluster.Fleetctl(m, "destroy", "hello.service"); err != nil {
290+
t.Fatalf("Failed to destroy unit: %v", err)
291+
}
292+
293+
stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend")
294+
if err != nil {
295+
t.Fatalf("Failed to run list-units: %v", err)
296+
}
297+
units = strings.Split(strings.TrimSpace(stdout), "\n")
298+
if len(stdout) != 0 && len(units) != 1 {
299+
t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout)
300+
}
301+
302+
// check for the unit being destroyed from the etcd registry
303+
etcdUnitPrefix := "/_coreos.com/fleet/unit/"
304+
etcdUnitPath := path.Join(etcdUnitPrefix, hashUnit)
305+
if stdout, _, err = util.RunEtcdctl("ls", etcdUnitPath); err != nil {
306+
t.Fatalf("Failed to list a unit from the registry: %v", err)
307+
}
308+
units = strings.Split(strings.TrimSpace(stdout), "\n")
309+
if len(stdout) != 0 && len(units) != 1 {
310+
t.Fatalf("The unit still remains in the registry: %v", err)
311+
}
312+
}
313+
314+
// retrieveJobObjectHash fetches the job hash value from
315+
// /_coreos.com/fleet/job/<jobName>/object in the etcd registry.
316+
func retrieveJobObjectHash(jobName string) (hash string, err error) {
317+
etcdJobPrefix := "/_coreos.com/fleet/job/"
318+
etcdJobPath := path.Join(etcdJobPrefix, jobName, "object")
319+
320+
var stdout string
321+
if stdout, _, err = util.RunEtcdctl("ls", etcdJobPath); err != nil {
322+
return "", fmt.Errorf("Failed to list a unit from the registry: %v", err)
323+
}
324+
units := strings.Split(strings.TrimSpace(stdout), "\n")
325+
if len(stdout) == 0 || len(units) == 0 {
326+
return "", fmt.Errorf("No such unit in the registry: %v", err)
327+
}
328+
329+
stdout, _, err = util.RunEtcdctl("get", etcdJobPath)
330+
stdout = strings.TrimSpace(stdout)
331+
objectBody := strings.Split(stdout, "\n")
332+
if err != nil || len(stdout) == 0 || len(objectBody) == 0 {
333+
return "", fmt.Errorf("Failed to get unit from the registry: %v", err)
334+
}
335+
336+
type jobModel struct {
337+
Name string
338+
UnitHash unit.Hash
339+
}
340+
var jm jobModel
341+
if err = json.Unmarshal([]byte(stdout), &jm); err != nil {
342+
return "", fmt.Errorf("Failed to unmarshal fleet unit in the registry: %v", err)
343+
}
344+
345+
return jm.UnitHash.String(), nil
346+
}
347+
348+
// retrieveUnitBody fetches unit body from /_coreos.com/fleet/unit/<hash>
349+
// in the etcd registry.
350+
func retrieveUnitBody(hashUnit string) (regBody string, err error) {
351+
etcdUnitPrefix := "/_coreos.com/fleet/unit/"
352+
etcdUnitPath := path.Join(etcdUnitPrefix, hashUnit)
353+
354+
var stdout string
355+
if stdout, _, err = util.RunEtcdctl("ls", etcdUnitPath); err != nil {
356+
return "", fmt.Errorf("Failed to list a unit from the registry: %v", err)
357+
}
358+
359+
units := strings.Split(strings.TrimSpace(stdout), "\n")
360+
if len(stdout) == 0 || len(units) == 0 {
361+
return "", fmt.Errorf("No such unit in the registry: %v", err)
362+
}
363+
stdout, _, err = util.RunEtcdctl("get", etcdUnitPath)
364+
stdout = strings.TrimSpace(stdout)
365+
unitBody := strings.Split(stdout, "\n")
366+
if err != nil || len(stdout) == 0 || len(unitBody) == 0 {
367+
return "", fmt.Errorf("Failed to get unit from the registry: %v", err)
368+
}
369+
370+
type rawModel struct {
371+
Raw string
372+
}
373+
374+
var rm rawModel
375+
if err = json.Unmarshal([]byte(stdout), &rm); err != nil {
376+
return "", fmt.Errorf("Failed to unmarshal fleet unit in the registry: %v", err)
377+
}
378+
return rm.Raw, nil
379+
}

0 commit comments

Comments
 (0)