Skip to content

Commit

Permalink
Fix integration tests on windows (#2912)
Browse files Browse the repository at this point in the history
* (feat): Fix integration tests to run on Windows 
Signed-off-by: Ludvig Liljenberg <ludfjig@hotmail.com>

---------

Signed-off-by: Ludvig Liljenberg <ludfjig@hotmail.com>
  • Loading branch information
ludfjig committed Sep 21, 2023
1 parent 4adf451 commit 2dc17e1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 51 deletions.
34 changes: 1 addition & 33 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"get.porter.sh/porter/pkg"
"golang.org/x/sync/errgroup"
)

// allow the tests to capture output
Expand Down Expand Up @@ -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
}
22 changes: 9 additions & 13 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/carolynvs/magex/shx"
Expand All @@ -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")
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/porter/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/porter/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package porter

import (
"context"
"path/filepath"
"testing"

"get.porter.sh/porter/pkg"
Expand Down Expand Up @@ -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")

Expand Down
6 changes: 5 additions & 1 deletion pkg/porter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)")
Expand Down
11 changes: 11 additions & 0 deletions pkg/porter/porter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package porter
import (
"os"
"path/filepath"
"runtime"
"testing"

"get.porter.sh/porter/pkg"
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"runtime"
"testing"

"get.porter.sh/porter/pkg"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2dc17e1

Please sign in to comment.