github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/internal/test/environment/environment.go (about) 1 package environment // import "github.com/docker/docker/internal/test/environment" 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/client" 11 "github.com/docker/docker/internal/test/fixtures/load" 12 "github.com/pkg/errors" 13 "golang.org/x/net/context" 14 ) 15 16 // Execution contains information about the current test execution and daemon 17 // under test 18 type Execution struct { 19 client client.APIClient 20 DaemonInfo types.Info 21 OSType string 22 PlatformDefaults PlatformDefaults 23 protectedElements protectedElements 24 } 25 26 // PlatformDefaults are defaults values for the platform of the daemon under test 27 type PlatformDefaults struct { 28 BaseImage string 29 VolumesConfigPath string 30 ContainerStoragePath string 31 } 32 33 // New creates a new Execution struct 34 func New() (*Execution, error) { 35 client, err := client.NewClientWithOpts(client.FromEnv) 36 if err != nil { 37 return nil, errors.Wrapf(err, "failed to create client") 38 } 39 40 info, err := client.Info(context.Background()) 41 if err != nil { 42 return nil, errors.Wrapf(err, "failed to get info from daemon") 43 } 44 45 osType := getOSType(info) 46 47 return &Execution{ 48 client: client, 49 DaemonInfo: info, 50 OSType: osType, 51 PlatformDefaults: getPlatformDefaults(info, osType), 52 protectedElements: newProtectedElements(), 53 }, nil 54 } 55 56 func getOSType(info types.Info) string { 57 // Docker EE does not set the OSType so allow the user to override this value. 58 userOsType := os.Getenv("TEST_OSTYPE") 59 if userOsType != "" { 60 return userOsType 61 } 62 return info.OSType 63 } 64 65 func getPlatformDefaults(info types.Info, osType string) PlatformDefaults { 66 volumesPath := filepath.Join(info.DockerRootDir, "volumes") 67 containersPath := filepath.Join(info.DockerRootDir, "containers") 68 69 switch osType { 70 case "linux": 71 return PlatformDefaults{ 72 BaseImage: "scratch", 73 VolumesConfigPath: toSlash(volumesPath), 74 ContainerStoragePath: toSlash(containersPath), 75 } 76 case "windows": 77 baseImage := "microsoft/windowsservercore" 78 if override := os.Getenv("WINDOWS_BASE_IMAGE"); override != "" { 79 baseImage = override 80 fmt.Println("INFO: Windows Base image is ", baseImage) 81 } 82 return PlatformDefaults{ 83 BaseImage: baseImage, 84 VolumesConfigPath: filepath.FromSlash(volumesPath), 85 ContainerStoragePath: filepath.FromSlash(containersPath), 86 } 87 default: 88 panic(fmt.Sprintf("unknown OSType for daemon: %s", osType)) 89 } 90 } 91 92 // Make sure in context of daemon, not the local platform. Note we can't 93 // use filepath.FromSlash or ToSlash here as they are a no-op on Unix. 94 func toSlash(path string) string { 95 return strings.Replace(path, `\`, `/`, -1) 96 } 97 98 // IsLocalDaemon is true if the daemon under test is on the same 99 // host as the test process. 100 // 101 // Deterministically working out the environment in which CI is running 102 // to evaluate whether the daemon is local or remote is not possible through 103 // a build tag. 104 // 105 // For example Windows to Linux CI under Jenkins tests the 64-bit 106 // Windows binary build with the daemon build tag, but calls a remote 107 // Linux daemon. 108 // 109 // We can't just say if Windows then assume the daemon is local as at 110 // some point, we will be testing the Windows CLI against a Windows daemon. 111 // 112 // Similarly, it will be perfectly valid to also run CLI tests from 113 // a Linux CLI (built with the daemon tag) against a Windows daemon. 114 func (e *Execution) IsLocalDaemon() bool { 115 return os.Getenv("DOCKER_REMOTE_DAEMON") == "" 116 } 117 118 // IsRemoteDaemon is true if the daemon under test is on different host 119 // as the test process. 120 func (e *Execution) IsRemoteDaemon() bool { 121 return !e.IsLocalDaemon() 122 } 123 124 // DaemonAPIVersion returns the negociated daemon api version 125 func (e *Execution) DaemonAPIVersion() string { 126 version, err := e.APIClient().ServerVersion(context.TODO()) 127 if err != nil { 128 return "" 129 } 130 return version.APIVersion 131 } 132 133 // Print the execution details to stdout 134 // TODO: print everything 135 func (e *Execution) Print() { 136 if e.IsLocalDaemon() { 137 fmt.Println("INFO: Testing against a local daemon") 138 } else { 139 fmt.Println("INFO: Testing against a remote daemon") 140 } 141 } 142 143 // APIClient returns an APIClient connected to the daemon under test 144 func (e *Execution) APIClient() client.APIClient { 145 return e.client 146 } 147 148 // EnsureFrozenImagesLinux loads frozen test images into the daemon 149 // if they aren't already loaded 150 func EnsureFrozenImagesLinux(testEnv *Execution) error { 151 if testEnv.OSType == "linux" { 152 err := load.FrozenImagesLinux(testEnv.APIClient(), frozenImages...) 153 if err != nil { 154 return errors.Wrap(err, "error loading frozen images") 155 } 156 } 157 return nil 158 }