devices_windows_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/integration/internal/container"
  10. "gotest.tools/v3/assert"
  11. "gotest.tools/v3/poll"
  12. "gotest.tools/v3/skip"
  13. )
  14. // TestWindowsDevices that Windows Devices are correctly propagated
  15. // via HostConfig.Devices through to the implementation in hcsshim.
  16. func TestWindowsDevices(t *testing.T) {
  17. skip.If(t, testEnv.DaemonInfo.OSType != "windows")
  18. defer setupTest(t)()
  19. client := testEnv.APIClient()
  20. ctx := context.Background()
  21. testData := []struct {
  22. doc string
  23. devices []string
  24. isolation containertypes.Isolation
  25. expectedStartFailure bool
  26. expectedStartFailureMessage string
  27. expectedExitCode int
  28. expectedStdout string
  29. expectedStderr string
  30. }{
  31. {
  32. doc: "process/no device mounted",
  33. isolation: containertypes.IsolationProcess,
  34. expectedExitCode: 1,
  35. },
  36. {
  37. doc: "process/class/5B45201D-F2F2-4F3B-85BB-30FF1F953599 mounted",
  38. devices: []string{"class/5B45201D-F2F2-4F3B-85BB-30FF1F953599"},
  39. isolation: containertypes.IsolationProcess,
  40. expectedStdout: "/Windows/System32/HostDriverStore/FileRepository",
  41. },
  42. {
  43. doc: "hyperv/no device mounted",
  44. isolation: containertypes.IsolationHyperV,
  45. expectedExitCode: 1,
  46. },
  47. {
  48. doc: "hyperv/class/5B45201D-F2F2-4F3B-85BB-30FF1F953599 mounted",
  49. devices: []string{"class/5B45201D-F2F2-4F3B-85BB-30FF1F953599"},
  50. isolation: containertypes.IsolationHyperV,
  51. expectedStartFailure: !testEnv.RuntimeIsWindowsContainerd(),
  52. expectedStartFailureMessage: "device assignment is not supported for HyperV containers",
  53. expectedStdout: "/Windows/System32/HostDriverStore/FileRepository",
  54. },
  55. }
  56. for _, d := range testData {
  57. d := d
  58. t.Run(d.doc, func(t *testing.T) {
  59. t.Parallel()
  60. deviceOptions := []func(*container.TestContainerConfig){container.WithIsolation(d.isolation)}
  61. for _, deviceName := range d.devices {
  62. deviceOptions = append(deviceOptions, container.WithWindowsDevice(deviceName))
  63. }
  64. id := container.Create(ctx, t, client, deviceOptions...)
  65. // Hyper-V isolation is failing even with no actual devices added.
  66. // TODO: Once https://github.com/moby/moby/issues/43395 is resolved,
  67. // remove this skip.If and validate the expected behaviour under Hyper-V.
  68. skip.If(t, d.isolation == containertypes.IsolationHyperV && !d.expectedStartFailure, "FIXME. HyperV isolation setup is probably incorrect in the test")
  69. err := client.ContainerStart(ctx, id, types.ContainerStartOptions{})
  70. if d.expectedStartFailure {
  71. assert.ErrorContains(t, err, d.expectedStartFailureMessage)
  72. return
  73. }
  74. assert.NilError(t, err)
  75. poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
  76. // /Windows/System32/HostDriverStore is mounted from the host when class GUID 5B45201D-F2F2-4F3B-85BB-30FF1F953599
  77. // is mounted. See `C:\windows\System32\containers\devices.def` on a Windows host for (slightly more) details.
  78. res, err := container.Exec(ctx, client, id, []string{"sh", "-c",
  79. "ls -d /Windows/System32/HostDriverStore/* | grep /Windows/System32/HostDriverStore/FileRepository"})
  80. assert.NilError(t, err)
  81. assert.Equal(t, d.expectedExitCode, res.ExitCode)
  82. if d.expectedExitCode == 0 {
  83. assert.Equal(t, d.expectedStdout, strings.TrimSpace(res.Stdout()))
  84. assert.Equal(t, d.expectedStderr, strings.TrimSpace(res.Stderr()))
  85. }
  86. })
  87. }
  88. }