From 57c5ff1eaeafe78c55d65d3ee0748f2d4ccdd36e Mon Sep 17 00:00:00 2001 From: Lucas Rodriguez Date: Wed, 24 Apr 2024 16:00:39 -0500 Subject: [PATCH] test: unpin version in bigbang extension test --- src/extensions/bigbang/test/bigbang_test.go | 126 ++++++++++---------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/src/extensions/bigbang/test/bigbang_test.go b/src/extensions/bigbang/test/bigbang_test.go index aef7bd5df0..64f0f95e23 100644 --- a/src/extensions/bigbang/test/bigbang_test.go +++ b/src/extensions/bigbang/test/bigbang_test.go @@ -5,9 +5,12 @@ package main import ( "context" + "encoding/json" "fmt" + "io" "net/http" "os" + "regexp" "strings" "testing" @@ -17,35 +20,28 @@ import ( "github.com/stretchr/testify/require" ) -// Code related to fetching the last two Big Bang versions -// and using them to set the BB_VERSION and BB_MAJOR variables -// has been commented out due to a bug in how Zarf clones and checks out git repos. -// -// https://github.com/defenseunicorns/zarf/actions/runs/8529925302/job/23403205495?pr=2411#step:9:897 -// -// The versions are currently hardcoded to the last two known working versions. -// TODO: fix the git clone/checkout bug and update this test to not be hardcoded. - // The Big Bang project ID on Repo1 -// const bbProjID = "2872" +const bbProjID = "2872" var ( - zarf string - // previous string - // latest string + zarf string + previous string + latest string ) func TestMain(m *testing.M) { + var err error + // Change to the build dir if err := os.Chdir("../../../../build/"); err != nil { panic(err) } - // // Get the latest and previous releases - // latest, previous, err = getReleases() - // if err != nil { - // panic(err) - // } + // Get the latest and previous releases + latest, previous, err = getReleases() + if err != nil { + panic(err) + } // Get the Zarf CLI path zarf = fmt.Sprintf("./%s", test.GetCLIName()) @@ -74,8 +70,8 @@ func TestReleases(t *testing.T) { require.NoError(t, err) // Build the previous version - bbVersion := "--set=BB_VERSION=2.22.0" - bbMajor := "--set=BB_MAJOR=2" + bbVersion := fmt.Sprintf("--set=BB_VERSION=%s", previous) + bbMajor := fmt.Sprintf("--set=BB_MAJOR=%s", previous[0:1]) stdOut, stdErr, err = zarfExec("package", "create", "../src/extensions/bigbang/test/package", bbVersion, bbMajor, tmpdir, "--confirm") require.NoError(t, err, stdOut, stdErr) @@ -84,7 +80,7 @@ func TestReleases(t *testing.T) { require.NoError(t, err, stdOut, stdErr) // Deploy the previous version - pkgPath := fmt.Sprintf("zarf-package-big-bang-test-%s-2.22.0.tar.zst", arch) + pkgPath := fmt.Sprintf("zarf-package-big-bang-test-%s-%s.tar.zst", arch, previous) stdOut, stdErr, err = zarfExec("package", "deploy", pkgPath, tmpdir, "--confirm") require.NoError(t, err, stdOut, stdErr) @@ -100,8 +96,8 @@ func TestReleases(t *testing.T) { require.NoError(t, err, stdOut, stdErr) // Build the latest version - bbVersion = "--set=BB_VERSION=2.23.0" - bbMajor = "--set=BB_MAJOR=2" + bbVersion = fmt.Sprintf("--set=BB_VERSION=%s", latest) + bbMajor = fmt.Sprintf("--set=BB_MAJOR=%s", latest[0:1]) stdOut, stdErr, err = zarfExec("package", "create", "../src/extensions/bigbang/test/package", bbVersion, bbMajor, "--differential", pkgPath, tmpdir, "--confirm") require.NoError(t, err, stdOut, stdErr) @@ -114,7 +110,7 @@ func TestReleases(t *testing.T) { require.NoError(t, err, stdOut, stdErr) // Deploy the latest version - pkgPath = fmt.Sprintf("zarf-package-big-bang-test-%s-2.22.0-differential-2.23.0.tar.zst", arch) + pkgPath = fmt.Sprintf("zarf-package-big-bang-test-%s-%s-differential-%s.tar.zst", arch, previous, latest) stdOut, stdErr, err = zarfExec("package", "deploy", pkgPath, tmpdir, "--confirm") require.NoError(t, err, stdOut, stdErr) @@ -156,44 +152,44 @@ func getZarfVersion(t *testing.T) string { return strings.Trim(stdOut, "\n") } -// func getReleases() (latest, previous string, err error) { -// // Create the URL for the API endpoint -// url := fmt.Sprintf("https://repo1.dso.mil/api/v4/projects/%s/repository/tags", bbProjID) - -// // Send an HTTP GET request to the API endpoint -// resp, err := http.Get(url) -// if err != nil { -// return latest, previous, err -// } -// defer resp.Body.Close() - -// // Read the response body -// body, err := io.ReadAll(resp.Body) -// if err != nil { -// return latest, previous, err -// } - -// // Parse the response body as a JSON array of objects -// var data []map[string]interface{} -// err = json.Unmarshal(body, &data) -// if err != nil { -// return latest, previous, err -// } - -// // Compile the regular expression for filtering tags that don't contain a hyphen -// re := regexp.MustCompile("^[^-]+$") - -// // Create a slice to store the tag names that match the regular expression -// var releases []string - -// // Iterate over the tags returned by the API, and filter out tags that don't match the regular expression -// for _, tag := range data { -// name := tag["name"].(string) -// if re.MatchString(name) { -// releases = append(releases, name) -// } -// } - -// // Set the latest and previous release variables to the first two releases -// return releases[0], releases[1], nil -// } +func getReleases() (latest, previous string, err error) { + // Create the URL for the API endpoint + url := fmt.Sprintf("https://repo1.dso.mil/api/v4/projects/%s/repository/tags", bbProjID) + + // Send an HTTP GET request to the API endpoint + resp, err := http.Get(url) + if err != nil { + return latest, previous, err + } + defer resp.Body.Close() + + // Read the response body + body, err := io.ReadAll(resp.Body) + if err != nil { + return latest, previous, err + } + + // Parse the response body as a JSON array of objects + var data []map[string]interface{} + err = json.Unmarshal(body, &data) + if err != nil { + return latest, previous, err + } + + // Compile the regular expression for filtering tags that don't contain a hyphen + re := regexp.MustCompile("^[^-]+$") + + // Create a slice to store the tag names that match the regular expression + var releases []string + + // Iterate over the tags returned by the API, and filter out tags that don't match the regular expression + for _, tag := range data { + name := tag["name"].(string) + if re.MatchString(name) { + releases = append(releases, name) + } + } + + // Set the latest and previous release variables to the first two releases + return releases[0], releases[1], nil +}