Prettify and compare strings instead of structs in fleetctl get tests (#3047)

* Prettify and compare strings instead of structs in fleetctl get tests

* Prettify JSON testdata

* Simplify scanner for multi json
This commit is contained in:
Tomas Touceda
2021-11-22 12:20:09 -03:00
committed by GitHub
parent 69a4985cac
commit 4cb0060070
3 changed files with 231 additions and 49 deletions
+46 -46
View File
@@ -1,12 +1,11 @@
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"time"
@@ -283,53 +282,63 @@ func TestGetHosts(t *testing.T) {
+------+------------+----------+-----------------+--------+
`
jsonPrettify := func(t *testing.T, v string) string {
var i interface{}
err := json.Unmarshal([]byte(v), &i)
require.NoError(t, err)
indented, err := json.MarshalIndent(i, "", " ")
require.NoError(t, err)
return string(indented)
}
yamlPrettify := func(t *testing.T, v string) string {
var i interface{}
err := yaml.Unmarshal([]byte(v), &i)
require.NoError(t, err)
indented, err := yaml.Marshal(i)
require.NoError(t, err)
return string(indented)
}
tests := []struct {
name string
goldenFile string
unmarshaler func(data []byte, v interface{}) error
scanner func(s string) []string
args []string
name string
goldenFile string
scanner func(s string) []string
prettifier func(t *testing.T, v string) string
args []string
}{
{
name: "get hosts --json",
goldenFile: "expectedListHostsJson.json",
unmarshaler: json.Unmarshal,
name: "get hosts --json",
goldenFile: "expectedListHostsJson.json",
scanner: func(s string) []string {
var parts []string
scanner := bufio.NewScanner(bytes.NewBufferString(s))
for scanner.Scan() {
parts = append(parts, scanner.Text())
}
return parts
parts := strings.Split(s, "}\n{")
return []string{parts[0] + "}", "{" + parts[1]}
},
args: []string{"get", "hosts", "--json"},
args: []string{"get", "hosts", "--json"},
prettifier: jsonPrettify,
},
{
name: "get hosts --json test_host",
goldenFile: "expectedHostDetailResponseJson.json",
unmarshaler: json.Unmarshal,
name: "get hosts --json test_host",
goldenFile: "expectedHostDetailResponseJson.json",
scanner: func(s string) []string { return []string{s} },
args: []string{"get", "hosts", "--json", "test_host"},
prettifier: jsonPrettify,
},
{
name: "get hosts --yaml",
goldenFile: "expectedListHostsYaml.yml",
scanner: func(s string) []string {
return []string{s}
},
args: []string{"get", "hosts", "--json", "test_host"},
args: []string{"get", "hosts", "--yaml"},
prettifier: yamlPrettify,
},
{
name: "get hosts --yaml",
goldenFile: "expectedListHostsYaml.yml",
unmarshaler: yaml.Unmarshal,
scanner: func(s string) []string {
return []string{s}
},
args: []string{"get", "hosts", "--yaml"},
},
{
name: "get hosts --yaml test_host",
goldenFile: "expectedHostDetailResponseYaml.yml",
unmarshaler: yaml.Unmarshal,
name: "get hosts --yaml test_host",
goldenFile: "expectedHostDetailResponseYaml.yml",
scanner: func(s string) []string {
return splitYaml(s)
},
args: []string{"get", "hosts", "--yaml", "test_host"},
args: []string{"get", "hosts", "--yaml", "test_host"},
prettifier: yamlPrettify,
},
}
for _, tt := range tests {
@@ -337,20 +346,11 @@ func TestGetHosts(t *testing.T) {
expected, err := ioutil.ReadFile(filepath.Join("testdata", tt.goldenFile))
require.NoError(t, err)
expectedResults := tt.scanner(string(expected))
expectedSpecs := make([]specGeneric, len(expectedResults))
for i, result := range expectedResults {
var got specGeneric
require.NoError(t, tt.unmarshaler([]byte(result), &got))
expectedSpecs[i] = got
}
actualResult := tt.scanner(runAppForTest(t, tt.args))
actualSpecs := make([]specGeneric, len(actualResult))
for i, result := range actualResult {
var spec specGeneric
require.NoError(t, tt.unmarshaler([]byte(result), &spec), result)
actualSpecs[i] = spec
require.Equal(t, len(expectedResults), len(actualResult))
for i := range expectedResults {
require.Equal(t, tt.prettifier(t, expectedResults[i]), tt.prettifier(t, actualResult[i]))
}
require.Equal(t, expectedSpecs, actualSpecs, actualResult)
})
}
+75 -1
View File
@@ -1 +1,75 @@
{"kind":"host","apiVersion":"v1","spec":{"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","id":0,"detail_updated_at":"0001-01-01T00:00:00Z","label_updated_at":"0001-01-01T00:00:00Z","policy_updated_at":"0001-01-01T00:00:00Z","last_enrolled_at":"0001-01-01T00:00:00Z","seen_time":"0001-01-01T00:00:00Z","refetch_requested":false,"hostname":"test_host","uuid":"","platform":"","osquery_version":"","os_version":"","build":"","platform_like":"","code_name":"","uptime":0,"memory":0,"cpu_type":"","cpu_subtype":"","cpu_brand":"","cpu_physical_cores":0,"cpu_logical_cores":0,"hardware_vendor":"","hardware_model":"","hardware_version":"","hardware_serial":"","computer_name":"test_host","primary_ip":"","primary_mac":"","distributed_interval":0,"config_tls_refresh":0,"logger_tls_period":0,"team_id":null,"pack_stats":null,"team_name":null,"gigs_disk_space_available":0,"percent_disk_space_available":0,"issues":{"total_issues_count":0,"failing_policies_count":0},"labels":[],"packs":[],"policies":[{"id":1,"query_id":2,"query_name":"query1","query_description":"","response":"passes","resolution":""},{"id":2,"query_id":43,"query_name":"query2","query_description":"","response":"fails","resolution":""}],"status":"mia","display_text":"test_host"}}
{
"kind":"host",
"apiVersion":"v1",
"spec":{
"created_at":"0001-01-01T00:00:00Z",
"updated_at":"0001-01-01T00:00:00Z",
"id":0,
"detail_updated_at":"0001-01-01T00:00:00Z",
"label_updated_at":"0001-01-01T00:00:00Z",
"policy_updated_at":"0001-01-01T00:00:00Z",
"last_enrolled_at":"0001-01-01T00:00:00Z",
"seen_time":"0001-01-01T00:00:00Z",
"refetch_requested":false,
"hostname":"test_host",
"uuid":"",
"platform":"",
"osquery_version":"",
"os_version":"",
"build":"",
"platform_like":"",
"code_name":"",
"uptime":0,
"memory":0,
"cpu_type":"",
"cpu_subtype":"",
"cpu_brand":"",
"cpu_physical_cores":0,
"cpu_logical_cores":0,
"hardware_vendor":"",
"hardware_model":"",
"hardware_version":"",
"hardware_serial":"",
"computer_name":"test_host",
"primary_ip":"",
"primary_mac":"",
"distributed_interval":0,
"config_tls_refresh":0,
"logger_tls_period":0,
"team_id":null,
"pack_stats":null,
"team_name":null,
"gigs_disk_space_available":0,
"percent_disk_space_available":0,
"issues":{
"total_issues_count":0,
"failing_policies_count":0
},
"labels":[
],
"packs":[
],
"policies":[
{
"id":1,
"query_id":2,
"query_name":"query1",
"query_description":"",
"response":"passes",
"resolution":""
},
{
"id":2,
"query_id":43,
"query_name":"query2",
"query_description":"",
"response":"fails",
"resolution":""
}
],
"status":"mia",
"display_text":"test_host"
}
}
+110 -2
View File
@@ -1,2 +1,110 @@
{"kind":"host","apiVersion":"v1","spec":{"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","id":0,"detail_updated_at":"0001-01-01T00:00:00Z","label_updated_at":"0001-01-01T00:00:00Z","last_enrolled_at":"0001-01-01T00:00:00Z","seen_time":"0001-01-01T00:00:00Z","refetch_requested":false,"hostname":"test_host","uuid":"","platform":"","osquery_version":"","os_version":"","build":"","platform_like":"","policy_updated_at":"0001-01-01T00:00:00Z","code_name":"","uptime":0,"memory":0,"cpu_type":"","cpu_subtype":"","cpu_brand":"","cpu_physical_cores":0,"cpu_logical_cores":0,"hardware_vendor":"","hardware_model":"","hardware_version":"","hardware_serial":"","computer_name":"test_host","primary_ip":"","primary_mac":"","distributed_interval":0,"config_tls_refresh":0,"logger_tls_period":0,"team_id":null,"pack_stats":null,"team_name":null,"additional":{"query1":[{"col1":"val","col2":42}]},"gigs_disk_space_available":0,"percent_disk_space_available":0,"issues":{"total_issues_count":0,"failing_policies_count":0},"status":"mia","display_text":"test_host"}}
{"kind":"host","apiVersion":"v1","spec":{"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","id":0,"detail_updated_at":"0001-01-01T00:00:00Z","label_updated_at":"0001-01-01T00:00:00Z","last_enrolled_at":"0001-01-01T00:00:00Z","seen_time":"0001-01-01T00:00:00Z","refetch_requested":false,"hostname":"test_host2","uuid":"","platform":"","osquery_version":"","os_version":"","build":"","platform_like":"","policy_updated_at":"0001-01-01T00:00:00Z","code_name":"","uptime":0,"memory":0,"cpu_type":"","cpu_subtype":"","cpu_brand":"","cpu_physical_cores":0,"cpu_logical_cores":0,"hardware_vendor":"","hardware_model":"","hardware_version":"","hardware_serial":"","computer_name":"test_host2","primary_ip":"","primary_mac":"","distributed_interval":0,"config_tls_refresh":0,"logger_tls_period":0,"team_id":null,"pack_stats":null,"team_name":null,"gigs_disk_space_available":0,"percent_disk_space_available":0,"issues":{"total_issues_count":0,"failing_policies_count":0},"status":"mia","display_text":"test_host2"}}
{
"kind":"host",
"apiVersion":"v1",
"spec":{
"created_at":"0001-01-01T00:00:00Z",
"updated_at":"0001-01-01T00:00:00Z",
"id":0,
"detail_updated_at":"0001-01-01T00:00:00Z",
"label_updated_at":"0001-01-01T00:00:00Z",
"last_enrolled_at":"0001-01-01T00:00:00Z",
"seen_time":"0001-01-01T00:00:00Z",
"refetch_requested":false,
"hostname":"test_host",
"uuid":"",
"platform":"",
"osquery_version":"",
"os_version":"",
"build":"",
"platform_like":"",
"policy_updated_at":"0001-01-01T00:00:00Z",
"code_name":"",
"uptime":0,
"memory":0,
"cpu_type":"",
"cpu_subtype":"",
"cpu_brand":"",
"cpu_physical_cores":0,
"cpu_logical_cores":0,
"hardware_vendor":"",
"hardware_model":"",
"hardware_version":"",
"hardware_serial":"",
"computer_name":"test_host",
"primary_ip":"",
"primary_mac":"",
"distributed_interval":0,
"config_tls_refresh":0,
"logger_tls_period":0,
"team_id":null,
"pack_stats":null,
"team_name":null,
"additional":{
"query1":[
{
"col1":"val",
"col2":42
}
]
},
"gigs_disk_space_available":0,
"percent_disk_space_available":0,
"issues":{
"total_issues_count":0,
"failing_policies_count":0
},
"status":"mia",
"display_text":"test_host"
}
}
{
"kind":"host",
"apiVersion":"v1",
"spec":{
"created_at":"0001-01-01T00:00:00Z",
"updated_at":"0001-01-01T00:00:00Z",
"id":0,
"detail_updated_at":"0001-01-01T00:00:00Z",
"label_updated_at":"0001-01-01T00:00:00Z",
"last_enrolled_at":"0001-01-01T00:00:00Z",
"seen_time":"0001-01-01T00:00:00Z",
"refetch_requested":false,
"hostname":"test_host2",
"uuid":"",
"platform":"",
"osquery_version":"",
"os_version":"",
"build":"",
"platform_like":"",
"policy_updated_at":"0001-01-01T00:00:00Z",
"code_name":"",
"uptime":0,
"memory":0,
"cpu_type":"",
"cpu_subtype":"",
"cpu_brand":"",
"cpu_physical_cores":0,
"cpu_logical_cores":0,
"hardware_vendor":"",
"hardware_model":"",
"hardware_version":"",
"hardware_serial":"",
"computer_name":"test_host2",
"primary_ip":"",
"primary_mac":"",
"distributed_interval":0,
"config_tls_refresh":0,
"logger_tls_period":0,
"team_id":null,
"pack_stats":null,
"team_name":null,
"gigs_disk_space_available":0,
"percent_disk_space_available":0,
"issues":{
"total_issues_count":0,
"failing_policies_count":0
},
"status":"mia",
"display_text":"test_host2"
}
}