|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestContainerDetailsHealth(t *testing.T) { |
| 10 | + cases := []struct { |
| 11 | + health HealthStatus |
| 12 | + hasCheck bool |
| 13 | + healthy bool |
| 14 | + }{ |
| 15 | + {"", false, false}, |
| 16 | + {HealthNone, false, false}, |
| 17 | + {HealthStarting, true, false}, |
| 18 | + {HealthHealthy, true, true}, |
| 19 | + {HealthUnhealthy, true, false}, |
| 20 | + } |
| 21 | + for _, c := range cases { |
| 22 | + d := ContainerDetails{Health: c.health} |
| 23 | + if got := d.HasHealthcheck(); got != c.hasCheck { |
| 24 | + t.Errorf("HasHealthcheck(%q) = %v, want %v", c.health, got, c.hasCheck) |
| 25 | + } |
| 26 | + if got := d.Healthy(); got != c.healthy { |
| 27 | + t.Errorf("Healthy(%q) = %v, want %v", c.health, got, c.healthy) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestMockContainerInspect(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + m := &MockClient{Details: map[string]ContainerDetails{ |
| 35 | + "shared-postgres": {ID: "abc", Name: "shared-postgres", State: "running", |
| 36 | + Running: true, Health: HealthHealthy}, |
| 37 | + }} |
| 38 | + |
| 39 | + d, err := m.ContainerInspect(ctx, "shared-postgres") |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + if !d.Running || !d.Healthy() { |
| 44 | + t.Errorf("inspect = %+v, want running+healthy", d) |
| 45 | + } |
| 46 | + |
| 47 | + if _, err := m.ContainerInspect(ctx, "ghost"); err == nil { |
| 48 | + t.Error("inspect of unseeded container should error") |
| 49 | + } |
| 50 | + |
| 51 | + sentinel := errors.New("boom") |
| 52 | + m.InspectErr = sentinel |
| 53 | + if _, err := m.ContainerInspect(ctx, "shared-postgres"); !errors.Is(err, sentinel) { |
| 54 | + t.Errorf("InspectErr not propagated: %v", err) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestMockContainerLogsTail(t *testing.T) { |
| 59 | + ctx := context.Background() |
| 60 | + m := &MockClient{LogLines: map[string]string{ |
| 61 | + "api": "l1\nl2\nl3\nl4\nl5\n", |
| 62 | + }} |
| 63 | + |
| 64 | + all, err := m.ContainerLogs(ctx, "api", 0) |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if all != "l1\nl2\nl3\nl4\nl5\n" { |
| 69 | + t.Errorf("tail=0 = %q, want all lines", all) |
| 70 | + } |
| 71 | + |
| 72 | + last2, err := m.ContainerLogs(ctx, "api", 2) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + if last2 != "l4\nl5\n" { |
| 77 | + t.Errorf("tail=2 = %q, want last 2 lines", last2) |
| 78 | + } |
| 79 | + |
| 80 | + // More requested than present → all returned, untouched. |
| 81 | + if got, _ := m.ContainerLogs(ctx, "api", 99); got != "l1\nl2\nl3\nl4\nl5\n" { |
| 82 | + t.Errorf("tail=99 = %q, want all lines", got) |
| 83 | + } |
| 84 | + |
| 85 | + // Unknown container → empty, no error (mirrors an empty log). |
| 86 | + if got, _ := m.ContainerLogs(ctx, "ghost", 5); got != "" { |
| 87 | + t.Errorf("unknown container logs = %q, want empty", got) |
| 88 | + } |
| 89 | + |
| 90 | + sentinel := errors.New("nope") |
| 91 | + m.LogsErr = sentinel |
| 92 | + if _, err := m.ContainerLogs(ctx, "api", 1); !errors.Is(err, sentinel) { |
| 93 | + t.Errorf("LogsErr not propagated: %v", err) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestLastLines(t *testing.T) { |
| 98 | + cases := []struct { |
| 99 | + in string |
| 100 | + n int |
| 101 | + want string |
| 102 | + }{ |
| 103 | + {"", 5, ""}, |
| 104 | + {"a\nb\nc\n", 0, ""}, |
| 105 | + {"a\nb\nc\n", 2, "b\nc\n"}, |
| 106 | + {"a\nb\nc", 2, "b\nc"}, // no trailing newline preserved |
| 107 | + {"a\nb\nc\n", 10, "a\nb\nc\n"}, |
| 108 | + {"solo", 1, "solo"}, |
| 109 | + } |
| 110 | + for _, c := range cases { |
| 111 | + if got := lastLines(c.in, c.n); got != c.want { |
| 112 | + t.Errorf("lastLines(%q, %d) = %q, want %q", c.in, c.n, got, c.want) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments