Last active
November 20, 2020 00:39
-
-
Save thockin/2cd9e4f3aa06262b5242533b2403de2a to your computer and use it in GitHub Desktop.
Code to generate all full set of service IP-Family test cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func strlist(strs ...string) []string { | |
return strs | |
} | |
func join(strlists ...[]string) []string { | |
out := []string{} | |
for _, l := range strlists { | |
out = append(out, l...) | |
} | |
return out | |
} | |
var stack = strlist | |
func fmtSingleDual(s []string) string { | |
if len(s) == 1 { | |
return "singlestack" | |
} | |
return "dualstack" | |
} | |
func fmtStack(s []string) string { | |
if len(s) == 1 { | |
return s[0] | |
} | |
return s[0] + s[1] | |
} | |
func apiStack(s []string) string { | |
if len(s) == 1 { | |
return apiOneStack(s[0]) | |
} | |
return fmt.Sprintf("%s, %s", apiOneStack(s[0]), apiOneStack(s[1])) | |
} | |
func apiOneStack(s string) string { | |
if s == "v4" { | |
return "api.IPv4Protocol" | |
} | |
return "api.IPv6Protocol" | |
} | |
func fmtGate(g bool) string { | |
if g { | |
return "gate:on" | |
} | |
return "gate:off" | |
} | |
var tweaks = strlist | |
func main() { | |
stacks := [][]string{ | |
stack("v4"), | |
stack("v6"), | |
stack("v4", "v6"), | |
stack("v6", "v4"), | |
} | |
gates := []bool{false, true} | |
fundamentals := []struct { | |
name string | |
tweaks []string | |
}{ | |
{"ExternalName", tweaks("tweakTypeExternalName")}, | |
{"ClusterIP:unset", tweaks()}, | |
{"ClusterIP:v4", tweaks("tweakClusterIP(\"10.0.0.1\")")}, | |
{"ClusterIP:v6", tweaks("tweakClusterIP(\"2000::1\")")}, | |
{"ClusterIPs:v4", tweaks("tweakClusterIPs(\"10.0.0.1\")")}, | |
{"ClusterIPs:v6", tweaks("tweakClusterIPs(\"2000::1\")")}, | |
{"ClusterIPs:v4v6", tweaks("tweakClusterIPs(\"10.0.0.1\", \"2000::1\")")}, | |
{"ClusterIPs:v6v4", tweaks("tweakClusterIPs(\"2000::1\", \"10.0.0.1\")")}, | |
{"Headless", tweaks("tweakHeadless")}, | |
{"HeadlessSelectorless", tweaks("tweakHeadless", "tweakClearSelector")}, | |
} | |
policies := []struct { | |
name string | |
tweaks []string | |
}{ | |
{"unset", tweaks()}, | |
{"SingleStack", tweaks("tweakIPFamilyPolicy(SingleStack)")}, | |
{"PreferDualStack", tweaks("tweakIPFamilyPolicy(PreferDualStack)")}, | |
{"RequireDualStack", tweaks("tweakIPFamilyPolicy(RequireDualStack)")}, | |
} | |
families := []struct { | |
name string | |
tweaks []string | |
}{ | |
{"unset", tweaks()}, | |
{"v4", tweaks("tweakIPFamilies(api.IPv4Protocol)")}, | |
{"v6", tweaks("tweakIPFamilies(api.IPv6Protocol)")}, | |
{"v4v6", tweaks("tweakIPFamilies(api.IPv4Protocol, api.IPv6Protocol)")}, | |
{"v6v4", tweaks("tweakIPFamilies(api.IPv6Protocol, api.IPv4Protocol)")}, | |
} | |
names := map[string]bool{} | |
for _, fund := range fundamentals { | |
for _, stack := range stacks { | |
fmt.Printf("//----------------------------------------\n") | |
fmt.Printf("// %s %s:%s\n", fund.name, fmtSingleDual(stack), fmtStack(stack)) | |
fmt.Printf("//----------------------------------------\n") | |
didOne := false | |
for _, pol := range policies { | |
for _, fam := range families { | |
for _, gate := range gates { | |
name := fmt.Sprintf("%s:%s_%s_%s_Policy:%s_Families:%s", | |
fmtSingleDual(stack), fmtStack(stack), | |
fmtGate(gate), | |
fund.name, pol.name, fam.name) | |
if names[name] { | |
panic(fmt.Sprintf("repeated name %q", name)) | |
} | |
names[name] = true | |
fmt.Printf("{\n") | |
fmt.Printf("\tname: %q,\n", name) | |
fmt.Printf("\tclusterFamilies: []api.IPFamily{%s},\n", apiStack(stack)) | |
fmt.Printf("\tenableDualStack: %v,\n", gate) | |
fmt.Printf("\tsvc: trivialService(%s),\n", strings.Join(join(fund.tweaks, pol.tweaks, fam.tweaks), ", ")) | |
fmt.Printf("\texpectError: false, //FIXME\n") | |
fmt.Printf("\texpectPolicy: nil, //FIXME\n") | |
fmt.Printf("\texpectFamilies: nil, //FIXME\n") | |
fmt.Printf("},") // no newline | |
didOne = true | |
} | |
} | |
} | |
if didOne { | |
fmt.Printf("\n") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment