runtime_unix_test.go 2.9 KB

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