github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/integration/requirements.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"runtime"
     8  	"strings"
     9  	"time"
    10  
    11  	check "gopkg.in/check.v1"
    12  )
    13  
    14  type testCondition func() bool
    15  
    16  type testRequirement struct {
    17  	Condition   testCondition
    18  	SkipMessage string
    19  }
    20  
    21  // List test requirements
    22  var (
    23  	IsWindows = testRequirement{
    24  		func() bool { return runtime.GOOS == "windows" },
    25  		"Test requires a Windows daemon",
    26  	}
    27  	IsLinux = testRequirement{
    28  		func() bool { return runtime.GOOS == "linux" },
    29  		"Test requires a Linux daemon",
    30  	}
    31  	Network = testRequirement{
    32  		func() bool {
    33  			// Set a timeout on the GET at 15s
    34  			var timeout = 15 * time.Second
    35  			var url = "https://hub.docker.com"
    36  
    37  			client := http.Client{
    38  				Timeout: timeout,
    39  			}
    40  
    41  			resp, err := client.Get(url)
    42  			if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
    43  				panic(fmt.Sprintf("Timeout for GET request on %s", url))
    44  			}
    45  			if resp != nil {
    46  				resp.Body.Close()
    47  			}
    48  			return err == nil
    49  		},
    50  		"Test requires network availability, environment variable set to none to run in a non-network enabled mode.",
    51  	}
    52  )
    53  
    54  func not(r testRequirement) testRequirement {
    55  	return testRequirement{
    56  		func() bool {
    57  			return !r.Condition()
    58  		},
    59  		fmt.Sprintf("Not(%s)", r.SkipMessage),
    60  	}
    61  }
    62  
    63  func DaemonVersionIs(version string) testRequirement {
    64  	return testRequirement{
    65  		func() bool {
    66  			return strings.Contains(os.Getenv("DOCKER_DAEMON_VERSION"), version)
    67  		},
    68  		"Test requires the daemon version to be " + version,
    69  	}
    70  }
    71  
    72  // testRequires checks if the environment satisfies the requirements
    73  // for the test to run or skips the tests.
    74  func testRequires(c *check.C, requirements ...testRequirement) {
    75  	for _, r := range requirements {
    76  		if !r.Condition() {
    77  			c.Skip(r.SkipMessage)
    78  		}
    79  	}
    80  }