github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/farm_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	. "github.com/containers/podman/v5/test/utils"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  func setupContainersConfWithSystemConnections() {
    14  	// make sure connections are not written to real user config on host
    15  	file := filepath.Join(podmanTest.TempDir, "containers.conf")
    16  	f, err := os.Create(file)
    17  	Expect(err).ToNot(HaveOccurred())
    18  	f.Close()
    19  	os.Setenv("CONTAINERS_CONF", file)
    20  
    21  	file = filepath.Join(podmanTest.TempDir, "connections.conf")
    22  	f, err = os.Create(file)
    23  	Expect(err).ToNot(HaveOccurred())
    24  	connections := `{"connection":{"default":"QA","connections":{"QA":{"uri":"ssh://root@podman.test:2222/run/podman/podman.sock"},"QB":{"uri":"ssh://root@podman.test:3333/run/podman/podman.sock"}}}}`
    25  	_, err = f.WriteString(connections)
    26  	Expect(err).ToNot(HaveOccurred())
    27  	f.Close()
    28  	os.Setenv("PODMAN_CONNECTIONS_CONF", file)
    29  }
    30  
    31  var _ = Describe("podman farm", func() {
    32  
    33  	BeforeEach(setupContainersConfWithSystemConnections)
    34  
    35  	Context("without running API service", func() {
    36  		It("verify system connections exist", func() {
    37  			session := podmanTest.Podman(systemConnectionListCmd)
    38  			session.WaitWithDefaultTimeout()
    39  			Expect(session).Should(ExitCleanly())
    40  			Expect(string(session.Out.Contents())).To(Equal(`QA ssh://root@podman.test:2222/run/podman/podman.sock  true true
    41  QB ssh://root@podman.test:3333/run/podman/podman.sock  false true
    42  `))
    43  		})
    44  
    45  		It("create farm", func() {
    46  			// create farm with multiple system connections
    47  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
    48  			session := podmanTest.Podman(cmd)
    49  			session.WaitWithDefaultTimeout()
    50  			Expect(session).Should(ExitCleanly())
    51  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
    52  
    53  			// create farm with only one system connection
    54  			cmd = []string{"farm", "create", "farm2", "QA"}
    55  			session = podmanTest.Podman(cmd)
    56  			session.WaitWithDefaultTimeout()
    57  			Expect(session).Should(ExitCleanly())
    58  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" created"))
    59  
    60  			// create empty farm
    61  			cmd = []string{"farm", "create", "farm3"}
    62  			session = podmanTest.Podman(cmd)
    63  			session.WaitWithDefaultTimeout()
    64  			Expect(session).Should(ExitCleanly())
    65  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm3\" created"))
    66  
    67  			session = podmanTest.Podman(farmListCmd)
    68  			session.WaitWithDefaultTimeout()
    69  			Expect(session).Should(ExitCleanly())
    70  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [QA QB] true true
    71  farm2 [QA] false true
    72  farm3 [] false true
    73  `))
    74  
    75  			// create existing farm should exit with error
    76  			cmd = []string{"farm", "create", "farm3"}
    77  			session = podmanTest.Podman(cmd)
    78  			session.WaitWithDefaultTimeout()
    79  			Expect(session).Should(Not(ExitCleanly()))
    80  		})
    81  
    82  		It("update existing farms", func() {
    83  			// create farm with multiple system connections
    84  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
    85  			session := podmanTest.Podman(cmd)
    86  			session.WaitWithDefaultTimeout()
    87  			Expect(session).Should(ExitCleanly())
    88  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
    89  
    90  			// create farm with only one system connection
    91  			cmd = []string{"farm", "create", "farm2", "QA"}
    92  			session = podmanTest.Podman(cmd)
    93  			session.WaitWithDefaultTimeout()
    94  			Expect(session).Should(ExitCleanly())
    95  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" created"))
    96  
    97  			// create empty farm
    98  			cmd = []string{"farm", "create", "farm3"}
    99  			session = podmanTest.Podman(cmd)
   100  			session.WaitWithDefaultTimeout()
   101  			Expect(session).Should(ExitCleanly())
   102  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm3\" created"))
   103  
   104  			session = podmanTest.Podman(farmListCmd)
   105  			session.WaitWithDefaultTimeout()
   106  			Expect(session).Should(ExitCleanly())
   107  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [QA QB] true true
   108  farm2 [QA] false true
   109  farm3 [] false true
   110  `))
   111  
   112  			// update farm1 to remove the QA connection from it
   113  			cmd = []string{"farm", "update", "--remove", "QA,QB", "farm1"}
   114  			session = podmanTest.Podman(cmd)
   115  			session.WaitWithDefaultTimeout()
   116  			Expect(session).Should(ExitCleanly())
   117  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" updated"))
   118  
   119  			// update farm3 to add QA and QB connections to it
   120  			cmd = []string{"farm", "update", "--add", "QB", "farm3"}
   121  			session = podmanTest.Podman(cmd)
   122  			session.WaitWithDefaultTimeout()
   123  			Expect(session).Should(ExitCleanly())
   124  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm3\" updated"))
   125  
   126  			// update farm2 to be the default farm
   127  			cmd = []string{"farm", "update", "--default", "farm2"}
   128  			session = podmanTest.Podman(cmd)
   129  			session.WaitWithDefaultTimeout()
   130  			Expect(session).Should(ExitCleanly())
   131  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" updated"))
   132  
   133  			session = podmanTest.Podman(farmListCmd)
   134  			session.WaitWithDefaultTimeout()
   135  			Expect(session).Should(ExitCleanly())
   136  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [] false true
   137  farm2 [QA] true true
   138  farm3 [QB] false true
   139  `))
   140  
   141  			// update farm2 to not be the default, no farms should be the default
   142  			cmd = []string{"farm", "update", "--default=false", "farm2"}
   143  			session = podmanTest.Podman(cmd)
   144  			session.WaitWithDefaultTimeout()
   145  			Expect(session).Should(ExitCleanly())
   146  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" updated"))
   147  
   148  			session = podmanTest.Podman(farmListCmd)
   149  			session.WaitWithDefaultTimeout()
   150  			Expect(session).Should(ExitCleanly())
   151  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [] false true
   152  farm2 [QA] false true
   153  farm3 [QB] false true
   154  `))
   155  		})
   156  
   157  		It("update farm with non-existing connections", func() {
   158  			// create farm with multiple system connections
   159  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
   160  			session := podmanTest.Podman(cmd)
   161  			session.WaitWithDefaultTimeout()
   162  			Expect(session).Should(ExitCleanly())
   163  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
   164  
   165  			// create farm with only one system connection
   166  			cmd = []string{"farm", "create", "farm2", "QA"}
   167  			session = podmanTest.Podman(cmd)
   168  			session.WaitWithDefaultTimeout()
   169  			Expect(session).Should(ExitCleanly())
   170  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" created"))
   171  
   172  			session = podmanTest.Podman(farmListCmd)
   173  			session.WaitWithDefaultTimeout()
   174  			Expect(session).Should(ExitCleanly())
   175  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [QA QB] true true
   176  farm2 [QA] false true
   177  `))
   178  
   179  			// update farm1 to add no-node connection to it
   180  			cmd = []string{"farm", "update", "--add", "no-node", "farm1"}
   181  			session = podmanTest.Podman(cmd)
   182  			session.WaitWithDefaultTimeout()
   183  			Expect(session).Should(ExitWithError(125, `cannot add to farm, "no-node" is not a system connection`))
   184  
   185  			// update farm2 to remove node not in farm connections from it
   186  			cmd = []string{"farm", "update", "--remove", "QB", "farm2"}
   187  			session = podmanTest.Podman(cmd)
   188  			session.WaitWithDefaultTimeout()
   189  			Expect(session).Should(ExitWithError(125, `cannot remove from farm, "QB" is not a connection in the farm`))
   190  
   191  			// check again to ensure that nothing has changed
   192  			session = podmanTest.Podman(farmListCmd)
   193  			session.WaitWithDefaultTimeout()
   194  			Expect(session).Should(ExitCleanly())
   195  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [QA QB] true true
   196  farm2 [QA] false true
   197  `))
   198  		})
   199  
   200  		It("update non-existent farm", func() {
   201  			// create farm with multiple system connections
   202  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
   203  			session := podmanTest.Podman(cmd)
   204  			session.WaitWithDefaultTimeout()
   205  			Expect(session).Should(ExitCleanly())
   206  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
   207  
   208  			// update non-existent farm to add QA connection to it
   209  			cmd = []string{"farm", "update", "--add", "no-node", "non-existent"}
   210  			session = podmanTest.Podman(cmd)
   211  			session.WaitWithDefaultTimeout()
   212  			Expect(session).Should(ExitWithError(125, `cannot update farm, "non-existent" farm doesn't exist`))
   213  
   214  			// update non-existent farm to default
   215  			cmd = []string{"farm", "update", "--default", "non-existent"}
   216  			session = podmanTest.Podman(cmd)
   217  			session.WaitWithDefaultTimeout()
   218  			Expect(session).Should(ExitWithError(125, `cannot update farm, "non-existent" farm doesn't exist`))
   219  
   220  			session = podmanTest.Podman(farmListCmd)
   221  			session.WaitWithDefaultTimeout()
   222  			Expect(session).Should(ExitCleanly())
   223  			Expect(session.OutputToString()).To(Equal(`farm1 [QA QB] true true`))
   224  		})
   225  
   226  		It("remove farms", func() {
   227  			// create farm with multiple system connections
   228  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
   229  			session := podmanTest.Podman(cmd)
   230  			session.WaitWithDefaultTimeout()
   231  			Expect(session).Should(ExitCleanly())
   232  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
   233  
   234  			// create farm with only one system connection
   235  			cmd = []string{"farm", "create", "farm2", "QA"}
   236  			session = podmanTest.Podman(cmd)
   237  			session.WaitWithDefaultTimeout()
   238  			Expect(session).Should(ExitCleanly())
   239  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" created"))
   240  
   241  			session = podmanTest.Podman(farmListCmd)
   242  			session.WaitWithDefaultTimeout()
   243  			Expect(session).Should(ExitCleanly())
   244  			Expect(string(session.Out.Contents())).To(Equal(`farm1 [QA QB] true true
   245  farm2 [QA] false true
   246  `))
   247  
   248  			// remove farm1 and a non-existent farm
   249  			// farm 1 should be removed and a warning printed for nonexistent-farm
   250  			cmd = []string{"farm", "rm", "farm1", "nonexistent-farm"}
   251  			session = podmanTest.Podman(cmd)
   252  			session.WaitWithDefaultTimeout()
   253  			Expect(session).Should(Exit(0))
   254  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" deleted"))
   255  			Expect(session.ErrorToString()).Should(ContainSubstring("doesn't exist; nothing to remove"))
   256  
   257  			session = podmanTest.Podman(farmListCmd)
   258  			session.WaitWithDefaultTimeout()
   259  			Expect(session).Should(ExitCleanly())
   260  			Expect(session.OutputToString()).To(Equal(`farm2 [QA] true true`))
   261  
   262  			// remove all non-existent farms and expect an error
   263  			cmd = []string{"farm", "rm", "foo", "bar"}
   264  			session = podmanTest.Podman(cmd)
   265  			session.WaitWithDefaultTimeout()
   266  			Expect(session).Should(Not(ExitCleanly()))
   267  		})
   268  
   269  		It("remove --all farms", func() {
   270  			// create farm with multiple system connections
   271  			cmd := []string{"farm", "create", "farm1", "QA", "QB"}
   272  			session := podmanTest.Podman(cmd)
   273  			session.WaitWithDefaultTimeout()
   274  			Expect(session).Should(ExitCleanly())
   275  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm1\" created"))
   276  
   277  			// create farm with only one system connection
   278  			cmd = []string{"farm", "create", "farm2", "QA"}
   279  			session = podmanTest.Podman(cmd)
   280  			session.WaitWithDefaultTimeout()
   281  			Expect(session).Should(ExitCleanly())
   282  			Expect(session.Out.Contents()).Should(ContainSubstring("Farm \"farm2\" created"))
   283  
   284  			// remove --all
   285  			cmd = []string{"farm", "rm", "--all"}
   286  			session = podmanTest.Podman(cmd)
   287  			session.WaitWithDefaultTimeout()
   288  			Expect(session).Should(ExitCleanly())
   289  			Expect(session.Out.Contents()).Should(ContainSubstring("All farms have been deleted"))
   290  
   291  			session = podmanTest.Podman(farmListCmd)
   292  			session.WaitWithDefaultTimeout()
   293  			Expect(session).Should(ExitCleanly())
   294  			Expect(session.OutputToString()).To(Equal(""))
   295  		})
   296  	})
   297  })