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

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