|
1 | 1 | package main_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bufio" |
5 |
| - "bytes" |
6 | 4 | "context"
|
7 | 5 | "crypto/tls"
|
8 | 6 | "errors"
|
9 | 7 | "fmt"
|
10 | 8 | "github.com/stretchr/testify/assert"
|
11 | 9 | "golang.org/x/net/proxy"
|
12 | 10 | "net"
|
| 11 | + "os" |
| 12 | + "os/exec" |
13 | 13 | "strings"
|
14 | 14 | "testing"
|
15 | 15 | "time"
|
16 |
| - |
17 |
| - cli "github.com/kapetan-io/querator/cmd/querator" |
18 | 16 | )
|
19 | 17 |
|
20 | 18 | func TestCLI(t *testing.T) {
|
21 |
| - tests := []struct { |
22 |
| - args []string |
23 |
| - config string |
24 |
| - name string |
25 |
| - contains []string |
26 |
| - }{ |
27 |
| - { |
28 |
| - name: "ShouldStartWithNoConfigProvided", |
29 |
| - args: []string{""}, |
30 |
| - contains: []string{"Server Started"}, |
31 |
| - }, |
32 |
| - { |
33 |
| - name: "ShouldStartWithSampleConfig", |
34 |
| - args: []string{"-config=../../example.yaml"}, |
35 |
| - contains: []string{ |
36 |
| - "Server Started", |
37 |
| - "Loaded config from file", |
38 |
| - }, |
39 |
| - }, |
40 |
| - } |
| 19 | + // Build the binary for testing |
| 20 | + binPath := buildTestBinary(t) |
| 21 | + defer func() { |
| 22 | + _ = os.Remove(binPath) |
| 23 | + }() |
| 24 | + |
| 25 | + t.Run("HelpCommand", func(t *testing.T) { |
| 26 | + cmd := exec.Command(binPath, "--help") |
| 27 | + output, err := cmd.CombinedOutput() |
| 28 | + assert.NoError(t, err) |
| 29 | + |
| 30 | + outputStr := string(output) |
| 31 | + assert.Contains(t, outputStr, "Querator is a distributed queue system") |
| 32 | + assert.Contains(t, outputStr, "server") |
| 33 | + assert.Contains(t, outputStr, "version") |
| 34 | + assert.Contains(t, outputStr, "--endpoint") |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("VersionCommand", func(t *testing.T) { |
| 38 | + cmd := exec.Command(binPath, "version") |
| 39 | + output, err := cmd.CombinedOutput() |
| 40 | + assert.NoError(t, err) |
| 41 | + |
| 42 | + outputStr := string(output) |
| 43 | + assert.Contains(t, outputStr, "querator dev-build") |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("ServerHelpCommand", func(t *testing.T) { |
| 47 | + cmd := exec.Command(binPath, "server", "--help") |
| 48 | + output, err := cmd.CombinedOutput() |
| 49 | + assert.NoError(t, err) |
| 50 | + |
| 51 | + outputStr := string(output) |
| 52 | + assert.Contains(t, outputStr, "Start the Querator daemon") |
| 53 | + assert.Contains(t, outputStr, "--config") |
| 54 | + assert.Contains(t, outputStr, "--address") |
| 55 | + assert.Contains(t, outputStr, "--log-level") |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("EndpointGlobalFlag", func(t *testing.T) { |
| 59 | + cmd := exec.Command(binPath, "--endpoint", "http://test:8080", "version") |
| 60 | + output, err := cmd.CombinedOutput() |
| 61 | + assert.NoError(t, err) |
| 62 | + |
| 63 | + outputStr := string(output) |
| 64 | + assert.Contains(t, outputStr, "querator dev-build") |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("InvalidCommand", func(t *testing.T) { |
| 68 | + cmd := exec.Command(binPath, "invalid-command") |
| 69 | + output, err := cmd.CombinedOutput() |
| 70 | + assert.Error(t, err) |
| 71 | + |
| 72 | + outputStr := string(output) |
| 73 | + assert.Contains(t, outputStr, "unknown command") |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("ServerWithoutConfig", func(t *testing.T) { |
| 77 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 78 | + defer cancel() |
41 | 79 |
|
42 |
| - for _, tt := range tests { |
43 |
| - t.Run(tt.name, func(t *testing.T) { |
44 |
| - ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
45 |
| - var buf bytes.Buffer |
46 |
| - w := bufio.NewWriter(&buf) |
47 |
| - |
48 |
| - waitCh := make(chan struct{}) |
49 |
| - go func() { |
50 |
| - err := cli.Start(ctx, tt.args, w) |
51 |
| - if err != nil { |
52 |
| - t.Logf("cli.Start() returned error: '%v'", err) |
53 |
| - } |
54 |
| - close(waitCh) |
55 |
| - }() |
56 |
| - |
57 |
| - err := waitForConnect(ctx, "localhost:2319", nil) |
58 |
| - assert.NoError(t, err) |
59 |
| - time.Sleep(time.Second * 1) |
60 |
| - cancel() |
61 |
| - |
62 |
| - <-waitCh |
63 |
| - _ = w.Flush() |
64 |
| - for _, s := range tt.contains { |
65 |
| - //t.Logf("Checking for '%s' in output", s) |
66 |
| - assert.Contains(t, buf.String(), s) |
| 80 | + cmd := exec.CommandContext(ctx, binPath, "server") |
| 81 | + |
| 82 | + // Start the server |
| 83 | + err := cmd.Start() |
| 84 | + assert.NoError(t, err) |
| 85 | + |
| 86 | + // Ensure process cleanup |
| 87 | + defer func() { |
| 88 | + if cmd.Process != nil { |
| 89 | + _ = cmd.Process.Signal(os.Interrupt) |
| 90 | + _ = cmd.Wait() |
67 | 91 | }
|
68 |
| - }) |
69 |
| - } |
| 92 | + }() |
| 93 | + |
| 94 | + // Wait for server to accept connections |
| 95 | + err = waitForConnect(ctx, "localhost:2319", nil) |
| 96 | + assert.NoError(t, err) |
| 97 | + |
| 98 | + // If we can connect, the server started successfully |
| 99 | + // This test verifies the server starts without config |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("ServerWithConfig", func(t *testing.T) { |
| 103 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 104 | + defer cancel() |
| 105 | + |
| 106 | + cmd := exec.CommandContext(ctx, binPath, "server", "--config", "../../example.yaml") |
| 107 | + |
| 108 | + // Start the server |
| 109 | + err := cmd.Start() |
| 110 | + assert.NoError(t, err) |
| 111 | + |
| 112 | + // Wait for server to accept connections |
| 113 | + err = waitForConnect(ctx, "localhost:2319", nil) |
| 114 | + assert.NoError(t, err) |
| 115 | + |
| 116 | + // If we can connect, the server started successfully with config |
| 117 | + // This test verifies the server starts with the example.yaml config |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +func buildTestBinary(t *testing.T) string { |
| 122 | + binPath := "./querator-test" |
| 123 | + cmd := exec.Command("go", "build", "-o", binPath, ".") |
| 124 | + err := cmd.Run() |
| 125 | + assert.NoError(t, err, "Failed to build test binary") |
| 126 | + return binPath |
70 | 127 | }
|
71 | 128 |
|
72 | 129 | // waitForConnect waits until the passed address is accepting connections.
|
|
0 commit comments