github.com/SophiaGitHub/hello@v1.7.1-rc3/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  
    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  	daemonExecDriver string
    25  
    26  	SameHostDaemon = TestRequirement{
    27  		func() bool { return isLocalDaemon },
    28  		"Test requires docker daemon to runs on the same machine as CLI",
    29  	}
    30  	UnixCli = TestRequirement{
    31  		func() bool { return isUnixCli },
    32  		"Test requires posix utilities or functionality to run.",
    33  	}
    34  	ExecSupport = TestRequirement{
    35  		func() bool { return supportsExec },
    36  		"Test requires 'docker exec' capabilities on the tested daemon.",
    37  	}
    38  	Network = TestRequirement{
    39  		func() bool {
    40  			resp, err := http.Get("http://hub.docker.com")
    41  			if resp != nil {
    42  				resp.Body.Close()
    43  			}
    44  			return err == nil
    45  		},
    46  		"Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
    47  	}
    48  	Apparmor = TestRequirement{
    49  		func() bool {
    50  			buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
    51  			return err == nil && len(buf) > 1 && buf[0] == 'Y'
    52  		},
    53  		"Test requires apparmor is enabled.",
    54  	}
    55  	RegistryHosting = TestRequirement{
    56  		func() bool {
    57  			// for now registry binary is built only if we're running inside
    58  			// container through `make test`. Figure that out by testing if
    59  			// registry binary is in PATH.
    60  			_, err := exec.LookPath(v2binary)
    61  			return err == nil
    62  		},
    63  		fmt.Sprintf("Test requires an environment that can host %s in the same host", v2binary),
    64  	}
    65  	NativeExecDriver = TestRequirement{
    66  		func() bool {
    67  			if daemonExecDriver == "" {
    68  				// get daemon info
    69  				status, body, err := sockRequest("GET", "/info", nil)
    70  				if err != nil || status != http.StatusOK {
    71  					log.Fatalf("sockRequest failed for /info: %v", err)
    72  				}
    73  
    74  				type infoJSON struct {
    75  					ExecutionDriver string
    76  				}
    77  				var info infoJSON
    78  				if err = json.Unmarshal(body, &info); err != nil {
    79  					log.Fatalf("unable to unmarshal body: %v", err)
    80  				}
    81  
    82  				daemonExecDriver = info.ExecutionDriver
    83  			}
    84  
    85  			return strings.HasPrefix(daemonExecDriver, "native")
    86  		},
    87  		"Test requires the native (libcontainer) exec driver.",
    88  	}
    89  	NotOverlay = TestRequirement{
    90  		func() bool {
    91  			cmd := exec.Command("grep", "^overlay / overlay", "/proc/mounts")
    92  			if err := cmd.Run(); err != nil {
    93  				return true
    94  			}
    95  			return false
    96  		},
    97  		"Test requires underlying root filesystem not be backed by overlay.",
    98  	}
    99  )
   100  
   101  // testRequires checks if the environment satisfies the requirements
   102  // for the test to run or skips the tests.
   103  func testRequires(c *check.C, requirements ...TestRequirement) {
   104  	for _, r := range requirements {
   105  		if !r.Condition() {
   106  			c.Skip(r.SkipMessage)
   107  		}
   108  	}
   109  }