|
| 1 | +package ci |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +type kindClusterConfig struct { |
| 14 | + Kind string `yaml:"kind"` |
| 15 | + APIVersion string `yaml:"apiVersion"` |
| 16 | + Nodes []kindNode `yaml:"nodes"` |
| 17 | +} |
| 18 | + |
| 19 | +type kindNode struct { |
| 20 | + Role string `yaml:"role"` |
| 21 | + ExtraPortMappings []kindPortMapping `yaml:"extraPortMappings"` |
| 22 | +} |
| 23 | + |
| 24 | +type kindPortMapping struct { |
| 25 | + ContainerPort int `yaml:"containerPort"` |
| 26 | + HostPort int `yaml:"hostPort"` |
| 27 | + Protocol string `yaml:"protocol"` |
| 28 | +} |
| 29 | + |
| 30 | +func TestKindCIConfigExposesConformancePorts(t *testing.T) { |
| 31 | + data := readFile(t, "kind-ci-config.yaml") |
| 32 | + |
| 33 | + var config kindClusterConfig |
| 34 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 35 | + t.Fatalf("parse kind-ci-config.yaml: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + if config.Kind != "Cluster" { |
| 39 | + t.Fatalf("kind = %q, want Cluster", config.Kind) |
| 40 | + } |
| 41 | + if config.APIVersion != "kind.x-k8s.io/v1alpha4" { |
| 42 | + t.Fatalf("apiVersion = %q, want kind.x-k8s.io/v1alpha4", config.APIVersion) |
| 43 | + } |
| 44 | + |
| 45 | + var controlPlane *kindNode |
| 46 | + controlPlaneCount := 0 |
| 47 | + for idx := range config.Nodes { |
| 48 | + if config.Nodes[idx].Role == "control-plane" { |
| 49 | + controlPlaneCount++ |
| 50 | + controlPlane = &config.Nodes[idx] |
| 51 | + } |
| 52 | + } |
| 53 | + if controlPlane == nil { |
| 54 | + t.Fatalf("kind-ci-config.yaml has no control-plane node") |
| 55 | + } |
| 56 | + if controlPlaneCount != 1 { |
| 57 | + t.Fatalf("kind-ci-config.yaml has %d control-plane nodes, want 1", controlPlaneCount) |
| 58 | + } |
| 59 | + |
| 60 | + mappings := make(map[string]kindPortMapping, len(controlPlane.ExtraPortMappings)) |
| 61 | + for _, mapping := range controlPlane.ExtraPortMappings { |
| 62 | + protocol := strings.ToUpper(mapping.Protocol) |
| 63 | + key := portKey(mapping.HostPort, protocol) |
| 64 | + if _, exists := mappings[key]; exists { |
| 65 | + t.Fatalf("duplicate host mapping for %s", key) |
| 66 | + } |
| 67 | + mapping.Protocol = protocol |
| 68 | + mappings[key] = mapping |
| 69 | + } |
| 70 | + |
| 71 | + required := []kindPortMapping{ |
| 72 | + {HostPort: 80, ContainerPort: 30080, Protocol: "TCP"}, |
| 73 | + {HostPort: 443, ContainerPort: 30443, Protocol: "TCP"}, |
| 74 | + {HostPort: 8080, ContainerPort: 32080, Protocol: "TCP"}, |
| 75 | + {HostPort: 8090, ContainerPort: 32090, Protocol: "TCP"}, |
| 76 | + {HostPort: 8443, ContainerPort: 32443, Protocol: "TCP"}, |
| 77 | + {HostPort: 8883, ContainerPort: 31883, Protocol: "TCP"}, |
| 78 | + {HostPort: 5300, ContainerPort: 31300, Protocol: "UDP"}, |
| 79 | + } |
| 80 | + |
| 81 | + for _, want := range required { |
| 82 | + got, ok := mappings[portKey(want.HostPort, want.Protocol)] |
| 83 | + if !ok { |
| 84 | + t.Fatalf("missing host port mapping %d/%s", want.HostPort, want.Protocol) |
| 85 | + } |
| 86 | + if got.ContainerPort != want.ContainerPort { |
| 87 | + t.Fatalf( |
| 88 | + "host port mapping %d/%s containerPort = %d, want %d", |
| 89 | + want.HostPort, |
| 90 | + want.Protocol, |
| 91 | + got.ContainerPort, |
| 92 | + want.ContainerPort, |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestWorkflowsUseSharedKindClusterHelper(t *testing.T) { |
| 99 | + for _, workflow := range []string{ |
| 100 | + repoPath(".github", "workflows", "e2e.yml"), |
| 101 | + repoPath(".github", "workflows", "conformance.yml"), |
| 102 | + } { |
| 103 | + t.Run(filepath.Base(workflow), func(t *testing.T) { |
| 104 | + contents := string(readFile(t, workflow)) |
| 105 | + if !strings.Contains(contents, "scripts/ci/create-kind-cluster.sh") { |
| 106 | + t.Fatalf("%s does not call scripts/ci/create-kind-cluster.sh", workflow) |
| 107 | + } |
| 108 | + if strings.Contains(contents, `kind create cluster --name "$CLUSTER_NAME" --wait 5m`) { |
| 109 | + t.Fatalf("%s still creates a plain kind cluster without --config", workflow) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestSmokeScriptForwardsToProgrammedGatewayListener(t *testing.T) { |
| 116 | + contents := string(readFile(t, repoPath("test", "e2e", "smoke", "run.sh"))) |
| 117 | + |
| 118 | + for _, want := range []string{ |
| 119 | + `LOCAL_HTTP_PORT="${LOCAL_HTTP_PORT:-10080}"`, |
| 120 | + `GATEWAY_HTTP_PORT="${GATEWAY_HTTP_PORT:-80}"`, |
| 121 | + `"${LOCAL_HTTP_PORT}:${GATEWAY_HTTP_PORT}"`, |
| 122 | + `request_deadline=`, |
| 123 | + } { |
| 124 | + if !strings.Contains(contents, want) { |
| 125 | + t.Fatalf("smoke script missing %q", want) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if strings.Contains(contents, "10080:10080") { |
| 130 | + t.Fatalf("smoke script still forwards stale dataplane port 10080 to 10080") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func portKey(port int, protocol string) string { |
| 135 | + return fmt.Sprintf("%s/%d", protocol, port) |
| 136 | +} |
| 137 | + |
| 138 | +func readFile(t *testing.T, path string) []byte { |
| 139 | + t.Helper() |
| 140 | + |
| 141 | + data, err := os.ReadFile(path) |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("read %s: %v", path, err) |
| 144 | + } |
| 145 | + return data |
| 146 | +} |
| 147 | + |
| 148 | +func repoPath(elem ...string) string { |
| 149 | + return filepath.Join(append([]string{"..", ".."}, elem...)...) |
| 150 | +} |
0 commit comments