github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/generate_kube_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/containers/podman/v3/libpod/define"
    11  
    12  	"github.com/containers/podman/v3/pkg/util"
    13  	. "github.com/containers/podman/v3/test/utils"
    14  	"github.com/ghodss/yaml"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gexec"
    18  	v1 "k8s.io/api/core/v1"
    19  )
    20  
    21  var _ = Describe("Podman generate kube", func() {
    22  	var (
    23  		tempdir    string
    24  		err        error
    25  		podmanTest *PodmanTestIntegration
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		tempdir, err = CreateTempDirInTempDir()
    30  		if err != nil {
    31  			os.Exit(1)
    32  		}
    33  		podmanTest = PodmanTestCreate(tempdir)
    34  		podmanTest.Setup()
    35  		podmanTest.SeedImages()
    36  	})
    37  
    38  	AfterEach(func() {
    39  		podmanTest.Cleanup()
    40  		f := CurrentGinkgoTestDescription()
    41  		processTestResult(f)
    42  
    43  	})
    44  
    45  	It("podman generate pod kube on bogus object", func() {
    46  		session := podmanTest.Podman([]string{"generate", "kube", "foobar"})
    47  		session.WaitWithDefaultTimeout()
    48  		Expect(session).To(ExitWithError())
    49  	})
    50  
    51  	It("podman generate service kube on bogus object", func() {
    52  		session := podmanTest.Podman([]string{"generate", "kube", "-s", "foobar"})
    53  		session.WaitWithDefaultTimeout()
    54  		Expect(session).To(ExitWithError())
    55  	})
    56  
    57  	It("podman generate kube on container", func() {
    58  		session := podmanTest.RunTopContainer("top")
    59  		session.WaitWithDefaultTimeout()
    60  		Expect(session).Should(Exit(0))
    61  
    62  		kube := podmanTest.Podman([]string{"generate", "kube", "top"})
    63  		kube.WaitWithDefaultTimeout()
    64  		Expect(kube).Should(Exit(0))
    65  
    66  		pod := new(v1.Pod)
    67  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
    68  		Expect(err).To(BeNil())
    69  		Expect(pod.Spec.HostNetwork).To(Equal(false))
    70  		Expect(pod.Spec.SecurityContext).To(BeNil())
    71  		Expect(pod.Spec.DNSConfig).To(BeNil())
    72  		Expect(pod.Spec.Containers[0].WorkingDir).To(Equal(""))
    73  		Expect(pod.Spec.Containers[0].Env).To(BeNil())
    74  
    75  		numContainers := 0
    76  		for range pod.Spec.Containers {
    77  			numContainers = numContainers + 1
    78  		}
    79  		Expect(numContainers).To(Equal(1))
    80  	})
    81  
    82  	It("podman generate service kube on container with --security-opt level", func() {
    83  		session := podmanTest.Podman([]string{"create", "--name", "test", "--security-opt", "label=level:s0:c100,c200", "alpine"})
    84  		session.WaitWithDefaultTimeout()
    85  		Expect(session).Should(Exit(0))
    86  
    87  		kube := podmanTest.Podman([]string{"generate", "kube", "test"})
    88  		kube.WaitWithDefaultTimeout()
    89  		Expect(kube).Should(Exit(0))
    90  
    91  		pod := new(v1.Pod)
    92  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
    93  		Expect(err).To(BeNil())
    94  		Expect(kube.OutputToString()).To(ContainSubstring("level: s0:c100,c200"))
    95  	})
    96  
    97  	It("podman generate service kube on container with --security-opt disable", func() {
    98  		session := podmanTest.Podman([]string{"create", "--name", "test-disable", "--security-opt", "label=disable", "alpine"})
    99  		session.WaitWithDefaultTimeout()
   100  		Expect(session).Should(Exit(0))
   101  
   102  		kube := podmanTest.Podman([]string{"generate", "kube", "test-disable"})
   103  		kube.WaitWithDefaultTimeout()
   104  		Expect(kube).Should(Exit(0))
   105  
   106  		pod := new(v1.Pod)
   107  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   108  		Expect(err).To(BeNil())
   109  		Expect(kube.OutputToString()).To(ContainSubstring("type: spc_t"))
   110  
   111  	})
   112  
   113  	It("podman generate service kube on container with --security-opt type", func() {
   114  		session := podmanTest.Podman([]string{"create", "--name", "test", "--security-opt", "label=type:foo_bar_t", "alpine"})
   115  		session.WaitWithDefaultTimeout()
   116  		Expect(session).Should(Exit(0))
   117  
   118  		kube := podmanTest.Podman([]string{"generate", "kube", "test"})
   119  		kube.WaitWithDefaultTimeout()
   120  		Expect(kube).Should(Exit(0))
   121  
   122  		pod := new(v1.Pod)
   123  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   124  		Expect(err).To(BeNil())
   125  		Expect(kube.OutputToString()).To(ContainSubstring("type: foo_bar_t"))
   126  	})
   127  
   128  	It("podman generate service kube on container - targetPort should match port name", func() {
   129  		session := podmanTest.Podman([]string{"create", "--name", "test-ctr", "-p", "3890:3890", ALPINE, "ls"})
   130  		session.WaitWithDefaultTimeout()
   131  		Expect(session).Should(Exit(0))
   132  
   133  		kube := podmanTest.Podman([]string{"generate", "kube", "-s", "test-ctr"})
   134  		kube.WaitWithDefaultTimeout()
   135  		Expect(kube).Should(Exit(0))
   136  
   137  		// Separate out the Service and Pod yaml
   138  		arr := strings.Split(string(kube.Out.Contents()), "---")
   139  		Expect(len(arr)).To(Equal(2))
   140  
   141  		svc := new(v1.Service)
   142  		err := yaml.Unmarshal([]byte(arr[0]), svc)
   143  		Expect(err).To(BeNil())
   144  		Expect(len(svc.Spec.Ports)).To(Equal(1))
   145  		Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(3890))
   146  
   147  		pod := new(v1.Pod)
   148  		err = yaml.Unmarshal([]byte(arr[1]), pod)
   149  		Expect(err).To(BeNil())
   150  	})
   151  
   152  	It("podman generate kube on pod", func() {
   153  		_, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {"toppod"}})
   154  		Expect(rc).To(Equal(0))
   155  
   156  		session := podmanTest.RunTopContainerInPod("topcontainer", "toppod")
   157  		session.WaitWithDefaultTimeout()
   158  		Expect(session).Should(Exit(0))
   159  
   160  		kube := podmanTest.Podman([]string{"generate", "kube", "toppod"})
   161  		kube.WaitWithDefaultTimeout()
   162  		Expect(kube).Should(Exit(0))
   163  
   164  		pod := new(v1.Pod)
   165  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   166  		Expect(err).To(BeNil())
   167  		Expect(pod.Spec.HostNetwork).To(Equal(false))
   168  
   169  		numContainers := 0
   170  		for range pod.Spec.Containers {
   171  			numContainers = numContainers + 1
   172  		}
   173  		Expect(numContainers).To(Equal(1))
   174  	})
   175  
   176  	It("podman generate kube multiple pods", func() {
   177  		pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", ALPINE, "top"})
   178  		pod1.WaitWithDefaultTimeout()
   179  		Expect(pod1).Should(Exit(0))
   180  
   181  		pod2 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod2", ALPINE, "top"})
   182  		pod2.WaitWithDefaultTimeout()
   183  		Expect(pod2).Should(Exit(0))
   184  
   185  		kube := podmanTest.Podman([]string{"generate", "kube", "pod1", "pod2"})
   186  		kube.WaitWithDefaultTimeout()
   187  		Expect(kube).Should(Exit(0))
   188  
   189  		Expect(string(kube.Out.Contents())).To(ContainSubstring(`name: pod1`))
   190  		Expect(string(kube.Out.Contents())).To(ContainSubstring(`name: pod2`))
   191  	})
   192  
   193  	It("podman generate kube on pod with init containers", func() {
   194  		session := podmanTest.Podman([]string{"create", "--pod", "new:toppod", "--init-ctr", "always", ALPINE, "echo", "hello"})
   195  		session.WaitWithDefaultTimeout()
   196  		Expect(session).Should(Exit(0))
   197  
   198  		session = podmanTest.Podman([]string{"create", "--pod", "toppod", "--init-ctr", "always", ALPINE, "echo", "world"})
   199  		session.WaitWithDefaultTimeout()
   200  		Expect(session).Should(Exit(0))
   201  
   202  		session = podmanTest.Podman([]string{"create", "--pod", "toppod", ALPINE, "top"})
   203  		session.WaitWithDefaultTimeout()
   204  		Expect(session).Should(Exit(0))
   205  
   206  		kube := podmanTest.Podman([]string{"generate", "kube", "toppod"})
   207  		kube.WaitWithDefaultTimeout()
   208  		Expect(kube).Should(Exit(0))
   209  
   210  		pod := new(v1.Pod)
   211  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   212  		Expect(err).To(BeNil())
   213  		Expect(pod.Spec.HostNetwork).To(Equal(false))
   214  
   215  		numContainers := len(pod.Spec.Containers) + len(pod.Spec.InitContainers)
   216  		Expect(numContainers).To(Equal(3))
   217  
   218  		// Init container should be in the generated kube yaml if created with "once" type and the pod has not been started
   219  		session = podmanTest.Podman([]string{"create", "--pod", "new:toppod-2", "--init-ctr", "once", ALPINE, "echo", "using once type"})
   220  		session.WaitWithDefaultTimeout()
   221  		Expect(session).Should(Exit(0))
   222  
   223  		session = podmanTest.Podman([]string{"create", "--pod", "toppod-2", ALPINE, "top"})
   224  		session.WaitWithDefaultTimeout()
   225  		Expect(session).Should(Exit(0))
   226  
   227  		kube = podmanTest.Podman([]string{"generate", "kube", "toppod-2"})
   228  		kube.WaitWithDefaultTimeout()
   229  		Expect(kube).Should(Exit(0))
   230  
   231  		pod = new(v1.Pod)
   232  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   233  		Expect(err).To(BeNil())
   234  		Expect(pod.Spec.HostNetwork).To(Equal(false))
   235  
   236  		numContainers = len(pod.Spec.Containers) + len(pod.Spec.InitContainers)
   237  		Expect(numContainers).To(Equal(2))
   238  
   239  		// Init container should not be in the generated kube yaml if created with "once" type and the pod has been started
   240  		session = podmanTest.Podman([]string{"create", "--pod", "new:toppod-3", "--init-ctr", "once", ALPINE, "echo", "using once type"})
   241  		session.WaitWithDefaultTimeout()
   242  		Expect(session).Should(Exit(0))
   243  
   244  		session = podmanTest.Podman([]string{"create", "--pod", "toppod-3", ALPINE, "top"})
   245  		session.WaitWithDefaultTimeout()
   246  		Expect(session).Should(Exit(0))
   247  
   248  		session = podmanTest.Podman([]string{"pod", "start", "toppod-3"})
   249  		session.WaitWithDefaultTimeout()
   250  		Expect(session).Should(Exit(0))
   251  
   252  		kube = podmanTest.Podman([]string{"generate", "kube", "toppod-3"})
   253  		kube.WaitWithDefaultTimeout()
   254  		Expect(kube).Should(Exit(0))
   255  
   256  		pod = new(v1.Pod)
   257  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   258  		Expect(err).To(BeNil())
   259  		Expect(pod.Spec.HostNetwork).To(Equal(false))
   260  
   261  		numContainers = len(pod.Spec.Containers) + len(pod.Spec.InitContainers)
   262  		Expect(numContainers).To(Equal(1))
   263  	})
   264  
   265  	It("podman generate kube on pod with host network", func() {
   266  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", "testHostNetwork", "--network", "host"})
   267  		podSession.WaitWithDefaultTimeout()
   268  		Expect(podSession).Should(Exit(0))
   269  
   270  		session := podmanTest.Podman([]string{"create", "--name", "topcontainer", "--pod", "testHostNetwork", "--network", "host", ALPINE, "top"})
   271  		session.WaitWithDefaultTimeout()
   272  		Expect(session).Should(Exit(0))
   273  
   274  		kube := podmanTest.Podman([]string{"generate", "kube", "testHostNetwork"})
   275  		kube.WaitWithDefaultTimeout()
   276  		Expect(kube).Should(Exit(0))
   277  
   278  		pod := new(v1.Pod)
   279  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   280  		Expect(err).To(BeNil())
   281  		Expect(pod.Spec.HostNetwork).To(Equal(true))
   282  	})
   283  
   284  	It("podman generate kube on container with host network", func() {
   285  		session := podmanTest.RunTopContainerWithArgs("topcontainer", []string{"--network", "host"})
   286  		session.WaitWithDefaultTimeout()
   287  		Expect(session).Should(Exit(0))
   288  
   289  		kube := podmanTest.Podman([]string{"generate", "kube", "topcontainer"})
   290  		kube.WaitWithDefaultTimeout()
   291  		Expect(kube).Should(Exit(0))
   292  
   293  		pod := new(v1.Pod)
   294  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   295  		Expect(err).To(BeNil())
   296  		Expect(pod.Spec.HostNetwork).To(Equal(true))
   297  	})
   298  
   299  	It("podman generate kube on pod with hostAliases", func() {
   300  		podName := "testHost"
   301  		testIP := "127.0.0.1"
   302  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName,
   303  			"--add-host", "test1.podman.io" + ":" + testIP,
   304  			"--add-host", "test2.podman.io" + ":" + testIP,
   305  		})
   306  		podSession.WaitWithDefaultTimeout()
   307  		Expect(podSession).Should(Exit(0))
   308  
   309  		ctr1Name := "ctr1"
   310  		ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, ALPINE, "top"})
   311  		ctr1Session.WaitWithDefaultTimeout()
   312  		Expect(ctr1Session).Should(Exit(0))
   313  
   314  		ctr2Name := "ctr2"
   315  		ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, ALPINE, "top"})
   316  		ctr2Session.WaitWithDefaultTimeout()
   317  		Expect(ctr2Session).Should(Exit(0))
   318  
   319  		kube := podmanTest.Podman([]string{"generate", "kube", podName})
   320  		kube.WaitWithDefaultTimeout()
   321  		Expect(kube).Should(Exit(0))
   322  
   323  		pod := new(v1.Pod)
   324  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   325  		Expect(err).To(BeNil())
   326  		Expect(len(pod.Spec.HostAliases)).To(Equal(2))
   327  		Expect(pod.Spec.HostAliases[0].IP).To(Equal(testIP))
   328  		Expect(pod.Spec.HostAliases[1].IP).To(Equal(testIP))
   329  	})
   330  
   331  	It("podman generate service kube on pod", func() {
   332  		session := podmanTest.Podman([]string{"create", "--pod", "new:test-pod", "-p", "4000:4000/udp", ALPINE, "ls"})
   333  		session.WaitWithDefaultTimeout()
   334  		Expect(session).Should(Exit(0))
   335  
   336  		kube := podmanTest.Podman([]string{"generate", "kube", "-s", "test-pod"})
   337  		kube.WaitWithDefaultTimeout()
   338  		Expect(kube).Should(Exit(0))
   339  
   340  		// Separate out the Service and Pod yaml
   341  		arr := strings.Split(string(kube.Out.Contents()), "---")
   342  		Expect(len(arr)).To(Equal(2))
   343  
   344  		svc := new(v1.Service)
   345  		err := yaml.Unmarshal([]byte(arr[0]), svc)
   346  		Expect(err).To(BeNil())
   347  		Expect(len(svc.Spec.Ports)).To(Equal(1))
   348  		Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(4000))
   349  		Expect(svc.Spec.Ports[0].Protocol).To(Equal(v1.ProtocolUDP))
   350  
   351  		pod := new(v1.Pod)
   352  		err = yaml.Unmarshal([]byte(arr[1]), pod)
   353  		Expect(err).To(BeNil())
   354  	})
   355  
   356  	It("podman generate kube on pod with restartPolicy", func() {
   357  		// podName,  set,  expect
   358  		testSli := [][]string{
   359  			{"testPod1", "", "Never"}, // some pod create from cmdline, so set it to Never
   360  			{"testPod2", "always", "Always"},
   361  			{"testPod3", "on-failure", "OnFailure"},
   362  			{"testPod4", "no", "Never"},
   363  		}
   364  
   365  		for k, v := range testSli {
   366  			podName := v[0]
   367  			podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName})
   368  			podSession.WaitWithDefaultTimeout()
   369  			Expect(podSession).Should(Exit(0))
   370  
   371  			ctrName := "ctr" + strconv.Itoa(k)
   372  			ctr1Session := podmanTest.Podman([]string{"create", "--name", ctrName, "--pod", podName,
   373  				"--restart", v[1], ALPINE, "top"})
   374  			ctr1Session.WaitWithDefaultTimeout()
   375  			Expect(ctr1Session).Should(Exit(0))
   376  
   377  			kube := podmanTest.Podman([]string{"generate", "kube", podName})
   378  			kube.WaitWithDefaultTimeout()
   379  			Expect(kube).Should(Exit(0))
   380  
   381  			pod := new(v1.Pod)
   382  			err := yaml.Unmarshal(kube.Out.Contents(), pod)
   383  			Expect(err).To(BeNil())
   384  
   385  			Expect(string(pod.Spec.RestartPolicy)).To(Equal(v[2]))
   386  		}
   387  	})
   388  
   389  	It("podman generate kube on pod with memory limit", func() {
   390  		podName := "testMemoryLimit"
   391  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName})
   392  		podSession.WaitWithDefaultTimeout()
   393  		Expect(podSession).Should(Exit(0))
   394  
   395  		ctr1Name := "ctr1"
   396  		ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, "--memory", "10Mi", ALPINE, "top"})
   397  		ctr1Session.WaitWithDefaultTimeout()
   398  		Expect(ctr1Session).Should(Exit(0))
   399  
   400  		kube := podmanTest.Podman([]string{"generate", "kube", podName})
   401  		kube.WaitWithDefaultTimeout()
   402  		Expect(kube).Should(Exit(0))
   403  
   404  		pod := new(v1.Pod)
   405  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   406  		Expect(err).To(BeNil())
   407  
   408  		for _, ctr := range pod.Spec.Containers {
   409  			memoryLimit, _ := ctr.Resources.Limits.Memory().AsInt64()
   410  			Expect(memoryLimit).To(Equal(int64(10 * 1024 * 1024)))
   411  		}
   412  	})
   413  
   414  	It("podman generate kube on pod with cpu limit", func() {
   415  		podName := "testCpuLimit"
   416  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName})
   417  		podSession.WaitWithDefaultTimeout()
   418  		Expect(podSession).Should(Exit(0))
   419  
   420  		ctr1Name := "ctr1"
   421  		ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName,
   422  			"--cpus", "0.5", ALPINE, "top"})
   423  		ctr1Session.WaitWithDefaultTimeout()
   424  		Expect(ctr1Session).Should(Exit(0))
   425  
   426  		ctr2Name := "ctr2"
   427  		ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName,
   428  			"--cpu-period", "100000", "--cpu-quota", "50000", ALPINE, "top"})
   429  		ctr2Session.WaitWithDefaultTimeout()
   430  		Expect(ctr2Session).Should(Exit(0))
   431  
   432  		kube := podmanTest.Podman([]string{"generate", "kube", podName})
   433  		kube.WaitWithDefaultTimeout()
   434  		Expect(kube).Should(Exit(0))
   435  
   436  		pod := new(v1.Pod)
   437  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   438  		Expect(err).To(BeNil())
   439  
   440  		for _, ctr := range pod.Spec.Containers {
   441  			cpuLimit := ctr.Resources.Limits.Cpu().MilliValue()
   442  			Expect(cpuLimit).To(Equal(int64(500)))
   443  		}
   444  	})
   445  
   446  	It("podman generate kube on pod with ports", func() {
   447  		podName := "test"
   448  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, "-p", "4000:4000", "-p", "5000:5000"})
   449  		podSession.WaitWithDefaultTimeout()
   450  		Expect(podSession).Should(Exit(0))
   451  
   452  		ctr1Name := "ctr1"
   453  		ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, ALPINE, "top"})
   454  		ctr1Session.WaitWithDefaultTimeout()
   455  		Expect(ctr1Session).Should(Exit(0))
   456  
   457  		ctr2Name := "ctr2"
   458  		ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, ALPINE, "top"})
   459  		ctr2Session.WaitWithDefaultTimeout()
   460  		Expect(ctr2Session).Should(Exit(0))
   461  
   462  		kube := podmanTest.Podman([]string{"generate", "kube", podName})
   463  		kube.WaitWithDefaultTimeout()
   464  		Expect(kube).Should(Exit(0))
   465  
   466  		pod := new(v1.Pod)
   467  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   468  		Expect(err).To(BeNil())
   469  
   470  		foundPort4000 := 0
   471  		foundPort5000 := 0
   472  		foundOtherPort := 0
   473  		for _, ctr := range pod.Spec.Containers {
   474  			for _, port := range ctr.Ports {
   475  				// Since we are using tcp here, the generated kube yaml shouldn't
   476  				// have anything for protocol under the ports as tcp is the default
   477  				// for k8s
   478  				Expect(port.Protocol).To(BeEmpty())
   479  				if port.HostPort == 4000 {
   480  					foundPort4000 = foundPort4000 + 1
   481  				} else if port.HostPort == 5000 {
   482  					foundPort5000 = foundPort5000 + 1
   483  				} else {
   484  					foundOtherPort = foundOtherPort + 1
   485  				}
   486  			}
   487  		}
   488  		Expect(foundPort4000).To(Equal(1))
   489  		Expect(foundPort5000).To(Equal(1))
   490  		Expect(foundOtherPort).To(Equal(0))
   491  
   492  		// Create container with UDP port and check the generated kube yaml
   493  		ctrWithUDP := podmanTest.Podman([]string{"create", "--pod", "new:test-pod", "-p", "6666:66/udp", ALPINE, "top"})
   494  		ctrWithUDP.WaitWithDefaultTimeout()
   495  		Expect(ctrWithUDP).Should(Exit(0))
   496  
   497  		kube = podmanTest.Podman([]string{"generate", "kube", "test-pod"})
   498  		kube.WaitWithDefaultTimeout()
   499  		Expect(kube).Should(Exit(0))
   500  
   501  		pod = new(v1.Pod)
   502  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   503  		Expect(err).To(BeNil())
   504  
   505  		containers := pod.Spec.Containers
   506  		Expect(len(containers)).To(Equal(1))
   507  		Expect(len(containers[0].Ports)).To(Equal(1))
   508  		Expect(containers[0].Ports[0].Protocol).To(Equal(v1.ProtocolUDP))
   509  	})
   510  
   511  	It("podman generate and reimport kube on pod", func() {
   512  		podName := "toppod"
   513  		_, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {podName}})
   514  		Expect(rc).To(Equal(0))
   515  
   516  		session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", ALPINE, "top"})
   517  		session.WaitWithDefaultTimeout()
   518  		Expect(session).Should(Exit(0))
   519  
   520  		session2 := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test2", ALPINE, "top"})
   521  		session2.WaitWithDefaultTimeout()
   522  		Expect(session2).Should(Exit(0))
   523  
   524  		outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
   525  		kube := podmanTest.Podman([]string{"generate", "kube", "-f", outputFile, podName})
   526  		kube.WaitWithDefaultTimeout()
   527  		Expect(kube).Should(Exit(0))
   528  
   529  		session3 := podmanTest.Podman([]string{"pod", "rm", "-af"})
   530  		session3.WaitWithDefaultTimeout()
   531  		Expect(session3).Should(Exit(0))
   532  
   533  		session4 := podmanTest.Podman([]string{"play", "kube", outputFile})
   534  		session4.WaitWithDefaultTimeout()
   535  		Expect(session4).Should(Exit(0))
   536  
   537  		session5 := podmanTest.Podman([]string{"pod", "ps"})
   538  		session5.WaitWithDefaultTimeout()
   539  		Expect(session5).Should(Exit(0))
   540  		Expect(session5.OutputToString()).To(ContainSubstring(podName))
   541  
   542  		session6 := podmanTest.Podman([]string{"ps", "-a"})
   543  		session6.WaitWithDefaultTimeout()
   544  		Expect(session6).Should(Exit(0))
   545  		psOut := session6.OutputToString()
   546  		Expect(psOut).To(ContainSubstring("test1"))
   547  		Expect(psOut).To(ContainSubstring("test2"))
   548  	})
   549  
   550  	It("podman generate with user and reimport kube on pod", func() {
   551  		podName := "toppod"
   552  		_, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {podName}})
   553  		Expect(rc).To(Equal(0))
   554  
   555  		session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", "--user", "100:200", ALPINE, "top"})
   556  		session.WaitWithDefaultTimeout()
   557  		Expect(session).Should(Exit(0))
   558  
   559  		inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.Config.User}}", "test1"})
   560  		inspect.WaitWithDefaultTimeout()
   561  		Expect(inspect).Should(Exit(0))
   562  		Expect(inspect.OutputToString()).To(ContainSubstring("100:200"))
   563  
   564  		outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
   565  		kube := podmanTest.Podman([]string{"generate", "kube", "-f", outputFile, podName})
   566  		kube.WaitWithDefaultTimeout()
   567  		Expect(kube).Should(Exit(0))
   568  
   569  		session = podmanTest.Podman([]string{"pod", "rm", "-af"})
   570  		session.WaitWithDefaultTimeout()
   571  		Expect(session).Should(Exit(0))
   572  
   573  		podmanTest.AddImageToRWStore(ALPINE)
   574  		session = podmanTest.Podman([]string{"play", "kube", outputFile})
   575  		session.WaitWithDefaultTimeout()
   576  		Expect(session).Should(Exit(0))
   577  
   578  		// container name in pod is <podName>-<ctrName>
   579  		inspect1 := podmanTest.Podman([]string{"inspect", "--format", "{{.Config.User}}", "toppod-test1"})
   580  		inspect1.WaitWithDefaultTimeout()
   581  		Expect(inspect1).Should(Exit(0))
   582  		Expect(inspect1.OutputToString()).To(ContainSubstring(inspect.OutputToString()))
   583  	})
   584  
   585  	It("podman generate kube with volume", func() {
   586  		vol1 := filepath.Join(podmanTest.TempDir, "vol-test1")
   587  		err := os.MkdirAll(vol1, 0755)
   588  		Expect(err).To(BeNil())
   589  
   590  		// we need a container name because IDs don't persist after rm/play
   591  		ctrName := "test-ctr"
   592  		ctrNameInKubePod := "test1-test-ctr"
   593  
   594  		session1 := podmanTest.Podman([]string{"run", "-d", "--pod", "new:test1", "--name", ctrName, "-v", vol1 + ":/volume/:z", "alpine", "top"})
   595  		session1.WaitWithDefaultTimeout()
   596  		Expect(session1).Should(Exit(0))
   597  
   598  		outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
   599  		kube := podmanTest.Podman([]string{"generate", "kube", "test1", "-f", outputFile})
   600  		kube.WaitWithDefaultTimeout()
   601  		Expect(kube).Should(Exit(0))
   602  
   603  		b, err := ioutil.ReadFile(outputFile)
   604  		Expect(err).ShouldNot(HaveOccurred())
   605  		pod := new(v1.Pod)
   606  		err = yaml.Unmarshal(b, pod)
   607  		Expect(err).To(BeNil())
   608  		val, found := pod.Annotations[define.BindMountPrefix+vol1]
   609  		Expect(found).To(BeTrue())
   610  		Expect(val).To(HaveSuffix("z"))
   611  
   612  		rm := podmanTest.Podman([]string{"pod", "rm", "-f", "test1"})
   613  		rm.WaitWithDefaultTimeout()
   614  		Expect(rm).Should(Exit(0))
   615  
   616  		play := podmanTest.Podman([]string{"play", "kube", outputFile})
   617  		play.WaitWithDefaultTimeout()
   618  		Expect(play).Should(Exit(0))
   619  
   620  		inspect := podmanTest.Podman([]string{"inspect", ctrNameInKubePod})
   621  		inspect.WaitWithDefaultTimeout()
   622  		Expect(inspect).Should(Exit(0))
   623  		Expect(inspect.OutputToString()).To(ContainSubstring(vol1))
   624  	})
   625  
   626  	It("podman generate kube when bind-mounting '/' and '/root' at the same time ", func() {
   627  		// Fixes https://github.com/containers/podman/issues/9764
   628  
   629  		ctrName := "mount-root-ctr"
   630  		session1 := podmanTest.Podman([]string{"run", "-d", "--pod", "new:mount-root-conflict", "--name", ctrName,
   631  			"-v", "/:/volume1/",
   632  			"-v", "/root:/volume2/",
   633  			"alpine", "top"})
   634  		session1.WaitWithDefaultTimeout()
   635  		Expect(session1).Should(Exit(0))
   636  
   637  		kube := podmanTest.Podman([]string{"generate", "kube", "mount-root-conflict"})
   638  		kube.WaitWithDefaultTimeout()
   639  		Expect(kube).Should(Exit(0))
   640  
   641  		pod := new(v1.Pod)
   642  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   643  		Expect(err).To(BeNil())
   644  
   645  		Expect(len(pod.Spec.Volumes)).To(Equal(2))
   646  
   647  	})
   648  
   649  	It("podman generate kube with persistent volume claim", func() {
   650  		vol := "vol-test-persistent-volume-claim"
   651  
   652  		// we need a container name because IDs don't persist after rm/play
   653  		ctrName := "test-persistent-volume-claim"
   654  		ctrNameInKubePod := "test1-test-persistent-volume-claim"
   655  
   656  		session := podmanTest.Podman([]string{"run", "-d", "--pod", "new:test1", "--name", ctrName, "-v", vol + ":/volume/:z", "alpine", "top"})
   657  		session.WaitWithDefaultTimeout()
   658  		Expect(session).Should(Exit(0))
   659  
   660  		outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
   661  		kube := podmanTest.Podman([]string{"generate", "kube", "test1", "-f", outputFile})
   662  		kube.WaitWithDefaultTimeout()
   663  		Expect(kube).Should(Exit(0))
   664  
   665  		rm := podmanTest.Podman([]string{"pod", "rm", "-f", "test1"})
   666  		rm.WaitWithDefaultTimeout()
   667  		Expect(rm).Should(Exit(0))
   668  
   669  		play := podmanTest.Podman([]string{"play", "kube", outputFile})
   670  		play.WaitWithDefaultTimeout()
   671  		Expect(play).Should(Exit(0))
   672  
   673  		inspect := podmanTest.Podman([]string{"inspect", ctrNameInKubePod})
   674  		inspect.WaitWithDefaultTimeout()
   675  		Expect(inspect).Should(Exit(0))
   676  		Expect(inspect.OutputToString()).To(ContainSubstring(vol))
   677  	})
   678  
   679  	It("podman generate kube sharing pid namespace", func() {
   680  		podName := "test"
   681  		podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, "--share", "pid"})
   682  		podSession.WaitWithDefaultTimeout()
   683  		Expect(podSession).Should(Exit(0))
   684  
   685  		session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", ALPINE, "top"})
   686  		session.WaitWithDefaultTimeout()
   687  		Expect(session).Should(Exit(0))
   688  
   689  		outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
   690  		kube := podmanTest.Podman([]string{"generate", "kube", podName, "-f", outputFile})
   691  		kube.WaitWithDefaultTimeout()
   692  		Expect(kube).Should(Exit(0))
   693  
   694  		rm := podmanTest.Podman([]string{"pod", "rm", "-f", podName})
   695  		rm.WaitWithDefaultTimeout()
   696  		Expect(rm).Should(Exit(0))
   697  
   698  		play := podmanTest.Podman([]string{"play", "kube", outputFile})
   699  		play.WaitWithDefaultTimeout()
   700  		Expect(play).Should(Exit(0))
   701  
   702  		inspect := podmanTest.Podman([]string{"pod", "inspect", podName})
   703  		inspect.WaitWithDefaultTimeout()
   704  		Expect(inspect).Should(Exit(0))
   705  		Expect(inspect.OutputToString()).To(ContainSubstring(`"pid"`))
   706  	})
   707  
   708  	It("podman generate kube with pods and containers", func() {
   709  		pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", ALPINE, "top"})
   710  		pod1.WaitWithDefaultTimeout()
   711  		Expect(pod1).Should(Exit(0))
   712  
   713  		pod2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top", ALPINE, "top"})
   714  		pod2.WaitWithDefaultTimeout()
   715  		Expect(pod2).Should(Exit(0))
   716  
   717  		kube := podmanTest.Podman([]string{"generate", "kube", "pod1", "top"})
   718  		kube.WaitWithDefaultTimeout()
   719  		Expect(kube).Should(Exit(0))
   720  	})
   721  
   722  	It("podman generate kube with containers in a pod should fail", func() {
   723  		pod1 := podmanTest.Podman([]string{"pod", "create", "--name", "pod1"})
   724  		pod1.WaitWithDefaultTimeout()
   725  		Expect(pod1).Should(Exit(0))
   726  
   727  		con := podmanTest.Podman([]string{"run", "-dt", "--pod", "pod1", "--name", "top", ALPINE, "top"})
   728  		con.WaitWithDefaultTimeout()
   729  		Expect(con).Should(Exit(0))
   730  
   731  		kube := podmanTest.Podman([]string{"generate", "kube", "top"})
   732  		kube.WaitWithDefaultTimeout()
   733  		Expect(kube).To(ExitWithError())
   734  	})
   735  
   736  	It("podman generate kube with multiple containers", func() {
   737  		con1 := podmanTest.Podman([]string{"run", "-dt", "--name", "con1", ALPINE, "top"})
   738  		con1.WaitWithDefaultTimeout()
   739  		Expect(con1).Should(Exit(0))
   740  
   741  		con2 := podmanTest.Podman([]string{"run", "-dt", "--name", "con2", ALPINE, "top"})
   742  		con2.WaitWithDefaultTimeout()
   743  		Expect(con2).Should(Exit(0))
   744  
   745  		kube := podmanTest.Podman([]string{"generate", "kube", "con1", "con2"})
   746  		kube.WaitWithDefaultTimeout()
   747  		Expect(kube).Should(Exit(0))
   748  	})
   749  
   750  	It("podman generate kube with containers in pods should fail", func() {
   751  		pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", "--name", "top1", ALPINE, "top"})
   752  		pod1.WaitWithDefaultTimeout()
   753  		Expect(pod1).Should(Exit(0))
   754  
   755  		pod2 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod2", "--name", "top2", ALPINE, "top"})
   756  		pod2.WaitWithDefaultTimeout()
   757  		Expect(pod2).Should(Exit(0))
   758  
   759  		kube := podmanTest.Podman([]string{"generate", "kube", "top1", "top2"})
   760  		kube.WaitWithDefaultTimeout()
   761  		Expect(kube).To(ExitWithError())
   762  	})
   763  
   764  	It("podman generate kube on a container with dns options", func() {
   765  		top := podmanTest.Podman([]string{"run", "-dt", "--name", "top", "--dns", "8.8.8.8", "--dns-search", "foobar.com", "--dns-opt", "color:blue", ALPINE, "top"})
   766  		top.WaitWithDefaultTimeout()
   767  		Expect(top).Should(Exit(0))
   768  
   769  		kube := podmanTest.Podman([]string{"generate", "kube", "top"})
   770  		kube.WaitWithDefaultTimeout()
   771  		Expect(kube).Should(Exit(0))
   772  
   773  		pod := new(v1.Pod)
   774  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   775  		Expect(err).To(BeNil())
   776  
   777  		Expect(StringInSlice("8.8.8.8", pod.Spec.DNSConfig.Nameservers)).To(BeTrue())
   778  		Expect(StringInSlice("foobar.com", pod.Spec.DNSConfig.Searches)).To(BeTrue())
   779  		Expect(len(pod.Spec.DNSConfig.Options)).To(BeNumerically(">", 0))
   780  		Expect(pod.Spec.DNSConfig.Options[0].Name).To(Equal("color"))
   781  		Expect(*pod.Spec.DNSConfig.Options[0].Value).To(Equal("blue"))
   782  	})
   783  
   784  	It("podman generate kube multiple container dns servers and options are cumulative", func() {
   785  		top1 := podmanTest.Podman([]string{"run", "-dt", "--name", "top1", "--dns", "8.8.8.8", "--dns-search", "foobar.com", ALPINE, "top"})
   786  		top1.WaitWithDefaultTimeout()
   787  		Expect(top1).Should(Exit(0))
   788  
   789  		top2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top2", "--dns", "8.7.7.7", "--dns-search", "homer.com", ALPINE, "top"})
   790  		top2.WaitWithDefaultTimeout()
   791  		Expect(top2).Should(Exit(0))
   792  
   793  		kube := podmanTest.Podman([]string{"generate", "kube", "top1", "top2"})
   794  		kube.WaitWithDefaultTimeout()
   795  		Expect(kube).Should(Exit(0))
   796  
   797  		pod := new(v1.Pod)
   798  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   799  		Expect(err).To(BeNil())
   800  
   801  		Expect(StringInSlice("8.8.8.8", pod.Spec.DNSConfig.Nameservers)).To(BeTrue())
   802  		Expect(StringInSlice("8.7.7.7", pod.Spec.DNSConfig.Nameservers)).To(BeTrue())
   803  		Expect(StringInSlice("foobar.com", pod.Spec.DNSConfig.Searches)).To(BeTrue())
   804  		Expect(StringInSlice("homer.com", pod.Spec.DNSConfig.Searches)).To(BeTrue())
   805  	})
   806  
   807  	It("podman generate kube on a pod with dns options", func() {
   808  		top := podmanTest.Podman([]string{"run", "--pod", "new:pod1", "-dt", "--name", "top", "--dns", "8.8.8.8", "--dns-search", "foobar.com", "--dns-opt", "color:blue", ALPINE, "top"})
   809  		top.WaitWithDefaultTimeout()
   810  		Expect(top).Should(Exit(0))
   811  
   812  		kube := podmanTest.Podman([]string{"generate", "kube", "pod1"})
   813  		kube.WaitWithDefaultTimeout()
   814  		Expect(kube).Should(Exit(0))
   815  
   816  		pod := new(v1.Pod)
   817  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   818  		Expect(err).To(BeNil())
   819  
   820  		Expect(StringInSlice("8.8.8.8", pod.Spec.DNSConfig.Nameservers)).To(BeTrue())
   821  		Expect(StringInSlice("foobar.com", pod.Spec.DNSConfig.Searches)).To(BeTrue())
   822  		Expect(len(pod.Spec.DNSConfig.Options)).To(BeNumerically(">", 0))
   823  		Expect(pod.Spec.DNSConfig.Options[0].Name).To(Equal("color"))
   824  		Expect(*pod.Spec.DNSConfig.Options[0].Value).To(Equal("blue"))
   825  	})
   826  
   827  	It("podman generate kube - set entrypoint as command", func() {
   828  		session := podmanTest.Podman([]string{"create", "--pod", "new:testpod", "--entrypoint", "/bin/sleep", ALPINE, "10s"})
   829  		session.WaitWithDefaultTimeout()
   830  		Expect(session).Should(Exit(0))
   831  
   832  		kube := podmanTest.Podman([]string{"generate", "kube", "testpod"})
   833  		kube.WaitWithDefaultTimeout()
   834  		Expect(kube).Should(Exit(0))
   835  
   836  		// Now make sure that the container's command is set to the
   837  		// entrypoint and it's arguments to "10s".
   838  		pod := new(v1.Pod)
   839  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   840  		Expect(err).To(BeNil())
   841  
   842  		containers := pod.Spec.Containers
   843  		Expect(len(containers)).To(Equal(1))
   844  
   845  		Expect(containers[0].Command).To(Equal([]string{"/bin/sleep"}))
   846  		Expect(containers[0].Args).To(Equal([]string{"10s"}))
   847  	})
   848  
   849  	It("podman generate kube - use command from image unless explicitly set in the podman command", func() {
   850  		session := podmanTest.Podman([]string{"create", "--name", "test", ALPINE})
   851  		session.WaitWithDefaultTimeout()
   852  		Expect(session).Should(Exit(0))
   853  
   854  		kube := podmanTest.Podman([]string{"generate", "kube", "test"})
   855  		kube.WaitWithDefaultTimeout()
   856  		Expect(kube).Should(Exit(0))
   857  
   858  		// Now make sure that the container's command in the kube yaml is not set to the
   859  		// image command.
   860  		pod := new(v1.Pod)
   861  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   862  		Expect(err).To(BeNil())
   863  
   864  		containers := pod.Spec.Containers
   865  		Expect(len(containers)).To(Equal(1))
   866  		Expect(len(containers[0].Command)).To(Equal(0))
   867  
   868  		cmd := []string{"echo", "hi"}
   869  		session = podmanTest.Podman(append([]string{"create", "--name", "test1", ALPINE}, cmd...))
   870  		session.WaitWithDefaultTimeout()
   871  		Expect(session).Should(Exit(0))
   872  
   873  		kube = podmanTest.Podman([]string{"generate", "kube", "test1"})
   874  		kube.WaitWithDefaultTimeout()
   875  		Expect(kube).Should(Exit(0))
   876  
   877  		// Now make sure that the container's command in the kube yaml is set to the
   878  		// command passed via the cli to podman create.
   879  		pod = new(v1.Pod)
   880  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   881  		Expect(err).To(BeNil())
   882  
   883  		containers = pod.Spec.Containers
   884  		Expect(len(containers)).To(Equal(1))
   885  		Expect(containers[0].Command).To(Equal(cmd))
   886  	})
   887  
   888  	It("podman generate kube - use entrypoint from image unless --entrypoint is set", func() {
   889  		// Build an image with an entrypoint.
   890  		containerfile := `FROM quay.io/libpod/alpine:latest
   891  ENTRYPOINT ["sleep"]`
   892  
   893  		targetPath, err := CreateTempDirInTempDir()
   894  		Expect(err).To(BeNil())
   895  		containerfilePath := filepath.Join(targetPath, "Containerfile")
   896  		err = ioutil.WriteFile(containerfilePath, []byte(containerfile), 0644)
   897  		Expect(err).To(BeNil())
   898  
   899  		image := "generatekube:test"
   900  		session := podmanTest.Podman([]string{"build", "--pull-never", "-f", containerfilePath, "-t", image})
   901  		session.WaitWithDefaultTimeout()
   902  		Expect(session).Should(Exit(0))
   903  
   904  		session = podmanTest.Podman([]string{"create", "--pod", "new:testpod", image, "10s"})
   905  		session.WaitWithDefaultTimeout()
   906  		Expect(session).Should(Exit(0))
   907  
   908  		kube := podmanTest.Podman([]string{"generate", "kube", "testpod"})
   909  		kube.WaitWithDefaultTimeout()
   910  		Expect(kube).Should(Exit(0))
   911  
   912  		// Now make sure that the container's command in the kube yaml is NOT set to the
   913  		// entrypoint but the arguments should be set to "10s".
   914  		pod := new(v1.Pod)
   915  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   916  		Expect(err).To(BeNil())
   917  
   918  		containers := pod.Spec.Containers
   919  		Expect(len(containers)).To(Equal(1))
   920  		Expect(containers[0].Args).To(Equal([]string{"10s"}))
   921  
   922  		session = podmanTest.Podman([]string{"create", "--pod", "new:testpod-2", "--entrypoint", "echo", image, "hello"})
   923  		session.WaitWithDefaultTimeout()
   924  		Expect(session).Should(Exit(0))
   925  
   926  		kube = podmanTest.Podman([]string{"generate", "kube", "testpod-2"})
   927  		kube.WaitWithDefaultTimeout()
   928  		Expect(kube).Should(Exit(0))
   929  
   930  		// Now make sure that the container's command in the kube yaml is set to the
   931  		// entrypoint defined by the --entrypoint flag and the arguments should be set to "hello".
   932  		pod = new(v1.Pod)
   933  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
   934  		Expect(err).To(BeNil())
   935  
   936  		containers = pod.Spec.Containers
   937  		Expect(len(containers)).To(Equal(1))
   938  		Expect(containers[0].Command).To(Equal([]string{"echo"}))
   939  		Expect(containers[0].Args).To(Equal([]string{"hello"}))
   940  	})
   941  
   942  	It("podman generate kube - --privileged container", func() {
   943  		session := podmanTest.Podman([]string{"create", "--pod", "new:testpod", "--privileged", ALPINE, "ls"})
   944  		session.WaitWithDefaultTimeout()
   945  		Expect(session).Should(Exit(0))
   946  
   947  		kube := podmanTest.Podman([]string{"generate", "kube", "testpod"})
   948  		kube.WaitWithDefaultTimeout()
   949  		Expect(kube).Should(Exit(0))
   950  
   951  		// Now make sure that the capabilities aren't set.
   952  		pod := new(v1.Pod)
   953  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
   954  		Expect(err).To(BeNil())
   955  
   956  		containers := pod.Spec.Containers
   957  		Expect(len(containers)).To(Equal(1))
   958  		Expect(containers[0].SecurityContext.Capabilities).To(BeNil())
   959  
   960  		// Now make sure we can also `play` it.
   961  		kubeFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
   962  
   963  		kube = podmanTest.Podman([]string{"generate", "kube", "testpod", "-f", kubeFile})
   964  		kube.WaitWithDefaultTimeout()
   965  		Expect(kube).Should(Exit(0))
   966  
   967  		// Remove the pod so play can recreate it.
   968  		kube = podmanTest.Podman([]string{"pod", "rm", "-f", "testpod"})
   969  		kube.WaitWithDefaultTimeout()
   970  		Expect(kube).Should(Exit(0))
   971  
   972  		kube = podmanTest.Podman([]string{"play", "kube", kubeFile})
   973  		kube.WaitWithDefaultTimeout()
   974  		Expect(kube).Should(Exit(0))
   975  	})
   976  
   977  	It("podman generate kube based on user in container", func() {
   978  		// Build an image with an entrypoint.
   979  		containerfile := `FROM quay.io/libpod/alpine:latest
   980  RUN adduser -u 10001 -S test1
   981  USER test1`
   982  
   983  		targetPath, err := CreateTempDirInTempDir()
   984  		Expect(err).To(BeNil())
   985  		containerfilePath := filepath.Join(targetPath, "Containerfile")
   986  		err = ioutil.WriteFile(containerfilePath, []byte(containerfile), 0644)
   987  		Expect(err).To(BeNil())
   988  
   989  		image := "generatekube:test"
   990  		session := podmanTest.Podman([]string{"build", "--pull-never", "-f", containerfilePath, "-t", image})
   991  		session.WaitWithDefaultTimeout()
   992  		Expect(session).Should(Exit(0))
   993  
   994  		session = podmanTest.Podman([]string{"create", "--pod", "new:testpod", image, "test1"})
   995  		session.WaitWithDefaultTimeout()
   996  		Expect(session).Should(Exit(0))
   997  
   998  		kube := podmanTest.Podman([]string{"generate", "kube", "testpod"})
   999  		kube.WaitWithDefaultTimeout()
  1000  		Expect(kube).Should(Exit(0))
  1001  
  1002  		pod := new(v1.Pod)
  1003  		err = yaml.Unmarshal(kube.Out.Contents(), pod)
  1004  		Expect(err).To(BeNil())
  1005  		Expect(pod.Spec.Containers[0].SecurityContext.RunAsUser).To(BeNil())
  1006  	})
  1007  
  1008  	It("podman generate kube on named volume", func() {
  1009  		vol := "simple-named-volume"
  1010  
  1011  		session := podmanTest.Podman([]string{"volume", "create", vol})
  1012  		session.WaitWithDefaultTimeout()
  1013  		Expect(session).Should(Exit(0))
  1014  
  1015  		kube := podmanTest.Podman([]string{"generate", "kube", vol})
  1016  		kube.WaitWithDefaultTimeout()
  1017  		Expect(kube).Should(Exit(0))
  1018  
  1019  		pvc := new(v1.PersistentVolumeClaim)
  1020  		err := yaml.Unmarshal(kube.Out.Contents(), pvc)
  1021  		Expect(err).To(BeNil())
  1022  		Expect(pvc.GetName()).To(Equal(vol))
  1023  		Expect(pvc.Spec.AccessModes[0]).To(Equal(v1.ReadWriteOnce))
  1024  		Expect(pvc.Spec.Resources.Requests.Storage().String()).To(Equal("1Gi"))
  1025  	})
  1026  
  1027  	It("podman generate kube on named volume with options", func() {
  1028  		vol := "complex-named-volume"
  1029  		volDevice := "tmpfs"
  1030  		volType := "tmpfs"
  1031  		volOpts := "nodev,noexec"
  1032  
  1033  		session := podmanTest.Podman([]string{"volume", "create", "--opt", "device=" + volDevice, "--opt", "type=" + volType, "--opt", "o=" + volOpts, vol})
  1034  		session.WaitWithDefaultTimeout()
  1035  		Expect(session).Should(Exit(0))
  1036  
  1037  		kube := podmanTest.Podman([]string{"generate", "kube", vol})
  1038  		kube.WaitWithDefaultTimeout()
  1039  		Expect(kube).Should(Exit(0))
  1040  
  1041  		pvc := new(v1.PersistentVolumeClaim)
  1042  		err := yaml.Unmarshal(kube.Out.Contents(), pvc)
  1043  		Expect(err).To(BeNil())
  1044  		Expect(pvc.GetName()).To(Equal(vol))
  1045  		Expect(pvc.Spec.AccessModes[0]).To(Equal(v1.ReadWriteOnce))
  1046  		Expect(pvc.Spec.Resources.Requests.Storage().String()).To(Equal("1Gi"))
  1047  
  1048  		for k, v := range pvc.GetAnnotations() {
  1049  			switch k {
  1050  			case util.VolumeDeviceAnnotation:
  1051  				Expect(v).To(Equal(volDevice))
  1052  			case util.VolumeTypeAnnotation:
  1053  				Expect(v).To(Equal(volType))
  1054  			case util.VolumeMountOptsAnnotation:
  1055  				Expect(v).To(Equal(volOpts))
  1056  			}
  1057  		}
  1058  	})
  1059  
  1060  	It("podman generate kube on container with auto update labels", func() {
  1061  		top := podmanTest.Podman([]string{"run", "-dt", "--name", "top", "--label", "io.containers.autoupdate=local", ALPINE, "top"})
  1062  		top.WaitWithDefaultTimeout()
  1063  		Expect(top).Should(Exit(0))
  1064  
  1065  		kube := podmanTest.Podman([]string{"generate", "kube", "top"})
  1066  		kube.WaitWithDefaultTimeout()
  1067  		Expect(kube).Should(Exit(0))
  1068  
  1069  		pod := new(v1.Pod)
  1070  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
  1071  		Expect(err).To(BeNil())
  1072  
  1073  		v, ok := pod.GetAnnotations()["io.containers.autoupdate/top"]
  1074  		Expect(ok).To(Equal(true))
  1075  		Expect(v).To(Equal("local"))
  1076  	})
  1077  
  1078  	It("podman generate kube on pod with auto update labels in all containers", func() {
  1079  		pod1 := podmanTest.Podman([]string{"pod", "create", "--name", "pod1"})
  1080  		pod1.WaitWithDefaultTimeout()
  1081  		Expect(pod1).Should(Exit(0))
  1082  
  1083  		top1 := podmanTest.Podman([]string{"run", "-dt", "--name", "top1", "--pod", "pod1", "--label", "io.containers.autoupdate=registry", "--label", "io.containers.autoupdate.authfile=/some/authfile.json", ALPINE, "top"})
  1084  		top1.WaitWithDefaultTimeout()
  1085  		Expect(top1).Should(Exit(0))
  1086  
  1087  		top2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top2", "--workdir", "/root", "--pod", "pod1", "--label", "io.containers.autoupdate=registry", "--label", "io.containers.autoupdate.authfile=/some/authfile.json", ALPINE, "top"})
  1088  		top2.WaitWithDefaultTimeout()
  1089  		Expect(top2).Should(Exit(0))
  1090  
  1091  		kube := podmanTest.Podman([]string{"generate", "kube", "pod1"})
  1092  		kube.WaitWithDefaultTimeout()
  1093  		Expect(kube).Should(Exit(0))
  1094  
  1095  		pod := new(v1.Pod)
  1096  		err := yaml.Unmarshal(kube.Out.Contents(), pod)
  1097  		Expect(err).To(BeNil())
  1098  		Expect(pod.Spec.Containers[0].WorkingDir).To(Equal(""))
  1099  		Expect(pod.Spec.Containers[1].WorkingDir).To(Equal("/root"))
  1100  
  1101  		for _, ctr := range []string{"top1", "top2"} {
  1102  			v, ok := pod.GetAnnotations()["io.containers.autoupdate/"+ctr]
  1103  			Expect(ok).To(Equal(true))
  1104  			Expect(v).To(Equal("registry"))
  1105  
  1106  			v, ok = pod.GetAnnotations()["io.containers.autoupdate.authfile/"+ctr]
  1107  			Expect(ok).To(Equal(true))
  1108  			Expect(v).To(Equal("/some/authfile.json"))
  1109  		}
  1110  	})
  1111  })