github.com/uriddle/docker@v0.0.0-20210926094723-4072e6aeb013/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/go-check/check"
    13  )
    14  
    15  type testCondition func() bool
    16  
    17  type testRequirement struct {
    18  	Condition   testCondition
    19  	SkipMessage string
    20  }
    21  
    22  // List test requirements
    23  var (
    24  	DaemonIsWindows = testRequirement{
    25  		func() bool { return daemonPlatform == "windows" },
    26  		"Test requires a Windows daemon",
    27  	}
    28  	DaemonIsLinux = testRequirement{
    29  		func() bool { return daemonPlatform == "linux" },
    30  		"Test requires a Linux daemon",
    31  	}
    32  	SameHostDaemon = testRequirement{
    33  		func() bool { return isLocalDaemon },
    34  		"Test requires docker daemon to run on the same machine as CLI",
    35  	}
    36  	UnixCli = testRequirement{
    37  		func() bool { return isUnixCli },
    38  		"Test requires posix utilities or functionality to run.",
    39  	}
    40  	ExecSupport = testRequirement{
    41  		func() bool { return supportsExec },
    42  		"Test requires 'docker exec' capabilities on the tested daemon.",
    43  	}
    44  	Network = testRequirement{
    45  		func() bool {
    46  			// Set a timeout on the GET at 15s
    47  			var timeout = time.Duration(15 * time.Second)
    48  			var url = "https://hub.docker.com"
    49  
    50  			client := http.Client{
    51  				Timeout: timeout,
    52  			}
    53  
    54  			resp, err := client.Get(url)
    55  			if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
    56  				panic(fmt.Sprintf("Timeout for GET request on %s", url))
    57  			}
    58  			if resp != nil {
    59  				resp.Body.Close()
    60  			}
    61  			return err == nil
    62  		},
    63  		"Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
    64  	}
    65  	Apparmor = testRequirement{
    66  		func() bool {
    67  			buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
    68  			return err == nil && len(buf) > 1 && buf[0] == 'Y'
    69  		},
    70  		"Test requires apparmor is enabled.",
    71  	}
    72  	RegistryHosting = testRequirement{
    73  		func() bool {
    74  			// for now registry binary is built only if we're running inside
    75  			// container through `make test`. Figure that out by testing if
    76  			// registry binary is in PATH.
    77  			_, err := exec.LookPath(v2binary)
    78  			return err == nil
    79  		},
    80  		fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
    81  	}
    82  	NotaryHosting = testRequirement{
    83  		func() bool {
    84  			// for now notary binary is built only if we're running inside
    85  			// container through `make test`. Figure that out by testing if
    86  			// notary-server binary is in PATH.
    87  			_, err := exec.LookPath(notaryBinary)
    88  			return err == nil
    89  		},
    90  		fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryBinary),
    91  	}
    92  	NotOverlay = testRequirement{
    93  		func() bool {
    94  			cmd := exec.Command("grep", "^overlay / overlay", "/proc/mounts")
    95  			if err := cmd.Run(); err != nil {
    96  				return true
    97  			}
    98  			return false
    99  		},
   100  		"Test requires underlying root filesystem not be backed by overlay.",
   101  	}
   102  	IPv6 = testRequirement{
   103  		func() bool {
   104  			cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
   105  
   106  			if err := cmd.Run(); err != nil {
   107  				return true
   108  			}
   109  			return false
   110  		},
   111  		"Test requires support for IPv6",
   112  	}
   113  	NotGCCGO = testRequirement{
   114  		func() bool {
   115  			out, err := exec.Command("go", "version").Output()
   116  			if err == nil && strings.Contains(string(out), "gccgo") {
   117  				return false
   118  			}
   119  			return true
   120  		},
   121  		"Test requires native Golang compiler instead of GCCGO",
   122  	}
   123  	NotUserNamespace = testRequirement{
   124  		func() bool {
   125  			root := os.Getenv("DOCKER_REMAP_ROOT")
   126  			if root != "" {
   127  				return false
   128  			}
   129  			return true
   130  		},
   131  		"Test cannot be run when remapping root",
   132  	}
   133  )
   134  
   135  // testRequires checks if the environment satisfies the requirements
   136  // for the test to run or skips the tests.
   137  func testRequires(c *check.C, requirements ...testRequirement) {
   138  	for _, r := range requirements {
   139  		if !r.Condition() {
   140  			c.Skip(r.SkipMessage)
   141  		}
   142  	}
   143  }