Skip to content

Commit

Permalink
using semver to parse version numbers for Big Bang
Browse files Browse the repository at this point in the history
  • Loading branch information
dgershman committed Apr 5, 2023
1 parent 6d6dcb0 commit 08e4866
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ test-upgrade: ## Run the Zarf CLI E2E tests for an external registry and cluster
cd src/test/upgrade-test && go test -failfast -v -timeout 30m

.PHONY: test-unit
test-unit: ensure-ui-build-dir ## Run unit tests within the src/pkg directory
test-unit: ensure-ui-build-dir ## Run unit tests within the src/pkg and the bigbang extension directory
cd src/pkg && go test ./... -failfast -v -timeout 30m
cd src/extensions/bigbang && go test ./. -failfast -v timeout 30m

.PHONY: test-built-ui
test-built-ui: ## Run the Zarf UI E2E tests (requires `make build-ui` first)
Expand Down
34 changes: 18 additions & 16 deletions src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ package bigbang
import (
"fmt"
"path"
"strconv"
"strings"
"time"

"github.com/blang/semver/v4"
"github.com/defenseunicorns/zarf/src/internal/packager/helm"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
Expand All @@ -24,8 +23,9 @@ import (

// Default location for pulling Big Bang.
const (
bb = "bigbang"
bbRepo = "https://repo1.dso.mil/big-bang/bigbang.git"
bb = "bigbang"
bbRepo = "https://repo1.dso.mil/big-bang/bigbang.git"
bbMinRequiredVersion = "1.54.0"
)

var tenMins = metav1.Duration{
Expand All @@ -43,8 +43,14 @@ func Run(tmpPaths types.ComponentPaths, c types.ZarfComponent) (types.ZarfCompon
cfg := c.Extensions.BigBang
manifests := []types.ZarfManifest{}

err, validVersionResponse := isValidVersion(cfg.Version)

if err != nil {
return c, fmt.Errorf("invalid Big Bang version: %s, parsing issue %s", cfg.Version, err)
}

// Make sure the version is valid.
if !isValidVersion(cfg.Version) {
if !validVersionResponse {
return c, fmt.Errorf("invalid Big Bang version: %s, must be at least 1.54.0", cfg.Version)
}

Expand Down Expand Up @@ -227,20 +233,16 @@ func Run(tmpPaths types.ComponentPaths, c types.ZarfComponent) (types.ZarfCompon
}

// isValidVersion check if the version is 1.54.0 or greater.
func isValidVersion(version string) bool {
// Split the version string into its major, minor, and patch components
parts := strings.Split(version, ".")
if len(parts) != 3 {
return false
}
func isValidVersion(version string) (error, bool) {
s, err := semver.Make(version)
minRequired, _ := semver.Make(bbMinRequiredVersion)

// Parse the major and minor components as integers.
// Ignore errors because we are checking the values later.
major, _ := strconv.Atoi(parts[0])
minor, _ := strconv.Atoi(parts[1])
if err != nil {
return err, false
}

// This extension requires BB 1.54.0 or greater.
return major == 1 && minor >= 54 || major > 2
return nil, s.GTE(minRequired)
}

// findBBResources takes a list of yaml objects (as a string) and
Expand Down
31 changes: 31 additions & 0 deletions src/extensions/bigbang/bigbang_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bigbang

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRequiredBigBangVersions(t *testing.T) {
// Support 1.54.0 and beyond
err, vv := isValidVersion("1.54.0")
assert.Equal(t, err, nil)
assert.Equal(t, vv, true)

// Do not support earlier than 1.54.0
err, vv = isValidVersion("1.53.0")
assert.Equal(t, err, nil)
assert.Equal(t, vv, false)

// Support for Big Bang release candidates
err, vv = isValidVersion("1.57.0-rc.0")
assert.Equal(t, err, nil)
assert.Equal(t, vv, true)

// Fail on non-semantic versions
err, vv = isValidVersion("1.57")
Expected := "No Major.Minor.Patch elements found"
if err.Error() != Expected {
t.Errorf("Error actual = %v, and Expected = %v.", err, Expected)
}
assert.Equal(t, vv, false)
}

0 comments on commit 08e4866

Please sign in to comment.