github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/internal/test/environment/environment.go (about) 1 package 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/integration-cli/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.NewEnvClient() 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 CLI. 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 // Print the execution details to stdout 119 // TODO: print everything 120 func (e *Execution) Print() { 121 if e.IsLocalDaemon() { 122 fmt.Println("INFO: Testing against a local daemon") 123 } else { 124 fmt.Println("INFO: Testing against a remote daemon") 125 } 126 } 127 128 // APIClient returns an APIClient connected to the daemon under test 129 func (e *Execution) APIClient() client.APIClient { 130 return e.client 131 } 132 133 // EnsureFrozenImagesLinux loads frozen test images into the daemon 134 // if they aren't already loaded 135 func EnsureFrozenImagesLinux(testEnv *Execution) error { 136 if testEnv.OSType == "linux" { 137 err := load.FrozenImagesLinux(testEnv.APIClient(), frozenImages...) 138 if err != nil { 139 return errors.Wrap(err, "error loading frozen images") 140 } 141 } 142 return nil 143 }