github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/devices_windows_test.go (about)

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