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

releases: support enterprise versions #148

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 0 additions & 20 deletions internal/releasesjson/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,13 @@ func (r *Releases) ListProductVersions(ctx context.Context, productName string)
continue
}

if ok, _ := versionIsSupported(v); !ok {
// Remove (currently unsupported) enterprise
// version and any other "custom" build
delete(p.Versions, rawVersion)
continue
}

p.Versions[rawVersion].Version = v
}

return p.Versions, nil
}

func (r *Releases) GetProductVersion(ctx context.Context, product string, version *version.Version) (*ProductVersion, error) {
if ok, err := versionIsSupported(version); !ok {
return nil, fmt.Errorf("%s: %w", product, err)
}

client := httpclient.NewHTTPClient()

indexURL := fmt.Sprintf("%s/%s/%s/index.json",
Expand Down Expand Up @@ -178,12 +167,3 @@ func (r *Releases) GetProductVersion(ctx context.Context, product string, versio

return pv, nil
}

func versionIsSupported(v *version.Version) (bool, error) {
isSupported := v.Metadata() == ""
if !isSupported {
return false, fmt.Errorf("cannot obtain %s (enterprise versions are not supported)",
v.String())
}
return true, nil
}
18 changes: 11 additions & 7 deletions internal/releasesjson/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/hc-install/internal/testutil"
)

func TestListProductVersions_excludesEnterpriseBuilds(t *testing.T) {
func TestListProductVersions_includesEnterpriseBuilds(t *testing.T) {
testutil.EndToEndTest(t)

r := NewReleases()
Expand All @@ -25,12 +25,12 @@ func TestListProductVersions_excludesEnterpriseBuilds(t *testing.T) {

testEntVersion := "1.9.8+ent"
_, ok := pVersions[testEntVersion]
if ok {
t.Fatalf("Found unexpected Consul Enterprise version %q", testEntVersion)
if !ok {
t.Fatalf("Failed to find expected Consul Enterprise version %q", testEntVersion)
}
}

func TestGetProductVersion_excludesEnterpriseBuild(t *testing.T) {
func TestGetProductVersion_includesEnterpriseBuild(t *testing.T) {
testutil.EndToEndTest(t)

r := NewReleases()
Expand All @@ -40,9 +40,13 @@ func TestGetProductVersion_excludesEnterpriseBuild(t *testing.T) {

testEntVersion := version.Must(version.NewVersion("1.9.8+ent"))

_, err := r.GetProductVersion(ctx, "consul", testEntVersion)
if err == nil {
t.Fatalf("Expected enterprise version %q to error out",
version, err := r.GetProductVersion(ctx, "consul", testEntVersion)
if err != nil {
t.Fatalf("Unexpected error getting enterprise version %q",
testEntVersion.String())
}

if version.RawVersion != testEntVersion.Original() {
t.Fatalf("Expected version %q, got %q", testEntVersion.String(), version.Version.String())
}
}
18 changes: 18 additions & 0 deletions releases/latest_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type LatestVersion struct {
InstallDir string
Timeout time.Duration
IncludePrereleases bool
Enterprise bool // Require "+ent" suffix?
EnterpriseMeta string // "hsm", "fips1402", "hsm.fips1402", etc.

SkipChecksumVerification bool

Expand Down Expand Up @@ -156,13 +158,18 @@ func (lv *LatestVersion) Remove(ctx context.Context) error {
}

func (lv *LatestVersion) findLatestMatchingVersion(pvs rjson.ProductVersionsMap, vc version.Constraints) (*rjson.ProductVersion, bool) {
requiredMetadata := lv.requiredMetadata()
versions := make(version.Collection, 0)
for _, pv := range pvs.AsSlice() {
if !lv.IncludePrereleases && pv.Version.Prerelease() != "" {
// skip prereleases if desired
continue
}

if pv.Version.Metadata() != requiredMetadata {
continue
}

versions = append(versions, pv.Version)
}

Expand All @@ -175,3 +182,14 @@ func (lv *LatestVersion) findLatestMatchingVersion(pvs rjson.ProductVersionsMap,

return pvs[latestVersion.Original()], true
}

func (lv *LatestVersion) requiredMetadata() string {
metadata := ""
if lv.Enterprise {
metadata += "ent"
}
if lv.EnterpriseMeta != "" {
metadata += "." + lv.EnterpriseMeta
}
return metadata
}
62 changes: 62 additions & 0 deletions releases/latest_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-version"
rjson "github.com/hashicorp/hc-install/internal/releasesjson"
"github.com/hashicorp/hc-install/product"
)

Expand Down Expand Up @@ -64,3 +66,63 @@ func TestLatestVersionValidate(t *testing.T) {
})
}
}

func TestLatestVersion_FindLatestMatchingVersion(t *testing.T) {
t.Parallel()

possibleVersions := rjson.ProductVersionsMap{
"1.14.0": &rjson.ProductVersion{
Version: version.Must(version.NewVersion("1.14.0")),
},
"1.14.1": &rjson.ProductVersion{
Version: version.Must(version.NewVersion("1.14.1")),
},
"1.14.1+ent": &rjson.ProductVersion{
Version: version.Must(version.NewVersion("1.14.1+ent")),
},
"1.14.1+ent.fips1402": &rjson.ProductVersion{
Version: version.Must(version.NewVersion("1.14.1+ent.fips1402")),
},
}

testCases := map[string]struct {
lv LatestVersion
expectedVersion string
}{
"oss": {
lv: LatestVersion{
Product: product.Vault,
},
expectedVersion: "1.14.1",
},
"enterprise": {
lv: LatestVersion{
Product: product.Vault,
Enterprise: true,
},
expectedVersion: "1.14.1+ent",
},
"enterprise-fips1402": {
lv: LatestVersion{
Product: product.Vault,
Enterprise: true,
EnterpriseMeta: "fips1402",
},
expectedVersion: "1.14.1+ent.fips1402",
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

latest, _ := testCase.lv.findLatestMatchingVersion(possibleVersions, testCase.lv.Constraints)

if latest.Version.Original() != testCase.expectedVersion {
t.Fatalf("expected version %s, got %s", testCase.expectedVersion, latest.Version.Original())
}
})
}
}