@@ -18,30 +18,32 @@ import (
18
18
"bytes"
19
19
"context"
20
20
"encoding/json"
21
- "fmt"
22
21
"io"
23
- "os"
24
22
"path/filepath"
25
- "strings"
26
23
"testing"
27
24
"time"
28
25
29
- "github.com/docker/docker/api/types/container"
30
- "github.com/docker/docker/api/types/mount"
31
- "github.com/google/go-cmp/cmp"
32
26
"github.com/splunk/splunk-technical-addon/internal/packaging"
33
27
"github.com/splunk/splunk-technical-addon/internal/testcommon"
34
28
"github.com/stretchr/testify/assert"
35
29
"github.com/stretchr/testify/require"
36
- "github.com/testcontainers/testcontainers-go"
37
30
"github.com/testcontainers/testcontainers-go/wait"
38
- "go.uber.org/zap"
39
31
)
40
32
41
33
type ExampleOutput struct {
34
+ Flags []string
35
+ EnvVars []string
36
+
37
+ SplunkHome string
38
+ TaHome string
39
+ PlatformHome string
40
+
41
+ EverythingSet string
42
+ MinimalSet string
43
+ MinimalSetRequired string
44
+ UnaryFlagWithEverythingSet string
45
+
42
46
Platform string
43
- Flags []string
44
- EnvVars []string
45
47
}
46
48
47
49
func TestPascalization (t * testing.T ) {
@@ -78,11 +80,14 @@ func TestRunner(t *testing.T) {
78
80
ctx := context .Background ()
79
81
addonPath := filepath .Join (t .TempDir (), "Sample_Addon.tgz" )
80
82
81
- buildDir := testcommon .GetBuildDir ()
83
+ buildDir := packaging .GetBuildDir ()
82
84
require .NotEmpty (t , buildDir )
83
85
err := packaging .PackageAddon (filepath .Join (buildDir , "Sample_Addon" ), addonPath )
84
86
require .NoError (t , err )
85
- tc := startSplunk (t , addonPath )
87
+ tc := testcommon .StartSplunk (t , testcommon.SplunkStartOpts {
88
+ AddonPaths : []string {addonPath },
89
+ WaitStrategy : wait .ForExec ([]string {"sudo" , "stat" , "/opt/splunk/var/log/splunk/Sample_Addon.log" }).WithStartupTimeout (time .Minute * 4 ),
90
+ })
86
91
87
92
// Check Schema
88
93
code , output , err := tc .Exec (ctx , []string {"sudo" , "/opt/splunk/bin/splunk" , "btool" , "check" , "--debug" })
@@ -98,10 +103,12 @@ func TestRunner(t *testing.T) {
98
103
require .NoError (t , err )
99
104
read , err = io .ReadAll (output )
100
105
assert .NoError (t , err )
101
- expectedJSON := `{"Flags":["--test-flag","$SPLUNK_OTEL_TA_HOME/ local/access_token","--test-flag"],"EnvVars":["EVERYTHING_SET=$SPLUNK_OTEL_TA_HOME/ local/access_token","UNARY_FLAG_WITH_EVERYTHING_SET=$SPLUNK_OTEL_TA_HOME/ local/access_token"],"Platform":"linux"}`
106
+ expectedJSON := `{"Flags":["--test-flag","/opt/splunk/etc/apps/Sample_Addon/ local/access_token","--test-flag"],"EnvVars":["EVERYTHING_SET=/opt/splunk/etc/apps/Sample_Addon/ local/access_token","UNARY_FLAG_WITH_EVERYTHING_SET=/opt/splunk/etc/apps/Sample_Addon/ local/access_token"], "SplunkHome":"/opt/splunk/etc", "TaHome":"/opt/splunk/etc/apps/Sample_Addon", "PlatformHome":"/opt/splunk/etc/apps/Sample_Addon/linux_x86_64", "EverythingSet":"/opt/splunk/etc/apps/Sample_Addon/local/access_token", "MinimalSet":"", "MinimalSetRequired":"", "UnaryFlagWithEverythingSet":"/opt/splunk/etc/apps/Sample_Addon/local/access_token" ,"Platform":"linux"}`
102
107
i := bytes .Index (read , []byte ("Sample output:" ))
103
108
unmarshalled := & ExampleOutput {}
104
- require .NoError (t , json .Unmarshal (read [i + len ("Sample output:" ):], unmarshalled ))
109
+ dec := json .NewDecoder (bytes .NewReader (read [i + len ("Sample output:" ):]))
110
+ dec .DisallowUnknownFields ()
111
+ require .NoError (t , dec .Decode (unmarshalled ))
105
112
expected := & ExampleOutput {}
106
113
require .NoError (t , json .Unmarshal ([]byte (expectedJSON ), expected ))
107
114
assert .EqualValues (t , expected , unmarshalled )
@@ -110,7 +117,7 @@ func TestRunner(t *testing.T) {
110
117
}
111
118
112
119
func TestRunnerConfigGeneration (t * testing.T ) {
113
- sourceDir , err := testcommon .GetSourceDir ()
120
+ sourceDir , err := packaging .GetSourceDir ()
114
121
require .NoError (t , err )
115
122
sourceDir = filepath .Join (sourceDir , "cmd" , "modinput_config_generator" , "internal" , "testdata" )
116
123
tests := []struct {
@@ -138,102 +145,33 @@ func TestRunnerConfigGeneration(t *testing.T) {
138
145
}
139
146
140
147
func TestInputsConfGeneration (t * testing.T ) {
141
- sourceDir , err := testcommon .GetSourceDir ()
148
+ sourceDir , err := packaging .GetSourceDir ()
142
149
require .NoError (t , err )
143
150
sourceDir = filepath .Join (sourceDir , "cmd" , "modinput_config_generator" , "internal" , "testdata" )
144
151
tests := []struct {
145
152
testSchemaName string
146
153
sampleYamlPath string
147
154
outDir string
148
- sourceDir string
155
+ addonSourceDir string
149
156
expectedSpecPath string
150
157
shouldError bool
151
158
}{
152
159
{
153
160
testSchemaName : "Sample_Addon" ,
154
161
outDir : t .TempDir (),
155
- sourceDir : filepath .Join (sourceDir , "pkg/ sample_addon" ),
156
- sampleYamlPath : filepath .Join (sourceDir , "pkg/ sample_addon/ runner/ modular-inputs.yaml" ),
162
+ addonSourceDir : filepath .Join (sourceDir , "pkg" , " sample_addon" ),
163
+ sampleYamlPath : filepath .Join (sourceDir , "pkg" , " sample_addon" , " runner" , " modular-inputs.yaml" ),
157
164
},
158
165
}
159
166
160
167
for _ , tc := range tests {
161
168
t .Run (tc .testSchemaName , func (tt * testing.T ) {
162
169
config , err := loadYaml (tc .sampleYamlPath , tc .testSchemaName )
163
170
assert .NoError (tt , err )
164
- err = generateTaModInputConfs (config , tc .sourceDir , tc .outDir )
171
+ err = generateTaModInputConfs (config , tc .addonSourceDir , tc .outDir )
165
172
assert .NoError (tt , err )
166
- assertFilesMatch (tt , filepath .Join ("internal" , "testdata" , "pkg" , "sample_addon" , "expected" , "inputs.conf" ), filepath .Join (tc .outDir , "default" , "inputs.conf" ))
167
- assertFilesMatch (tt , filepath .Join ("internal" , "testdata" , "pkg" , "sample_addon" , "expected" , "inputs.conf.spec" ), filepath .Join (tc .outDir , "README" , "inputs.conf.spec" ))
173
+ testcommon . AssertFilesMatch (tt , filepath .Join ("internal" , "testdata" , "pkg" , "sample_addon" , "expected" , "inputs.conf" ), filepath .Join (tc .outDir , tc . testSchemaName , "default" , "inputs.conf" ))
174
+ testcommon . AssertFilesMatch (tt , filepath .Join ("internal" , "testdata" , "pkg" , "sample_addon" , "expected" , "inputs.conf.spec" ), filepath .Join (tc .outDir , tc . testSchemaName , "README" , "inputs.conf.spec" ))
168
175
})
169
176
}
170
177
}
171
-
172
- func assertFilesMatch (tt * testing.T , expectedPath string , actualPath string ) {
173
- require .FileExists (tt , actualPath )
174
- require .FileExists (tt , expectedPath )
175
- expected , err := os .ReadFile (expectedPath )
176
- if err != nil {
177
- tt .Fatalf ("Failed to read expected file: %v" , err )
178
- }
179
-
180
- actual , err := os .ReadFile (actualPath )
181
- if err != nil {
182
- tt .Fatalf ("Failed to read actual file: %v" , err )
183
- }
184
-
185
- if diff := cmp .Diff (string (expected ), string (actual )); diff != "" {
186
- tt .Errorf ("File contents mismatch (-expected +actual)\n paths: (%s, %s):\n %s" , expectedPath , actualPath , diff )
187
- }
188
- }
189
-
190
- func startSplunk (t * testing.T , taPath string ) testcontainers.Container {
191
- logger , err := zap .NewProduction ()
192
- if err != nil {
193
- panic (err )
194
- }
195
- conContext := context .Background ()
196
- addonLocation := fmt .Sprintf ("/tmp/local-tas/%v" , filepath .Base (taPath ))
197
-
198
- req := testcontainers.ContainerRequest {
199
- Image : "splunk/splunk:9.1.2" ,
200
- HostConfigModifier : func (c * container.HostConfig ) {
201
- c .NetworkMode = "host"
202
- c .Mounts = append (c .Mounts , mount.Mount {
203
- Source : filepath .Dir (taPath ),
204
- Target : filepath .Dir (addonLocation ),
205
- Type : mount .TypeBind ,
206
- })
207
- },
208
- Env : map [string ]string {
209
- "SPLUNK_START_ARGS" : "--accept-license" ,
210
- "SPLUNK_PASSWORD" : "Chang3d!" ,
211
- "SPLUNK_APPS_URL" : addonLocation ,
212
- },
213
- WaitingFor : wait .ForAll (
214
- wait .NewHTTPStrategy ("/en-US/account/login" ).WithPort ("8000" ),
215
- wait .ForExec ([]string {"sudo" , "stat" , "/opt/splunk/var/log/splunk/Sample_Addon.log" }),
216
- ).WithDeadline (4 * time .Minute + 20 * time .Second ).WithStartupTimeoutDefault (4 * time .Minute ),
217
- LogConsumerCfg : & testcontainers.LogConsumerConfig {
218
- Consumers : []testcontainers.LogConsumer {& testLogConsumer {t : t }},
219
- },
220
- }
221
-
222
- tc , err := testcontainers .GenericContainer (conContext , testcontainers.GenericContainerRequest {
223
- ContainerRequest : req ,
224
- Started : true ,
225
- })
226
- if err != nil {
227
- logger .Info ("Error while creating container" )
228
- panic (err )
229
- }
230
- return tc
231
- }
232
-
233
- type testLogConsumer struct {
234
- t * testing.T
235
- }
236
-
237
- func (l * testLogConsumer ) Accept (log testcontainers.Log ) {
238
- l .t .Log (log .LogType + ": " + strings .TrimSpace (string (log .Content )))
239
- }
0 commit comments