|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/replicatedhq/embedded-cluster/e2e/docker" |
| 10 | + "github.com/replicatedhq/embedded-cluster/pkg/preflights" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPreflights(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + cli := docker.NewCLI(t) |
| 17 | + |
| 18 | + container := docker.NewContainer(t). |
| 19 | + WithImage("debian:bookworm-slim"). |
| 20 | + WithECBinary() |
| 21 | + if licensePath := os.Getenv("LICENSE_PATH"); licensePath != "" { |
| 22 | + t.Logf("using license %s", licensePath) |
| 23 | + container = container.WithLicense(licensePath) |
| 24 | + } |
| 25 | + container.Start(cli) |
| 26 | + |
| 27 | + t.Cleanup(func() { |
| 28 | + container.Destroy(cli) |
| 29 | + }) |
| 30 | + |
| 31 | + _, stderr, err := container.Exec(cli, |
| 32 | + "apt-get update && apt-get install -y apt-utils kmod", |
| 33 | + ) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("failed to install deps: err=%v, stderr=%s", err, stderr) |
| 36 | + } |
| 37 | + |
| 38 | + runCmd := fmt.Sprintf("%s install run-preflights --no-prompt", container.GetECBinaryPath()) |
| 39 | + if os.Getenv("LICENSE_PATH") != "" { |
| 40 | + runCmd = fmt.Sprintf("%s --license %s", runCmd, container.GetLicensePath()) |
| 41 | + } |
| 42 | + |
| 43 | + // we are more interested in the results |
| 44 | + runStdout, runStderr, runErr := container.Exec(cli, runCmd) |
| 45 | + |
| 46 | + stdout, stderr, err := container.Exec(cli, |
| 47 | + "cat /var/lib/embedded-cluster/support/host-preflight-results.json", |
| 48 | + ) |
| 49 | + if err != nil { |
| 50 | + t.Logf("run-preflights: err=%v, stdout=%s, stderr=%s", runErr, runStdout, runStderr) |
| 51 | + t.Fatalf("failed to get preflight results: err=%v, stderr=%s", err, stderr) |
| 52 | + } |
| 53 | + |
| 54 | + results, err := preflights.OutputFromReader(strings.NewReader(stdout)) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("failed to parse preflight results: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + tests := []struct { |
| 60 | + name string |
| 61 | + assert func(t *testing.T, results *preflights.Output) |
| 62 | + }{ |
| 63 | + { |
| 64 | + name: "Should contain fio results", |
| 65 | + assert: func(t *testing.T, results *preflights.Output) { |
| 66 | + for _, res := range results.Pass { |
| 67 | + if res.Title == "Filesystem Write Latency" { |
| 68 | + t.Logf("fio test passed: %s", res.Message) |
| 69 | + return |
| 70 | + } |
| 71 | + } |
| 72 | + for _, res := range results.Fail { |
| 73 | + if !strings.Contains(res.Message, "Write latency is high") { |
| 74 | + t.Errorf("fio test failed: %s", res.Message) |
| 75 | + } |
| 76 | + // as long as fio ran successfully, we're good |
| 77 | + t.Logf("fio test failed: %s", res.Message) |
| 78 | + } |
| 79 | + |
| 80 | + t.Errorf("fio test not found") |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Should not contain unexpected failures", |
| 85 | + assert: func(t *testing.T, results *preflights.Output) { |
| 86 | + expected := map[string]bool{ |
| 87 | + // TODO: work to remove these |
| 88 | + "System Clock": true, |
| 89 | + "'devices' Cgroup Controller": true, |
| 90 | + "Default Route": true, |
| 91 | + "API Access": true, |
| 92 | + "Proxy Registry Access": true, |
| 93 | + // as long as fio ran successfully, we're good |
| 94 | + "Filesystem Write Latency": true, |
| 95 | + } |
| 96 | + for _, res := range results.Fail { |
| 97 | + if !expected[res.Title] { |
| 98 | + t.Errorf("unexpected failure: %q, %q", res.Title, res.Message) |
| 99 | + } else { |
| 100 | + t.Logf("found expected failure: %q, %q", res.Title, res.Message) |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "Should not contain unexpected warnings", |
| 107 | + assert: func(t *testing.T, results *preflights.Output) { |
| 108 | + expected := map[string]bool{} |
| 109 | + for _, res := range results.Warn { |
| 110 | + if !expected[res.Title] { |
| 111 | + t.Errorf("unexpected warning: %q, %q", res.Title, res.Message) |
| 112 | + } else { |
| 113 | + t.Logf("found expected warning: %q, %q", res.Title, res.Message) |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + tt.assert(t, results) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments