Skip to content

Instantly share code, notes, and snippets.

@thockin
Created November 25, 2020 08:24
Show Gist options
  • Save thockin/cccc9c9a580b4830ee0946ddd43eeafe to your computer and use it in GitHub Desktop.
Save thockin/cccc9c9a580b4830ee0946ddd43eeafe to your computer and use it in GitHub Desktop.
Hack to generate testcases for service REST create-test
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) == 0 {
return "unset"
}
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"
}
var tweaks = strlist
func pref(p string, cluster []string) []string {
if cluster[0] == p || len(cluster) == 1 {
return cluster
}
return []string{cluster[1], cluster[0]}
}
func main() {
clusterStacks := [][]string{
stack("v4"),
stack("v6"),
stack("v4", "v6"),
stack("v6", "v4"),
}
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)")},
}
fundamentals := []struct {
name string
tweaks []string
skip func(cluster []string, pol string, fam string) bool
reqs []string
}{{
"ClusterIPs:unset",
tweaks(),
func(cluster []string, pol string, fam string) bool {
return false
},
nil,
}, {
"ClusterIPs:v4",
tweaks("tweakClusterIPs(\"10.0.0.1\")"),
func(cluster []string, pol string, fam string) bool {
return fmtStack(cluster) == "v6"
},
stack("v4"),
}, {
"ClusterIPs:v6",
tweaks("tweakClusterIPs(\"2000::1\")"),
func(cluster []string, pol string, fam string) bool {
return fmtStack(cluster) == "v4"
},
stack("v6"),
}, {
"ClusterIPs:v4v6",
tweaks("tweakClusterIPs(\"10.0.0.1\", \"2000::1\")"),
func(cluster []string, pol string, fam string) bool {
return false
},
stack("v4", "v6"),
}, {
"ClusterIPs:v6v4",
tweaks("tweakClusterIPs(\"2000::1\", \"10.0.0.1\")"),
func(cluster []string, pol string, fam string) bool {
return false
},
stack("v6", "v4"),
}, {
"Headless",
tweaks("tweakHeadless"),
func(cluster []string, pol string, fam string) bool {
return false
},
nil,
}, {
"HeadlessSelectorless",
tweaks("tweakHeadless", "tweakClearSelector"),
func(stack []string, pol string, fam string) bool {
return false
},
nil,
}}
for _, cluster := range clusterStacks {
fmt.Printf("{\n")
fmt.Printf(" name: \"%s:%s\",\n", fmtSingleDual(cluster), fmtStack(cluster))
fmt.Printf(" clusterFamilies: []api.IPFamily{%s},\n", apiStack(cluster))
fmt.Printf(" cases: []testCase {\n")
names := map[string]bool{}
for _, fund := range fundamentals {
didOne := false
for _, pol := range policies {
for _, fam := range families {
name := fmt.Sprintf("%s_Policy:%s_Families:%s", fund.name, pol.name, fam.name)
if names[name] {
panic(fmt.Sprintf("repeated name %q", name))
}
names[name] = true
if fund.skip(cluster, pol.name, fam.name) {
continue
}
if !didOne {
fmt.Printf(" //----------------------------------------\n")
fmt.Printf(" // %s:%s %s\n", fmtSingleDual(cluster), fmtStack(cluster), fund.name)
fmt.Printf(" //----------------------------------------\n")
}
didOne = true
fmt.Printf(" {\n")
expectError := false
expectPolicy := "FIXME"
expectFamilies := "FIXME"
// Error if testcase (fund) requests more than the cluster
// offers, NOTE that headless+selectorless is a
// special-case and undoes this.
if len(fund.reqs) > len(cluster) {
expectError = true
}
// Error if testcase requires dual when the cluster doesn't
// offer it. Again, headless+selectorless is special.
if len(cluster) == 1 {
if len(fam.name) == 4 || pol.name == "RequireDualStack" {
expectError = true
}
}
// Error if the cluster doesn't offer the families needed.
if fam.name != "unset" {
switch {
case strings.Contains(fmtStack(cluster), fam.name): // OK
case len(fam.name) == 4 && len(fmtStack(cluster)) == 4: // OK
default:
expectError = true
}
}
// Error if fund and fam disagree on which family is first.
if r, f := fmtStack(fund.reqs), fam.name; r != "unset" && f != "unset" && r[0:2] != f[0:2] {
expectError = true
}
expectPolicy = pol.name // we don't change the request unless unset
// Handle the permutations in the most obvious ways. This
// list was seeded by a program.
switch {
case pol.name == "unset" && fam.name == "unset" && fmtStack(fund.reqs) == "unset":
expectPolicy = "SingleStack"
expectFamilies = apiOneStack(cluster[0])
case pol.name == "unset" && fam.name == "unset" && fmtStack(fund.reqs) == "v4":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v4"))
case pol.name == "unset" && fam.name == "unset" && fmtStack(fund.reqs) == "v6":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v6"))
case pol.name == "unset" && fam.name == "unset" && fmtStack(fund.reqs) == "v4v6":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v4", "v6"))
case pol.name == "unset" && fam.name == "unset" && fmtStack(fund.reqs) == "v6v4":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v6", "v4"))
case pol.name == "unset" && fam.name == "v4" && fmtStack(fund.reqs) == "unset":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v4"))
case pol.name == "unset" && fam.name == "v4" && fmtStack(fund.reqs) == "v4":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v4"))
case pol.name == "unset" && fam.name == "v4" && fmtStack(fund.reqs) == "v6":
expectError = true
case pol.name == "unset" && fam.name == "v4" && fmtStack(fund.reqs) == "v4v6":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v4", "v6"))
case pol.name == "unset" && fam.name == "v4" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "unset" && fam.name == "v6" && fmtStack(fund.reqs) == "unset":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v6"))
case pol.name == "unset" && fam.name == "v6" && fmtStack(fund.reqs) == "v4":
expectError = true
case pol.name == "unset" && fam.name == "v6" && fmtStack(fund.reqs) == "v6":
expectPolicy = "SingleStack"
expectFamilies = apiStack(stack("v6"))
case pol.name == "unset" && fam.name == "v6" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "unset" && fam.name == "v6" && fmtStack(fund.reqs) == "v6v4":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v6", "v4"))
case pol.name == "unset" && fam.name == "v4v6" && fmtStack(fund.reqs) == "unset":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v4", "v6"))
case pol.name == "unset" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v4", "v6"))
case pol.name == "unset" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6":
expectError = true
case pol.name == "unset" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4v6":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v4", "v6"))
case pol.name == "unset" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "unset" && fam.name == "v6v4" && fmtStack(fund.reqs) == "unset":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v6", "v4"))
case pol.name == "unset" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4":
expectError = true
case pol.name == "unset" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v6", "v4"))
case pol.name == "unset" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "unset" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6v4":
expectPolicy = "RequireDualStack"
expectFamilies = apiStack(stack("v6", "v4"))
case pol.name == "SingleStack" && fam.name == "unset" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiOneStack(cluster[0])
case pol.name == "SingleStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(stack("v4"))
case pol.name == "SingleStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(stack("v6"))
case pol.name == "SingleStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(stack("v4"))
case pol.name == "SingleStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(stack("v4"))
case pol.name == "SingleStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(stack("v6"))
case pol.name == "SingleStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(stack("v6"))
case pol.name == "SingleStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "unset":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "unset":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4v6":
expectError = true
case pol.name == "SingleStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6v4":
expectError = true
case pol.name == "PreferDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(cluster)
case pol.name == "PreferDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "PreferDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(cluster)
case pol.name == "RequireDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "unset" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v4v6" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "unset":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6":
expectFamilies = apiStack(pref("v6", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v4v6":
expectFamilies = apiStack(pref("v4", cluster))
case pol.name == "RequireDualStack" && fam.name == "v6v4" && fmtStack(fund.reqs) == "v6v4":
expectFamilies = apiStack(pref("v6", cluster))
}
// Special-cases.
if fund.name == "HeadlessSelectorless" {
expectError = false
if pol.name == "unset" {
expectPolicy = "RequireDualStack"
}
if pol.name == "unset" || pol.name == "RequireDualStack" || pol.name == "PreferDualStack" {
if strings.HasPrefix(fam.name, "v4") {
expectFamilies = apiStack(stack("v4", "v6"))
} else if strings.HasPrefix(fam.name, "v6") {
expectFamilies = apiStack(stack("v6", "v4"))
} else if fam.name == "unset" {
if cluster[0] == "v4" {
expectFamilies = apiStack(stack("v4", "v6"))
} else if cluster[0] == "v6" {
expectFamilies = apiStack(stack("v6", "v4"))
}
}
}
if pol.name == "SingleStack" && len(fam.name) == 4 {
expectError = true
}
}
fmt.Printf(" name: %q,\n", name)
fmt.Printf(" svc: trivialService(%s),\n", strings.Join(join(fund.tweaks, pol.tweaks, fam.tweaks), ", "))
if expectError {
fmt.Printf(" expectError: %v,\n", expectError)
} else {
fmt.Printf(" expectPolicy: &%v,\n", expectPolicy)
fmt.Printf(" expectFamilies: []api.IPFamily{%v},\n", expectFamilies)
}
fmt.Printf(" },") // no newline
}
}
if didOne {
fmt.Printf("\n")
}
}
fmt.Printf(" },\n},") // no newline
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment