github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/integration-cli/requirements_test.go (about)

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