1/*
2Copyright 2015 CoreOS Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package machine1
18
19import (
20	"fmt"
21	"math/rand"
22	"os"
23	"os/exec"
24	"path/filepath"
25	"testing"
26	"time"
27
28	"github.com/coreos/go-systemd/dbus"
29)
30
31const (
32	machinePrefix = "machined-test-"
33)
34
35func mustCreateTestProcess(machineName string) (pid int) {
36	testServiceName := machineName + ".service"
37	systemdRun, err := exec.LookPath("systemd-run")
38	if err != nil {
39		panic(err.Error())
40	}
41	sleep, err := exec.LookPath("sleep")
42	if err != nil {
43		panic(err.Error())
44	}
45	cmd := exec.Command(systemdRun, "--unit="+testServiceName, sleep, "5000")
46	out, err := cmd.CombinedOutput()
47	if err != nil {
48		panic(fmt.Errorf("systemd-run failed: %q", out))
49	}
50	dbusConn, err := dbus.New()
51	if err != nil {
52		panic(err.Error())
53	}
54	defer dbusConn.Close()
55	mainPIDProperty, err := dbusConn.GetServiceProperty(testServiceName, "MainPID")
56	if err != nil {
57		panic(err.Error())
58	}
59	mainPID := mainPIDProperty.Value.Value().(uint32)
60	return int(mainPID)
61}
62
63func TestMachine(t *testing.T) {
64	machineName := machinePrefix + generateRandomLabel(8)
65	leader := mustCreateTestProcess(machineName)
66
67	conn, newErr := New()
68	if newErr != nil {
69		t.Fatal(newErr)
70	}
71
72	regErr := conn.RegisterMachine(machineName, nil, "go-systemd", "container", leader, "")
73	if regErr != nil {
74		t.Fatal(regErr)
75	}
76
77	machine, getErr := conn.GetMachine(machineName)
78	if getErr != nil {
79		t.Fatal(getErr)
80	}
81	if len(machine) == 0 {
82		t.Fatalf("did not find machine named %s", machineName)
83	}
84
85	listMachines, getErr := conn.ListMachines()
86	if getErr != nil {
87		t.Fatal(getErr)
88	}
89
90	// listMachines includes also `.host`, so by default the length should be greater than 1
91	if len(listMachines) <= 1 {
92		t.Fatalf("did not find any machine")
93	}
94
95	tErr := conn.TerminateMachine(machineName)
96	if tErr != nil {
97		t.Fatal(tErr)
98	}
99
100	machine, getErr = conn.GetMachine(machineName)
101	if len(machine) != 0 {
102		t.Fatalf("unexpectedly found machine named %s", machineName)
103	} else if getErr == nil {
104		t.Fatal("expected error but got nil")
105	}
106}
107
108func generateRandomLabel(n int) string {
109	letters := []rune("abcdefghijklmnopqrstuvwxyz")
110	s := make([]rune, n)
111	rand.Seed(time.Now().UnixNano())
112	for i := range s {
113		s[i] = letters[rand.Intn(len(letters))]
114	}
115	return string(s)
116}
117
118func TestImages(t *testing.T) {
119	imageName := machinePrefix + generateRandomLabel(8)
120	imagePath := filepath.Join("/var/lib/machines", imageName)
121
122	if _, err := os.Create(imagePath); err != nil {
123		t.Fatal(err)
124	}
125	defer os.Remove(imagePath)
126
127	if err := os.Truncate(imagePath, 500*1024*1024); err != nil {
128		t.Fatal(err)
129	}
130
131	conn, newErr := New()
132	if newErr != nil {
133		t.Fatal(newErr)
134	}
135
136	listImages, listErr := conn.ListImages()
137	if listErr != nil {
138		t.Fatal(listErr)
139	}
140
141	if len(listImages) < 1 {
142		t.Fatalf("did not find any image")
143	}
144}
145