@@ -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
3132const (
@@ -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
6364func 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