From a6de23278a6e5b7f27c093eb19c201b77b7c4416 Mon Sep 17 00:00:00 2001
From: baude <bbaude@redhat.com>
Date: Tue, 31 Jul 2018 17:16:08 -0500
Subject: [PATCH] Use REGISTRIES_CONFIG_PATH for all tests

We should not be using the test systems registries.conf file for integration
tests. We should always use a constructed file created specifically for the
integration tests or we stand to have unpredictable results.  The beforeTest
function now sets an environment variable pointing to a registries.conf file
in the test's tempdir.  That file will container docker.io as a default.

The afterTest function then clears the environment variable.

Signed-off-by: baude <bbaude@redhat.com>

Closes: #1197
Approved by: rhatdan
---
 test/e2e/libpod_suite_test.go | 74 ++++++++++++++++++++++++++++-------
 test/e2e/run_signal_test.go   |  2 +-
 test/e2e/run_test.go          |  2 +-
 test/e2e/search_test.go       | 30 ++++----------
 test/registries.conf          |  2 +-
 5 files changed, 70 insertions(+), 40 deletions(-)

diff --git a/test/e2e/libpod_suite_test.go b/test/e2e/libpod_suite_test.go
index ab09d5004b..e95b03cb98 100644
--- a/test/e2e/libpod_suite_test.go
+++ b/test/e2e/libpod_suite_test.go
@@ -1,6 +1,7 @@
 package integration
 
 import (
+	"bufio"
 	"context"
 	"encoding/json"
 	"fmt"
@@ -60,6 +61,12 @@ type PodmanTest struct {
 	TempDir             string
 }
 
+// HostOS is a simple struct for the test os
+type HostOS struct {
+	Distribution string
+	Version      string
+}
+
 // TestLibpod ginkgo master function
 func TestLibpod(t *testing.T) {
 	if reexec.Init() {
@@ -91,7 +98,20 @@ var _ = BeforeSuite(func() {
 			os.Exit(1)
 		}
 	}
-
+	host := GetHostDistributionInfo()
+	if host.Distribution == "rhel" && strings.HasPrefix(host.Version, "7") {
+		f, err := os.OpenFile("/proc/sys/user/max_user_namespaces", os.O_WRONLY, 0644)
+		if err != nil {
+			fmt.Println("Unable to enable userspace on RHEL 7")
+			os.Exit(1)
+		}
+		_, err = f.WriteString("15000")
+		if err != nil {
+			fmt.Println("Unable to enable userspace on RHEL 7")
+			os.Exit(1)
+		}
+		f.Close()
+	}
 })
 
 // CreateTempDirin
@@ -101,6 +121,7 @@ func CreateTempDirInTempDir() (string, error) {
 
 // PodmanCreate creates a PodmanTest instance for the tests
 func PodmanCreate(tempDir string) PodmanTest {
+
 	cwd, _ := os.Getwd()
 
 	podmanBinary := filepath.Join(cwd, "../../bin/podman")
@@ -123,7 +144,7 @@ func PodmanCreate(tempDir string) PodmanTest {
 	runCBinary := "/usr/bin/runc"
 	CNIConfigDir := "/etc/cni/net.d"
 
-	return PodmanTest{
+	p := PodmanTest{
 		PodmanBinary:        podmanBinary,
 		ConmonBinary:        conmonBinary,
 		CrioRoot:            filepath.Join(tempDir, "crio"),
@@ -135,6 +156,10 @@ func PodmanCreate(tempDir string) PodmanTest {
 		ArtifactPath:        ARTIFACT_DIR,
 		TempDir:             tempDir,
 	}
+
+	// Setup registries.conf ENV variable
+	p.setDefaultRegistriesConfigEnv()
+	return p
 }
 
 //MakeOptions assembles all the podman main options
@@ -201,6 +226,9 @@ func (p *PodmanTest) Cleanup() {
 	if err := os.RemoveAll(p.TempDir); err != nil {
 		fmt.Printf("%q\n", err)
 	}
+
+	// Clean up the registries configuration file ENV variable set in Create
+	resetRegistriesConfigEnv()
 }
 
 // CleanupPod cleans up the temporary store
@@ -571,24 +599,40 @@ func (p *PodmanTest) BuildImage(dockerfile, imageName string, layers string) {
 	Expect(session.ExitCode()).To(Equal(0))
 }
 
-//GetHostDistribution returns the dist in string format. If the
-//distribution cannot be determined, an empty string will be returned.
-func (p *PodmanTest) GetHostDistribution() string {
-	content, err := ioutil.ReadFile("/etc/os-release")
+//GetHostDistributionInfo returns a struct with its distribution name and version
+func GetHostDistributionInfo() HostOS {
+	f, err := os.Open("/etc/os-release")
+	defer f.Close()
 	if err != nil {
-		return ""
+		return HostOS{}
 	}
-	for _, line := range content {
-		if strings.HasPrefix(fmt.Sprintf("%x", line), "ID") {
-			fields := strings.Split(fmt.Sprintf("%x", line), "=")
-			if len(fields) < 2 {
-				return ""
-			}
-			return strings.Trim(fields[1], "\"")
 
+	l := bufio.NewScanner(f)
+	host := HostOS{}
+	for l.Scan() {
+		if strings.HasPrefix(l.Text(), "ID=") {
+			host.Distribution = strings.Replace(strings.TrimSpace(strings.Join(strings.Split(l.Text(), "=")[1:], "")), "\"", "", -1)
+		}
+		if strings.HasPrefix(l.Text(), "VERSION_ID=") {
+			host.Version = strings.Replace(strings.TrimSpace(strings.Join(strings.Split(l.Text(), "=")[1:], "")), "\"", "", -1)
 		}
 	}
-	return ""
+	return host
+}
+
+func (p *PodmanTest) setDefaultRegistriesConfigEnv() {
+	defaultFile := filepath.Join(INTEGRATION_ROOT, "test/registries.conf")
+	os.Setenv("REGISTRIES_CONFIG_PATH", defaultFile)
+}
+
+func (p *PodmanTest) setRegistriesConfigEnv(b []byte) {
+	outfile := filepath.Join(p.TempDir, "registries.conf")
+	os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
+	ioutil.WriteFile(outfile, b, 0644)
+}
+
+func resetRegistriesConfigEnv() {
+	os.Setenv("REGISTRIES_CONFIG_PATH", "")
 }
 
 // IsKernelNewThan compares the current kernel version to one provided.  If
diff --git a/test/e2e/run_signal_test.go b/test/e2e/run_signal_test.go
index bd00b8aa96..02b6f49415 100644
--- a/test/e2e/run_signal_test.go
+++ b/test/e2e/run_signal_test.go
@@ -63,7 +63,7 @@ var _ = Describe("Podman run with --sig-proxy", func() {
 		udsPath := filepath.Join(udsDir, "fifo")
 		syscall.Mkfifo(udsPath, 0600)
 
-		_, pid := podmanTest.PodmanPID([]string{"run", "-it", "-v", fmt.Sprintf("%s:/h", udsDir), fedoraMinimal, "bash", "-c", sigCatch})
+		_, pid := podmanTest.PodmanPID([]string{"run", "-it", "-v", fmt.Sprintf("%s:/h:Z", udsDir), fedoraMinimal, "bash", "-c", sigCatch})
 
 		uds, _ := os.OpenFile(udsPath, os.O_RDONLY, 0600)
 		defer uds.Close()
diff --git a/test/e2e/run_test.go b/test/e2e/run_test.go
index 1c86d48bc9..7bba1e31ef 100644
--- a/test/e2e/run_test.go
+++ b/test/e2e/run_test.go
@@ -562,7 +562,7 @@ USER mail`
 		err = os.MkdirAll(vol2, 0755)
 		Expect(err).To(BeNil())
 
-		session := podmanTest.Podman([]string{"run", "--volume", vol1 + ":/myvol1:ro", "--volume", vol2 + ":/myvol2", ALPINE, "touch", "/myvol2/foo.txt"})
+		session := podmanTest.Podman([]string{"run", "--volume", vol1 + ":/myvol1:z", "--volume", vol2 + ":/myvol2:z", ALPINE, "touch", "/myvol2/foo.txt"})
 		session.WaitWithDefaultTimeout()
 		Expect(session.ExitCode()).To(Equal(0))
 	})
diff --git a/test/e2e/search_test.go b/test/e2e/search_test.go
index bdbd5e770d..7b9612a35a 100644
--- a/test/e2e/search_test.go
+++ b/test/e2e/search_test.go
@@ -2,9 +2,7 @@ package integration
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
-	"path/filepath"
 	"strconv"
 
 	. "github.com/onsi/ginkgo"
@@ -177,10 +175,7 @@ var _ = Describe("Podman search", func() {
 		Expect(push.ExitCode()).To(Equal(0))
 
 		// registries.conf set up
-		regFileBytes := []byte(regFileContents)
-		outfile := filepath.Join(podmanTest.TempDir, "registries.conf")
-		os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
-		ioutil.WriteFile(outfile, regFileBytes, 0644)
+		podmanTest.setRegistriesConfigEnv([]byte(regFileContents))
 
 		search := podmanTest.Podman([]string{"search", "localhost:5000/my-alpine"})
 		search.WaitWithDefaultTimeout()
@@ -191,7 +186,7 @@ var _ = Describe("Podman search", func() {
 		Expect(search.ErrorToString()).Should(BeEmpty())
 
 		// cleanup
-		os.Setenv("REGISTRIES_CONFIG_PATH", "")
+		resetRegistriesConfigEnv()
 	})
 
 	It("podman search doesn't attempt HTTP if force secure is true", func() {
@@ -208,10 +203,7 @@ var _ = Describe("Podman search", func() {
 		Expect(push.ExitCode()).To(Equal(0))
 
 		// registries.conf set up
-		regFileBytes := []byte(regFileContents)
-		outfile := filepath.Join(podmanTest.TempDir, "registries.conf")
-		os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
-		ioutil.WriteFile(outfile, regFileBytes, 0644)
+		podmanTest.setRegistriesConfigEnv([]byte(regFileContents))
 
 		search := podmanTest.Podman([]string{"search", "localhost:5000/my-alpine", "--tls-verify=true"})
 		search.WaitWithDefaultTimeout()
@@ -222,7 +214,7 @@ var _ = Describe("Podman search", func() {
 		Expect(match).Should(BeTrue())
 
 		// cleanup
-		os.Setenv("REGISTRIES_CONFIG_PATH", "")
+		resetRegistriesConfigEnv()
 	})
 
 	It("podman search doesn't attempt HTTP if registry is not listed as insecure", func() {
@@ -239,10 +231,7 @@ var _ = Describe("Podman search", func() {
 		Expect(push.ExitCode()).To(Equal(0))
 
 		// registries.conf set up
-		regFileBytes := []byte(badRegFileContents)
-		outfile := filepath.Join(podmanTest.TempDir, "registries.conf")
-		os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
-		ioutil.WriteFile(outfile, regFileBytes, 0644)
+		podmanTest.setRegistriesConfigEnv([]byte(badRegFileContents))
 
 		search := podmanTest.Podman([]string{"search", "localhost:5000/my-alpine"})
 		search.WaitWithDefaultTimeout()
@@ -253,7 +242,7 @@ var _ = Describe("Podman search", func() {
 		Expect(match).Should(BeTrue())
 
 		// cleanup
-		os.Setenv("REGISTRIES_CONFIG_PATH", "")
+		resetRegistriesConfigEnv()
 	})
 
 	It("podman search doesn't attempt HTTP if one registry is not listed as insecure", func() {
@@ -278,10 +267,7 @@ var _ = Describe("Podman search", func() {
 		Expect(push.ExitCode()).To(Equal(0))
 
 		// registries.conf set up
-		regFileBytes := []byte(regFileContents2)
-		outfile := filepath.Join(podmanTest.TempDir, "registries.conf")
-		os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
-		ioutil.WriteFile(outfile, regFileBytes, 0644)
+		podmanTest.setRegistriesConfigEnv([]byte(regFileContents2))
 
 		search := podmanTest.Podman([]string{"search", "my-alpine"})
 		search.WaitWithDefaultTimeout()
@@ -292,6 +278,6 @@ var _ = Describe("Podman search", func() {
 		Expect(match).Should(BeTrue())
 
 		// cleanup
-		os.Setenv("REGISTRIES_CONFIG_PATH", "")
+		resetRegistriesConfigEnv()
 	})
 })
diff --git a/test/registries.conf b/test/registries.conf
index f3bf092b0c..6c9d39bbc8 100644
--- a/test/registries.conf
+++ b/test/registries.conf
@@ -1,5 +1,5 @@
 [registries.search]
-registries = ['registry.access.redhat.com', 'registry.fedoraproject.org', 'docker.io']
+registries = ['docker.io', 'quay.io']
 
 [registries.insecure]
 registries = []