Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky OCI test setup #960

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package helm

import (
"fmt"
"math/rand"
"net"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -1234,26 +1232,17 @@ func TestAccResourceRelease_manifest(t *testing.T) {
})
}

func randPort() int {
for {
p := rand.Intn(65535-1024) + 1024
hp := fmt.Sprintf("0.0.0.0:%d", p)
c, err := net.DialTimeout("tcp", hp, time.Second)
if err != nil {
return p
}
c.Close()
}
}

func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
dockerPath, err := exec.LookPath("docker")
if err != nil {
t.Skip("Starting the OCI registry requires docker to be installed in the PATH")
}

ociRegistryPort := randPort()
ociRegistryURL := fmt.Sprintf("oci://localhost:%d/helm-charts", ociRegistryPort)
helmPath, err := exec.LookPath("helm")
if err != nil {
t.Skip("Starting the OCI registry requires helm to be installed in the PATH")
}

regitryContainerName := randName("registry")

// start OCI registry
Expand All @@ -1264,7 +1253,7 @@ func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
runflags := []string{
"run",
"--detach",
"--publish", fmt.Sprintf("%d:5000", ociRegistryPort),
"--publish", "5000",
"--name", regitryContainerName,
}
if usepassword {
Expand All @@ -1285,11 +1274,25 @@ func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
// wait a few seconds for the server to start
t.Log("Waiting for registry to start...")
time.Sleep(5 * time.Second)

// grab the randomly chosen port
cmd = exec.Command(dockerPath, "port", regitryContainerName)
out, err = cmd.CombinedOutput()
t.Log(string(out))
if err != nil {
t.Errorf("Failed to get port for OCI registry: %v", err)
return "", nil
}

portOutput := strings.Split(string(out), "\n")[0]
ociRegistryPort := strings.TrimSpace(strings.Split(strings.Split(portOutput, " -> ")[1], ":")[1])
ociRegistryURL := fmt.Sprintf("oci://localhost:%s/helm-charts", ociRegistryPort)

t.Log("OCI registry started at", ociRegistryURL)

// package chart
t.Log("packaging test-chart")
cmd = exec.Command("helm", "package", "testdata/charts/test-chart")
cmd = exec.Command(helmPath, "package", "testdata/charts/test-chart")
out, err = cmd.CombinedOutput()
t.Log(string(out))
if err != nil {
Expand All @@ -1300,8 +1303,8 @@ func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
if usepassword {
// log into OCI registry
t.Log("logging in to test-chart to OCI registry")
cmd = exec.Command("helm", "registry", "login",
fmt.Sprintf("localhost:%d", ociRegistryPort),
cmd = exec.Command(helmPath, "registry", "login",
fmt.Sprintf("localhost:%s", ociRegistryPort),
"--username", "hashicorp",
"--password", "terraform")
out, err = cmd.CombinedOutput()
Expand All @@ -1314,7 +1317,7 @@ func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {

// push chart to OCI registry
t.Log("pushing test-chart to OCI registry")
cmd = exec.Command("helm", "push",
cmd = exec.Command(helmPath, "push",
"test-chart-1.2.3.tgz",
ociRegistryURL)
out, err = cmd.CombinedOutput()
Expand All @@ -1326,7 +1329,7 @@ func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {

return ociRegistryURL, func() {
t.Log("stopping OCI registry")
cmd := exec.Command("docker", "rm",
cmd := exec.Command(dockerPath, "rm",
"--force", regitryContainerName)
out, err := cmd.CombinedOutput()
t.Log(string(out))
Expand Down