Skip to content

Commit 1652836

Browse files
author
Luca Bruno
authored
Merge pull request #309 from Xuanwo/add-methods
machine1: Add CreateMachine call and network support
2 parents 362f06e + fc6a519 commit 1652836

File tree

2 files changed

+78
-27
lines changed

2 files changed

+78
-27
lines changed

machine1/dbus.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"syscall"
2525

2626
"github.com/godbus/dbus"
27+
28+
sd_dbus "github.com/coreos/go-systemd/dbus"
2729
)
2830

2931
const (
@@ -110,6 +112,16 @@ func (c *Conn) getPath(method string, args ...interface{}) (dbus.ObjectPath, err
110112
return path, nil
111113
}
112114

115+
// CreateMachine creates a new virtual machine or container with systemd-machined, generating a scope unit for it
116+
func (c *Conn) CreateMachine(name string, id []byte, service string, class string, pid int, root_directory string, scope_properties []sd_dbus.Property) error {
117+
return c.object.Call(dbusInterface+".CreateMachine", 0, name, id, service, class, uint32(pid), root_directory, scope_properties).Err
118+
}
119+
120+
// CreateMachineWithNetwork creates the container with its network config with systemd-machined
121+
func (c *Conn) CreateMachineWithNetwork(name string, id []byte, service string, class string, pid int, root_directory string, ifindices []int, scope_properties []sd_dbus.Property) error {
122+
return c.object.Call(dbusInterface+".CreateMachineWithNetwork", 0, name, id, service, class, uint32(pid), root_directory, ifindices, scope_properties).Err
123+
}
124+
113125
// GetMachine gets a specific container with systemd-machined
114126
func (c *Conn) GetMachine(name string) (dbus.ObjectPath, error) {
115127
return c.getPath("GetMachine", name)
@@ -164,6 +176,11 @@ func (c *Conn) RegisterMachine(name string, id []byte, service string, class str
164176
return c.object.Call(dbusInterface+".RegisterMachine", 0, name, id, service, class, uint32(pid), root_directory).Err
165177
}
166178

179+
// RegisterMachineWithNetwork registers the container with its network with systemd-machined
180+
func (c *Conn) RegisterMachineWithNetwork(name string, id []byte, service string, class string, pid int, root_directory string, ifindices []int) error {
181+
return c.object.Call(dbusInterface+".RegisterMachineWithNetwork", 0, name, id, service, class, uint32(pid), root_directory, ifindices).Err
182+
}
183+
167184
func machineFromInterfaces(machine []interface{}) (*MachineStatus, error) {
168185
if len(machine) < 4 {
169186
return nil, fmt.Errorf("invalid number of machine fields: %d", len(machine))

machine1/dbus_test.go

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
"testing"
2626
"time"
2727

28-
"github.com/coreos/go-systemd/dbus"
28+
sd_dbus "github.com/coreos/go-systemd/dbus"
29+
"github.com/godbus/dbus"
2930
)
3031

3132
const (
@@ -47,7 +48,7 @@ func mustCreateTestProcess(machineName string) (pid int) {
4748
if err != nil {
4849
panic(fmt.Errorf("systemd-run failed: %q", out))
4950
}
50-
dbusConn, err := dbus.New()
51+
dbusConn, err := sd_dbus.New()
5152
if err != nil {
5253
panic(err.Error())
5354
}
@@ -61,53 +62,86 @@ func mustCreateTestProcess(machineName string) (pid int) {
6162
}
6263

6364
func TestMachine(t *testing.T) {
64-
machineName := machinePrefix + generateRandomLabel(8)
65-
leader := mustCreateTestProcess(machineName)
65+
machineNames := []string{
66+
machinePrefix + "register-" + generateRandomLabel(8),
67+
machinePrefix + "register-with-network-" + generateRandomLabel(8),
68+
machinePrefix + "create-" + generateRandomLabel(8),
69+
machinePrefix + "create-with-network-" + generateRandomLabel(8),
70+
}
71+
leaders := []int{
72+
mustCreateTestProcess(machineNames[0]),
73+
mustCreateTestProcess(machineNames[1]),
74+
mustCreateTestProcess(machineNames[2]),
75+
mustCreateTestProcess(machineNames[3]),
76+
}
6677

6778
conn, newErr := New()
6879
if newErr != nil {
6980
t.Fatal(newErr)
7081
}
7182

72-
regErr := conn.RegisterMachine(machineName, nil, "go-systemd", "container", leader, "")
83+
regErr := conn.RegisterMachine(machineNames[0], nil, "go-systemd", "container", leaders[0], "")
7384
if regErr != nil {
7485
t.Fatal(regErr)
7586
}
7687

77-
machine, getErr := conn.GetMachine(machineName)
78-
if getErr != nil {
79-
t.Fatal(getErr)
88+
regWithNetworkErr := conn.RegisterMachineWithNetwork(machineNames[1], nil, "go-systemd", "container", leaders[1], "", nil)
89+
if regWithNetworkErr != nil {
90+
t.Fatal(regWithNetworkErr)
8091
}
81-
if len(machine) == 0 {
82-
t.Fatalf("did not find machine named %s", machineName)
92+
93+
createErr := conn.CreateMachine(machineNames[2], nil, "go-systemd", "container", leaders[2], "", nil)
94+
if createErr != nil {
95+
t.Fatal(createErr)
96+
}
97+
98+
createWithNetworkErr := conn.CreateMachineWithNetwork(machineNames[3], nil, "go-systemd", "container", leaders[3], "", nil, nil)
99+
if createWithNetworkErr != nil {
100+
t.Fatal(createWithNetworkErr)
101+
}
102+
103+
machines := make([]dbus.ObjectPath, 0)
104+
for _, v := range machineNames {
105+
machine, getErr := conn.GetMachine(v)
106+
if getErr != nil {
107+
t.Fatal(getErr)
108+
}
109+
if machine != "" {
110+
machines = append(machines, machine)
111+
}
112+
}
113+
if len(machines) != 4 {
114+
t.Fatalf("did not find all machine nameds %s", machineNames)
83115
}
84116

85117
listMachines, getErr := conn.ListMachines()
86118
if getErr != nil {
87119
t.Fatal(getErr)
88120
}
89121

90-
// listMachines includes also `.host`, so by default the length should be greater than 1
91-
if len(listMachines) <= 1 {
122+
// listMachines includes also `.host`, so by default the length should be greater than 2
123+
if len(listMachines) <= 4 {
92124
t.Fatalf("did not find any machine")
93125
}
94126

95-
tErr := conn.TerminateMachine(machineName)
96-
if tErr != nil {
97-
t.Fatal(tErr)
98-
}
99-
100-
for i := 1; i <= 10; i++ {
101-
machine, getErr = conn.GetMachine(machineName)
102-
if len(machine) == 0 && getErr != nil {
103-
break
127+
for _, v := range machineNames {
128+
tErr := conn.TerminateMachine(v)
129+
if tErr != nil {
130+
t.Fatal(tErr)
131+
}
132+
var machine dbus.ObjectPath
133+
for i := 1; i <= 10; i++ {
134+
machine, getErr = conn.GetMachine(v)
135+
if len(machine) == 0 && getErr != nil {
136+
break
137+
}
138+
time.Sleep(1 * time.Second)
139+
}
140+
if len(machine) != 0 {
141+
t.Fatalf("unexpectedly found machine named %s", v)
142+
} else if getErr == nil {
143+
t.Fatal("expected error but got nil")
104144
}
105-
time.Sleep(1 * time.Second)
106-
}
107-
if len(machine) != 0 {
108-
t.Fatalf("unexpectedly found machine named %s", machineName)
109-
} else if getErr == nil {
110-
t.Fatal("expected error but got nil")
111145
}
112146
}
113147

0 commit comments

Comments
 (0)