github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration-cli/requirements_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"os"
     9  	"os/exec"
    10  	"strconv"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/docker/docker/api/types"
    15  	"github.com/docker/docker/api/types/swarm"
    16  	"github.com/docker/docker/api/types/versions"
    17  	"github.com/docker/docker/client"
    18  	"github.com/docker/docker/integration-cli/requirement"
    19  	"github.com/docker/docker/internal/test/registry"
    20  )
    21  
    22  func ArchitectureIsNot(arch string) bool {
    23  	return os.Getenv("DOCKER_ENGINE_GOARCH") != arch
    24  }
    25  
    26  func DaemonIsWindows() bool {
    27  	return testEnv.OSType == "windows"
    28  }
    29  
    30  func DaemonIsWindowsAtLeastBuild(buildNumber int) func() bool {
    31  	return func() bool {
    32  		if testEnv.OSType != "windows" {
    33  			return false
    34  		}
    35  		version := testEnv.DaemonInfo.KernelVersion
    36  		numVersion, _ := strconv.Atoi(strings.Split(version, " ")[1])
    37  		return numVersion >= buildNumber
    38  	}
    39  }
    40  
    41  func DaemonIsLinux() bool {
    42  	return testEnv.OSType == "linux"
    43  }
    44  
    45  func MinimumAPIVersion(version string) func() bool {
    46  	return func() bool {
    47  		return versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), version)
    48  	}
    49  }
    50  
    51  func OnlyDefaultNetworks() bool {
    52  	cli, err := client.NewClientWithOpts(client.FromEnv)
    53  	if err != nil {
    54  		return false
    55  	}
    56  	networks, err := cli.NetworkList(context.TODO(), types.NetworkListOptions{})
    57  	if err != nil || len(networks) > 0 {
    58  		return false
    59  	}
    60  	return true
    61  }
    62  
    63  func IsAmd64() bool {
    64  	return os.Getenv("DOCKER_ENGINE_GOARCH") == "amd64"
    65  }
    66  
    67  func NotArm() bool {
    68  	return ArchitectureIsNot("arm")
    69  }
    70  
    71  func NotArm64() bool {
    72  	return ArchitectureIsNot("arm64")
    73  }
    74  
    75  func NotPpc64le() bool {
    76  	return ArchitectureIsNot("ppc64le")
    77  }
    78  
    79  func UnixCli() bool {
    80  	return isUnixCli
    81  }
    82  
    83  func ExecSupport() bool {
    84  	return supportsExec
    85  }
    86  
    87  func Network() bool {
    88  	// Set a timeout on the GET at 15s
    89  	var timeout = time.Duration(15 * time.Second)
    90  	var url = "https://hub.docker.com"
    91  
    92  	client := http.Client{
    93  		Timeout: timeout,
    94  	}
    95  
    96  	resp, err := client.Get(url)
    97  	if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
    98  		panic(fmt.Sprintf("Timeout for GET request on %s", url))
    99  	}
   100  	if resp != nil {
   101  		resp.Body.Close()
   102  	}
   103  	return err == nil
   104  }
   105  
   106  func Apparmor() bool {
   107  	if strings.HasPrefix(testEnv.DaemonInfo.OperatingSystem, "SUSE Linux Enterprise Server ") {
   108  		return false
   109  	}
   110  	buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
   111  	return err == nil && len(buf) > 1 && buf[0] == 'Y'
   112  }
   113  
   114  func Devicemapper() bool {
   115  	return strings.HasPrefix(testEnv.DaemonInfo.Driver, "devicemapper")
   116  }
   117  
   118  func IPv6() bool {
   119  	cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
   120  	return cmd.Run() != nil
   121  }
   122  
   123  func UserNamespaceROMount() bool {
   124  	// quick case--userns not enabled in this test run
   125  	if os.Getenv("DOCKER_REMAP_ROOT") == "" {
   126  		return true
   127  	}
   128  	if _, _, err := dockerCmdWithError("run", "--rm", "--read-only", "busybox", "date"); err != nil {
   129  		return false
   130  	}
   131  	return true
   132  }
   133  
   134  func NotUserNamespace() bool {
   135  	root := os.Getenv("DOCKER_REMAP_ROOT")
   136  	return root == ""
   137  }
   138  
   139  func UserNamespaceInKernel() bool {
   140  	if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
   141  		/*
   142  		 * This kernel-provided file only exists if user namespaces are
   143  		 * supported
   144  		 */
   145  		return false
   146  	}
   147  
   148  	// We need extra check on redhat based distributions
   149  	if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
   150  		defer f.Close()
   151  		b := make([]byte, 1)
   152  		_, _ = f.Read(b)
   153  		return string(b) != "N"
   154  	}
   155  
   156  	return true
   157  }
   158  
   159  func IsPausable() bool {
   160  	if testEnv.OSType == "windows" {
   161  		return testEnv.DaemonInfo.Isolation == "hyperv"
   162  	}
   163  	return true
   164  }
   165  
   166  func IsolationIs(expectedIsolation string) bool {
   167  	return testEnv.OSType == "windows" && string(testEnv.DaemonInfo.Isolation) == expectedIsolation
   168  }
   169  
   170  func IsolationIsHyperv() bool {
   171  	return IsolationIs("hyperv")
   172  }
   173  
   174  func IsolationIsProcess() bool {
   175  	return IsolationIs("process")
   176  }
   177  
   178  // RegistryHosting returns whether the host can host a registry (v2) or not
   179  func RegistryHosting() bool {
   180  	// for now registry binary is built only if we're running inside
   181  	// container through `make test`. Figure that out by testing if
   182  	// registry binary is in PATH.
   183  	_, err := exec.LookPath(registry.V2binary)
   184  	return err == nil
   185  }
   186  
   187  func SwarmInactive() bool {
   188  	return testEnv.DaemonInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
   189  }
   190  
   191  func TODOBuildkit() bool {
   192  	return os.Getenv("DOCKER_BUILDKIT") == ""
   193  }
   194  
   195  // testRequires checks if the environment satisfies the requirements
   196  // for the test to run or skips the tests.
   197  func testRequires(c requirement.SkipT, requirements ...requirement.Test) {
   198  	requirement.Is(c, requirements...)
   199  }