github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration/container/mounts_linux_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/container"
    11  	"github.com/docker/docker/api/types/mount"
    12  	"github.com/docker/docker/integration/util/request"
    13  )
    14  
    15  func TestMountDaemonRoot(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	client := request.NewAPIClient(t)
    19  	ctx := context.Background()
    20  	info, err := client.Info(ctx)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	for _, test := range []struct {
    26  		desc        string
    27  		propagation mount.Propagation
    28  		expected    mount.Propagation
    29  	}{
    30  		{
    31  			desc:        "default",
    32  			propagation: "",
    33  			expected:    mount.PropagationRSlave,
    34  		},
    35  		{
    36  			desc:        "private",
    37  			propagation: mount.PropagationPrivate,
    38  		},
    39  		{
    40  			desc:        "rprivate",
    41  			propagation: mount.PropagationRPrivate,
    42  		},
    43  		{
    44  			desc:        "slave",
    45  			propagation: mount.PropagationSlave,
    46  		},
    47  		{
    48  			desc:        "rslave",
    49  			propagation: mount.PropagationRSlave,
    50  			expected:    mount.PropagationRSlave,
    51  		},
    52  		{
    53  			desc:        "shared",
    54  			propagation: mount.PropagationShared,
    55  		},
    56  		{
    57  			desc:        "rshared",
    58  			propagation: mount.PropagationRShared,
    59  			expected:    mount.PropagationRShared,
    60  		},
    61  	} {
    62  		t.Run(test.desc, func(t *testing.T) {
    63  			test := test
    64  			t.Parallel()
    65  
    66  			propagationSpec := fmt.Sprintf(":%s", test.propagation)
    67  			if test.propagation == "" {
    68  				propagationSpec = ""
    69  			}
    70  			bindSpecRoot := info.DockerRootDir + ":" + "/foo" + propagationSpec
    71  			bindSpecSub := filepath.Join(info.DockerRootDir, "containers") + ":/foo" + propagationSpec
    72  
    73  			for name, hc := range map[string]*container.HostConfig{
    74  				"bind root":    {Binds: []string{bindSpecRoot}},
    75  				"bind subpath": {Binds: []string{bindSpecSub}},
    76  				"mount root": {
    77  					Mounts: []mount.Mount{
    78  						{
    79  							Type:        mount.TypeBind,
    80  							Source:      info.DockerRootDir,
    81  							Target:      "/foo",
    82  							BindOptions: &mount.BindOptions{Propagation: test.propagation},
    83  						},
    84  					},
    85  				},
    86  				"mount subpath": {
    87  					Mounts: []mount.Mount{
    88  						{
    89  							Type:        mount.TypeBind,
    90  							Source:      filepath.Join(info.DockerRootDir, "containers"),
    91  							Target:      "/foo",
    92  							BindOptions: &mount.BindOptions{Propagation: test.propagation},
    93  						},
    94  					},
    95  				},
    96  			} {
    97  				t.Run(name, func(t *testing.T) {
    98  					hc := hc
    99  					t.Parallel()
   100  
   101  					c, err := client.ContainerCreate(ctx, &container.Config{
   102  						Image: "busybox",
   103  						Cmd:   []string{"true"},
   104  					}, hc, nil, "")
   105  
   106  					if err != nil {
   107  						if test.expected != "" {
   108  							t.Fatal(err)
   109  						}
   110  						// expected an error, so this is ok and should not continue
   111  						return
   112  					}
   113  					if test.expected == "" {
   114  						t.Fatal("expected create to fail")
   115  					}
   116  
   117  					defer func() {
   118  						if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
   119  							panic(err)
   120  						}
   121  					}()
   122  
   123  					inspect, err := client.ContainerInspect(ctx, c.ID)
   124  					if err != nil {
   125  						t.Fatal(err)
   126  					}
   127  					if len(inspect.Mounts) != 1 {
   128  						t.Fatalf("unexpected number of mounts: %+v", inspect.Mounts)
   129  					}
   130  
   131  					m := inspect.Mounts[0]
   132  					if m.Propagation != test.expected {
   133  						t.Fatalf("got unexpected propagation mode, expected %q, got: %v", test.expected, m.Propagation)
   134  					}
   135  				})
   136  			}
   137  		})
   138  	}
   139  }