-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_test.go
68 lines (63 loc) · 1.59 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"testing"
"time"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/valkyrie-fnd/valkyrie/internal/testutils"
)
func TestMain(t *testing.T) {
oldArgs := os.Args
defer func() {
os.Args = oldArgs
}()
tests := []struct {
Name string
Args []string
ExpectedExit int
OutputContains string
}{
{
"Checking version should work just fine",
[]string{"-version"},
0,
"Version: devel",
},
{
"Starting Valkyrie without test config",
[]string{},
1,
"Failed to read config",
},
{
"Starting Valkyrie with test config",
[]string{"-config", "./configs/testdata/valkyrie_config.test.yml"},
0,
"Operator server listening on 'localhost:",
},
}
for _, test := range tests {
t.Run(test.Name, func(tt *testing.T) {
tt.Setenv("VALK_PROFILES", "testlog")
p, _ := testutils.GetFreePort()
o, _ := testutils.GetFreePort()
tt.Setenv("PROVIDER_ADDRESS", fmt.Sprintf("localhost:%d", p))
tt.Setenv("OPERATOR_ADDRESS", fmt.Sprintf("localhost:%d", o))
flag.CommandLine = flag.NewFlagSet(test.Name, flag.ExitOnError)
os.Args = append([]string{test.Name}, test.Args...)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var buf bytes.Buffer
log.Logger = log.Output(&buf)
exitCode := mainReal(ctx, &buf)
assert.Equal(tt, test.ExpectedExit, exitCode, "Invalid exit code")
output := buf.String()
assert.Contains(t, output, test.OutputContains, "Output of log did not contain expected output")
})
}
}