Skip to content

Commit

Permalink
atc/testing: make setup test compatible with nodeports for demos
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Nov 24, 2024
1 parent dbe8bb6 commit 9d09c61
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cmd/atc/internal/testing/apis/backend/flight/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"cmp"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -34,6 +35,8 @@ func run() error {
}
}

backend.Spec.ServicePort = cmp.Or(backend.Spec.ServicePort, 3000)

if backend.Spec.Labels == nil {
backend.Spec.Labels = map[string]string{}
}
Expand Down Expand Up @@ -74,14 +77,14 @@ func createDeployment(backend v1.Backend) *appsv1.Deployment {
Env: []corev1.EnvVar{
{
Name: "PORT",
Value: strconv.Itoa(3000),
Value: strconv.Itoa(backend.Spec.ServicePort),
},
},
Ports: []corev1.ContainerPort{
{
Name: backend.Name,
Protocol: corev1.ProtocolTCP,
ContainerPort: 3000,
ContainerPort: int32(backend.Spec.ServicePort),
},
},
},
Expand Down
9 changes: 5 additions & 4 deletions cmd/atc/internal/testing/apis/backend/v1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type Backend struct {
}

type BackendSpec struct {
Image string `json:"image"`
Replicas int32 `json:"replicas"`
Labels map[string]string `json:"labels,omitempty"`
NodePort int `json:"nodePort,omitempty"`
Image string `json:"image"`
Replicas int32 `json:"replicas"`
Labels map[string]string `json:"labels,omitempty"`
NodePort int `json:"nodePort,omitempty"`
ServicePort int `json:"port,omitempty"`
}

func (backend Backend) MarshalJSON() ([]byte, error) {
Expand Down
13 changes: 12 additions & 1 deletion cmd/atc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ func TestAirTrafficController(t *testing.T) {
require.NoError(t, os.MkdirAll("./test_output", 0o755))

require.NoError(t, x.X("kind delete clusters --all"))
require.NoError(t, x.X("kind create cluster --name=atc-test"))

require.NoError(t, x.X("kind create cluster --name=atc-test --config -", x.Input(strings.NewReader(`
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 80
listenAddress: "127.0.0.1"
protocol: TCP
`))))

require.NoError(t, x.X(
"go build -o ./test_output/atc-installer.wasm ../atc-installer",
Expand Down
13 changes: 11 additions & 2 deletions internal/x/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package x

import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
Expand All @@ -12,8 +13,9 @@ import (
var cyan = ansi.MakeStyle(ansi.FgCyan)

type xoptions struct {
Env []string
Dir string
Env []string
Dir string
Input io.Reader
}

func Env(e ...string) XOpt {
Expand All @@ -28,6 +30,12 @@ func Dir(d string) XOpt {
}
}

func Input(r io.Reader) XOpt {
return func(opts *xoptions) {
opts.Input = r
}
}

type XOpt func(*xoptions)

func X(line string, opts ...XOpt) error {
Expand All @@ -47,6 +55,7 @@ func Xf(line string, printArgs []any, opts ...XOpt) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = options.Input
cmd.Env = append(os.Environ(), options.Env...)
cmd.Dir = options.Dir

Expand Down

0 comments on commit 9d09c61

Please sign in to comment.