Skip to content

Commit 00f6218

Browse files
authored
Merge pull request #16 from NETWAYS/refactor-strings
Refactor strings
2 parents f24f5ec + f96f217 commit 00f6218

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
run:
2-
deadline: 1m
2+
timeout: 5m
3+
skip-files:
4+
- '(.+)_test\.go'
35

46
linters:
57
disable-all: false

check.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/pflag"
1010
"net/url"
1111
"os"
12+
"strings"
1213
)
1314

1415
type Config struct {
@@ -111,8 +112,10 @@ func (c *Config) Run() (rc int, output string, err error) {
111112
byLocation[index] = append(byLocation[index], threat)
112113
}
113114

115+
var sb strings.Builder
116+
114117
for index, list := range byLocation {
115-
output += fmt.Sprintf("\n## %s\n\n", index)
118+
sb.WriteString(fmt.Sprintf("\n## %s\n\n", index))
116119

117120
for _, threat := range list {
118121
var stateText string
@@ -127,29 +130,31 @@ func (c *Config) Run() (rc int, output string, err error) {
127130
stateText = "WARNING"
128131
}
129132

130-
output += fmt.Sprintf("[%s] [%s] %s: (%s) %s (%s)\n",
133+
sb.WriteString(fmt.Sprintf("[%s] [%s] %s: (%s) %s (%s)\n",
131134
threat.ThreatInfo.CreatedAt.Local().Format("2006-01-02 15:04 MST"),
132135
stateText,
133136
threat.AgentRealtimeInfo.AgentComputerName,
134137
threat.ThreatInfo.Classification,
135138
threat.ThreatInfo.ThreatName,
136139
threat.ThreatInfo.MitigationStatusDescription,
137-
)
140+
))
138141
}
139142
}
140143

141144
// Add summary on top
142-
output = fmt.Sprintf("%d threats found, %d not mitigated\n", total, notMitigated) + output
145+
sb.WriteString(fmt.Sprintf("%d threats found, %d not mitigated\n", total, notMitigated) + sb.String())
146+
143147
if c.SiteName != "" && c.ComputerName == "" {
144-
output = fmt.Sprintf("site %s - ", c.SiteName) + output
148+
sb.WriteString(fmt.Sprintf("site %s - ", c.SiteName) + sb.String())
145149
} else if c.ComputerName != "" {
146-
output = fmt.Sprintf("Computer %s - ", c.ComputerName) + output
150+
sb.WriteString(fmt.Sprintf("Computer %s - ", c.ComputerName) + sb.String())
147151
}
148152

149153
// Add perfdata
150-
output += "|"
151-
output += fmt.Sprintf(" threats=%d", total)
152-
output += fmt.Sprintf(" threats_not_mitigated=%d", notMitigated)
154+
sb.WriteString("|")
155+
sb.WriteString(fmt.Sprintf(" threats=%d", total))
156+
sb.WriteString(fmt.Sprintf(" threats_not_mitigated=%d", notMitigated))
157+
output = sb.String()
153158

154159
// determine final state
155160
if notMitigated > 0 {

check_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestMain_ConnectionRefused(t *testing.T) {
12+
cmd := exec.Command("go", "run", "./...")
13+
out, _ := cmd.CombinedOutput()
14+
15+
actual := string(out)
16+
expected := "UNKNOWN - url and token are required"
17+
18+
if !strings.Contains(actual, expected) {
19+
t.Error("\nActual: ", actual, "\nExpected: ", expected)
20+
}
21+
}
22+
23+
type IntegrationTest struct {
24+
name string
25+
server *httptest.Server
26+
args []string
27+
expected string
28+
}
29+
30+
func TestMainCmd(t *testing.T) {
31+
tests := []IntegrationTest{
32+
{
33+
name: "invalid-json",
34+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35+
w.WriteHeader(http.StatusOK)
36+
w.Write([]byte(`{"foo}`))
37+
})),
38+
args: []string{"run", "./...", "-T", "test", "--url"},
39+
expected: "UNKNOWN - could not decode JSON from body",
40+
},
41+
{
42+
name: "empty-response",
43+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44+
w.WriteHeader(http.StatusOK)
45+
w.Write([]byte(`{"data" : [ {"id": "c86dc437", "name": "test1"}], "pagination": { "nextCursor": null, "totalItems": 2}}`))
46+
})),
47+
args: []string{"run", "./...", "-T", "test", "--url"},
48+
expected: "WARNING -",
49+
},
50+
}
51+
52+
for _, test := range tests {
53+
t.Run(test.name, func(t *testing.T) {
54+
defer test.server.Close()
55+
56+
cmd := exec.Command("go", append(test.args, test.server.URL)...)
57+
out, _ := cmd.CombinedOutput()
58+
59+
actual := string(out)
60+
61+
if !strings.Contains(actual, test.expected) {
62+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)