@@ -4,17 +4,18 @@ import (
44 "fmt"
55 "net"
66 "net/url"
7+ "strconv"
78 "sync"
89 "time"
910
1011 "github.com/containers/podman/v6/pkg/machine/define"
12+ jsoniter "github.com/json-iterator/go"
1113 . "github.com/onsi/ginkgo/v2"
1214 . "github.com/onsi/gomega"
1315 . "github.com/onsi/gomega/gexec"
1416)
1517
1618var _ = Describe ("podman machine start" , func () {
17-
1819 It ("start simple machine" , func () {
1920 i := new (initMachine )
2021 session , err := mb .setCmd (i .withImage (mb .imagePath )).run ()
@@ -67,6 +68,7 @@ var _ = Describe("podman machine start", func() {
6768 session , err := machineTestBuilderInit .run ()
6869 Expect (err ).ToNot (HaveOccurred ())
6970 Expect (session ).To (Exit (0 ))
71+
7072 s := new (startMachine )
7173 startSession , err := mb .setCmd (s ).run ()
7274 Expect (err ).ToNot (HaveOccurred ())
@@ -184,7 +186,7 @@ var _ = Describe("podman machine start", func() {
184186 defer GinkgoRecover ()
185187 defer wg .Done ()
186188 s := & startMachine {}
187- startSession1 , err = mb .setName (machine1 ).setCmd (s ).setTimeout (time .Minute * 10 ).run ()
189+ startSession1 , err = mb .setName (machine1 ).setCmd (s . withUpdateConnection ( ptrBool ( false )) ).setTimeout (time .Minute * 10 ).run ()
188190 Expect (err ).ToNot (HaveOccurred ())
189191 }()
190192 go func () {
@@ -197,7 +199,7 @@ var _ = Describe("podman machine start", func() {
197199 // second run.
198200 nmb , err := newMB ()
199201 Expect (err ).ToNot (HaveOccurred ())
200- startSession2 , err = nmb .setName (machine2 ).setCmd (s ).setTimeout (time .Minute * 10 ).run ()
202+ startSession2 , err = nmb .setName (machine2 ).setCmd (s . withUpdateConnection ( ptrBool ( false )) ).setTimeout (time .Minute * 10 ).run ()
201203 Expect (err ).ToNot (HaveOccurred ())
202204 }()
203205 wg .Wait ()
@@ -218,6 +220,89 @@ var _ = Describe("podman machine start", func() {
218220 Expect (startSession1 .errorToString ()).To (ContainSubstring ("%s already starting or running: only one VM can be active at a time" , machine2 ))
219221 }
220222 })
223+
224+ It ("machine start with --update-connection" , func () {
225+ // Add a connection and verify it was set to the default
226+ defConnName := "QA"
227+ err := addSystemConnection (defConnName , true )
228+ Expect (err ).ToNot (HaveOccurred ())
229+
230+ listings , err := getSystemConnectionsAsSysConns ()
231+ Expect (err ).ToNot (HaveOccurred ())
232+ Expect (listings .IsDefault (defConnName )).To (BeTrue ())
233+
234+ // Create a new machine
235+ i := initMachine {}
236+ machineName := randomString ()
237+ initSession , err := mb .setName (machineName ).setCmd (i .withImage (mb .imagePath )).run ()
238+ Expect (err ).ToNot (HaveOccurred ())
239+ Expect (initSession ).To (Exit (0 ))
240+
241+ // Start the new machine with --update-connection=false
242+ s := startMachine {}
243+ startSession , err := mb .setName (machineName ).setCmd (s .withUpdateConnection (ptrBool (false ))).run ()
244+ Expect (err ).ToNot (HaveOccurred ())
245+ Expect (startSession ).To (Exit (0 ))
246+
247+ // We started the machine with --update-connection=false so it should not be default
248+ listings , err = getSystemConnectionsAsSysConns ()
249+ Expect (err ).ToNot (HaveOccurred ())
250+ Expect (listings .IsDefault (defConnName )).To (BeTrue ())
251+
252+ // Stop the machine
253+ halt := stopMachine {}
254+ stopSession , err := mb .setName (machineName ).setCmd (halt ).run ()
255+ Expect (err ).ToNot (HaveOccurred ())
256+ Expect (stopSession ).To (Exit (0 ))
257+
258+ // Start the new machine with --update-connection
259+ startSession , err = mb .setName (machineName ).setCmd (s .withUpdateConnection (ptrBool (true ))).run ()
260+ Expect (err ).ToNot (HaveOccurred ())
261+ Expect (startSession ).To (Exit (0 ))
262+
263+ // We set true so the new default connection should have changed
264+ listings , err = getSystemConnectionsAsSysConns ()
265+ Expect (err ).ToNot (HaveOccurred ())
266+ Expect (listings .IsDefault (machineName )).To (BeTrue ())
267+ })
268+ It ("machine init --now with --update-connection" , func () {
269+ // Add a connection and verify it was set to the default
270+ defConnName := "QA"
271+ err := addSystemConnection (defConnName , true )
272+ Expect (err ).ToNot (HaveOccurred ())
273+
274+ listings , err := getSystemConnectionsAsSysConns ()
275+ Expect (err ).ToNot (HaveOccurred ())
276+ Expect (listings .IsDefault (defConnName )).To (BeTrue ())
277+
278+ // Create a new machine
279+ i := initMachine {}
280+ machineName1 := randomString ()
281+ initSession , err := mb .setName (machineName1 ).setCmd (i .withImage (mb .imagePath ).withUpdateConnection (ptrBool (false )).withNow ()).run ()
282+ Expect (err ).ToNot (HaveOccurred ())
283+ Expect (initSession ).To (Exit (0 ))
284+
285+ // We started the machine with --update-connection=false so it should not be default
286+ listings , err = getSystemConnectionsAsSysConns ()
287+ Expect (err ).ToNot (HaveOccurred ())
288+ Expect (listings .IsDefault (defConnName )).To (BeTrue ())
289+
290+ // Stop the machine
291+ halt := stopMachine {}
292+ stopSession , err := mb .setName (machineName1 ).setCmd (halt ).run ()
293+ Expect (err ).ToNot (HaveOccurred ())
294+ Expect (stopSession ).To (Exit (0 ))
295+
296+ // Create another machine
297+ machineName2 := randomString ()
298+ initSession2 , err := mb .setName (machineName2 ).setCmd (i .withImage (mb .imagePath ).withUpdateConnection (ptrBool (true )).withNow ()).run ()
299+ Expect (err ).ToNot (HaveOccurred ())
300+ Expect (initSession2 ).To (Exit (0 ))
301+
302+ listings , err = getSystemConnectionsAsSysConns ()
303+ Expect (err ).ToNot (HaveOccurred ())
304+ Expect (listings .IsDefault (machineName2 )).To (BeTrue ())
305+ })
221306})
222307
223308func mapToPort (uris []string ) ([]string , error ) {
@@ -238,3 +323,70 @@ func mapToPort(uris []string) ([]string, error) {
238323 }
239324 return ports , nil
240325}
326+
327+ func addSystemConnection (name string , setDefault bool ) error {
328+ addConn := []string {
329+ "system" , "connection" , "add" ,
330+ fmt .Sprintf ("--default=%s" , strconv .FormatBool (setDefault )),
331+ "--identity" , "~/.ssh/id_rsa" ,
332+ name ,
333+ "ssh://[email protected] :2222/run/podman/podman.sock" ,
334+ }
335+ mb .cmd = addConn
336+ addConnSession , err := mb .run ()
337+ if err != nil {
338+ return err
339+ }
340+ if addConnSession .ExitCode () != 0 {
341+ fmt .Println (addConnSession .outputToString ())
342+ return fmt .Errorf ("error: %s" , addConnSession .errorToString ())
343+ }
344+ return nil
345+ }
346+
347+ func systemConnectionLsToSysConns (output []byte ) (SysConns , error ) {
348+ var conns SysConns
349+ err := jsoniter .Unmarshal (output , & conns )
350+ return conns , err
351+ }
352+
353+ type SysConn struct {
354+ Name string
355+ URI string
356+ Identity string
357+ IsMachine bool
358+ Default bool
359+ ReadWrite bool
360+ }
361+
362+ type SysConns []SysConn
363+
364+ func (s SysConns ) IsDefault (name string ) bool {
365+ for _ , conn := range s {
366+ if conn .Name == name {
367+ return conn .Default
368+ }
369+ }
370+ return false
371+ }
372+
373+ func (s SysConns ) GetDefault () (SysConn , error ) {
374+ for _ , conn := range s {
375+ if conn .Default {
376+ return conn , nil
377+ }
378+ }
379+ return SysConn {}, fmt .Errorf ("no default connection found" )
380+ }
381+
382+ func getSystemConnectionsAsSysConns () (SysConns , error ) {
383+ connections := new (listSystemConnection )
384+ connSession , err := mb .setCmd (connections .withFormat ("json" )).run ()
385+ if err != nil {
386+ return nil , err
387+ }
388+ if connSession .ExitCode () != 0 {
389+ return nil , fmt .Errorf ("error: %s" , connSession .errorToString ())
390+ }
391+ return systemConnectionLsToSysConns (connSession .Out .Contents ())
392+ }
0 commit comments