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