From 4cb006007072d332be3e60db646d39c5b44cf43d Mon Sep 17 00:00:00 2001 From: Tomas Touceda Date: Mon, 22 Nov 2021 12:20:09 -0300 Subject: [PATCH] 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 --- cmd/fleetctl/get_test.go | 92 +++++++------- .../expectedHostDetailResponseJson.json | 76 +++++++++++- .../testdata/expectedListHostsJson.json | 112 +++++++++++++++++- 3 files changed, 231 insertions(+), 49 deletions(-) diff --git a/cmd/fleetctl/get_test.go b/cmd/fleetctl/get_test.go index c4332816c5..08706a8270 100644 --- a/cmd/fleetctl/get_test.go +++ b/cmd/fleetctl/get_test.go @@ -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) }) } diff --git a/cmd/fleetctl/testdata/expectedHostDetailResponseJson.json b/cmd/fleetctl/testdata/expectedHostDetailResponseJson.json index 6334e5b7e3..808019e7a5 100644 --- a/cmd/fleetctl/testdata/expectedHostDetailResponseJson.json +++ b/cmd/fleetctl/testdata/expectedHostDetailResponseJson.json @@ -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"}} \ No newline at end of file +{ + "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" + } +} \ No newline at end of file diff --git a/cmd/fleetctl/testdata/expectedListHostsJson.json b/cmd/fleetctl/testdata/expectedListHostsJson.json index 7dabdd2787..4c24311761 100644 --- a/cmd/fleetctl/testdata/expectedListHostsJson.json +++ b/cmd/fleetctl/testdata/expectedListHostsJson.json @@ -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"}} \ No newline at end of file +{ + "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" + } +} \ No newline at end of file