github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/internal/test/environment/testenv.go (about) 1 package environment 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/pkg/errors" 11 "gotest.tools/v3/icmd" 12 "gotest.tools/v3/poll" 13 "gotest.tools/v3/skip" 14 ) 15 16 // Setup a new environment 17 func Setup() error { 18 dockerHost := os.Getenv("TEST_DOCKER_HOST") 19 if dockerHost == "" { 20 return errors.New("$TEST_DOCKER_HOST must be set") 21 } 22 if err := os.Setenv("DOCKER_HOST", dockerHost); err != nil { 23 return err 24 } 25 26 if dockerCertPath := os.Getenv("TEST_DOCKER_CERT_PATH"); dockerCertPath != "" { 27 if err := os.Setenv("DOCKER_CERT_PATH", dockerCertPath); err != nil { 28 return err 29 } 30 if err := os.Setenv("DOCKER_TLS_VERIFY", "1"); err != nil { 31 return err 32 } 33 } 34 35 if kubeConfig := os.Getenv("TEST_KUBECONFIG"); kubeConfig != "" { 36 if err := os.Setenv("KUBECONFIG", kubeConfig); err != nil { 37 return err 38 } 39 } 40 41 if val := boolFromString(os.Getenv("TEST_REMOTE_DAEMON")); val { 42 if err := os.Setenv("REMOTE_DAEMON", "1"); err != nil { 43 return err 44 } 45 } 46 47 if val := boolFromString(os.Getenv("TEST_SKIP_PLUGIN_TESTS")); val { 48 if err := os.Setenv("SKIP_PLUGIN_TESTS", "1"); err != nil { 49 return err 50 } 51 } 52 53 return nil 54 } 55 56 // KubernetesEnabled returns if Kubernetes testing is enabled 57 func KubernetesEnabled() bool { 58 return os.Getenv("KUBECONFIG") != "" 59 } 60 61 // RemoteDaemon returns true if running against a remote daemon 62 func RemoteDaemon() bool { 63 return os.Getenv("REMOTE_DAEMON") != "" 64 } 65 66 // SkipPluginTests returns if plugin tests should be skipped 67 func SkipPluginTests() bool { 68 return os.Getenv("SKIP_PLUGIN_TESTS") != "" 69 } 70 71 // boolFromString determines boolean value from string 72 func boolFromString(val string) bool { 73 switch strings.ToLower(val) { 74 case "true", "1": 75 return true 76 default: 77 return false 78 } 79 } 80 81 // DefaultPollSettings used with gotestyourself/poll 82 var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond) 83 84 // SkipIfNotExperimentalDaemon returns whether the test docker daemon is in experimental mode 85 func SkipIfNotExperimentalDaemon(t *testing.T) { 86 t.Helper() 87 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.ExperimentalBuild}}")) 88 result.Assert(t, icmd.Expected{Err: icmd.None}) 89 experimentalBuild := strings.TrimSpace(result.Stdout()) == "true" 90 skip.If(t, !experimentalBuild, "running against a non-experimental daemon") 91 } 92 93 // SkipIfDaemonNotLinux skips the test unless the running docker daemon is on Linux 94 func SkipIfDaemonNotLinux(t *testing.T) { 95 t.Helper() 96 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.OSType}}")) 97 result.Assert(t, icmd.Expected{Err: icmd.None}) 98 isLinux := strings.TrimSpace(result.Stdout()) == "linux" 99 skip.If(t, !isLinux, "running against a Linux daemon") 100 } 101 102 // SkipIfCgroupNamespacesNotSupported skips the test if the running docker daemon doesn't support cgroup namespaces 103 func SkipIfCgroupNamespacesNotSupported(t *testing.T) { 104 t.Helper() 105 result := icmd.RunCmd(icmd.Command("docker", "info", "--format", "{{.SecurityOptions}}")) 106 result.Assert(t, icmd.Expected{Err: icmd.None}) 107 cgroupNsFound := strings.Contains(result.Stdout(), "name=cgroupns") 108 109 skip.If(t, !cgroupNsFound, fmt.Sprintf("running against a daemon that doesn't support cgroup namespaces (security options: %s)", result.Stdout())) 110 }