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