Skip to content

Commit 4061998

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
linter fixes
- move snclientConfigFileWithScript function out of snclient_test.go to snclient_windows_test.go , since it is only being used to test windows powershell functions for now. it was not being used for other platforms - remove some unused code lines - use idiomatic assert functions - line length - nolint couple unwanted rules like unparam and duplicate
1 parent 083e582 commit 4061998

File tree

2 files changed

+81
-78
lines changed

2 files changed

+81
-78
lines changed

pkg/snclient/snclient_test.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,6 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13-
// Generates a config file, where snclient can call a script.
14-
// scriptName does not have an extension
15-
// scriptFilename does have (most likely an OS specific) script extension.
16-
// It registers four commands for script
17-
// scriptName_arg1 : ./${SCRIPT_FILENAME} "$ARG1$"
18-
// scriptName_arg_numbered : ./${SCRIPT_FILENAME} "$ARG1$" "$ARG2$" "$ARG3$" "$ARG4$" "$ARG5$" "$ARG6$" "$ARG7$" "$ARG8$" "$ARG9$" "$ARG10$"
19-
// scriptName_args : ./${SCRIPT_FILENAME} "$ARGS$"
20-
// scriptName_args_quouted : ./${SCRIPT_FILENAME} "$ARGS"$"
21-
func snclientConfigFileWithScript(t *testing.T, scriptsDir, scriptName, scriptFilename string) string {
22-
t.Helper()
23-
24-
configTemplate := `
25-
[/modules]
26-
CheckExternalScripts = enabled
27-
28-
[/paths]
29-
scripts = ${SCRIPTS_DIR}
30-
shared-path = %(scripts)
31-
32-
[/settings/external scripts]
33-
timeout = 1111111
34-
allow arguments = true
35-
36-
[/settings/external scripts/scripts]
37-
${SCRIPT_NAME}_arg1 = ./${SCRIPT_FILENAME} $ARG1$
38-
39-
[/settings/external scripts/scripts/${SCRIPT_NAME}_arg1]
40-
allow arguments = true
41-
allow nasty characters = true
42-
43-
[/settings/external scripts/scripts]
44-
${SCRIPT_NAME}_arg_numbered = ./${SCRIPT_FILENAME} $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$
45-
46-
[/settings/external scripts/scripts/${SCRIPT_NAME}_arg_numbered]
47-
allow arguments = true
48-
allow nasty characters = true
49-
50-
[/settings/external scripts/scripts]
51-
${SCRIPT_NAME}_args = ./${SCRIPT_FILENAME} $ARGS$
52-
53-
[/settings/external scripts/scripts/${SCRIPT_NAME}_args]
54-
allow arguments = true
55-
allow nasty characters = true
56-
57-
[/settings/external scripts/scripts]
58-
${SCRIPT_NAME}_args_quouted = ./${SCRIPT_FILENAME} $ARGS"$
59-
60-
[/settings/external scripts/scripts/${SCRIPT_NAME}_args_quouted]
61-
allow arguments = true
62-
allow nasty characters = true
63-
`
64-
65-
mapper := func(placeholderName string) string {
66-
switch placeholderName {
67-
case "SCRIPTS_DIR":
68-
return scriptsDir
69-
case "SCRIPT_NAME":
70-
return scriptName
71-
case "SCRIPT_FILENAME":
72-
return scriptFilename
73-
default:
74-
// if its not some value we know, leave it as is
75-
return "$" + placeholderName
76-
}
77-
}
78-
79-
return os.Expand(configTemplate, mapper)
80-
}
81-
8213
func TestPasswords(t *testing.T) {
8314
config := fmt.Sprintf(`
8415
[/settings]

pkg/snclient/snclient_windows_test.go

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,88 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

14+
// Generates a config file, where snclient can call a script.
15+
// scriptName does not have an extension
16+
// scriptFilename does have (most likely an OS specific) script extension.
17+
// It registers four commands for script
18+
// scriptName_arg1 : ./${SCRIPT_FILENAME} "$ARG1$"
19+
// scriptName_arg_numbered : ./${SCRIPT_FILENAME} "$ARG1$" "$ARG2$" "$ARG3$" "$ARG4$" "$ARG5$" "$ARG6$" "$ARG7$" "$ARG8$" "$ARG9$" "$ARG10$"
20+
// scriptName_args : ./${SCRIPT_FILENAME} "$ARGS$"
21+
// scriptName_args_quouted : ./${SCRIPT_FILENAME} "$ARGS"$"
22+
//
23+
//nolint:unparam // scriptName is so far always "powershell_detail" , no other test script uses this function. Keep it as a parameter for future use.
24+
func snclientConfigFileWithScript(t *testing.T, scriptsDir, scriptName, scriptFilename string) string {
25+
t.Helper()
26+
27+
configTemplate := `
28+
[/modules]
29+
CheckExternalScripts = enabled
30+
31+
[/paths]
32+
scripts = ${SCRIPTS_DIR}
33+
shared-path = %(scripts)
34+
35+
[/settings/external scripts]
36+
timeout = 1111111
37+
allow arguments = true
38+
39+
[/settings/external scripts/scripts]
40+
${SCRIPT_NAME}_arg1 = ./${SCRIPT_FILENAME} $ARG1$
41+
42+
[/settings/external scripts/scripts/${SCRIPT_NAME}_arg1]
43+
allow arguments = true
44+
allow nasty characters = true
45+
46+
[/settings/external scripts/scripts]
47+
${SCRIPT_NAME}_arg_numbered = ./${SCRIPT_FILENAME} $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$
48+
49+
[/settings/external scripts/scripts/${SCRIPT_NAME}_arg_numbered]
50+
allow arguments = true
51+
allow nasty characters = true
52+
53+
[/settings/external scripts/scripts]
54+
${SCRIPT_NAME}_args = ./${SCRIPT_FILENAME} $ARGS$
55+
56+
[/settings/external scripts/scripts/${SCRIPT_NAME}_args]
57+
allow arguments = true
58+
allow nasty characters = true
59+
60+
[/settings/external scripts/scripts]
61+
${SCRIPT_NAME}_args_quouted = ./${SCRIPT_FILENAME} $ARGS"$
62+
63+
[/settings/external scripts/scripts/${SCRIPT_NAME}_args_quouted]
64+
allow arguments = true
65+
allow nasty characters = true
66+
`
67+
68+
mapper := func(placeholderName string) string {
69+
switch placeholderName {
70+
case "SCRIPTS_DIR":
71+
return scriptsDir
72+
case "SCRIPT_NAME":
73+
return scriptName
74+
case "SCRIPT_FILENAME":
75+
return scriptFilename
76+
default:
77+
// if its not some value we know, leave it as is
78+
return "$" + placeholderName
79+
}
80+
}
81+
82+
return os.Expand(configTemplate, mapper)
83+
}
84+
1385
func TestMakeCmd(t *testing.T) {
1486
config := ""
1587
snc := StartTestAgent(t, config)
1688

1789
commandString := `.\t\scripts\powershell_detail.ps1 -option1 option1 -option2 'option2' -option3 "option3" -option4 'option4.option4,option4:option4;option4|option4$option4' `
1890
cmd, err := snc.makeCmd(context.TODO(), commandString)
1991

20-
assert.Nilf(t, err, "there should not be any errors when converting command: %s into an exec.Cmd of os/exec", commandString)
92+
require.NoErrorf(t, err, "there should not be any errors when converting command: %s into an exec.Cmd of os/exec", commandString)
2193

2294
assert.NotEmptyf(t, cmd.SysProcAttr.CmdLine, "exec.Cmd from command: %s should not have an empty SysProcAttr.CmdLine", commandString)
2395

@@ -42,7 +114,10 @@ func TestMakeCmd(t *testing.T) {
42114
// users have to use it like this --optionX 'foo,bar' to have it accepted as a string
43115

44116
cmdLineExpectedContains := `-option1 option1 -option2 'option2' -option3 "option3" -option4 'option4.option4,option4:option4;option4|option4$option4'`
45-
assert.Containsf(t, cmd.SysProcAttr.CmdLine, cmdLineExpectedContains, "exec.Cmd from command: %s\nshould contain this substring: %s\nbut it looks like this: %s", commandString, cmdLineExpectedContains, cmd.SysProcAttr.CmdLine)
117+
assert.Containsf(t, cmd.SysProcAttr.CmdLine,
118+
cmdLineExpectedContains,
119+
"exec.Cmd from command: %s\nshould contain this substring: %s\nbut it looks like this: %s",
120+
commandString, cmdLineExpectedContains, cmd.SysProcAttr.CmdLine)
46121

47122
var pathEnv string
48123
for _, envVar := range cmd.Env {
@@ -51,13 +126,13 @@ func TestMakeCmd(t *testing.T) {
51126
}
52127
}
53128

54-
assert.NotEmpty(t, pathEnv, cmd.Env, "converted exec.Cmd from command: %s should contain PATH environment variable", commandString)
129+
assert.NotEmpty(t, pathEnv, "converted exec.Cmd from command: %s should contain PATH environment variable", commandString)
55130

56131
// script is found under C:\Users\sorus\repositories\snclient\pkg\snclient\t
57132
// scriptsPath, _ := snc.config.Section("/paths").GetString("scripts")
58133
// assert.Containsf(t, pathEnv, scriptsPath+":", "converted exec.Cmd from command: %s should have its PATH variable: %s include the config ScriptsPath: %s", commandString, pathEnv, scriptsPath)
59134

60-
assert.Equal(t, true, cmd.SysProcAttr.HideWindow, "converted exec.Cmd from command: %s should hide its spawned window", commandString)
135+
assert.Truef(t, cmd.SysProcAttr.HideWindow, "converted exec.Cmd from command: %s should hide its spawned window", commandString)
61136

62137
StopTestAgent(t, snc)
63138
}
@@ -75,7 +150,6 @@ func TestPowershell1(t *testing.T) {
75150
res := snc.RunCheck("powershell_detail_arg1", []string{})
76151

77152
outputString := string(res.BuildPluginOutput())
78-
// t.Logf("\n%s\n", outputString)
79153

80154
assert.Equalf(t, CheckExitOK, res.State, "check should return state ok")
81155

@@ -88,7 +162,6 @@ func TestPowershell1(t *testing.T) {
88162
for _, rawCommandLineExpectedItem := range rawCommandlineExpected {
89163
assert.Containsf(t, outputString, rawCommandLineExpectedItem, "raw commandline should contain: %s", rawCommandLineExpectedItem)
90164
}
91-
92165
}
93166

94167
func TestPowershellScriptArg1(t *testing.T) {
@@ -111,7 +184,6 @@ func TestPowershellScriptArg1(t *testing.T) {
111184
assert.NotNilf(t, checkData, "check should return a checkData")
112185

113186
outputString := string(checkResult.BuildPluginOutput())
114-
// t.Logf("\n%s\n", outputString)
115187

116188
assert.Equalf(t, CheckExitOK, checkResult.State, "check should return state OK")
117189

@@ -138,6 +210,7 @@ func TestPowershellScriptArg1(t *testing.T) {
138210
}
139211
}
140212

213+
//nolint:dupl // the functions are largely the same, but scriptMacroType is different. Redefining expected strings for each macro type is easier to understand.
141214
func TestPowershellScriptArgNumbered(t *testing.T) {
142215
testDir, _ := os.Getwd()
143216
scriptsDir := filepath.Join(testDir, "t", "scripts")
@@ -167,7 +240,6 @@ func TestPowershellScriptArgNumbered(t *testing.T) {
167240
assert.NotNilf(t, checkData, "check should return a checkData")
168241

169242
outputString := string(checkResult.BuildPluginOutput())
170-
//t.Logf("\n%s\n", outputString)
171243

172244
assert.Equalf(t, CheckExitOK, checkResult.State, "check should return state OK")
173245

@@ -191,6 +263,7 @@ func TestPowershellScriptArgNumbered(t *testing.T) {
191263
}
192264
}
193265

266+
//nolint:dupl // the functions are largely the same, but scriptMacroType is different. Redefining expected strings for each macro type is easier to understand.
194267
func TestPowershellScriptArgs(t *testing.T) {
195268
testDir, _ := os.Getwd()
196269
scriptsDir := filepath.Join(testDir, "t", "scripts")
@@ -220,7 +293,6 @@ func TestPowershellScriptArgs(t *testing.T) {
220293
assert.NotNilf(t, checkData, "check should return a checkData")
221294

222295
outputString := string(checkResult.BuildPluginOutput())
223-
//t.Logf("\n%s\n", outputString)
224296

225297
assert.Equalf(t, CheckExitOK, checkResult.State, "check should return state OK")
226298

0 commit comments

Comments
 (0)