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

     1  package integration
     2  
     3  import (
     4  	"os"
     5  
     6  	. "github.com/containers/podman/v4/test/utils"
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("Podman run", func() {
    13  
    14  	It("podman run environment test", func() {
    15  		session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "HOME"})
    16  		session.WaitWithDefaultTimeout()
    17  		Expect(session).Should(ExitCleanly())
    18  		Expect(session.OutputToString()).To(ContainSubstring("/root"))
    19  
    20  		session = podmanTest.Podman([]string{"run", "--rm", "--user", "2", ALPINE, "printenv", "HOME"})
    21  		session.WaitWithDefaultTimeout()
    22  		Expect(session).Should(ExitCleanly())
    23  		Expect(session.OutputToString()).To(ContainSubstring("/sbin"))
    24  
    25  		session = podmanTest.Podman([]string{"run", "--rm", "--env", "HOME=/foo", ALPINE, "printenv", "HOME"})
    26  		session.WaitWithDefaultTimeout()
    27  		Expect(session).Should(ExitCleanly())
    28  		Expect(session.OutputToString()).To(ContainSubstring("/foo"))
    29  
    30  		session = podmanTest.Podman([]string{"run", "--rm", "--env", "FOO=BAR,BAZ", ALPINE, "printenv", "FOO"})
    31  		session.WaitWithDefaultTimeout()
    32  		Expect(session).Should(ExitCleanly())
    33  		Expect(session.OutputToString()).To(ContainSubstring("BAR,BAZ"))
    34  
    35  		session = podmanTest.Podman([]string{"run", "--rm", "--env", "PATH=/bin", ALPINE, "printenv", "PATH"})
    36  		session.WaitWithDefaultTimeout()
    37  		Expect(session).Should(ExitCleanly())
    38  		Expect(session.OutputToString()).To(ContainSubstring("/bin"))
    39  
    40  		// Verify environ keys with spaces do not blow up podman command
    41  		os.Setenv("FOO BAR", "BAZ")
    42  		session = podmanTest.Podman([]string{"run", "--rm", ALPINE, "true"})
    43  		session.WaitWithDefaultTimeout()
    44  		Expect(session).Should(ExitCleanly())
    45  		os.Unsetenv("FOO BAR")
    46  
    47  		os.Setenv("FOO", "BAR")
    48  		session = podmanTest.Podman([]string{"run", "--rm", "--env", "FOO", ALPINE, "printenv", "FOO"})
    49  		session.WaitWithDefaultTimeout()
    50  		Expect(session).Should(ExitCleanly())
    51  		Expect(session.OutputToString()).To(ContainSubstring("BAR"))
    52  		os.Unsetenv("FOO")
    53  
    54  		session = podmanTest.Podman([]string{"run", "--rm", "--env", "FOO", ALPINE, "printenv", "FOO"})
    55  		session.WaitWithDefaultTimeout()
    56  		Expect(session.OutputToString()).To(BeEmpty())
    57  		Expect(session).Should(Exit(1))
    58  
    59  		session = podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv"})
    60  		session.WaitWithDefaultTimeout()
    61  		Expect(session).Should(ExitCleanly())
    62  
    63  		// This currently does not work
    64  		// Re-enable when hostname is an env variable
    65  		session = podmanTest.Podman([]string{"run", "--rm", ALPINE, "sh", "-c", "printenv"})
    66  		session.Wait(10)
    67  		Expect(session).Should(ExitCleanly())
    68  		Expect(session.OutputToString()).To(ContainSubstring("HOSTNAME"))
    69  	})
    70  
    71  	It("podman run with --env-merge", func() {
    72  		dockerfile := `FROM quay.io/libpod/alpine:latest
    73  ENV hello=world
    74  `
    75  		podmanTest.BuildImage(dockerfile, "test", "false")
    76  		session := podmanTest.Podman([]string{"run", "--rm", "--env-merge", "hello=${hello}-earth", "test", "env"})
    77  		session.WaitWithDefaultTimeout()
    78  		Expect(session).Should(ExitCleanly())
    79  		Expect(session.OutputToString()).To(ContainSubstring("world-earth"))
    80  
    81  		session = podmanTest.Podman([]string{"run", "--rm", "--env-merge", "foo=${bar}-earth", "test", "printenv", "foo"})
    82  		session.WaitWithDefaultTimeout()
    83  		Expect(session).Should(ExitCleanly())
    84  		Expect(session.OutputToString()).To(Equal("-earth"))
    85  	})
    86  
    87  	It("podman run --env-host environment test", func() {
    88  		env := append(os.Environ(), "FOO=BAR")
    89  		session := podmanTest.PodmanAsUser([]string{"run", "--rm", "--env-host", ALPINE, "/bin/printenv", "FOO"}, 0, 0, "", env)
    90  		session.WaitWithDefaultTimeout()
    91  		if IsRemote() {
    92  			// podman-remote does not support --env-host
    93  			Expect(session).Should(Exit(125))
    94  			Expect(session.ErrorToString()).To(ContainSubstring("unknown flag: --env-host"))
    95  			return
    96  		}
    97  		Expect(session).Should(ExitCleanly())
    98  		Expect(session.OutputToString()).To(ContainSubstring("BAR"))
    99  
   100  		session = podmanTest.PodmanAsUser([]string{"run", "--rm", "--env", "FOO=BAR1", "--env-host", ALPINE, "/bin/printenv", "FOO"}, 0, 0, "", env)
   101  		session.WaitWithDefaultTimeout()
   102  		Expect(session).Should(ExitCleanly())
   103  		Expect(session.OutputToString()).To(ContainSubstring("BAR1"))
   104  		os.Unsetenv("FOO")
   105  	})
   106  
   107  	It("podman run --http-proxy test", func() {
   108  		if env, found := os.LookupEnv("http_proxy"); found {
   109  			defer os.Setenv("http_proxy", env)
   110  		} else {
   111  			defer os.Unsetenv("http_proxy")
   112  		}
   113  		os.Setenv("http_proxy", "1.2.3.4")
   114  		if IsRemote() {
   115  			podmanTest.StopRemoteService()
   116  			podmanTest.StartRemoteService()
   117  			// set proxy env again so it will only effect the client
   118  			// the remote client should still use the proxy that was set for the server
   119  			os.Setenv("http_proxy", "127.0.0.2")
   120  		}
   121  		session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "http_proxy"})
   122  		session.WaitWithDefaultTimeout()
   123  		Expect(session).Should(ExitCleanly())
   124  		Expect(session.OutputToString()).To(ContainSubstring("1.2.3.4"))
   125  
   126  		session = podmanTest.Podman([]string{"run", "--http-proxy=false", ALPINE, "printenv", "http_proxy"})
   127  		session.WaitWithDefaultTimeout()
   128  		Expect(session).Should(Exit(1))
   129  		Expect(session.OutputToString()).To(Equal(""))
   130  
   131  		session = podmanTest.Podman([]string{"run", "--env", "http_proxy=5.6.7.8", ALPINE, "printenv", "http_proxy"})
   132  		session.WaitWithDefaultTimeout()
   133  		Expect(session).Should(ExitCleanly())
   134  		Expect(session.OutputToString()).To(ContainSubstring("5.6.7.8"))
   135  
   136  		session = podmanTest.Podman([]string{"run", "--http-proxy=false", "--env", "http_proxy=5.6.7.8", ALPINE, "printenv", "http_proxy"})
   137  		session.WaitWithDefaultTimeout()
   138  		Expect(session).Should(ExitCleanly())
   139  		Expect(session.OutputToString()).To(ContainSubstring("5.6.7.8"))
   140  	})
   141  })