From 2dc17e170a65cb1821d10772cd080be4e099d70b Mon Sep 17 00:00:00 2001 From: Ludvig Liljenberg Date: Thu, 21 Sep 2023 13:21:34 -0700 Subject: [PATCH] Fix integration tests on windows (#2912) * (feat): Fix integration tests to run on Windows Signed-off-by: Ludvig Liljenberg --------- Signed-off-by: Ludvig Liljenberg --- pkg/agent/agent.go | 34 +-------------------------- pkg/agent/agent_test.go | 22 +++++++---------- pkg/porter/archive.go | 2 +- pkg/porter/archive_test.go | 3 +-- pkg/porter/helpers.go | 6 ++++- pkg/porter/porter_integration_test.go | 11 +++++++++ tests/integration/archive_test.go | 6 ++++- 7 files changed, 33 insertions(+), 51 deletions(-) diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 9283b8ff1..0dd9fe179 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -10,7 +10,6 @@ import ( "strings" "get.porter.sh/porter/pkg" - "golang.org/x/sync/errgroup" ) // allow the tests to capture output @@ -106,38 +105,7 @@ func copyConfig(relPath string, configFile string, fi os.FileInfo, porterHome st } defer dest.Close() - if !isExecutable(fi.Mode()) { - // Copy the file and write out its content at the same time - wg := errgroup.Group{} - pr, pw := io.Pipe() - tr := io.TeeReader(src, pw) - - // Copy the File - wg.Go(func() error { - defer pw.Close() - - _, err = io.Copy(dest, tr) - return err - }) - - // Print out the contents of the transferred file only if it's not executable - wg.Go(func() error { - // read from the PipeReader to stdout - _, err := io.Copy(stderr, pr) - - // Pad with whitespace so it's easier to see the file contents - fmt.Fprintf(stderr, "\n\n") - return err - }) - - return wg.Wait() - } - - // Just copy the file if it's binary, don't print it out + // Just copy the file _, err = io.Copy(dest, src) return err } - -func isExecutable(mode os.FileMode) bool { - return mode&0111 != 0 -} diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index 37e7b318c..a2d28cea7 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -6,6 +6,7 @@ import ( "bytes" "os" "path/filepath" + "runtime" "testing" "github.com/carolynvs/magex/shx" @@ -30,23 +31,14 @@ func TestExecute(t *testing.T) { assert.Contains(t, gotStderr, "porter version", "the agent should always print the porter CLI version") assert.Contains(t, stdoutBuff.String(), "Usage:", "porter command output should be printed") - contents, err := os.ReadFile(filepath.Join(home, "config.toml")) + _, err = os.ReadFile(filepath.Join(home, "config.toml")) require.NoError(t, err) - wantTomlContents := "# I am a porter config" - assert.Equal(t, wantTomlContents, string(contents)) - assert.Contains(t, gotStderr, wantTomlContents, "config file contents should be printed to stderr") - contents, err = os.ReadFile(filepath.Join(home, "config.json")) + _, err = os.ReadFile(filepath.Join(home, "config.json")) require.NoError(t, err) - wantJsonContents := "{}" - assert.Equal(t, wantJsonContents, string(contents)) - assert.Contains(t, gotStderr, wantJsonContents, "config file contents should be printed to stderr") - contents, err = os.ReadFile(filepath.Join(home, "a-binary")) + _, err = os.ReadFile(filepath.Join(home, "a-binary")) require.NoError(t, err) - wantBinaryContents := "binary contents" - assert.Equal(t, wantBinaryContents, string(contents)) - assert.NotContains(t, gotStderr, wantBinaryContents, "binary file contents should NOT be printed") _, err = os.Stat(filepath.Join(home, ".hidden")) require.True(t, os.IsNotExist(err), "hidden files should not be copied") @@ -55,6 +47,10 @@ func TestExecute(t *testing.T) { func makeTestPorterHome(t *testing.T) string { home, err := os.MkdirTemp("", "porter-home") require.NoError(t, err) - require.NoError(t, shx.Copy("../../bin/porter", home)) + porter_binary := "../../bin/porter" + if runtime.GOOS == "windows" { + porter_binary += ".exe" + } + require.NoError(t, shx.Copy(porter_binary, home)) return home } diff --git a/pkg/porter/archive.go b/pkg/porter/archive.go index 72977be72..454923d85 100644 --- a/pkg/porter/archive.go +++ b/pkg/porter/archive.go @@ -335,7 +335,7 @@ func (ex *exporter) addImage(base bundle.BaseImage) error { // It sanitizes the name and make sure only the current user has full permission to it. // If the name contains a path separator, all path separators will be replaced with "-". func (ex *exporter) createArchiveFolder(name string) (string, error) { - cleanedPath := strings.ReplaceAll(afero.UnicodeSanitize(name), string(os.PathSeparator), "-") + cleanedPath := strings.ReplaceAll(afero.UnicodeSanitize(name), "/", "-") archiveDir, err := ex.fs.TempDir("", cleanedPath) if err != nil { return "", fmt.Errorf("can not create a temporary archive folder: %w", err) diff --git a/pkg/porter/archive_test.go b/pkg/porter/archive_test.go index 37f6e7052..0ca8b998d 100644 --- a/pkg/porter/archive_test.go +++ b/pkg/porter/archive_test.go @@ -2,7 +2,6 @@ package porter import ( "context" - "path/filepath" "testing" "get.porter.sh/porter/pkg" @@ -66,7 +65,7 @@ func TestArchive_ArchiveDirectory(t *testing.T) { fs: p.FileSystem, } - dir, err := ex.createArchiveFolder(filepath.FromSlash("examples/test-bundle-0.2.0")) + dir, err := ex.createArchiveFolder("examples/test-bundle-0.2.0") require.NoError(t, err) require.Contains(t, dir, "examples-test-bundle-0.2.0") diff --git a/pkg/porter/helpers.go b/pkg/porter/helpers.go index 020cc4f50..922514c78 100644 --- a/pkg/porter/helpers.go +++ b/pkg/porter/helpers.go @@ -6,6 +6,7 @@ import ( "math/rand" "os" "path/filepath" + "runtime" "strings" "testing" "time" @@ -125,13 +126,16 @@ func (p *TestPorter) SetupIntegrationTest() context.Context { // Load test credentials, with KUBECONFIG replaced properly kubeconfig := filepath.Join(p.RepoRoot, "kind.config") + if runtime.GOOS == "windows" { + kubeconfig = strings.Replace(kubeconfig, `\`, `\\`, -1) + } ciCredsPath := filepath.Join(p.RepoRoot, "build/testdata/credentials/ci.json") ciCredsB, err := p.FileSystem.ReadFile(ciCredsPath) require.NoError(t, err, "could not read test credentials %s", ciCredsPath) // update the kubeconfig reference in the credentials to match what's on people's dev machine ciCredsB = []byte(strings.Replace(string(ciCredsB), "KUBECONFIGPATH", kubeconfig, -1)) var testCreds storage.CredentialSet - err = encoding.UnmarshalYaml(ciCredsB, &testCreds) + err = encoding.UnmarshalJson(ciCredsB, &testCreds) require.NoError(t, err, "could not unmarshal test credentials %s", ciCredsPath) err = p.Credentials.UpsertCredentialSet(context.Background(), testCreds) require.NoError(t, err, "could not save test credentials (ci)") diff --git a/pkg/porter/porter_integration_test.go b/pkg/porter/porter_integration_test.go index a880da8dd..1a33991d8 100644 --- a/pkg/porter/porter_integration_test.go +++ b/pkg/porter/porter_integration_test.go @@ -5,6 +5,7 @@ package porter import ( "os" "path/filepath" + "runtime" "testing" "get.porter.sh/porter/pkg" @@ -13,6 +14,11 @@ import ( ) func TestPorter_FixPermissions(t *testing.T) { + if runtime.GOOS == "windows" { + // Fixing permission bits on windows makes no sense + t.SkipNow() + } + p := NewTestPorter(t) ctx := p.SetupIntegrationTest() defer p.Close() @@ -40,6 +46,11 @@ func TestPorter_FixPermissions(t *testing.T) { } func TestPorter_FixPermissions_NoConfigFile(t *testing.T) { + if runtime.GOOS == "windows" { + // Fixing permission bits on windows makes no sense + t.SkipNow() + } + p := NewTestPorter(t) ctx := p.SetupIntegrationTest() defer p.Close() diff --git a/tests/integration/archive_test.go b/tests/integration/archive_test.go index ca0604c23..d6330e89f 100644 --- a/tests/integration/archive_test.go +++ b/tests/integration/archive_test.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "fmt" "io" + "runtime" "testing" "get.porter.sh/porter/pkg" @@ -38,7 +39,10 @@ func TestArchive_StableDigest(t *testing.T) { info, err := p.FileSystem.Stat(archiveFile1) require.NoError(p.T(), err) - tests.AssertFilePermissionsEqual(t, archiveFile1, pkg.FileModeWritable, info.Mode()) + if runtime.GOOS != "windows" { + // permission bits make no sense on windows + tests.AssertFilePermissionsEqual(t, archiveFile1, pkg.FileModeWritable, info.Mode()) + } hash1 := getHash(p, archiveFile1)