github.com/torfuzx/docker@v1.8.1/integration-cli/requirements.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"net/http"
     9  	"os/exec"
    10  	"strings"
    11  	"time"
    12  
    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  	daemonExecDriver string
    26  
    27  	SameHostDaemon = testRequirement{
    28  		func() bool { return isLocalDaemon },
    29  		"Test requires docker daemon to runs on the same machine as CLI",
    30  	}
    31  	UnixCli = testRequirement{
    32  		func() bool { return isUnixCli },
    33  		"Test requires posix utilities or functionality to run.",
    34  	}
    35  	ExecSupport = testRequirement{
    36  		func() bool { return supportsExec },
    37  		"Test requires 'docker exec' capabilities on the tested daemon.",
    38  	}
    39  	Network = testRequirement{
    40  		func() bool {
    41  			// Set a timeout on the GET at 15s
    42  			var timeout = time.Duration(15 * time.Second)
    43  			var url = "https://hub.docker.com"
    44  
    45  			client := http.Client{
    46  				Timeout: timeout,
    47  			}
    48  
    49  			resp, err := client.Get(url)
    50  			if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
    51  				panic(fmt.Sprintf("Timeout for GET request on %s", url))
    52  			}
    53  			if resp != nil {
    54  				resp.Body.Close()
    55  			}
    56  			return err == nil
    57  		},
    58  		"Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
    59  	}
    60  	Apparmor = testRequirement{
    61  		func() bool {
    62  			buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
    63  			return err == nil && len(buf) > 1 && buf[0] == 'Y'
    64  		},
    65  		"Test requires apparmor is enabled.",
    66  	}
    67  	RegistryHosting = testRequirement{
    68  		func() bool {
    69  			// for now registry binary is built only if we're running inside
    70  			// container through `make test`. Figure that out by testing if
    71  			// registry binary is in PATH.
    72  			_, err := exec.LookPath(v2binary)
    73  			return err == nil
    74  		},
    75  		fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
    76  	}
    77  	NotaryHosting = testRequirement{
    78  		func() bool {
    79  			// for now notary binary is built only if we're running inside
    80  			// container through `make test`. Figure that out by testing if
    81  			// notary-server binary is in PATH.
    82  			_, err := exec.LookPath(notaryBinary)
    83  			return err == nil
    84  		},
    85  		fmt.Sprintf("Test requires an environment that can host %s in the same host", notaryBinary),
    86  	}
    87  	NativeExecDriver = testRequirement{
    88  		func() bool {
    89  			if daemonExecDriver == "" {
    90  				// get daemon info
    91  				status, body, err := sockRequest("GET", "/info", nil)
    92  				if err != nil || status != http.StatusOK {
    93  					log.Fatalf("sockRequest failed for /info: %v", err)
    94  				}
    95  
    96  				type infoJSON struct {
    97  					ExecutionDriver string
    98  				}
    99  				var info infoJSON
   100  				if err = json.Unmarshal(body, &info); err != nil {
   101  					log.Fatalf("unable to unmarshal body: %v", err)
   102  				}
   103  
   104  				daemonExecDriver = info.ExecutionDriver
   105  			}
   106  
   107  			return strings.HasPrefix(daemonExecDriver, "native")
   108  		},
   109  		"Test requires the native (libcontainer) exec driver.",
   110  	}
   111  	NotOverlay = testRequirement{
   112  		func() bool {
   113  			cmd := exec.Command("grep", "^overlay / overlay", "/proc/mounts")
   114  			if err := cmd.Run(); err != nil {
   115  				return true
   116  			}
   117  			return false
   118  		},
   119  		"Test requires underlying root filesystem not be backed by overlay.",
   120  	}
   121  	IPv6 = testRequirement{
   122  		func() bool {
   123  			cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
   124  
   125  			if err := cmd.Run(); err != nil {
   126  				return true
   127  			}
   128  			return false
   129  		},
   130  		"Test requires support for IPv6",
   131  	}
   132  )
   133  
   134  // testRequires checks if the environment satisfies the requirements
   135  // for the test to run or skips the tests.
   136  func testRequires(c *check.C, requirements ...testRequirement) {
   137  	for _, r := range requirements {
   138  		if !r.Condition() {
   139  			c.Skip(r.SkipMessage)
   140  		}
   141  	}
   142  }