Skip to content

Commit

Permalink
Move tests to go
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume committed Feb 6, 2024
1 parent 923790c commit 0753025
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ jobs:
- name: Go vet
run: go vet ./...

staticcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

- uses: dominikh/[email protected]
with:
install-go: false
version: "v0.4.6"

lint:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -77,7 +63,6 @@ jobs:
needs:
- build
- go-vet
- staticcheck
- lint
- unit-tests
uses: ./.github/workflows/build-rock.yaml
Expand Down
6 changes: 2 additions & 4 deletions e2etests/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func RunContainer(containerName, networkName, imageName, configPath, certsPath s

ctx := context.Background()

// Setup port bindings and exposed ports
portBindings := nat.PortMap{}
exposedPorts := nat.PortSet{}
for containerPort, hostPort := range ports {
Expand All @@ -31,10 +30,9 @@ func RunContainer(containerName, networkName, imageName, configPath, certsPath s
exposedPorts[port] = struct{}{}
}

// Include ExposedPorts in the container configuration
contConfig := &container.Config{
Image: imageName,
ExposedPorts: exposedPorts, // Explicitly declare exposed ports
ExposedPorts: exposedPorts,
}

hostConfig := &container.HostConfig{
Expand All @@ -51,7 +49,7 @@ func RunContainer(containerName, networkName, imageName, configPath, certsPath s
return fmt.Errorf("failed to create container: %v", err)
}

if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return fmt.Errorf("failed to start container: %v", err)
}

Expand Down
64 changes: 37 additions & 27 deletions e2etests/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package e2etests

import (
"crypto/tls"
Expand All @@ -14,10 +14,13 @@ import (
"github.com/dot-5g/sepp/e2etests/docker"
)

const ProjectPath = "/home/guillaume/code/sepp/e2etests/"
const PLMNASBIFQDN = "https://0.0.0.0:1232"
const PLMNACertsPath = "plmnA/certs/"
const PLMNBCertsPath = "plmnB/certs/"
const ClientCertsPath = "client/certs/"
const PLMNACertsPath = ProjectPath + "plmnA/certs/"
const PLMNAConfigPath = ProjectPath + "plmnA/config.yaml"
const PLMNBCertsPath = ProjectPath + "plmnB/certs/"
const PLMNBConfigPath = ProjectPath + "plmnB/config.yaml"
const ClientCertsPath = ProjectPath + "client/certs/"
const PLMNASEPPHostname = "sepp-plmn-a"
const PLMNBSEPPHostname = "sepp-plmn-b"
const DockerNetworkName = "n32"
Expand All @@ -28,23 +31,32 @@ func Setup(seppAHostname string, seppBHostname string, dockerNetworkName string)
if err := docker.CreateNetwork(dockerNetworkName); err != nil {
log.Fatalf("Failed to create Docker network: %v", err)
}
if err = docker.RunContainer(seppAHostname, dockerNetworkName, "sepp:0.1", "/home/guillaume/code/sepp/e2etests/plmnA/config.yaml", "/home/guillaume/code/sepp/e2etests/plmnA/certs/", map[string]string{"1231": "1231", "1232": "1232"}); err != nil {
if err = docker.RunContainer(seppAHostname, dockerNetworkName, "sepp:0.1", PLMNAConfigPath, PLMNACertsPath, map[string]string{"1231": "1231", "1232": "1232"}); err != nil {
log.Fatalf("Failed to run PLMN A container: %v", err)
}
if err = docker.RunContainer(seppBHostname, dockerNetworkName, "sepp:0.1", "/home/guillaume/code/sepp/e2etests/plmnB/config.yaml", "/home/guillaume/code/sepp/e2etests/plmnB/certs/", map[string]string{"1233": "1233", "1234": "1234"}); err != nil {
if err = docker.RunContainer(seppBHostname, dockerNetworkName, "sepp:0.1", PLMNBConfigPath, PLMNBCertsPath, map[string]string{"1233": "1233", "1234": "1234"}); err != nil {
log.Fatalf("Failed to run PLMN B container: %v", err)
}
}

func Cleanup(seppAID string, seppBID string, networkName string) {
docker.StopAndRemoveContainer(seppAID)
docker.StopAndRemoveContainer(seppBID)
docker.RemoveNetwork(networkName)
err := docker.StopAndRemoveContainer(seppAID)
if err != nil {
log.Printf("Failed to stop and remove container %s: %v", seppAID, err)
}
err = docker.StopAndRemoveContainer(seppBID)
if err != nil {
log.Printf("Failed to stop and remove container %s: %v", seppBID, err)
}
err = docker.RemoveNetwork(networkName)
if err != nil {
log.Printf("Failed to remove network %s: %v", networkName, err)
}
}

func waitForService(url string, maxRetries int) error {
func waitForService(client *http.Client, url string, maxRetries int) error {
for i := 0; i < maxRetries; i++ {
resp, err := http.Get(url)
resp, err := client.Get(url)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
Expand All @@ -58,20 +70,18 @@ func waitForService(url string, maxRetries int) error {
return fmt.Errorf("service not available at %s", url)
}

func TestEndToEnd(t *testing.T) {
Setup(PLMNASEPPHostname, PLMNBSEPPHostname, DockerNetworkName)
defer Cleanup(PLMNASEPPHostname, PLMNBSEPPHostname, DockerNetworkName)
func getClient(caCertPath string) (*http.Client, error) {
clientCert, err := tls.LoadX509KeyPair(ClientCertsPath+"client.crt", ClientCertsPath+"client.key")
if err != nil {
t.Fatalf("Failed to load client certificate: %v", err)
return nil, fmt.Errorf("Failed to load client certificate: %v", err)
}
caCert, err := os.ReadFile(ClientCertsPath + "ca.crt")
if err != nil {
t.Fatalf("Failed to read CA certificate: %v", err)
return nil, fmt.Errorf("Failed to read CA certificate: %v", err)
}
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
t.Fatal("Failed to append CA certificate")
return nil, fmt.Errorf("Failed to append CA certificate")
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientCert},
Expand All @@ -82,17 +92,17 @@ func TestEndToEnd(t *testing.T) {
TLSClientConfig: tlsConfig,
},
}
if err := waitForService(PLMNASBIFQDN, 10); err != nil {
t.Fatalf("Failed to connect to SEPP in PLMN A: %v", err)
}
address := PLMNASBIFQDN
resp, err := client.Get(address)
return client, nil
}

func TestEndToEnd(t *testing.T) {
Setup(PLMNASEPPHostname, PLMNBSEPPHostname, DockerNetworkName)
defer Cleanup(PLMNASEPPHostname, PLMNBSEPPHostname, DockerNetworkName)
client, err := getClient(PLMNACertsPath + "ca.crt")
if err != nil {
t.Fatalf("Failed to make request: %v", err)
t.Fatalf("Failed to create client: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected status code: got %v want %v", resp.StatusCode, http.StatusOK)
if err := waitForService(client, PLMNASBIFQDN, 10); err != nil {
t.Fatalf("Failed to connect to SEPP in PLMN A: %v", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.21.6

require (
github.com/docker/docker v25.0.2+incompatible
github.com/docker/go-connections v0.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand Down

0 comments on commit 0753025

Please sign in to comment.