|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFlagParsing(t *testing.T) { |
| 16 | + for name, tc := range map[string]struct { |
| 17 | + arguments []string |
| 18 | + yaml string |
| 19 | + stdoutMessage string // string that must be included in stdout |
| 20 | + stderrMessage string // string that must be included in stderr |
| 21 | + }{ |
| 22 | + "help": { |
| 23 | + arguments: []string{"-h"}, |
| 24 | + stderrMessage: configFileOption, |
| 25 | + }, |
| 26 | + |
| 27 | + // check that config file is used |
| 28 | + "config with unknown target": { |
| 29 | + yaml: "target: unknown", |
| 30 | + stderrMessage: "unrecognised module name: unknown", |
| 31 | + }, |
| 32 | + |
| 33 | + "argument with unknown target": { |
| 34 | + arguments: []string{"-target=unknown"}, |
| 35 | + stderrMessage: "unrecognised module name: unknown", |
| 36 | + }, |
| 37 | + |
| 38 | + "unknown flag": { |
| 39 | + arguments: []string{"-unknown.flag"}, |
| 40 | + stderrMessage: "-unknown.flag", |
| 41 | + }, |
| 42 | + |
| 43 | + "config with wrong argument override": { |
| 44 | + yaml: "target: ingester", |
| 45 | + arguments: []string{"-target=unknown"}, |
| 46 | + stderrMessage: "unrecognised module name: unknown", |
| 47 | + }, |
| 48 | + |
| 49 | + "default values": { |
| 50 | + stdoutMessage: "target: all\n", |
| 51 | + }, |
| 52 | + |
| 53 | + "config": { |
| 54 | + yaml: "target: ingester", |
| 55 | + stdoutMessage: "target: ingester\n", |
| 56 | + }, |
| 57 | + |
| 58 | + "config with arguments override": { |
| 59 | + yaml: "target: ingester", |
| 60 | + arguments: []string{"-target=distributor"}, |
| 61 | + stdoutMessage: "target: distributor\n", |
| 62 | + }, |
| 63 | + |
| 64 | + // we cannot test the happy path, as cortex would then fully start |
| 65 | + } { |
| 66 | + t.Run(name, func(t *testing.T) { |
| 67 | + testSingle(t, tc.arguments, tc.yaml, []byte(tc.stdoutMessage), []byte(tc.stderrMessage)) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func testSingle(t *testing.T, arguments []string, yaml string, stdoutMessage, stderrMessage []byte) { |
| 73 | + oldArgs, oldStdout, oldStderr, oldTestMode := os.Args, os.Stdout, os.Stderr, testMode |
| 74 | + defer func() { |
| 75 | + os.Stdout = oldStdout |
| 76 | + os.Stderr = oldStderr |
| 77 | + os.Args = oldArgs |
| 78 | + testMode = oldTestMode |
| 79 | + }() |
| 80 | + |
| 81 | + if yaml != "" { |
| 82 | + tempFile, err := ioutil.TempFile("", "test") |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + defer func() { |
| 86 | + require.NoError(t, tempFile.Close()) |
| 87 | + require.NoError(t, os.Remove(tempFile.Name())) |
| 88 | + }() |
| 89 | + |
| 90 | + _, err = tempFile.WriteString(yaml) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + arguments = append([]string{"-" + configFileOption, tempFile.Name()}, arguments...) |
| 94 | + } |
| 95 | + |
| 96 | + arguments = append([]string{"./cortex"}, arguments...) |
| 97 | + |
| 98 | + testMode = true |
| 99 | + os.Args = arguments |
| 100 | + co := captureOutput(t) |
| 101 | + |
| 102 | + // reset default flags |
| 103 | + flag.CommandLine = flag.NewFlagSet(arguments[0], flag.ExitOnError) |
| 104 | + |
| 105 | + main() |
| 106 | + |
| 107 | + stdout, stderr := co.Done() |
| 108 | + if !bytes.Contains(stdout, stdoutMessage) { |
| 109 | + t.Errorf("Expected on stdout: %q, stdout: %s\n", stdoutMessage, stdout) |
| 110 | + } |
| 111 | + if !bytes.Contains(stderr, stderrMessage) { |
| 112 | + t.Errorf("Expected on stderr: %q, stderr: %s\n", stderrMessage, stderr) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +type capturedOutput struct { |
| 117 | + stdoutBuf bytes.Buffer |
| 118 | + stderrBuf bytes.Buffer |
| 119 | + |
| 120 | + wg sync.WaitGroup |
| 121 | + stdoutReader, stdoutWriter *os.File |
| 122 | + stderrReader, stderrWriter *os.File |
| 123 | +} |
| 124 | + |
| 125 | +func captureOutput(t *testing.T) *capturedOutput { |
| 126 | + stdoutR, stdoutW, err := os.Pipe() |
| 127 | + require.NoError(t, err) |
| 128 | + os.Stdout = stdoutW |
| 129 | + |
| 130 | + stderrR, stderrW, err := os.Pipe() |
| 131 | + require.NoError(t, err) |
| 132 | + os.Stderr = stderrW |
| 133 | + |
| 134 | + co := &capturedOutput{ |
| 135 | + stdoutReader: stdoutR, |
| 136 | + stdoutWriter: stdoutW, |
| 137 | + stderrReader: stderrR, |
| 138 | + stderrWriter: stderrW, |
| 139 | + } |
| 140 | + co.wg.Add(1) |
| 141 | + go func() { |
| 142 | + defer co.wg.Done() |
| 143 | + _, _ = io.Copy(&co.stdoutBuf, stdoutR) |
| 144 | + }() |
| 145 | + |
| 146 | + co.wg.Add(1) |
| 147 | + go func() { |
| 148 | + defer co.wg.Done() |
| 149 | + _, _ = io.Copy(&co.stderrBuf, stderrR) |
| 150 | + }() |
| 151 | + |
| 152 | + return co |
| 153 | +} |
| 154 | + |
| 155 | +func (co *capturedOutput) Done() (stdout []byte, stderr []byte) { |
| 156 | + // we need to close writers for readers to stop |
| 157 | + _ = co.stdoutWriter.Close() |
| 158 | + _ = co.stderrWriter.Close() |
| 159 | + |
| 160 | + co.wg.Wait() |
| 161 | + |
| 162 | + return co.stdoutBuf.Bytes(), co.stderrBuf.Bytes() |
| 163 | +} |
0 commit comments