Skip to content

Commit

Permalink
Merge pull request #20 from carolynvs/fix-porter-runtime-check
Browse files Browse the repository at this point in the history
Only download porter runtime when porter changes
  • Loading branch information
carolynvs authored Jan 12, 2023
2 parents 5efe467 + b5a64ac commit 44f56b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
13 changes: 10 additions & 3 deletions porter/porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ func EnsurePorterAt(version string) {
runtimesDir := filepath.Join(home, "runtimes")
os.MkdirAll(runtimesDir, 0770)

var forceDownloadRuntime bool

clientPath := filepath.Join(home, "porter"+xplat.FileExt())
if clientFound, _ := pkg.IsCommandAvailable(clientPath, "--version", version); !clientFound {
log.Println("Porter client not found at", clientPath)
// When we download a new client, always download a new runtime
forceDownloadRuntime = true

log.Printf("Porter %s not found at %s\n", version, clientPath)
log.Println("Installing porter into", home)
opts := downloads.DownloadOptions{
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}",
Expand All @@ -50,9 +55,11 @@ func EnsurePorterAt(version string) {
mgx.Must(downloads.Download(home, opts))
}

// Only check if the runtime file _exists_ but don't try to check the version
// The binary isn't runnable in all cases because it will always be a linux binary, and the client could be windows or mac
// So we can't really check the version here...
runtimePath := filepath.Join(home, "runtimes", "porter-runtime")
if runtimeFound, _ := pkg.IsCommandAvailable(runtimePath, "--version", version); !runtimeFound {
log.Println("Porter runtime not found at", runtimePath)
if _, err := os.Stat(runtimePath); forceDownloadRuntime || os.IsNotExist(err) {
opts := downloads.DownloadOptions{
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-linux-amd64",
Name: "porter-runtime",
Expand Down
37 changes: 34 additions & 3 deletions porter/porter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func TestEnsurePorterAt(t *testing.T) {

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
tmp, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

UsePorterHome(tmp)
EnsurePorterAt(tc.wantVersion)
Expand All @@ -55,4 +53,37 @@ func TestEnsurePorterAt(t *testing.T) {
assert.True(t, ok, "could not resolve the desired porter version")
})
}

// when the runtime binary already exists, leave it
t.Run("runtime binary only downloaded when client is stale", func(t *testing.T) {
tmp := t.TempDir()

UsePorterHome(tmp)
EnsurePorter()

porterPath := filepath.Join(tmp, "porter"+xplat.FileExt())
runtimePath := filepath.Join(tmp, "runtimes", "porter-runtime")
origPorterStat, err := os.Stat(porterPath)
require.NoError(t, err, "failed to stat the porter binary")
origRuntimeStat, err := os.Stat(runtimePath)
require.NoError(t, err, "failed to stat the porter-runtime binary")

// Nothing should be downloaded
EnsurePorterAt(DefaultPorterVersion)
newPorterStat, err := os.Stat(porterPath)
require.NoError(t, err, "failed to stat the porter binary")
require.Equal(t, origPorterStat.ModTime(), newPorterStat.ModTime(), "expected the porter binary to not be re-downloaded")
newRuntimeStat, err := os.Stat(runtimePath)
require.NoError(t, err, "failed to stat the porter-runtime binary")
require.Equal(t, origRuntimeStat.ModTime(), newRuntimeStat.ModTime(), "expected the porter-runtime binary to not be re-downloaded")

// Both should be re-downloaded
EnsurePorterAt("v1.0.0-rc.1")
newPorterStat, err = os.Stat(porterPath)
require.NoError(t, err, "failed to stat the porter binary")
require.Less(t, origPorterStat.ModTime(), newPorterStat.ModTime(), "expected the porter binary to be re-downloaded")
newRuntimeStat, err = os.Stat(runtimePath)
require.NoError(t, err, "failed to stat the porter-runtime binary")
require.Less(t, origRuntimeStat.ModTime(), newRuntimeStat.ModTime(), "expected the porter-runtime binary to be re-downloaded")
})
}

0 comments on commit 44f56b6

Please sign in to comment.