github.com/containers/podman/v4@v4.9.4/test/e2e/generate_systemd_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	. "github.com/containers/podman/v4/test/utils"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("Podman generate systemd", func() {
    14  
    15  	It("podman generate systemd on bogus container/pod", func() {
    16  		session := podmanTest.Podman([]string{"generate", "systemd", "foobar"})
    17  		session.WaitWithDefaultTimeout()
    18  		Expect(session).To(ExitWithError())
    19  	})
    20  
    21  	It("podman generate systemd bad restart policy", func() {
    22  		session := podmanTest.Podman([]string{"generate", "systemd", "--restart-policy", "never", "foobar"})
    23  		session.WaitWithDefaultTimeout()
    24  		Expect(session).To(ExitWithError())
    25  	})
    26  
    27  	It("podman generate systemd bad timeout value", func() {
    28  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "-1", "foobar"})
    29  		session.WaitWithDefaultTimeout()
    30  		Expect(session).To(ExitWithError())
    31  	})
    32  
    33  	It("podman generate systemd bad restart-policy value", func() {
    34  		session := podmanTest.Podman([]string{"create", "--name", "foobar", "alpine", "top"})
    35  		session.WaitWithDefaultTimeout()
    36  		Expect(session).Should(Exit(0))
    37  
    38  		session = podmanTest.Podman([]string{"generate", "systemd", "--restart-policy", "bogus", "foobar"})
    39  		session.WaitWithDefaultTimeout()
    40  		Expect(session).To(ExitWithError())
    41  		Expect(session.ErrorToString()).To(ContainSubstring("bogus is not a valid restart policy"))
    42  	})
    43  
    44  	It("podman generate systemd with --no-header=true", func() {
    45  		session := podmanTest.Podman([]string{"create", "--name", "foobar", "alpine", "top"})
    46  		session.WaitWithDefaultTimeout()
    47  		Expect(session).Should(Exit(0))
    48  
    49  		session = podmanTest.Podman([]string{"generate", "systemd", "foobar", "--no-header=true"})
    50  		session.WaitWithDefaultTimeout()
    51  		Expect(session).Should(Exit(0))
    52  
    53  		Expect(session.OutputToString()).NotTo(ContainSubstring("autogenerated by"))
    54  	})
    55  
    56  	It("podman generate systemd with --no-header", func() {
    57  		session := podmanTest.Podman([]string{"create", "--name", "foobar", "alpine", "top"})
    58  		session.WaitWithDefaultTimeout()
    59  		Expect(session).Should(Exit(0))
    60  
    61  		session = podmanTest.Podman([]string{"generate", "systemd", "foobar", "--no-header"})
    62  		session.WaitWithDefaultTimeout()
    63  		Expect(session).Should(Exit(0))
    64  
    65  		Expect(session.OutputToString()).NotTo(ContainSubstring("autogenerated by"))
    66  	})
    67  
    68  	It("podman generate systemd with --no-header=false", func() {
    69  		session := podmanTest.Podman([]string{"create", "--name", "foobar", "alpine", "top"})
    70  		session.WaitWithDefaultTimeout()
    71  		Expect(session).Should(Exit(0))
    72  
    73  		session = podmanTest.Podman([]string{"generate", "systemd", "foobar", "--no-header=false"})
    74  		session.WaitWithDefaultTimeout()
    75  		Expect(session).Should(Exit(0))
    76  
    77  		Expect(session.OutputToString()).To(ContainSubstring("autogenerated by"))
    78  	})
    79  
    80  	It("podman generate systemd good timeout value", func() {
    81  		session := podmanTest.Podman([]string{"create", "--name", "foobar", "alpine", "top"})
    82  		session.WaitWithDefaultTimeout()
    83  		Expect(session).Should(Exit(0))
    84  
    85  		session = podmanTest.Podman([]string{"generate", "systemd", "--time", "1234", "foobar"})
    86  		session.WaitWithDefaultTimeout()
    87  		Expect(session).Should(Exit(0))
    88  		Expect(session.OutputToString()).To(ContainSubstring("TimeoutStopSec=1294"))
    89  		Expect(session.OutputToString()).To(ContainSubstring("-t 1234"))
    90  	})
    91  
    92  	It("podman generate systemd", func() {
    93  		n := podmanTest.Podman([]string{"run", "--name", "nginx", "-dt", NGINX_IMAGE})
    94  		n.WaitWithDefaultTimeout()
    95  		Expect(n).Should(Exit(0))
    96  
    97  		session := podmanTest.Podman([]string{"generate", "systemd", "nginx"})
    98  		session.WaitWithDefaultTimeout()
    99  		Expect(session).Should(Exit(0))
   100  
   101  		// The podman commands in the unit should not contain the root flags
   102  		Expect(session.OutputToString()).ToNot(ContainSubstring(" --runroot"))
   103  	})
   104  
   105  	It("podman generate systemd --files --name", func() {
   106  		n := podmanTest.Podman([]string{"run", "--name", "nginx", "-dt", NGINX_IMAGE})
   107  		n.WaitWithDefaultTimeout()
   108  		Expect(n).Should(Exit(0))
   109  
   110  		session := podmanTest.Podman([]string{"generate", "systemd", "--files", "--name", "nginx"})
   111  		session.WaitWithDefaultTimeout()
   112  		Expect(session).Should(Exit(0))
   113  
   114  		for _, file := range session.OutputToStringArray() {
   115  			os.Remove(file)
   116  		}
   117  		Expect(session.OutputToString()).To(ContainSubstring("/container-nginx.service"))
   118  	})
   119  
   120  	It("podman generate systemd with timeout", func() {
   121  		n := podmanTest.Podman([]string{"run", "--name", "nginx", "-dt", NGINX_IMAGE})
   122  		n.WaitWithDefaultTimeout()
   123  		Expect(n).Should(Exit(0))
   124  
   125  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "5", "nginx"})
   126  		session.WaitWithDefaultTimeout()
   127  		Expect(session).Should(Exit(0))
   128  		Expect(session.OutputToString()).To(ContainSubstring("TimeoutStopSec=65"))
   129  		Expect(session.OutputToString()).ToNot(ContainSubstring("TimeoutStartSec="))
   130  		Expect(session.OutputToString()).To(ContainSubstring("podman stop"))
   131  		Expect(session.OutputToString()).To(ContainSubstring("-t 5"))
   132  
   133  		session = podmanTest.Podman([]string{"generate", "systemd", "--stop-timeout", "5", "--start-timeout", "123", "nginx"})
   134  		session.WaitWithDefaultTimeout()
   135  		Expect(session).Should(Exit(0))
   136  		Expect(session.OutputToString()).To(ContainSubstring("TimeoutStartSec=123"))
   137  		Expect(session.OutputToString()).To(ContainSubstring("TimeoutStopSec=65"))
   138  		Expect(session.OutputToString()).To(ContainSubstring("-t 5"))
   139  	})
   140  
   141  	It("podman generate systemd with user-defined dependencies", func() {
   142  		n := podmanTest.Podman([]string{"run", "--name", "nginx", "-dt", NGINX_IMAGE})
   143  		n.WaitWithDefaultTimeout()
   144  		Expect(n).Should(Exit(0))
   145  
   146  		session := podmanTest.Podman([]string{"generate", "systemd", "--wants", "foobar.service", "nginx"})
   147  		session.WaitWithDefaultTimeout()
   148  		Expect(session).Should(Exit(0))
   149  
   150  		// The generated systemd unit should contain the User-defined Wants option
   151  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   152  		Expect(session.OutputToString()).To(ContainSubstring("Wants=foobar.service"))
   153  
   154  		session = podmanTest.Podman([]string{"generate", "systemd", "--after", "foobar.service", "nginx"})
   155  		session.WaitWithDefaultTimeout()
   156  		Expect(session).Should(Exit(0))
   157  
   158  		// The generated systemd unit should contain the User-defined After option
   159  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   160  		Expect(session.OutputToString()).To(ContainSubstring("After=foobar.service"))
   161  
   162  		session = podmanTest.Podman([]string{"generate", "systemd", "--requires", "foobar.service", "nginx"})
   163  		session.WaitWithDefaultTimeout()
   164  		Expect(session).Should(Exit(0))
   165  
   166  		// The generated systemd unit should contain the User-defined Requires option
   167  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   168  		Expect(session.OutputToString()).To(ContainSubstring("Requires=foobar.service"))
   169  
   170  		session = podmanTest.Podman([]string{
   171  			"generate", "systemd",
   172  			"--wants", "foobar.service", "--wants", "barfoo.service",
   173  			"--after", "foobar.service", "--after", "barfoo.service",
   174  			"--requires", "foobar.service", "--requires", "barfoo.service", "nginx"})
   175  		session.WaitWithDefaultTimeout()
   176  		Expect(session).Should(Exit(0))
   177  
   178  		// The generated systemd unit should contain the User-defined Want, After, Requires options
   179  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   180  		Expect(session.OutputToString()).To(ContainSubstring("Wants=foobar.service barfoo.service"))
   181  		Expect(session.OutputToString()).To(ContainSubstring("After=foobar.service barfoo.service"))
   182  		Expect(session.OutputToString()).To(ContainSubstring("Requires=foobar.service barfoo.service"))
   183  	})
   184  
   185  	It("podman generate systemd pod --name", func() {
   186  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   187  		n.WaitWithDefaultTimeout()
   188  		Expect(n).Should(Exit(0))
   189  
   190  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   191  		n.WaitWithDefaultTimeout()
   192  		Expect(n).Should(Exit(0))
   193  
   194  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"})
   195  		n.WaitWithDefaultTimeout()
   196  		Expect(n).Should(Exit(0))
   197  
   198  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "foo"})
   199  		session.WaitWithDefaultTimeout()
   200  		Expect(session).Should(Exit(0))
   201  
   202  		// Grepping the output (in addition to unit tests)
   203  		output := session.OutputToString()
   204  		Expect(output).To(ContainSubstring("# pod-foo.service"))
   205  		Expect(output).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
   206  		Expect(output).To(ContainSubstring("# container-foo-1.service"))
   207  		Expect(output).To(ContainSubstring(" start foo-1"))
   208  		Expect(output).To(ContainSubstring("-infra")) // infra container
   209  		Expect(output).To(ContainSubstring("# container-foo-2.service"))
   210  		Expect(output).To(ContainSubstring("podman stop"))
   211  		Expect(output).To(ContainSubstring("-t 42 foo-2"))
   212  		Expect(output).To(ContainSubstring("BindsTo=pod-foo.service"))
   213  		Expect(output).To(ContainSubstring("PIDFile="))
   214  		Expect(output).To(ContainSubstring("/userdata/conmon.pid"))
   215  		Expect(strings.Count(output, "RequiresMountsFor="+podmanTest.RunRoot)).To(Equal(3))
   216  		// The podman commands in the unit should not contain the root flags
   217  		Expect(output).ToNot(ContainSubstring(" --runroot"))
   218  
   219  		// Generating pod/container units for an init container is not
   220  		// supported (see #18585).
   221  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--init-ctr", "always", "--name", "foo-init", "alpine", "top"})
   222  		n.WaitWithDefaultTimeout()
   223  		Expect(n).Should(Exit(0))
   224  		// Fail for the pod
   225  		session = podmanTest.Podman([]string{"generate", "systemd", "foo"})
   226  		session.WaitWithDefaultTimeout()
   227  		Expect(session).Should(Exit(125))
   228  		Expect(session.ErrorToString()).To(ContainSubstring("cannot generate systemd units for init containers"))
   229  		// Fail for the init container
   230  		session = podmanTest.Podman([]string{"generate", "systemd", "foo-init"})
   231  		session.WaitWithDefaultTimeout()
   232  		Expect(session).Should(Exit(125))
   233  		Expect(session.ErrorToString()).To(ContainSubstring("cannot generate systemd units for init containers"))
   234  	})
   235  
   236  	It("podman generate systemd pod --name --files", func() {
   237  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   238  		n.WaitWithDefaultTimeout()
   239  		Expect(n).Should(Exit(0))
   240  
   241  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   242  		n.WaitWithDefaultTimeout()
   243  		Expect(n).Should(Exit(0))
   244  
   245  		session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--files", "foo"})
   246  		session.WaitWithDefaultTimeout()
   247  		Expect(session).Should(Exit(0))
   248  
   249  		for _, file := range session.OutputToStringArray() {
   250  			os.Remove(file)
   251  		}
   252  
   253  		Expect(session.OutputToString()).To(ContainSubstring("/pod-foo.service"))
   254  		Expect(session.OutputToString()).To(ContainSubstring("/container-foo-1.service"))
   255  	})
   256  
   257  	It("podman generate systemd pod with user-defined dependencies", func() {
   258  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   259  		n.WaitWithDefaultTimeout()
   260  		Expect(n).Should(Exit(0))
   261  
   262  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   263  		n.WaitWithDefaultTimeout()
   264  		Expect(n).Should(Exit(0))
   265  
   266  		session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--wants", "foobar.service", "foo"})
   267  		session.WaitWithDefaultTimeout()
   268  		Expect(session).Should(Exit(0))
   269  
   270  		// The generated systemd unit should contain the User-defined Wants option
   271  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   272  		Expect(session.OutputToString()).To(ContainSubstring("Wants=foobar.service"))
   273  
   274  		session = podmanTest.Podman([]string{"generate", "systemd", "--name", "--after", "foobar.service", "foo"})
   275  		session.WaitWithDefaultTimeout()
   276  		Expect(session).Should(Exit(0))
   277  
   278  		// The generated systemd unit should contain the User-defined After option
   279  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   280  		Expect(session.OutputToString()).To(ContainSubstring("After=foobar.service"))
   281  
   282  		session = podmanTest.Podman([]string{"generate", "systemd", "--name", "--requires", "foobar.service", "foo"})
   283  		session.WaitWithDefaultTimeout()
   284  		Expect(session).Should(Exit(0))
   285  
   286  		// The generated systemd unit should contain the User-defined Requires option
   287  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   288  		Expect(session.OutputToString()).To(ContainSubstring("Requires=foobar.service"))
   289  
   290  		session = podmanTest.Podman([]string{
   291  			"generate", "systemd", "--name",
   292  			"--wants", "foobar.service", "--wants", "barfoo.service",
   293  			"--after", "foobar.service", "--after", "barfoo.service",
   294  			"--requires", "foobar.service", "--requires", "barfoo.service", "foo"})
   295  		session.WaitWithDefaultTimeout()
   296  		Expect(session).Should(Exit(0))
   297  
   298  		// The generated systemd unit should contain the User-defined Want, After, Requires options
   299  		Expect(session.OutputToString()).To(ContainSubstring("# User-defined dependencies"))
   300  		Expect(session.OutputToString()).To(ContainSubstring("Wants=foobar.service barfoo.service"))
   301  		Expect(session.OutputToString()).To(ContainSubstring("After=foobar.service barfoo.service"))
   302  		Expect(session.OutputToString()).To(ContainSubstring("Requires=foobar.service barfoo.service"))
   303  	})
   304  
   305  	It("podman generate systemd --new --name foo", func() {
   306  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   307  		n.WaitWithDefaultTimeout()
   308  		Expect(n).Should(Exit(0))
   309  
   310  		session := podmanTest.Podman([]string{"generate", "systemd", "-t", "42", "--name", "--new", "foo"})
   311  		session.WaitWithDefaultTimeout()
   312  		Expect(session).Should(Exit(0))
   313  
   314  		// Grepping the output (in addition to unit tests)
   315  		Expect(session.OutputToString()).To(ContainSubstring("# container-foo.service"))
   316  		Expect(session.OutputToString()).To(ContainSubstring(" --replace "))
   317  		if !IsRemote() {
   318  			// The podman commands in the unit should contain the root flags if generate systemd --new is used
   319  			Expect(session.OutputToString()).To(ContainSubstring(" --runroot"))
   320  		}
   321  	})
   322  
   323  	It("podman generate systemd --new --name=foo", func() {
   324  		n := podmanTest.Podman([]string{"create", "--name=foo", "alpine", "top"})
   325  		n.WaitWithDefaultTimeout()
   326  		Expect(n).Should(Exit(0))
   327  
   328  		session := podmanTest.Podman([]string{"generate", "systemd", "-t", "42", "--name", "--new", "foo"})
   329  		session.WaitWithDefaultTimeout()
   330  		Expect(session).Should(Exit(0))
   331  
   332  		// Grepping the output (in addition to unit tests)
   333  		Expect(session.OutputToString()).To(ContainSubstring("# container-foo.service"))
   334  		Expect(session.OutputToString()).To(ContainSubstring(" --replace "))
   335  	})
   336  
   337  	It("podman generate systemd --new without explicit detaching param", func() {
   338  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   339  		n.WaitWithDefaultTimeout()
   340  		Expect(n).Should(Exit(0))
   341  
   342  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "--new", "foo"})
   343  		session.WaitWithDefaultTimeout()
   344  		Expect(session).Should(Exit(0))
   345  
   346  		// Grepping the output (in addition to unit tests)
   347  		Expect(session.OutputToString()).To(ContainSubstring(" -d "))
   348  	})
   349  
   350  	It("podman generate systemd --new with explicit detaching param in middle", func() {
   351  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   352  		n.WaitWithDefaultTimeout()
   353  		Expect(n).Should(Exit(0))
   354  
   355  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "--new", "foo"})
   356  		session.WaitWithDefaultTimeout()
   357  		Expect(session).Should(Exit(0))
   358  
   359  		// Grepping the output (in addition to unit tests)
   360  		Expect(session.OutputToString()).To(ContainSubstring("--name foo alpine top"))
   361  	})
   362  
   363  	It("podman generate systemd --new pod", func() {
   364  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   365  		n.WaitWithDefaultTimeout()
   366  		Expect(n).Should(Exit(0))
   367  
   368  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "--new", "foo"})
   369  		session.WaitWithDefaultTimeout()
   370  		Expect(session).Should(Exit(0))
   371  		Expect(session.OutputToString()).To(ContainSubstring(" pod create "))
   372  	})
   373  
   374  	It("podman generate systemd --restart-sec 15 --name foo", func() {
   375  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   376  		n.WaitWithDefaultTimeout()
   377  		Expect(n).Should(Exit(0))
   378  
   379  		session := podmanTest.Podman([]string{"generate", "systemd", "--restart-sec", "15", "--name", "foo"})
   380  		session.WaitWithDefaultTimeout()
   381  		Expect(session).Should(Exit(0))
   382  
   383  		// Grepping the output (in addition to unit tests)
   384  		Expect(session.OutputToString()).To(ContainSubstring("RestartSec=15"))
   385  
   386  		n = podmanTest.Podman([]string{"create", "--name", "foocontainer", "alpine", "top"})
   387  		n.WaitWithDefaultTimeout()
   388  		Expect(n).Should(Exit(0))
   389  
   390  		session2 := podmanTest.Podman([]string{"generate", "systemd", "--restart-sec", "15", "--name", "foocontainer"})
   391  		session2.WaitWithDefaultTimeout()
   392  		Expect(session2).Should(Exit(0))
   393  		Expect(session2.OutputToString()).To(ContainSubstring("RestartSec=15"))
   394  	})
   395  
   396  	It("podman generate systemd --new=false pod", func() {
   397  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   398  		n.WaitWithDefaultTimeout()
   399  		Expect(n).Should(Exit(0))
   400  
   401  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "--new=false", "foo"})
   402  		session.WaitWithDefaultTimeout()
   403  		Expect(session).Should(Exit(0))
   404  		Expect(session.OutputToString()).NotTo(ContainSubstring(" pod create "))
   405  	})
   406  
   407  	It("podman generate systemd --new=true pod", func() {
   408  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   409  		n.WaitWithDefaultTimeout()
   410  		Expect(n).Should(Exit(0))
   411  
   412  		session := podmanTest.Podman([]string{"generate", "systemd", "--time", "42", "--name", "--new=true", "foo"})
   413  		session.WaitWithDefaultTimeout()
   414  		Expect(session).Should(Exit(0))
   415  		Expect(session.OutputToString()).To(ContainSubstring(" pod create "))
   416  	})
   417  
   418  	It("podman generate systemd --container-prefix con", func() {
   419  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   420  		n.WaitWithDefaultTimeout()
   421  		Expect(n).Should(Exit(0))
   422  
   423  		session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--container-prefix", "con", "foo"})
   424  		session.WaitWithDefaultTimeout()
   425  		Expect(session).Should(Exit(0))
   426  
   427  		// Grepping the output (in addition to unit tests)
   428  		Expect(session.OutputToString()).To(ContainSubstring("# con-foo.service"))
   429  
   430  	})
   431  
   432  	It("podman generate systemd --container-prefix ''", func() {
   433  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   434  		n.WaitWithDefaultTimeout()
   435  		Expect(n).Should(Exit(0))
   436  
   437  		session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--container-prefix", "", "foo"})
   438  		session.WaitWithDefaultTimeout()
   439  		Expect(session).Should(Exit(0))
   440  
   441  		// Grepping the output (in addition to unit tests)
   442  		Expect(session.OutputToString()).To(ContainSubstring("# foo.service"))
   443  
   444  	})
   445  
   446  	It("podman generate systemd --separator _", func() {
   447  		n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"})
   448  		n.WaitWithDefaultTimeout()
   449  		Expect(n).Should(Exit(0))
   450  
   451  		session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--separator", "_", "foo"})
   452  		session.WaitWithDefaultTimeout()
   453  		Expect(session).Should(Exit(0))
   454  
   455  		// Grepping the output (in addition to unit tests)
   456  		Expect(session.OutputToString()).To(ContainSubstring("# container_foo.service"))
   457  	})
   458  
   459  	It("podman generate systemd pod --pod-prefix p", func() {
   460  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   461  		n.WaitWithDefaultTimeout()
   462  		Expect(n).Should(Exit(0))
   463  
   464  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   465  		n.WaitWithDefaultTimeout()
   466  		Expect(n).Should(Exit(0))
   467  
   468  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"})
   469  		n.WaitWithDefaultTimeout()
   470  		Expect(n).Should(Exit(0))
   471  
   472  		session := podmanTest.Podman([]string{"generate", "systemd", "--pod-prefix", "p", "--name", "foo"})
   473  		session.WaitWithDefaultTimeout()
   474  		Expect(session).Should(Exit(0))
   475  
   476  		// Grepping the output (in addition to unit tests)
   477  		Expect(session.OutputToString()).To(ContainSubstring("# p-foo.service"))
   478  		Expect(session.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
   479  		Expect(session.OutputToString()).To(ContainSubstring("# container-foo-1.service"))
   480  		Expect(session.OutputToString()).To(ContainSubstring("BindsTo=p-foo.service"))
   481  	})
   482  
   483  	It("podman generate systemd pod --pod-prefix p --container-prefix con --separator _ change all prefixes/separator", func() {
   484  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   485  		n.WaitWithDefaultTimeout()
   486  		Expect(n).Should(Exit(0))
   487  
   488  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   489  		n.WaitWithDefaultTimeout()
   490  		Expect(n).Should(Exit(0))
   491  
   492  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"})
   493  		n.WaitWithDefaultTimeout()
   494  		Expect(n).Should(Exit(0))
   495  
   496  		session := podmanTest.Podman([]string{"generate", "systemd", "--container-prefix", "con", "--pod-prefix", "p", "--separator", "_", "--name", "foo"})
   497  		session.WaitWithDefaultTimeout()
   498  		Expect(session).Should(Exit(0))
   499  
   500  		// Grepping the output (in addition to unit tests)
   501  		Expect(session.OutputToString()).To(ContainSubstring("# p_foo.service"))
   502  		Expect(session.OutputToString()).To(ContainSubstring("Wants=con_foo-1.service con_foo-2.service"))
   503  		Expect(session.OutputToString()).To(ContainSubstring("# con_foo-1.service"))
   504  		Expect(session.OutputToString()).To(ContainSubstring("# con_foo-2.service"))
   505  		Expect(session.OutputToString()).To(ContainSubstring("BindsTo=p_foo.service"))
   506  	})
   507  
   508  	It("podman generate systemd pod --pod-prefix '' --container-prefix '' --separator _ change all prefixes/separator", func() {
   509  		n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
   510  		n.WaitWithDefaultTimeout()
   511  		Expect(n).Should(Exit(0))
   512  
   513  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   514  		n.WaitWithDefaultTimeout()
   515  		Expect(n).Should(Exit(0))
   516  
   517  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"})
   518  		n.WaitWithDefaultTimeout()
   519  		Expect(n).Should(Exit(0))
   520  
   521  		// test systemd generate with empty pod prefix
   522  		session1 := podmanTest.Podman([]string{"generate", "systemd", "--pod-prefix", "", "--name", "foo"})
   523  		session1.WaitWithDefaultTimeout()
   524  		Expect(session1).Should(Exit(0))
   525  
   526  		// Grepping the output (in addition to unit tests)
   527  		Expect(session1.OutputToString()).To(ContainSubstring("# foo.service"))
   528  		Expect(session1.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
   529  		Expect(session1.OutputToString()).To(ContainSubstring("# container-foo-1.service"))
   530  		Expect(session1.OutputToString()).To(ContainSubstring("BindsTo=foo.service"))
   531  
   532  		// test systemd generate with empty container and pod prefix
   533  		session2 := podmanTest.Podman([]string{"generate", "systemd", "--container-prefix", "", "--pod-prefix", "", "--separator", "_", "--name", "foo"})
   534  		session2.WaitWithDefaultTimeout()
   535  		Expect(session2).Should(Exit(0))
   536  
   537  		// Grepping the output (in addition to unit tests)
   538  		Expect(session2.OutputToString()).To(ContainSubstring("# foo.service"))
   539  		Expect(session2.OutputToString()).To(ContainSubstring("Wants=foo-1.service foo-2.service"))
   540  		Expect(session2.OutputToString()).To(ContainSubstring("# foo-1.service"))
   541  		Expect(session2.OutputToString()).To(ContainSubstring("# foo-2.service"))
   542  		Expect(session2.OutputToString()).To(ContainSubstring("BindsTo=foo.service"))
   543  
   544  	})
   545  
   546  	It("podman generate systemd pod with containers --new", func() {
   547  		tmpDir := GinkgoT().TempDir()
   548  		tmpFile := tmpDir + "podID"
   549  
   550  		n := podmanTest.Podman([]string{"pod", "create", "--pod-id-file", tmpFile, "--name", "foo"})
   551  		n.WaitWithDefaultTimeout()
   552  		Expect(n).Should(Exit(0))
   553  
   554  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"})
   555  		n.WaitWithDefaultTimeout()
   556  		Expect(n).Should(Exit(0))
   557  
   558  		n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"})
   559  		n.WaitWithDefaultTimeout()
   560  		Expect(n).Should(Exit(0))
   561  
   562  		session := podmanTest.Podman([]string{"generate", "systemd", "--new", "--name", "foo"})
   563  		session.WaitWithDefaultTimeout()
   564  		Expect(session).Should(Exit(0))
   565  
   566  		// Grepping the output (in addition to unit tests)
   567  		Expect(session.OutputToString()).To(ContainSubstring("# pod-foo.service"))
   568  		Expect(session.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
   569  		Expect(session.OutputToString()).To(ContainSubstring("BindsTo=pod-foo.service"))
   570  		Expect(session.OutputToString()).To(ContainSubstring("pod create"))
   571  		Expect(session.OutputToString()).To(ContainSubstring("--infra-conmon-pidfile %t/pod-foo.pid"))
   572  		Expect(session.OutputToString()).To(ContainSubstring("--pod-id-file %t/pod-foo.pod-id"))
   573  		Expect(session.OutputToString()).To(ContainSubstring("--exit-policy=stop"))
   574  		Expect(session.OutputToString()).To(ContainSubstring("--name foo"))
   575  		Expect(session.OutputToString()).To(ContainSubstring("pod stop"))
   576  		Expect(session.OutputToString()).To(ContainSubstring("--ignore"))
   577  		Expect(session.OutputToString()).To(ContainSubstring("--pod-id-file %t/pod-foo.pod-id"))
   578  		Expect(session.OutputToString()).To(ContainSubstring("-t 10"))
   579  		Expect(session.OutputToString()).To(ContainSubstring("pod rm"))
   580  		Expect(session.OutputToString()).To(ContainSubstring("--ignore"))
   581  		Expect(session.OutputToString()).To(ContainSubstring("-f"))
   582  		Expect(session.OutputToString()).To(ContainSubstring("--pod-id-file %t/pod-foo.pod-id"))
   583  	})
   584  
   585  	It("podman generate systemd --format json", func() {
   586  		n := podmanTest.Podman([]string{"create", "--name", "foo", ALPINE})
   587  		n.WaitWithDefaultTimeout()
   588  		Expect(n).Should(Exit(0))
   589  
   590  		session := podmanTest.Podman([]string{"generate", "systemd", "--format", "json", "foo"})
   591  		session.WaitWithDefaultTimeout()
   592  		Expect(session).Should(Exit(0))
   593  		Expect(session.OutputToString()).To(BeValidJSON())
   594  	})
   595  
   596  	It("podman generate systemd --new create command with double curly braces", func() {
   597  		SkipIfInContainer("journald inside a container doesn't work")
   598  		// Regression test for #9034
   599  		session := podmanTest.Podman([]string{"create", "--name", "foo", "--log-driver=journald", "--log-opt=tag={{.Name}}", ALPINE})
   600  		session.WaitWithDefaultTimeout()
   601  		Expect(session).Should(Exit(0))
   602  
   603  		session = podmanTest.Podman([]string{"generate", "systemd", "--new", "foo"})
   604  		session.WaitWithDefaultTimeout()
   605  		Expect(session).Should(Exit(0))
   606  		Expect(session.OutputToString()).To(ContainSubstring(" --log-opt=tag={{.Name}} "))
   607  
   608  		session = podmanTest.Podman([]string{"pod", "create", "--name", "pod", "--label", "key={{someval}}"})
   609  		session.WaitWithDefaultTimeout()
   610  		Expect(session).Should(Exit(0))
   611  
   612  		session = podmanTest.Podman([]string{"generate", "systemd", "--new", "pod"})
   613  		session.WaitWithDefaultTimeout()
   614  		Expect(session).Should(Exit(0))
   615  		Expect(session.OutputToString()).To(ContainSubstring(" --label key={{someval}}"))
   616  	})
   617  
   618  	It("podman generate systemd --env", func() {
   619  		session := podmanTest.RunTopContainer("test")
   620  		session.WaitWithDefaultTimeout()
   621  		Expect(session).Should(Exit(0))
   622  
   623  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "foo=bar", "-e", "hoge=fuga", "test"})
   624  		session.WaitWithDefaultTimeout()
   625  		Expect(session).Should(Exit(0))
   626  		Expect(session.OutputToString()).To(ContainSubstring("Environment=foo=bar"))
   627  		Expect(session.OutputToString()).To(ContainSubstring("Environment=hoge=fuga"))
   628  
   629  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "=bar", "-e", "hoge=fuga", "test"})
   630  		session.WaitWithDefaultTimeout()
   631  		Expect(session).Should(Exit(125))
   632  		Expect(session.ErrorToString()).To(ContainSubstring("invalid variable"))
   633  
   634  		// Use -e/--env option with --new option
   635  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "foo=bar", "-e", "hoge=fuga", "--new", "test"})
   636  		session.WaitWithDefaultTimeout()
   637  		Expect(session).Should(Exit(0))
   638  		Expect(session.OutputToString()).To(ContainSubstring("Environment=foo=bar"))
   639  		Expect(session.OutputToString()).To(ContainSubstring("Environment=hoge=fuga"))
   640  
   641  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "foo=bar", "-e", "=fuga", "--new", "test"})
   642  		session.WaitWithDefaultTimeout()
   643  		Expect(session).Should(Exit(125))
   644  		Expect(session.ErrorToString()).To(ContainSubstring("invalid variable"))
   645  
   646  		// Escape systemd arguments
   647  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "BAR=my test", "-e", "USER=%a", "test"})
   648  		session.WaitWithDefaultTimeout()
   649  		Expect(session).Should(Exit(0))
   650  		Expect(session.OutputToString()).To(ContainSubstring("\"BAR=my test\""))
   651  		Expect(session.OutputToString()).To(ContainSubstring("USER=%%a"))
   652  
   653  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "BAR=my test", "-e", "USER=%a", "--new", "test"})
   654  		session.WaitWithDefaultTimeout()
   655  		Expect(session).Should(Exit(0))
   656  		Expect(session.OutputToString()).To(ContainSubstring("\"BAR=my test\""))
   657  		Expect(session.OutputToString()).To(ContainSubstring("USER=%%a"))
   658  
   659  		// Specify the environment variables without a value
   660  		os.Setenv("FOO1", "BAR1")
   661  		os.Setenv("FOO2", "BAR2")
   662  		os.Setenv("FOO3", "BAR3")
   663  		defer os.Unsetenv("FOO1")
   664  		defer os.Unsetenv("FOO2")
   665  		defer os.Unsetenv("FOO3")
   666  
   667  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "FOO1", "test"})
   668  		session.WaitWithDefaultTimeout()
   669  		Expect(session).Should(Exit(0))
   670  		Expect(session.OutputToString()).To(ContainSubstring("BAR1"))
   671  		Expect(session.OutputToString()).NotTo(ContainSubstring("BAR2"))
   672  		Expect(session.OutputToString()).NotTo(ContainSubstring("BAR3"))
   673  
   674  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "FOO*", "test"})
   675  		session.WaitWithDefaultTimeout()
   676  		Expect(session).Should(Exit(0))
   677  		Expect(session.OutputToString()).To(ContainSubstring("BAR1"))
   678  		Expect(session.OutputToString()).To(ContainSubstring("BAR2"))
   679  		Expect(session.OutputToString()).To(ContainSubstring("BAR3"))
   680  
   681  		session = podmanTest.Podman([]string{"generate", "systemd", "--env", "FOO*", "--new", "test"})
   682  		session.WaitWithDefaultTimeout()
   683  		Expect(session).Should(Exit(0))
   684  		Expect(session.OutputToString()).To(ContainSubstring("BAR1"))
   685  		Expect(session.OutputToString()).To(ContainSubstring("BAR2"))
   686  		Expect(session.OutputToString()).To(ContainSubstring("BAR3"))
   687  	})
   688  })