-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcsv_utils.go
More file actions
76 lines (66 loc) · 2.12 KB
/
csv_utils.go
File metadata and controls
76 lines (66 loc) · 2.12 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
//go:build test_e2e || test_compatibility
package tests
import (
"encoding/csv"
"fmt"
"net/http"
"net/url"
"github.com/stackrox/rox/pkg/sliceutils"
"github.com/stackrox/rox/pkg/testutils"
"github.com/stackrox/rox/pkg/testutils/centralgrpc"
"github.com/stackrox/rox/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func verifyRiskEventTimelineCSV(t testutils.T, deploymentID string, eventNamesExpected []string) {
// Export a CSV of the deployment's event timeline, and verify its content
const baseURL = "/api/risk/timeline/export/csv"
params := url.Values{}
params.Add("query", fmt.Sprintf("Deployment ID:\"%s\"", deploymentID))
escapedURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
// Get an HTTP client and query for csv content response
client := centralgrpc.HTTPClientForCentral(t)
resp, err := client.Get(escapedURL)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer utils.IgnoreError(resp.Body.Close)
// Read the CSV content
cr := csv.NewReader(resp.Body)
rows, err := cr.ReadAll()
require.NoError(t, err)
// We expect a certain ordering of the columns. Check the first row since it is the header
// The very first item has the byte order mark as well.
assert.Equal(
t,
[]string{
"\ufeffEvent Timestamp",
"Event Type",
"Event Name",
"Process Args",
"Process Parent Name",
"Process Baselined",
"Process UID",
"Process Parent UID",
"Container Exit Code",
"Container Exit Reason",
"Container ID",
"Container Name",
"Container Start Time",
"Deployment ID",
"Pod ID",
"Pod Name",
"Pod Start Time",
"Pod Container Count",
},
rows[0])
// Remove headers
rows = rows[1:]
// Check event names match
// Index 0 of a row is the timestamp and 2 is the event name
eventNamesInCSV := sliceutils.Map(rows, func(row []string) string { return row[2] })
assert.ElementsMatch(t, eventNamesExpected, eventNamesInCSV)
// All the records should be ordered by their event timestamp in a reverse order (latest first)
for i := 0; i < len(rows)-1; i++ {
assert.GreaterOrEqual(t, rows[i][0], rows[i+1][0])
}
}