|
17 | 17 | package sbserver |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "context" |
20 | 21 | "math" |
| 22 | + "reflect" |
21 | 23 | "testing" |
22 | 24 | "time" |
23 | 25 |
|
24 | 26 | v1 "github.com/containerd/cgroups/v3/cgroup1/stats" |
25 | 27 | v2 "github.com/containerd/cgroups/v3/cgroup2/stats" |
| 28 | + "github.com/containerd/containerd/api/types" |
26 | 29 | containerstore "github.com/containerd/containerd/pkg/cri/store/container" |
| 30 | + sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox" |
27 | 31 | "github.com/stretchr/testify/assert" |
28 | 32 | runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
29 | 33 | ) |
@@ -328,3 +332,103 @@ func TestContainerMetricsMemory(t *testing.T) { |
328 | 332 | }) |
329 | 333 | } |
330 | 334 | } |
| 335 | + |
| 336 | +func TestListContainerStats(t *testing.T) { |
| 337 | + c := newTestCRIService() |
| 338 | + type args struct { |
| 339 | + ctx context.Context |
| 340 | + stats []*types.Metric |
| 341 | + containers []containerstore.Container |
| 342 | + } |
| 343 | + tests := []struct { |
| 344 | + name string |
| 345 | + args args |
| 346 | + before func() |
| 347 | + after func() |
| 348 | + want *runtime.ListContainerStatsResponse |
| 349 | + wantErr bool |
| 350 | + }{ |
| 351 | + { |
| 352 | + name: "args containers having c1,but containerStore not found c1, so filter c1", |
| 353 | + args: args{ |
| 354 | + ctx: context.Background(), |
| 355 | + stats: []*types.Metric{ |
| 356 | + { |
| 357 | + ID: "c1", |
| 358 | + }, |
| 359 | + }, |
| 360 | + containers: []containerstore.Container{ |
| 361 | + { |
| 362 | + Metadata: containerstore.Metadata{ |
| 363 | + ID: "c1", |
| 364 | + SandboxID: "s1", |
| 365 | + }, |
| 366 | + }, |
| 367 | + }, |
| 368 | + }, |
| 369 | + want: &runtime.ListContainerStatsResponse{}, |
| 370 | + }, |
| 371 | + { |
| 372 | + name: "args containers having c1,c2, but containerStore not found c1, so filter c1", |
| 373 | + args: args{ |
| 374 | + ctx: context.Background(), |
| 375 | + stats: []*types.Metric{ |
| 376 | + { |
| 377 | + ID: "c1", |
| 378 | + }, |
| 379 | + { |
| 380 | + ID: "c2", |
| 381 | + }, |
| 382 | + }, |
| 383 | + containers: []containerstore.Container{ |
| 384 | + { |
| 385 | + Metadata: containerstore.Metadata{ |
| 386 | + ID: "c1", |
| 387 | + SandboxID: "s1", |
| 388 | + }, |
| 389 | + }, |
| 390 | + { |
| 391 | + Metadata: containerstore.Metadata{ |
| 392 | + ID: "c2", |
| 393 | + SandboxID: "s2", |
| 394 | + }, |
| 395 | + }, |
| 396 | + }, |
| 397 | + }, |
| 398 | + before: func() { |
| 399 | + c.containerStore.Add(containerstore.Container{ |
| 400 | + Metadata: containerstore.Metadata{ |
| 401 | + ID: "c2", |
| 402 | + }, |
| 403 | + }) |
| 404 | + c.sandboxStore.Add(sandboxstore.Sandbox{ |
| 405 | + Metadata: sandboxstore.Metadata{ |
| 406 | + ID: "s2", |
| 407 | + }, |
| 408 | + }) |
| 409 | + }, |
| 410 | + wantErr: true, |
| 411 | + want: nil, |
| 412 | + }, |
| 413 | + } |
| 414 | + |
| 415 | + for _, tt := range tests { |
| 416 | + t.Run(tt.name, func(t *testing.T) { |
| 417 | + if tt.before != nil { |
| 418 | + tt.before() |
| 419 | + } |
| 420 | + got, err := c.toCRIContainerStats(tt.args.ctx, tt.args.stats, tt.args.containers) |
| 421 | + if tt.after != nil { |
| 422 | + tt.after() |
| 423 | + } |
| 424 | + if (err != nil) != tt.wantErr { |
| 425 | + t.Errorf("ListContainerStats() error = %v, wantErr %v", err, tt.wantErr) |
| 426 | + return |
| 427 | + } |
| 428 | + if !reflect.DeepEqual(got, tt.want) { |
| 429 | + t.Errorf("ListContainerStats() = %v, want %v", got, tt.want) |
| 430 | + } |
| 431 | + }) |
| 432 | + } |
| 433 | + |
| 434 | +} |
0 commit comments