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