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