|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package plugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/containerd/containerd/services" |
| 23 | +) |
| 24 | + |
| 25 | +func registerClear() { |
| 26 | + register.Lock() |
| 27 | + defer register.Unlock() |
| 28 | + register.r = nil |
| 29 | +} |
| 30 | + |
| 31 | +func mockPluginFilter(*Registration) bool { |
| 32 | + return false |
| 33 | +} |
| 34 | + |
| 35 | +var tasksServiceRequires = []Type{ |
| 36 | + RuntimePlugin, |
| 37 | + RuntimePluginV2, |
| 38 | + MetadataPlugin, |
| 39 | + TaskMonitorPlugin, |
| 40 | +} |
| 41 | + |
| 42 | +// TestContainerdPlugin tests the logic of Graph, use the containerd's plugin |
| 43 | +func TestContainerdPlugin(t *testing.T) { |
| 44 | + registerClear() |
| 45 | + Register(&Registration{ |
| 46 | + Type: TaskMonitorPlugin, |
| 47 | + ID: "cgroups", |
| 48 | + }) |
| 49 | + Register(&Registration{ |
| 50 | + Type: ServicePlugin, |
| 51 | + ID: services.TasksService, |
| 52 | + Requires: tasksServiceRequires, |
| 53 | + }) |
| 54 | + Register(&Registration{ |
| 55 | + Type: ServicePlugin, |
| 56 | + ID: services.IntrospectionService, |
| 57 | + }) |
| 58 | + Register(&Registration{ |
| 59 | + Type: ServicePlugin, |
| 60 | + ID: services.NamespacesService, |
| 61 | + Requires: []Type{ |
| 62 | + MetadataPlugin, |
| 63 | + }, |
| 64 | + }) |
| 65 | + Register(&Registration{ |
| 66 | + Type: GRPCPlugin, |
| 67 | + ID: "namespaces", |
| 68 | + Requires: []Type{ |
| 69 | + ServicePlugin, |
| 70 | + }, |
| 71 | + }) |
| 72 | + Register(&Registration{ |
| 73 | + Type: GRPCPlugin, |
| 74 | + ID: "content", |
| 75 | + Requires: []Type{ |
| 76 | + ServicePlugin, |
| 77 | + }, |
| 78 | + }) |
| 79 | + Register(&Registration{ |
| 80 | + Type: GRPCPlugin, |
| 81 | + ID: "containers", |
| 82 | + Requires: []Type{ |
| 83 | + ServicePlugin, |
| 84 | + }, |
| 85 | + }) |
| 86 | + Register(&Registration{ |
| 87 | + Type: ServicePlugin, |
| 88 | + ID: services.ContainersService, |
| 89 | + Requires: []Type{ |
| 90 | + MetadataPlugin, |
| 91 | + }, |
| 92 | + }) |
| 93 | + Register(&Registration{ |
| 94 | + Type: GRPCPlugin, |
| 95 | + ID: "events", |
| 96 | + }) |
| 97 | + Register(&Registration{ |
| 98 | + Type: GRPCPlugin, |
| 99 | + ID: "leases", |
| 100 | + Requires: []Type{ |
| 101 | + ServicePlugin, |
| 102 | + }, |
| 103 | + }) |
| 104 | + Register(&Registration{ |
| 105 | + Type: ServicePlugin, |
| 106 | + ID: services.LeasesService, |
| 107 | + Requires: []Type{ |
| 108 | + MetadataPlugin, |
| 109 | + }, |
| 110 | + }) |
| 111 | + Register(&Registration{ |
| 112 | + Type: GRPCPlugin, |
| 113 | + ID: "diff", |
| 114 | + Requires: []Type{ |
| 115 | + ServicePlugin, |
| 116 | + }, |
| 117 | + }) |
| 118 | + Register(&Registration{ |
| 119 | + Type: ServicePlugin, |
| 120 | + ID: services.DiffService, |
| 121 | + Requires: []Type{ |
| 122 | + DiffPlugin, |
| 123 | + }, |
| 124 | + }) |
| 125 | + Register(&Registration{ |
| 126 | + Type: ServicePlugin, |
| 127 | + ID: services.SnapshotsService, |
| 128 | + Requires: []Type{ |
| 129 | + MetadataPlugin, |
| 130 | + }, |
| 131 | + }) |
| 132 | + Register(&Registration{ |
| 133 | + Type: GRPCPlugin, |
| 134 | + ID: "snapshots", |
| 135 | + Requires: []Type{ |
| 136 | + ServicePlugin, |
| 137 | + }, |
| 138 | + }) |
| 139 | + Register(&Registration{ |
| 140 | + Type: GRPCPlugin, |
| 141 | + ID: "version", |
| 142 | + }) |
| 143 | + Register(&Registration{ |
| 144 | + Type: GRPCPlugin, |
| 145 | + ID: "images", |
| 146 | + Requires: []Type{ |
| 147 | + ServicePlugin, |
| 148 | + }, |
| 149 | + }) |
| 150 | + Register(&Registration{ |
| 151 | + Type: GCPlugin, |
| 152 | + ID: "scheduler", |
| 153 | + Requires: []Type{ |
| 154 | + MetadataPlugin, |
| 155 | + }, |
| 156 | + }) |
| 157 | + Register(&Registration{ |
| 158 | + Type: RuntimePluginV2, |
| 159 | + ID: "task", |
| 160 | + Requires: []Type{ |
| 161 | + MetadataPlugin, |
| 162 | + }, |
| 163 | + }) |
| 164 | + Register(&Registration{ |
| 165 | + Type: GRPCPlugin, |
| 166 | + ID: "tasks", |
| 167 | + Requires: []Type{ |
| 168 | + ServicePlugin, |
| 169 | + }, |
| 170 | + }) |
| 171 | + Register(&Registration{ |
| 172 | + Type: GRPCPlugin, |
| 173 | + ID: "introspection", |
| 174 | + Requires: []Type{"*"}, |
| 175 | + }) |
| 176 | + Register(&Registration{ |
| 177 | + Type: ServicePlugin, |
| 178 | + ID: services.ContentService, |
| 179 | + Requires: []Type{ |
| 180 | + MetadataPlugin, |
| 181 | + }, |
| 182 | + }) |
| 183 | + Register(&Registration{ |
| 184 | + Type: GRPCPlugin, |
| 185 | + ID: "healthcheck", |
| 186 | + }) |
| 187 | + Register(&Registration{ |
| 188 | + Type: InternalPlugin, |
| 189 | + ID: "opt", |
| 190 | + }) |
| 191 | + Register(&Registration{ |
| 192 | + Type: GRPCPlugin, |
| 193 | + ID: "cri", |
| 194 | + Requires: []Type{ |
| 195 | + ServicePlugin, |
| 196 | + }, |
| 197 | + }) |
| 198 | + Register(&Registration{ |
| 199 | + Type: RuntimePlugin, |
| 200 | + ID: "linux", |
| 201 | + Requires: []Type{ |
| 202 | + MetadataPlugin, |
| 203 | + }, |
| 204 | + }) |
| 205 | + Register(&Registration{ |
| 206 | + Type: InternalPlugin, |
| 207 | + Requires: []Type{ |
| 208 | + ServicePlugin, |
| 209 | + }, |
| 210 | + ID: "restart", |
| 211 | + }) |
| 212 | + Register(&Registration{ |
| 213 | + Type: DiffPlugin, |
| 214 | + ID: "walking", |
| 215 | + Requires: []Type{ |
| 216 | + MetadataPlugin, |
| 217 | + }, |
| 218 | + }) |
| 219 | + Register(&Registration{ |
| 220 | + Type: SnapshotPlugin, |
| 221 | + ID: "native", |
| 222 | + }) |
| 223 | + Register(&Registration{ |
| 224 | + Type: SnapshotPlugin, |
| 225 | + ID: "overlayfs", |
| 226 | + }) |
| 227 | + Register(&Registration{ |
| 228 | + Type: ContentPlugin, |
| 229 | + ID: "content", |
| 230 | + }) |
| 231 | + Register(&Registration{ |
| 232 | + Type: MetadataPlugin, |
| 233 | + ID: "bolt", |
| 234 | + Requires: []Type{ |
| 235 | + ContentPlugin, |
| 236 | + SnapshotPlugin, |
| 237 | + }, |
| 238 | + }) |
| 239 | + |
| 240 | + ordered := Graph(mockPluginFilter) |
| 241 | + expectedURI := []string{ |
| 242 | + "io.containerd.monitor.v1.cgroups", |
| 243 | + "io.containerd.content.v1.content", |
| 244 | + "io.containerd.snapshotter.v1.native", |
| 245 | + "io.containerd.snapshotter.v1.overlayfs", |
| 246 | + "io.containerd.metadata.v1.bolt", |
| 247 | + "io.containerd.runtime.v1.linux", |
| 248 | + "io.containerd.runtime.v2.task", |
| 249 | + "io.containerd.service.v1.tasks-service", |
| 250 | + "io.containerd.service.v1.introspection-service", |
| 251 | + "io.containerd.service.v1.namespaces-service", |
| 252 | + "io.containerd.service.v1.containers-service", |
| 253 | + "io.containerd.service.v1.leases-service", |
| 254 | + "io.containerd.differ.v1.walking", |
| 255 | + "io.containerd.service.v1.diff-service", |
| 256 | + "io.containerd.service.v1.snapshots-service", |
| 257 | + "io.containerd.service.v1.content-service", |
| 258 | + "io.containerd.grpc.v1.namespaces", |
| 259 | + "io.containerd.grpc.v1.content", |
| 260 | + "io.containerd.grpc.v1.containers", |
| 261 | + "io.containerd.grpc.v1.events", |
| 262 | + "io.containerd.grpc.v1.leases", |
| 263 | + "io.containerd.grpc.v1.diff", |
| 264 | + "io.containerd.grpc.v1.snapshots", |
| 265 | + "io.containerd.grpc.v1.version", |
| 266 | + "io.containerd.grpc.v1.images", |
| 267 | + "io.containerd.gc.v1.scheduler", |
| 268 | + "io.containerd.grpc.v1.tasks", |
| 269 | + "io.containerd.grpc.v1.healthcheck", |
| 270 | + "io.containerd.internal.v1.opt", |
| 271 | + "io.containerd.grpc.v1.cri", |
| 272 | + "io.containerd.internal.v1.restart", |
| 273 | + "io.containerd.grpc.v1.introspection", |
| 274 | + } |
| 275 | + cmpOrdered(t, ordered, expectedURI) |
| 276 | +} |
| 277 | + |
| 278 | +func cmpOrdered(t *testing.T, ordered []*Registration, expectedURI []string) { |
| 279 | + if len(ordered) != len(expectedURI) { |
| 280 | + t.Fatalf("ordered compare failed, %d != %d", len(ordered), len(expectedURI)) |
| 281 | + } |
| 282 | + for i := range ordered { |
| 283 | + if ordered[i].URI() != expectedURI[i] { |
| 284 | + t.Fatalf("graph failed, expected: %s, but return: %s", expectedURI[i], ordered[i].URI()) |
| 285 | + } |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +// TestPluginGraph tests the logic of Graph |
| 290 | +func TestPluginGraph(t *testing.T) { |
| 291 | + for _, testcase := range []struct { |
| 292 | + input []*Registration |
| 293 | + expectedURI []string |
| 294 | + }{ |
| 295 | + // test requires * |
| 296 | + { |
| 297 | + input: []*Registration{ |
| 298 | + { |
| 299 | + Type: "grpc", |
| 300 | + ID: "introspection", |
| 301 | + Requires: []Type{ |
| 302 | + "*", |
| 303 | + }, |
| 304 | + }, |
| 305 | + { |
| 306 | + Type: "service", |
| 307 | + ID: "container", |
| 308 | + }, |
| 309 | + }, |
| 310 | + expectedURI: []string{ |
| 311 | + "service.container", |
| 312 | + "grpc.introspection", |
| 313 | + }, |
| 314 | + }, |
| 315 | + // test requires |
| 316 | + { |
| 317 | + input: []*Registration{ |
| 318 | + { |
| 319 | + Type: "service", |
| 320 | + ID: "container", |
| 321 | + Requires: []Type{ |
| 322 | + "metadata", |
| 323 | + }, |
| 324 | + }, |
| 325 | + { |
| 326 | + Type: "metadata", |
| 327 | + ID: "bolt", |
| 328 | + }, |
| 329 | + }, |
| 330 | + expectedURI: []string{ |
| 331 | + "metadata.bolt", |
| 332 | + "service.container", |
| 333 | + }, |
| 334 | + }, |
| 335 | + { |
| 336 | + input: []*Registration{ |
| 337 | + { |
| 338 | + Type: "metadata", |
| 339 | + ID: "bolt", |
| 340 | + Requires: []Type{ |
| 341 | + "content", |
| 342 | + "snapshotter", |
| 343 | + }, |
| 344 | + }, |
| 345 | + { |
| 346 | + Type: "snapshotter", |
| 347 | + ID: "overlayfs", |
| 348 | + }, |
| 349 | + { |
| 350 | + Type: "content", |
| 351 | + ID: "content", |
| 352 | + }, |
| 353 | + }, |
| 354 | + expectedURI: []string{ |
| 355 | + "content.content", |
| 356 | + "snapshotter.overlayfs", |
| 357 | + "metadata.bolt", |
| 358 | + }, |
| 359 | + }, |
| 360 | + // test disable |
| 361 | + { |
| 362 | + input: []*Registration{ |
| 363 | + { |
| 364 | + Type: "content", |
| 365 | + ID: "content", |
| 366 | + }, |
| 367 | + { |
| 368 | + Type: "disable", |
| 369 | + ID: "disable", |
| 370 | + Disable: true, |
| 371 | + }, |
| 372 | + }, |
| 373 | + expectedURI: []string{ |
| 374 | + "content.content", |
| 375 | + }, |
| 376 | + }, |
| 377 | + } { |
| 378 | + registerClear() |
| 379 | + for _, in := range testcase.input { |
| 380 | + Register(in) |
| 381 | + } |
| 382 | + ordered := Graph(mockPluginFilter) |
| 383 | + cmpOrdered(t, ordered, testcase.expectedURI) |
| 384 | + } |
| 385 | +} |
0 commit comments