Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
137 lines (116 loc) · 3.8 KB
/
Copy pathmain_test.go
File metadata and controls
137 lines (116 loc) · 3.8 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadZeekEnv(t *testing.T) {
t.Run("Missing File Returns Empty", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
vals, err := readZeekEnv(path)
require.NoError(t, err)
require.Empty(t, vals)
})
t.Run("Skips Comments And Blanks", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
content := "# a comment\n\nZEEK_INTERFACE=eth0\n\n# another\nZEEK_WORKERS=8\n"
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
vals, err := readZeekEnv(path)
require.NoError(t, err)
require.Equal(t, map[string]string{
"ZEEK_INTERFACE": "eth0",
"ZEEK_WORKERS": "8",
}, vals)
})
t.Run("Trims Whitespace", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
require.NoError(t, os.WriteFile(path, []byte(" ZEEK_INTERFACE = eth0 \n"), 0644))
vals, err := readZeekEnv(path)
require.NoError(t, err)
require.Equal(t, "eth0", vals["ZEEK_INTERFACE"])
})
t.Run("Skips Lines Without Equals", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
content := "ZEEK_INTERFACE=eth0\nbroken line\nZEEK_WORKERS=8\n"
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
vals, err := readZeekEnv(path)
require.NoError(t, err)
require.Equal(t, map[string]string{
"ZEEK_INTERFACE": "eth0",
"ZEEK_WORKERS": "8",
}, vals)
})
}
func TestWriteZeekEnv(t *testing.T) {
t.Run("Writes All Keys", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
expected := map[string]string{
"ZEEK_WORKERS": "8",
"ZEEK_INTERFACE": "eth0",
}
require.NoError(t, writeZeekEnv(path, expected))
got, err := readZeekEnv(path)
require.NoError(t, err)
require.Equal(t, expected, got)
})
t.Run("Overwrites Existing File", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "zeek.env")
require.NoError(t, os.WriteFile(path, []byte("# old comment\nZEEK_INTERFACE=old\n"), 0644))
require.NoError(t, writeZeekEnv(path, map[string]string{"ZEEK_INTERFACE": "new"}))
got, err := readZeekEnv(path)
require.NoError(t, err)
require.Equal(t, map[string]string{"ZEEK_INTERFACE": "new"}, got)
})
}
func TestResolveConfig(t *testing.T) {
setEnv := func(t *testing.T, vals map[string]string) {
t.Helper()
for _, k := range []string{"ZEEK_TOP_DIR", "zeek_top_dir"} {
t.Setenv(k, vals[k])
}
}
withBuildVars := func(t *testing.T, version, imageTag string) {
t.Helper()
origVersion, origTag := Version, ImageTag
Version, ImageTag = version, imageTag
t.Cleanup(func() { Version, ImageTag = origVersion, origTag })
}
t.Run("Errors When Version Empty", func(t *testing.T) {
setEnv(t, nil)
withBuildVars(t, "", "8.0.6")
_, _, err := resolveConfig()
require.ErrorIs(t, err, ErrNotBuilt)
})
t.Run("Errors When ImageTag Empty", func(t *testing.T) {
setEnv(t, nil)
withBuildVars(t, "v8.0.6", "")
_, _, err := resolveConfig()
require.ErrorIs(t, err, ErrNotBuilt)
})
t.Run("Uses ImageTag For Image", func(t *testing.T) {
setEnv(t, nil)
withBuildVars(t, "v8.0.6", "8.0.6")
image, hostDir, err := resolveConfig()
require.NoError(t, err)
require.Equal(t, "/opt/zeek", hostDir)
require.Equal(t, "activecm/zeek:8.0.6", image)
})
t.Run("ZEEK_TOP_DIR works", func(t *testing.T) {
setEnv(t, map[string]string{
"ZEEK_TOP_DIR": "/new/path",
"zeek_top_dir": "/old/path",
})
withBuildVars(t, "v8.0.6", "8.0.6")
_, hostDir, err := resolveConfig()
require.NoError(t, err)
require.Equal(t, "/new/path", hostDir)
})
t.Run("Legacy zeek_top_dir Works", func(t *testing.T) {
setEnv(t, map[string]string{"zeek_top_dir": "/legacy/path"})
withBuildVars(t, "v8.0.6", "8.0.6")
_, hostDir, err := resolveConfig()
require.NoError(t, err)
require.Equal(t, "/legacy/path", hostDir)
})
}