Skip to content

Commit

Permalink
test: refactor 'artifacts_test.go'
Browse files Browse the repository at this point in the history
Signed-off-by: zyy17 <[email protected]>
  • Loading branch information
zyy17 committed Aug 7, 2023
1 parent c46f234 commit b85f25c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
35 changes: 21 additions & 14 deletions pkg/deployer/baremetal/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,10 @@ func (am *ArtifactManager) PrepareArtifact(ctx context.Context, typ ArtifactType
return nil
}

version := artifact.Version

// Get the latest greptime released version.
if typ == GreptimeArtifactType && artifact.Version == "latest" {
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(ctx, GreptimeGitHubOrg, GreptimeDBGithubRepo)
if err != nil {
return err
}
version = *release.TagName
}

var (
pkgDir = path.Join(am.dir, typ.String(), version, "pkg")
binDir = path.Join(am.dir, typ.String(), version, "bin")
version = artifact.Version
pkgDir = path.Join(am.dir, typ.String(), version, "pkg")
binDir = path.Join(am.dir, typ.String(), version, "bin")
)

artifactFile, err := am.download(ctx, typ, version, pkgDir)
Expand Down Expand Up @@ -290,6 +279,15 @@ func (am *ArtifactManager) getGreptimeLatestVersion() (string, error) {
}

func (am *ArtifactManager) greptimeDownloadURL(version string) (string, error) {
if version == "latest" {
// Get the latest greptime released version.
latestVersion, err := am.getGreptimeLatestVersion()
if err != nil {
return "", err
}
version = latestVersion
}

newVersion, err := am.isBreakingVersion(version)
if err != nil {
return "", err
Expand Down Expand Up @@ -326,6 +324,15 @@ func (am *ArtifactManager) etcdDownloadURL(version string) (string, error) {
}

func (am *ArtifactManager) isBreakingVersion(version string) (bool, error) {
if version == "latest" {
// Get the latest greptime released version.
latestVersion, err := am.getGreptimeLatestVersion()
if err != nil {
return false, err
}
version = latestVersion
}

newVersion, err := semverutils.Compare(version, BreakingChangeVersion)
if err != nil {
return false, err
Expand Down
39 changes: 33 additions & 6 deletions pkg/deployer/baremetal/artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,48 @@ package baremetal

import (
"context"
"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
"os"
"sigs.k8s.io/kind/pkg/log"
"testing"

"sigs.k8s.io/kind/pkg/log"

"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)

const (
testDir = "/tmp/gtctl-test-am"
)

func TestArtifactManager(t *testing.T) {
am, err := NewArtifactManager("/tmp/gtctl-test-am", logger.New(os.Stdout, log.Level(4), logger.WithColored()), false)
am, err := NewArtifactManager(testDir, logger.New(os.Stdout, log.Level(4), logger.WithColored()), false)
if err != nil {
t.Errorf("failed to create artifact manager: %v", err)
}

// Cleanup test directory.
defer func() {
os.RemoveAll(testDir)
}()

testConfigs := []*config.Artifact{
{
Version: "latest",
},
{
Version: BreakingChangeVersion,
},
}

ctx := context.Background()
if err := am.PrepareArtifact(ctx, GreptimeArtifactType, &config.Artifact{Version: "latest"}); err != nil {
t.Errorf("failed to prepare latest greptime artifact: %v", err)
for _, tc := range testConfigs {
if err := am.PrepareArtifact(ctx, GreptimeArtifactType, tc); err != nil {
t.Errorf("failed to prepare artifact: %v", err)
}

_, err := am.BinaryPath(GreptimeArtifactType, tc)
if err != nil {
t.Errorf("failed to get binary path: %v", err)
}
}
}

0 comments on commit b85f25c

Please sign in to comment.