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

test: unpin version in bigbang extension test #2459

Merged
merged 2 commits into from
Apr 25, 2024
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
126 changes: 61 additions & 65 deletions src/extensions/bigbang/test/bigbang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"testing"

Expand All @@ -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())
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Loading