runtime_unix_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. d := &Daemon{configStore: config.New()}
  22. d.configStore.Root = t.TempDir()
  23. assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))
  24. d.configStore.Runtimes = map[string]types.Runtime{
  25. configuredRtName: configuredRuntime,
  26. }
  27. configureRuntimes(d.configStore)
  28. assert.Assert(t, d.loadRuntimes())
  29. stockRuntime, ok := d.configStore.Runtimes[config.StockRuntimeName]
  30. assert.Assert(t, ok, "stock runtime could not be found (test needs to be updated)")
  31. configdOpts := *stockRuntime.Shim.Opts.(*v2runcoptions.Options)
  32. configdOpts.BinaryName = configuredRuntime.Path
  33. wantConfigdRuntime := configuredRuntime
  34. wantConfigdRuntime.Shim = &types.ShimConfig{
  35. Binary: stockRuntime.Shim.Binary,
  36. Opts: &configdOpts,
  37. }
  38. for _, tt := range []struct {
  39. name, runtime string
  40. want *types.Runtime
  41. }{
  42. {
  43. name: "StockRuntime",
  44. runtime: config.StockRuntimeName,
  45. want: &stockRuntime,
  46. },
  47. {
  48. name: "ShimName",
  49. runtime: "io.containerd.my-shim.v42",
  50. want: &types.Runtime{Shim: &types.ShimConfig{Binary: "io.containerd.my-shim.v42"}},
  51. },
  52. {
  53. // containerd is pretty loose about the format of runtime names. Perhaps too
  54. // loose. The only requirements are that the name contain a dot and (depending
  55. // on the containerd version) not start with a dot. It does not enforce any
  56. // particular format of the dot-delimited components of the name.
  57. name: "VersionlessShimName",
  58. runtime: "io.containerd.my-shim",
  59. want: &types.Runtime{Shim: &types.ShimConfig{Binary: "io.containerd.my-shim"}},
  60. },
  61. {
  62. name: "IllformedShimName",
  63. runtime: "myshim",
  64. },
  65. {
  66. name: "EmptyString",
  67. runtime: "",
  68. },
  69. {
  70. name: "PathToShim",
  71. runtime: "/path/to/runc",
  72. },
  73. {
  74. name: "PathToShimName",
  75. runtime: "/path/to/io.containerd.runc.v2",
  76. },
  77. {
  78. name: "RelPathToShim",
  79. runtime: "my/io.containerd.runc.v2",
  80. },
  81. {
  82. name: "ConfiguredRuntime",
  83. runtime: configuredRtName,
  84. want: &wantConfigdRuntime,
  85. },
  86. } {
  87. tt := tt
  88. t.Run(tt.name, func(t *testing.T) {
  89. got, err := d.getRuntime(tt.runtime)
  90. assert.Check(t, is.DeepEqual(got, tt.want))
  91. if tt.want != nil {
  92. assert.Check(t, err)
  93. } else {
  94. assert.Check(t, errdefs.IsInvalidParameter(err))
  95. }
  96. })
  97. }
  98. }