github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/build_test.go (about)

     1  // +build !remoteclient
     2  
     3  package integration
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"runtime"
    10  	"strings"
    11  
    12  	. "github.com/containers/libpod/test/utils"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Podman build", func() {
    18  	var (
    19  		tempdir    string
    20  		err        error
    21  		podmanTest *PodmanTestIntegration
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		tempdir, err = CreateTempDirInTempDir()
    26  		if err != nil {
    27  			os.Exit(1)
    28  		}
    29  		podmanTest = PodmanTestCreate(tempdir)
    30  		podmanTest.Setup()
    31  		podmanTest.RestoreAllArtifacts()
    32  	})
    33  
    34  	AfterEach(func() {
    35  		podmanTest.Cleanup()
    36  		f := CurrentGinkgoTestDescription()
    37  		processTestResult(f)
    38  	})
    39  
    40  	// Let's first do the most simple build possible to make sure stuff is
    41  	// happy and then clean up after ourselves to make sure that works too.
    42  	It("podman build and remove basic alpine", func() {
    43  		session := podmanTest.PodmanNoCache([]string{"build", "build/basicalpine"})
    44  		session.WaitWithDefaultTimeout()
    45  		Expect(session.ExitCode()).To(Equal(0))
    46  
    47  		iid := session.OutputToStringArray()[len(session.OutputToStringArray())-1]
    48  
    49  		// Verify that OS and Arch are being set
    50  		inspect := podmanTest.PodmanNoCache([]string{"inspect", iid})
    51  		inspect.WaitWithDefaultTimeout()
    52  		data := inspect.InspectImageJSON()
    53  		Expect(data[0].Os).To(Equal(runtime.GOOS))
    54  		Expect(data[0].Architecture).To(Equal(runtime.GOARCH))
    55  
    56  		session = podmanTest.PodmanNoCache([]string{"rmi", "alpine"})
    57  		session.WaitWithDefaultTimeout()
    58  		Expect(session.ExitCode()).To(Equal(0))
    59  	})
    60  
    61  	// If the context directory is pointing at a file and not a directory,
    62  	// that's a no no, fail out.
    63  	It("podman build context directory a file", func() {
    64  		session := podmanTest.PodmanNoCache([]string{"build", "build/context_dir_a_file"})
    65  		session.WaitWithDefaultTimeout()
    66  		Expect(session.ExitCode()).To(Equal(125))
    67  	})
    68  
    69  	// Check that builds with different values for the squash options
    70  	// create the appropriate number of layers, then clean up after.
    71  	It("podman build basic alpine with squash", func() {
    72  		session := podmanTest.PodmanNoCache([]string{"build", "-f", "build/squash/Dockerfile.squash-a", "-t", "test-squash-a:latest", "build/squash"})
    73  		session.WaitWithDefaultTimeout()
    74  		Expect(session.ExitCode()).To(Equal(0))
    75  
    76  		session = podmanTest.PodmanNoCache([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-a"})
    77  		session.WaitWithDefaultTimeout()
    78  		Expect(session.ExitCode()).To(Equal(0))
    79  		// Check for two layers
    80  		Expect(len(strings.Fields(session.OutputToString()))).To(Equal(2))
    81  
    82  		session = podmanTest.PodmanNoCache([]string{"build", "-f", "build/squash/Dockerfile.squash-b", "--squash", "-t", "test-squash-b:latest", "build/squash"})
    83  		session.WaitWithDefaultTimeout()
    84  		Expect(session.ExitCode()).To(Equal(0))
    85  
    86  		session = podmanTest.PodmanNoCache([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-b"})
    87  		session.WaitWithDefaultTimeout()
    88  		Expect(session.ExitCode()).To(Equal(0))
    89  		// Check for three layers
    90  		Expect(len(strings.Fields(session.OutputToString()))).To(Equal(3))
    91  
    92  		session = podmanTest.PodmanNoCache([]string{"build", "-f", "build/squash/Dockerfile.squash-c", "--squash", "-t", "test-squash-c:latest", "build/squash"})
    93  		session.WaitWithDefaultTimeout()
    94  		Expect(session.ExitCode()).To(Equal(0))
    95  
    96  		session = podmanTest.PodmanNoCache([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-c"})
    97  		session.WaitWithDefaultTimeout()
    98  		Expect(session.ExitCode()).To(Equal(0))
    99  		// Check for two layers
   100  		Expect(len(strings.Fields(session.OutputToString()))).To(Equal(2))
   101  
   102  		session = podmanTest.PodmanNoCache([]string{"build", "-f", "build/squash/Dockerfile.squash-c", "--squash-all", "-t", "test-squash-d:latest", "build/squash"})
   103  		session.WaitWithDefaultTimeout()
   104  		Expect(session.ExitCode()).To(Equal(0))
   105  
   106  		session = podmanTest.PodmanNoCache([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-d"})
   107  		session.WaitWithDefaultTimeout()
   108  		Expect(session.ExitCode()).To(Equal(0))
   109  		// Check for one layers
   110  		Expect(len(strings.Fields(session.OutputToString()))).To(Equal(1))
   111  
   112  		session = podmanTest.PodmanNoCache([]string{"rm", "-a"})
   113  		session.WaitWithDefaultTimeout()
   114  		Expect(session.ExitCode()).To(Equal(0))
   115  
   116  		session = podmanTest.PodmanNoCache([]string{"rmi", "-a", "-f"})
   117  		session.WaitWithDefaultTimeout()
   118  		Expect(session.ExitCode()).To(Equal(0))
   119  	})
   120  
   121  	It("podman build Containerfile locations", func() {
   122  		// Given
   123  		// Switch to temp dir and restore it afterwards
   124  		cwd, err := os.Getwd()
   125  		Expect(err).To(BeNil())
   126  		Expect(os.Chdir(os.TempDir())).To(BeNil())
   127  		defer Expect(os.Chdir(cwd)).To(BeNil())
   128  
   129  		// Write target and fake files
   130  		targetPath := filepath.Join(os.TempDir(), "dir")
   131  		Expect(os.MkdirAll(targetPath, 0755)).To(BeNil())
   132  
   133  		fakeFile := filepath.Join(os.TempDir(), "Containerfile")
   134  		Expect(ioutil.WriteFile(fakeFile, []byte("FROM alpine"), 0755)).To(BeNil())
   135  
   136  		targetFile := filepath.Join(targetPath, "Containerfile")
   137  		Expect(ioutil.WriteFile(targetFile, []byte("FROM scratch"), 0755)).To(BeNil())
   138  
   139  		defer func() {
   140  			Expect(os.RemoveAll(fakeFile)).To(BeNil())
   141  			Expect(os.RemoveAll(targetFile)).To(BeNil())
   142  		}()
   143  
   144  		// When
   145  		session := podmanTest.PodmanNoCache([]string{
   146  			"build", "-f", targetFile, "-t", "test-locations",
   147  		})
   148  		session.WaitWithDefaultTimeout()
   149  
   150  		// Then
   151  		Expect(session.ExitCode()).To(Equal(0))
   152  		Expect(strings.Fields(session.OutputToString())).
   153  			To(ContainElement("scratch"))
   154  	})
   155  
   156  	It("podman build basic alpine and print id to external file", func() {
   157  
   158  		// Switch to temp dir and restore it afterwards
   159  		cwd, err := os.Getwd()
   160  		Expect(err).To(BeNil())
   161  		Expect(os.Chdir(os.TempDir())).To(BeNil())
   162  		defer Expect(os.Chdir(cwd)).To(BeNil())
   163  
   164  		targetPath := filepath.Join(os.TempDir(), "dir")
   165  		targetFile := filepath.Join(targetPath, "idFile")
   166  
   167  		session := podmanTest.PodmanNoCache([]string{"build", "build/basicalpine", "--iidfile", targetFile})
   168  		session.WaitWithDefaultTimeout()
   169  		Expect(session.ExitCode()).To(Equal(0))
   170  		id, _ := ioutil.ReadFile(targetFile)
   171  
   172  		// Verify that id is correct
   173  		inspect := podmanTest.PodmanNoCache([]string{"inspect", string(id)})
   174  		inspect.WaitWithDefaultTimeout()
   175  		data := inspect.InspectImageJSON()
   176  		Expect(data[0].ID).To(Equal(string(id)))
   177  	})
   178  
   179  	It("podman Test PATH in built image", func() {
   180  		path := "/tmp:/bin:/usr/bin:/usr/sbin"
   181  		session := podmanTest.PodmanNoCache([]string{
   182  			"build", "-f", "build/basicalpine/Containerfile.path", "-t", "test-path",
   183  		})
   184  		session.WaitWithDefaultTimeout()
   185  		Expect(session.ExitCode()).To(Equal(0))
   186  
   187  		session = podmanTest.Podman([]string{"run", "test-path", "printenv", "PATH"})
   188  		session.WaitWithDefaultTimeout()
   189  		Expect(session.ExitCode()).To(Equal(0))
   190  		stdoutLines := session.OutputToStringArray()
   191  		Expect(stdoutLines[0]).Should(Equal(path))
   192  
   193  		session = podmanTest.PodmanNoCache([]string{"rmi", "-a", "-f"})
   194  		session.WaitWithDefaultTimeout()
   195  		Expect(session.ExitCode()).To(Equal(0))
   196  	})
   197  
   198  })