github.com/uppal0016/docker_new@v0.0.0-20240123060250-1c98be13ac2c/integration-cli/requirements.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"os/exec"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/docker/docker/utils"
    13  	"github.com/go-check/check"
    14  )
    15  
    16  type testCondition func() bool
    17  
    18  type testRequirement struct {
    19  	Condition   testCondition
    20  	SkipMessage string
    21  }
    22  
    23  // List test requirements
    24  var (
    25  	DaemonIsWindows = testRequirement{
    26  		func() bool { return daemonPlatform == "windows" },
    27  		"Test requires a Windows daemon",
    28  	}
    29  	DaemonIsLinux = testRequirement{
    30  		func() bool { return daemonPlatform == "linux" },
    31  		"Test requires a Linux daemon",
    32  	}
    33  	NotExperimentalDaemon = testRequirement{
    34  		func() bool { return !utils.ExperimentalBuild() },
    35  		"Test requires a non experimental daemon",
    36  	}
    37  	NotArm = testRequirement{
    38  		func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "arm" },
    39  		"Test requires a daemon not running on ARM",
    40  	}
    41  	NotPpc64le = testRequirement{
    42  		func() bool { return os.Getenv("DOCKER_ENGINE_GOARCH") != "ppc64le" },
    43  		"Test requires a daemon not running on ppc64le",
    44  	}
    45  	SameHostDaemon = testRequirement{
    46  		func() bool { return isLocalDaemon },
    47  		"Test requires docker daemon to run on the same machine as CLI",
    48  	}
    49  	UnixCli = testRequirement{
    50  		func() bool { return isUnixCli },
    51  		"Test requires posix utilities or functionality to run.",
    52  	}
    53  	ExecSupport = testRequirement{
    54  		func() bool { return supportsExec },
    55  		"Test requires 'docker exec' capabilities on the tested daemon.",
    56  	}
    57  	Network = testRequirement{
    58  		func() bool {
    59  			// Set a timeout on the GET at 15s
    60  			var timeout = time.Duration(15 * time.Second)
    61  			var url = "https://hub.docker.com"
    62  
    63  			client := http.Client{
    64  				Timeout: timeout,
    65  			}
    66  
    67  			resp, err := client.Get(url)
    68  			if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
    69  				panic(fmt.Sprintf("Timeout for GET request on %s", url))
    70  			}
    71  			if resp != nil {
    72  				resp.Body.Close()
    73  			}
    74  			return err == nil
    75  		},
    76  		"Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
    77  	}
    78  	Apparmor = testRequirement{
    79  		func() bool {
    80  			buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
    81  			return err == nil && len(buf) > 1 && buf[0] == 'Y'
    82  		},
    83  		"Test requires apparmor is enabled.",
    84  	}
    85  	RegistryHosting = testRequirement{
    86  		func() bool {
    87  			// for now registry binary is built only if we're running inside
    88  			// container through `make test`. Figure that out by testing if
    89  			// registry binary is in PATH.
    90  			_, err := exec.LookPath(v2binary)
    91  			return err == nil
    92  		},
    93  		fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
    94  	}
    95  	NotaryHosting = testRequirement{
    96  		func() bool {
    97  			// for now notary binary is built only if we're running inside
    98  			// container through `make test`. Figure that out by testing if
    99  			// notary-server binary is in PATH.
   100  			_, err := exec.LookPath(notaryServerBinary)
   101  			return err == nil
   102  		},
   103  		fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryServerBinary),
   104  	}
   105  	NotaryServerHosting = testRequirement{
   106  		func() bool {
   107  			// for now notary-server binary is built only if we're running inside
   108  			// container through `make test`. Figure that out by testing if
   109  			// notary-server binary is in PATH.
   110  			_, err := exec.LookPath(notaryServerBinary)
   111  			return err == nil
   112  		},
   113  		fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryServerBinary),
   114  	}
   115  	NotOverlay = testRequirement{
   116  		func() bool {
   117  			return !strings.HasPrefix(daemonStorageDriver, "overlay")
   118  		},
   119  		"Test requires underlying root filesystem not be backed by overlay.",
   120  	}
   121  
   122  	Devicemapper = testRequirement{
   123  		func() bool {
   124  			return strings.HasPrefix(daemonStorageDriver, "devicemapper")
   125  		},
   126  		"Test requires underlying root filesystem to be backed by devicemapper.",
   127  	}
   128  
   129  	IPv6 = testRequirement{
   130  		func() bool {
   131  			cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
   132  
   133  			if err := cmd.Run(); err != nil {
   134  				return true
   135  			}
   136  			return false
   137  		},
   138  		"Test requires support for IPv6",
   139  	}
   140  	NotGCCGO = testRequirement{
   141  		func() bool {
   142  			out, err := exec.Command("go", "version").Output()
   143  			if err == nil && strings.Contains(string(out), "gccgo") {
   144  				return false
   145  			}
   146  			return true
   147  		},
   148  		"Test requires native Golang compiler instead of GCCGO",
   149  	}
   150  	UserNamespaceInKernel = testRequirement{
   151  		func() bool {
   152  			if _, err := os.Stat("/proc/self/uid_map"); os.IsNotExist(err) {
   153  				/*
   154  				 * This kernel-provided file only exists if user namespaces are
   155  				 * supported
   156  				 */
   157  				return false
   158  			}
   159  
   160  			// We need extra check on redhat based distributions
   161  			if f, err := os.Open("/sys/module/user_namespace/parameters/enable"); err == nil {
   162  				b := make([]byte, 1)
   163  				_, _ = f.Read(b)
   164  				if string(b) == "N" {
   165  					return false
   166  				}
   167  				return true
   168  			}
   169  
   170  			return true
   171  		},
   172  		"Kernel must have user namespaces configured and enabled.",
   173  	}
   174  	NotUserNamespace = testRequirement{
   175  		func() bool {
   176  			root := os.Getenv("DOCKER_REMAP_ROOT")
   177  			if root != "" {
   178  				return false
   179  			}
   180  			return true
   181  		},
   182  		"Test cannot be run when remapping root",
   183  	}
   184  )
   185  
   186  // testRequires checks if the environment satisfies the requirements
   187  // for the test to run or skips the tests.
   188  func testRequires(c *check.C, requirements ...testRequirement) {
   189  	for _, r := range requirements {
   190  		if !r.Condition() {
   191  			c.Skip(r.SkipMessage)
   192  		}
   193  	}
   194  }