environment.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package environment
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/client"
  9. "github.com/pkg/errors"
  10. "golang.org/x/net/context"
  11. )
  12. // Execution contains information about the current test execution and daemon
  13. // under test
  14. type Execution struct {
  15. client client.APIClient
  16. DaemonInfo types.Info
  17. PlatformDefaults PlatformDefaults
  18. protectedElements protectedElements
  19. }
  20. // PlatformDefaults are defaults values for the platform of the daemon under test
  21. type PlatformDefaults struct {
  22. BaseImage string
  23. VolumesConfigPath string
  24. ContainerStoragePath string
  25. }
  26. // New creates a new Execution struct
  27. func New() (*Execution, error) {
  28. client, err := client.NewEnvClient()
  29. if err != nil {
  30. return nil, errors.Wrapf(err, "failed to create client")
  31. }
  32. info, err := client.Info(context.Background())
  33. if err != nil {
  34. return nil, errors.Wrapf(err, "failed to get info from daemon")
  35. }
  36. return &Execution{
  37. client: client,
  38. DaemonInfo: info,
  39. PlatformDefaults: getPlatformDefaults(info),
  40. protectedElements: newProtectedElements(),
  41. }, nil
  42. }
  43. func getPlatformDefaults(info types.Info) PlatformDefaults {
  44. volumesPath := filepath.Join(info.DockerRootDir, "volumes")
  45. containersPath := filepath.Join(info.DockerRootDir, "containers")
  46. switch info.OSType {
  47. case "linux":
  48. return PlatformDefaults{
  49. BaseImage: "scratch",
  50. VolumesConfigPath: toSlash(volumesPath),
  51. ContainerStoragePath: toSlash(containersPath),
  52. }
  53. case "windows":
  54. baseImage := "microsoft/windowsservercore"
  55. if override := os.Getenv("WINDOWS_BASE_IMAGE"); override != "" {
  56. baseImage = override
  57. fmt.Println("INFO: Windows Base image is ", baseImage)
  58. }
  59. return PlatformDefaults{
  60. BaseImage: baseImage,
  61. VolumesConfigPath: filepath.FromSlash(volumesPath),
  62. ContainerStoragePath: filepath.FromSlash(containersPath),
  63. }
  64. default:
  65. panic(fmt.Sprintf("unknown info.OSType for daemon: %s", info.OSType))
  66. }
  67. }
  68. // Make sure in context of daemon, not the local platform. Note we can't
  69. // use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
  70. func toSlash(path string) string {
  71. return strings.Replace(path, `\`, `/`, -1)
  72. }
  73. // IsLocalDaemon is true if the daemon under test is on the same
  74. // host as the CLI.
  75. //
  76. // Deterministically working out the environment in which CI is running
  77. // to evaluate whether the daemon is local or remote is not possible through
  78. // a build tag.
  79. //
  80. // For example Windows to Linux CI under Jenkins tests the 64-bit
  81. // Windows binary build with the daemon build tag, but calls a remote
  82. // Linux daemon.
  83. //
  84. // We can't just say if Windows then assume the daemon is local as at
  85. // some point, we will be testing the Windows CLI against a Windows daemon.
  86. //
  87. // Similarly, it will be perfectly valid to also run CLI tests from
  88. // a Linux CLI (built with the daemon tag) against a Windows daemon.
  89. func (e *Execution) IsLocalDaemon() bool {
  90. return os.Getenv("DOCKER_REMOTE_DAEMON") == ""
  91. }
  92. // Print the execution details to stdout
  93. // TODO: print everything
  94. func (e *Execution) Print() {
  95. if e.IsLocalDaemon() {
  96. fmt.Println("INFO: Testing against a local daemon")
  97. } else {
  98. fmt.Println("INFO: Testing against a remote daemon")
  99. }
  100. }
  101. // APIClient returns an APIClient connected to the daemon under test
  102. func (e *Execution) APIClient() client.APIClient {
  103. return e.client
  104. }