github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/daemon/runtime_unix_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package daemon
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
    12  	"gotest.tools/v3/assert"
    13  	is "gotest.tools/v3/assert/cmp"
    14  
    15  	"github.com/docker/docker/api/types"
    16  	"github.com/docker/docker/daemon/config"
    17  	"github.com/docker/docker/errdefs"
    18  )
    19  
    20  func TestGetRuntime(t *testing.T) {
    21  	// Configured runtimes can have any arbitrary name, including names
    22  	// which would not be allowed as implicit runtime names. Explicit takes
    23  	// precedence over implicit.
    24  	const configuredRtName = "my/custom.shim.v1"
    25  	configuredRuntime := types.Runtime{Path: "/bin/true"}
    26  
    27  	cfg, err := config.New()
    28  	assert.NilError(t, err)
    29  
    30  	d := &Daemon{configStore: cfg}
    31  	d.configStore.Root = t.TempDir()
    32  	assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))
    33  	d.configStore.Runtimes = map[string]types.Runtime{
    34  		configuredRtName: configuredRuntime,
    35  	}
    36  	configureRuntimes(d.configStore)
    37  	assert.Assert(t, d.loadRuntimes())
    38  
    39  	stockRuntime, ok := d.configStore.Runtimes[config.StockRuntimeName]
    40  	assert.Assert(t, ok, "stock runtime could not be found (test needs to be updated)")
    41  
    42  	configdOpts := *stockRuntime.Shim.Opts.(*v2runcoptions.Options)
    43  	configdOpts.BinaryName = configuredRuntime.Path
    44  	wantConfigdRuntime := configuredRuntime
    45  	wantConfigdRuntime.Shim = &types.ShimConfig{
    46  		Binary: stockRuntime.Shim.Binary,
    47  		Opts:   &configdOpts,
    48  	}
    49  
    50  	for _, tt := range []struct {
    51  		name, runtime string
    52  		want          *types.Runtime
    53  	}{
    54  		{
    55  			name:    "StockRuntime",
    56  			runtime: config.StockRuntimeName,
    57  			want:    &stockRuntime,
    58  		},
    59  		{
    60  			name:    "ShimName",
    61  			runtime: "io.containerd.my-shim.v42",
    62  			want:    &types.Runtime{Shim: &types.ShimConfig{Binary: "io.containerd.my-shim.v42"}},
    63  		},
    64  		{
    65  			// containerd is pretty loose about the format of runtime names. Perhaps too
    66  			// loose. The only requirements are that the name contain a dot and (depending
    67  			// on the containerd version) not start with a dot. It does not enforce any
    68  			// particular format of the dot-delimited components of the name.
    69  			name:    "VersionlessShimName",
    70  			runtime: "io.containerd.my-shim",
    71  			want:    &types.Runtime{Shim: &types.ShimConfig{Binary: "io.containerd.my-shim"}},
    72  		},
    73  		{
    74  			name:    "IllformedShimName",
    75  			runtime: "myshim",
    76  		},
    77  		{
    78  			name:    "EmptyString",
    79  			runtime: "",
    80  		},
    81  		{
    82  			name:    "PathToShim",
    83  			runtime: "/path/to/runc",
    84  		},
    85  		{
    86  			name:    "PathToShimName",
    87  			runtime: "/path/to/io.containerd.runc.v2",
    88  		},
    89  		{
    90  			name:    "RelPathToShim",
    91  			runtime: "my/io.containerd.runc.v2",
    92  		},
    93  		{
    94  			name:    "ConfiguredRuntime",
    95  			runtime: configuredRtName,
    96  			want:    &wantConfigdRuntime,
    97  		},
    98  	} {
    99  		tt := tt
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			got, err := d.getRuntime(tt.runtime)
   102  			assert.Check(t, is.DeepEqual(got, tt.want))
   103  			if tt.want != nil {
   104  				assert.Check(t, err)
   105  			} else {
   106  				assert.Check(t, errdefs.IsInvalidParameter(err))
   107  			}
   108  		})
   109  	}
   110  }