From 9a206fdc9f73f0b5f761bd48a1e306618cbecc48 Mon Sep 17 00:00:00 2001
From: Valentin Rothberg <vrothberg@redhat.com>
Date: Fri, 13 Jan 2023 13:59:26 +0100
Subject: [PATCH] kube play: complete container spec

Make sure that the specs of containers generated by `kube play` are
correctly completed.  They have not before which surfaced in default
environment variables not being set.

Fixes: #17016
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
---
 pkg/domain/infra/abi/play.go | 10 ++++++++++
 test/system/700-play.bats    | 18 ++++++++++--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/pkg/domain/infra/abi/play.go b/pkg/domain/infra/abi/play.go
index 7a351d2417..a562c008f8 100644
--- a/pkg/domain/infra/abi/play.go
+++ b/pkg/domain/infra/abi/play.go
@@ -732,6 +732,16 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
 		if err != nil {
 			return nil, nil, err
 		}
+
+		// Make sure to complete the spec (#17016)
+		warn, err := generate.CompleteSpec(ctx, ic.Libpod, specGen)
+		if err != nil {
+			return nil, nil, err
+		}
+		for _, w := range warn {
+			fmt.Fprintf(os.Stderr, "%s\n", w)
+		}
+
 		specGen.RawImageName = container.Image
 		rtSpec, spec, opts, err := generate.MakeContainer(ctx, ic.Libpod, specGen, false, nil)
 		if err != nil {
diff --git a/test/system/700-play.bats b/test/system/700-play.bats
index 32101f9762..248bdec27a 100644
--- a/test/system/700-play.bats
+++ b/test/system/700-play.bats
@@ -156,13 +156,6 @@ EOF
         cat <<EOF               >>$outfile
   - command:
     - $command
-    env:
-    - name: PATH
-      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
-    - name: TERM
-      value: xterm
-    - name: container
-      value: podman
     image: $image
     name: $ctrname
     resources: {}
@@ -341,10 +334,19 @@ from $IMAGE
 USER bin
 _EOF
 
-    run_podman build -t userimage $PODMAN_TMPDIR
+    # Unset the PATH during build and make sure that all default env variables
+    # are correctly set for the created container.
+    run_podman build --unsetenv PATH -t userimage $PODMAN_TMPDIR
+    run_podman image inspect userimage --format "{{.Config.Env}}"
+    is "$output" "\[\]" "image does not set PATH - env is empty"
+
     run_podman play kube --start=false $PODMAN_TMPDIR/test.yaml
     run_podman inspect --format "{{ .Config.User }}" test_pod-test
     is "$output" bin "expect container within pod to run as the bin user"
+    run_podman inspect --format "{{ .Config.Env }}" test_pod-test
+    is "$output" ".*PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.*" "expect PATH to be set"
+    is "$output" ".*TERM=xterm.*" "expect TERM to be set"
+    is "$output" ".*container=podman.*" "expect container to be set"
 
     run_podman stop -a -t 0
     run_podman pod rm -t 0 -f test_pod