Skip to content

Commit

Permalink
Modified tests using Service resources to expect EndpointSlices for n…
Browse files Browse the repository at this point in the history
…ewer versions of k8s (carvel-dev#265)
  • Loading branch information
100mik authored Aug 10, 2021
1 parent 04c72c6 commit 94381d3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
34 changes: 32 additions & 2 deletions test/e2e/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
out, _ := kapp.RunWithOpts([]string{"inspect", "-a", name, "--json"}, RunOpts{})

resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -68,7 +69,21 @@ spec:
"reconcile_state": "ok",
}}

if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {
if hasEndpointSlice(respRows) {
respRows = removeEndpointSliceNameSuffix(respRows)
expected = append(expected, map[string]string{
"age": "<replaced>",
"conditions": "",
"kind": "EndpointSlice",
"name": "redis-primary",
"namespace": "kapp-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
})
}

if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})
Expand All @@ -77,6 +92,7 @@ spec:
out, _ := kapp.RunWithOpts([]string{"inspect", "-a", name, "-t", "--json"}, RunOpts{})

resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -98,7 +114,21 @@ spec:
"reconcile_state": "ok",
}}

if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {
if hasEndpointSlice(respRows) {
respRows = removeEndpointSliceNameSuffix(respRows)
expected = append(expected, map[string]string{
"age": "<replaced>",
"conditions": "",
"kind": "EndpointSlice",
"name": " L redis-primary",
"namespace": "kapp-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
})
}

if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})
Expand Down
81 changes: 77 additions & 4 deletions test/e2e/transient_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ spec:
logger.Section("inspect shows transient resource", func() {
out, _ := kapp.RunWithOpts([]string{"inspect", "-a", name, "--json"}, RunOpts{})
resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -66,14 +67,30 @@ spec:
"reconcile_info": "",
"reconcile_state": "ok",
}}
if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {

if hasEndpointSlice(respRows) {
respRows = removeEndpointSliceNameSuffix(respRows)
expected = append(expected, map[string]string{
"age": "<replaced>",
"conditions": "",
"kind": "EndpointSlice",
"name": "redis-svc",
"namespace": "kapp-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
})
}

if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})

logger.Section("delete includes transient resource", func() {
out, _ := kapp.RunWithOpts([]string{"delete", "-a", name, "--json"}, RunOpts{})
resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -98,7 +115,24 @@ spec:
"reconcile_info": "",
"reconcile_state": "ok",
}}
if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {

if hasEndpointSlice(respRows) {
respRows = removeEndpointSliceNameSuffix(respRows)
expected = append(expected, map[string]string{
"age": "<replaced>",
"op": "",
"op_strategy": "",
"wait_to": "delete",
"conditions": "",
"kind": "EndpointSlice",
"name": "redis-svc",
"namespace": "kapp-test",
"reconcile_info": "",
"reconcile_state": "ok",
})
}

if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})
Expand Down Expand Up @@ -149,6 +183,7 @@ metadata:
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})

resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -162,14 +197,15 @@ metadata:
"reconcile_info": "",
"reconcile_state": "ok",
}}
if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {
if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})

logger.Section("delete with previously transient resource (now non-transient)", func() {
out, _ := kapp.RunWithOpts([]string{"delete", "-a", name, "--json"}, RunOpts{})
resp := uitest.JSONUIFromBytes(t, []byte(out))
respRows := resp.Tables[0].Rows

expected := []map[string]string{{
"age": "<replaced>",
Expand All @@ -194,8 +230,45 @@ metadata:
"reconcile_info": "",
"reconcile_state": "ok",
}}
if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {

if hasEndpointSlice(respRows) {
respRows = removeEndpointSliceNameSuffix(respRows)
expected = append(expected, map[string]string{
"age": "<replaced>",
"op": "",
"op_strategy": "",
"wait_to": "delete",
"conditions": "",
"kind": "EndpointSlice",
"name": "redis-svc",
"namespace": "kapp-test",
"reconcile_info": "",
"reconcile_state": "ok",
})
}

if !reflect.DeepEqual(replaceAge(respRows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})
}

func removeEndpointSliceNameSuffix(result []map[string]string) []map[string]string {
for i, row := range result {
if row["kind"] == "EndpointSlice" && len(row["name"]) > 0 {
lastIndexOfDash := strings.LastIndex(row["name"], "-")
row["name"] = row["name"][:lastIndexOfDash]
}
result[i] = row
}
return result
}

func hasEndpointSlice(result []map[string]string) bool {
for _, row := range result {
if row["kind"] == "EndpointSlice" {
return true
}
}
return false
}

0 comments on commit 94381d3

Please sign in to comment.