github.com/ctmnz/docker@v1.6.0-rc3/integration-cli/requirements.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  type TestCondition func() bool
    13  
    14  type TestRequirement struct {
    15  	Condition   TestCondition
    16  	SkipMessage string
    17  }
    18  
    19  // List test requirements
    20  var (
    21  	daemonExecDriver string
    22  
    23  	SameHostDaemon = TestRequirement{
    24  		func() bool { return isLocalDaemon },
    25  		"Test requires docker daemon to runs on the same machine as CLI",
    26  	}
    27  	UnixCli = TestRequirement{
    28  		func() bool { return isUnixCli },
    29  		"Test requires posix utilities or functionality to run.",
    30  	}
    31  	ExecSupport = TestRequirement{
    32  		func() bool { return supportsExec },
    33  		"Test requires 'docker exec' capabilities on the tested daemon.",
    34  	}
    35  	RegistryHosting = TestRequirement{
    36  		func() bool {
    37  			// for now registry binary is built only if we're running inside
    38  			// container through `make test`. Figure that out by testing if
    39  			// registry binary is in PATH.
    40  			_, err := exec.LookPath(v2binary)
    41  			return err == nil
    42  		},
    43  		fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
    44  	}
    45  	NativeExecDriver = TestRequirement{
    46  		func() bool {
    47  			if daemonExecDriver == "" {
    48  				// get daemon info
    49  				body, err := sockRequest("GET", "/info", nil)
    50  				if err != nil {
    51  					log.Fatalf("sockRequest failed for /info: %v", err)
    52  				}
    53  
    54  				type infoJSON struct {
    55  					ExecutionDriver string
    56  				}
    57  				var info infoJSON
    58  				if err = json.Unmarshal(body, &info); err != nil {
    59  					log.Fatalf("unable to unmarshal body: %v", err)
    60  				}
    61  
    62  				daemonExecDriver = info.ExecutionDriver
    63  			}
    64  
    65  			return strings.HasPrefix(daemonExecDriver, "native")
    66  		},
    67  		"Test requires the native (libcontainer) exec driver.",
    68  	}
    69  
    70  	NotOverlay = TestRequirement{
    71  		func() bool {
    72  			cmd := exec.Command("grep", "^overlay / overlay", "/proc/mounts")
    73  			if err := cmd.Run(); err != nil {
    74  				return true
    75  			}
    76  			return false
    77  		},
    78  		"Test requires underlying root filesystem not be backed by overlay.",
    79  	}
    80  )
    81  
    82  // testRequires checks if the environment satisfies the requirements
    83  // for the test to run or skips the tests.
    84  func testRequires(t *testing.T, requirements ...TestRequirement) {
    85  	for _, r := range requirements {
    86  		if !r.Condition() {
    87  			t.Skip(r.SkipMessage)
    88  		}
    89  	}
    90  }