get.porter.sh/porter@v1.3.0/pkg/storage/migrations/testhelpers/helpers.go (about)

     1  package testhelpers
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/config"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/uwu-tools/magex/pkg/downloads"
    13  	"github.com/uwu-tools/magex/shx"
    14  	"github.com/uwu-tools/magex/xplat"
    15  )
    16  
    17  // CreateLegacyPorterHome creates a porter v0.38 PORTER_HOME with legacy data
    18  func CreateLegacyPorterHome(t *testing.T) *config.TestConfig {
    19  	tmp, err := os.MkdirTemp("", "porter")
    20  	require.NoError(t, err)
    21  
    22  	c := config.NewTestConfig(t)
    23  	c.DataLoader = config.LoadFromFilesystem()
    24  	c.TestContext.UseFilesystem()
    25  	c.SetHomeDir(tmp)
    26  	c.TestContext.AddCleanupDir(tmp) // Remove the temp home directory when the context is closed
    27  
    28  	// Copy old data into PORTER_HOME
    29  	c.TestContext.AddTestDirectoryFromRoot("tests/testdata/porter_home/v0", tmp)
    30  
    31  	// Download a v0.38 release into our old porter home
    32  	// Cache it in bin/v0 so that we don't re-download it unnecessarily
    33  	oldPorterDir := filepath.Join(c.TestContext.FindRepoRoot(), "bin", "v0")
    34  	oldPorterPath := filepath.Join(oldPorterDir, "porter"+xplat.FileExt())
    35  	if _, err = os.Stat(oldPorterPath); os.IsNotExist(err) {
    36  		require.NoError(t, os.MkdirAll(oldPorterDir, 0700))
    37  		opts := downloads.DownloadOptions{
    38  			UrlTemplate: "https://github.com/getporter/porter/releases/download/{{.VERSION}}/porter-{{.GOOS}}-amd64{{.EXT}}",
    39  			Name:        "porter",
    40  			Version:     "v0.38.10",
    41  		}
    42  		if runtime.GOOS == "windows" {
    43  			opts.Ext = ".exe"
    44  		}
    45  		err = downloads.Download(oldPorterDir, opts)
    46  		require.NoError(t, err, "Failed to download a copy of the old version of porter")
    47  	}
    48  
    49  	// Put the old version of porter into PORTER_HOME
    50  	err = shx.Copy(filepath.Join(oldPorterDir, "porter"+xplat.FileExt()), tmp)
    51  	require.NoError(t, err, "Failed to copy the old version of porter into the temp PORTER_HOME")
    52  
    53  	// fixup permissions on the home directory to make the filesystem plugin happy
    54  	// I'm calling porter so that I don't reimplement this functionality
    55  	require.NoError(t, shx.Command(oldPorterPath, "storage", "fix-permissions").
    56  		Env("PORTER_HOME="+tmp).Must().RunS())
    57  
    58  	_, err = c.Load(context.Background(), nil)
    59  	require.NoError(t, err, "Failed to load the test context from the temp PORTER_HOME")
    60  	return c
    61  }