runtime_unix_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //go:build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/containerd/containerd/plugin"
  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 TestInitRuntimes_InvalidConfigs(t *testing.T) {
  16. cases := []struct {
  17. name string
  18. runtime types.Runtime
  19. expectErr string
  20. }{
  21. {
  22. name: "Empty",
  23. expectErr: "either a runtimeType or a path must be configured",
  24. },
  25. {
  26. name: "ArgsOnly",
  27. runtime: types.Runtime{Args: []string{"foo", "bar"}},
  28. expectErr: "either a runtimeType or a path must be configured",
  29. },
  30. {
  31. name: "OptionsOnly",
  32. runtime: types.Runtime{Options: map[string]interface{}{"hello": "world"}},
  33. expectErr: "either a runtimeType or a path must be configured",
  34. },
  35. {
  36. name: "PathAndType",
  37. runtime: types.Runtime{Path: "/bin/true", Type: "io.containerd.runsc.v1"},
  38. expectErr: "cannot configure both",
  39. },
  40. {
  41. name: "PathAndOptions",
  42. runtime: types.Runtime{Path: "/bin/true", Options: map[string]interface{}{"a": "b"}},
  43. expectErr: "options cannot be used with a path runtime",
  44. },
  45. {
  46. name: "TypeAndArgs",
  47. runtime: types.Runtime{Type: "io.containerd.runsc.v1", Args: []string{"--version"}},
  48. expectErr: "args cannot be used with a runtimeType runtime",
  49. },
  50. {
  51. name: "PathArgsOptions",
  52. runtime: types.Runtime{
  53. Path: "/bin/true",
  54. Args: []string{"--version"},
  55. Options: map[string]interface{}{"hmm": 3},
  56. },
  57. expectErr: "options cannot be used with a path runtime",
  58. },
  59. {
  60. name: "TypeOptionsArgs",
  61. runtime: types.Runtime{
  62. Type: "io.containerd.kata.v2",
  63. Options: map[string]interface{}{"a": "b"},
  64. Args: []string{"--help"},
  65. },
  66. expectErr: "args cannot be used with a runtimeType runtime",
  67. },
  68. {
  69. name: "PathArgsTypeOptions",
  70. runtime: types.Runtime{
  71. Path: "/bin/true",
  72. Args: []string{"foo"},
  73. Type: "io.containerd.runsc.v1",
  74. Options: map[string]interface{}{"a": "b"},
  75. },
  76. expectErr: "cannot configure both",
  77. },
  78. }
  79. for _, tt := range cases {
  80. t.Run(tt.name, func(t *testing.T) {
  81. cfg, err := config.New()
  82. assert.NilError(t, err)
  83. d := &Daemon{configStore: cfg}
  84. d.configStore.Root = t.TempDir()
  85. assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))
  86. err = d.initRuntimes(map[string]types.Runtime{"myruntime": tt.runtime})
  87. assert.Check(t, is.ErrorContains(err, tt.expectErr))
  88. })
  89. }
  90. }
  91. func TestGetRuntime(t *testing.T) {
  92. // Configured runtimes can have any arbitrary name, including names
  93. // which would not be allowed as implicit runtime names. Explicit takes
  94. // precedence over implicit.
  95. const configuredRtName = "my/custom.runtime.v1"
  96. configuredRuntime := types.Runtime{Path: "/bin/true"}
  97. const rtWithArgsName = "withargs"
  98. rtWithArgs := types.Runtime{
  99. Path: "/bin/false",
  100. Args: []string{"--version"},
  101. }
  102. const shimWithOptsName = "shimwithopts"
  103. shimWithOpts := types.Runtime{
  104. Type: plugin.RuntimeRuncV2,
  105. Options: map[string]interface{}{"IoUid": 42},
  106. }
  107. const shimAliasName = "wasmedge"
  108. shimAlias := types.Runtime{Type: "io.containerd.wasmedge.v1"}
  109. const configuredShimByPathName = "shimwithpath"
  110. configuredShimByPath := types.Runtime{Type: "/path/to/my/shim"}
  111. cfg, err := config.New()
  112. assert.NilError(t, err)
  113. d := &Daemon{configStore: cfg}
  114. d.configStore.Root = t.TempDir()
  115. assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))
  116. d.configStore.Runtimes = map[string]types.Runtime{
  117. configuredRtName: configuredRuntime,
  118. rtWithArgsName: rtWithArgs,
  119. shimWithOptsName: shimWithOpts,
  120. shimAliasName: shimAlias,
  121. configuredShimByPathName: configuredShimByPath,
  122. }
  123. configureRuntimes(d.configStore)
  124. assert.Assert(t, d.loadRuntimes())
  125. stockRuntime, ok := d.configStore.Runtimes[config.StockRuntimeName]
  126. assert.Assert(t, ok, "stock runtime could not be found (test needs to be updated)")
  127. configdOpts := *stockRuntime.ShimConfig.Opts.(*v2runcoptions.Options)
  128. configdOpts.BinaryName = configuredRuntime.Path
  129. for _, tt := range []struct {
  130. name, runtime string
  131. wantShim string
  132. wantOpts interface{}
  133. }{
  134. {
  135. name: "StockRuntime",
  136. runtime: config.StockRuntimeName,
  137. wantShim: stockRuntime.ShimConfig.Binary,
  138. wantOpts: stockRuntime.ShimConfig.Opts,
  139. },
  140. {
  141. name: "ShimName",
  142. runtime: "io.containerd.my-shim.v42",
  143. wantShim: "io.containerd.my-shim.v42",
  144. },
  145. {
  146. // containerd is pretty loose about the format of runtime names. Perhaps too
  147. // loose. The only requirements are that the name contain a dot and (depending
  148. // on the containerd version) not start with a dot. It does not enforce any
  149. // particular format of the dot-delimited components of the name.
  150. name: "VersionlessShimName",
  151. runtime: "io.containerd.my-shim",
  152. wantShim: "io.containerd.my-shim",
  153. },
  154. {
  155. name: "IllformedShimName",
  156. runtime: "myshim",
  157. },
  158. {
  159. name: "EmptyString",
  160. runtime: "",
  161. },
  162. {
  163. name: "PathToShim",
  164. runtime: "/path/to/runc",
  165. },
  166. {
  167. name: "PathToShimName",
  168. runtime: "/path/to/io.containerd.runc.v2",
  169. },
  170. {
  171. name: "RelPathToShim",
  172. runtime: "my/io.containerd.runc.v2",
  173. },
  174. {
  175. name: "ConfiguredRuntime",
  176. runtime: configuredRtName,
  177. wantShim: stockRuntime.ShimConfig.Binary,
  178. wantOpts: &configdOpts,
  179. },
  180. {
  181. name: "RuntimeWithArgs",
  182. runtime: rtWithArgsName,
  183. wantShim: stockRuntime.ShimConfig.Binary,
  184. wantOpts: defaultV2ShimConfig(
  185. d.configStore,
  186. d.rewriteRuntimePath(
  187. rtWithArgsName,
  188. rtWithArgs.Path,
  189. rtWithArgs.Args)).Opts,
  190. },
  191. {
  192. name: "ShimWithOpts",
  193. runtime: shimWithOptsName,
  194. wantShim: shimWithOpts.Type,
  195. wantOpts: &v2runcoptions.Options{IoUid: 42},
  196. },
  197. {
  198. name: "ShimAlias",
  199. runtime: shimAliasName,
  200. wantShim: shimAlias.Type,
  201. },
  202. {
  203. name: "ConfiguredShimByPath",
  204. runtime: configuredShimByPathName,
  205. wantShim: configuredShimByPath.Type,
  206. },
  207. } {
  208. tt := tt
  209. t.Run(tt.name, func(t *testing.T) {
  210. gotShim, gotOpts, err := d.getRuntime(tt.runtime)
  211. assert.Check(t, is.Equal(gotShim, tt.wantShim))
  212. assert.Check(t, is.DeepEqual(gotOpts, tt.wantOpts))
  213. if tt.wantShim != "" {
  214. assert.Check(t, err)
  215. } else {
  216. assert.Check(t, errdefs.IsInvalidParameter(err))
  217. }
  218. })
  219. }
  220. }