Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnitTests fail on windows due to path formatting problems #2855

Merged
merged 16 commits into from
Aug 7, 2023
3 changes: 1 addition & 2 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cache
import (
"fmt"
"os"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -160,7 +159,7 @@ func TestStoreRelocationMapping(t *testing.T) {
relocationMapping: relocation.ImageRelocationMap{
"asd": "asdf",
},
wantedReloPath: path.Join(home+"/.porter/cache", kahn1dot0Hash, "cnab", "relocation-mapping.json"),
wantedReloPath: filepath.Join(home+"/.porter/cache", kahn1dot0Hash, "cnab", "relocation-mapping.json"),
},
"no relocation file gets no path": {
tag: kahnlatest,
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestConfig_GetHomeDir(t *testing.T) {
home, err := c.GetHomeDir()
require.NoError(t, err)

assert.Equal(t, "/home/myuser/.porter", home)
assert.Equal(t, filepath.FromSlash("/home/myuser/.porter"), home)
}

func TestConfig_GetHomeDirFromSymlink(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTestConfig(t *testing.T) *TestConfig {
// SetupUnitTest initializes the unit test filesystem with the supporting files in the PORTER_HOME directory.
func (c *TestConfig) SetupUnitTest() {
// Set up the test porter home directory
home := "/home/myuser/.porter"
home := filepath.FromSlash("/home/myuser/.porter")
c.SetHomeDir(home)

// Fake out the porter home directory
Expand Down
8 changes: 6 additions & 2 deletions pkg/exec/builder/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package builder

import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -159,8 +158,13 @@ func TestExecuteStep_SpecifiesCustomWorkingDirectory(t *testing.T) {
SuffixArguments: []string{},
}

if runtime.GOOS == "windows" {
step.TestStep.Command = "cmd.exe"
step.Arguments = []string{"/c", "cd"}
}

_, err := ExecuteStep(ctx, c.RuntimeConfig, step)
assert.Equal(t, fmt.Sprintln(wd), c.TestContext.GetOutput())
assert.Equal(t, wd, strings.TrimRight(c.TestContext.GetOutput(), "\r\n"))
require.NoError(t, err, "Execute Step failed")
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/mixin/pkgmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func TestRunner_BuildCommand(t *testing.T) {
runnerCommand string
wantCommand string
}{
{"build", "build", "/home/myuser/.porter/mixins/exec/exec build"},
{"install", "install", "/home/myuser/.porter/mixins/exec/exec install"},
{"upgrade", "upgrade", "/home/myuser/.porter/mixins/exec/exec upgrade"},
{"uninstall", "uninstall", "/home/myuser/.porter/mixins/exec/exec uninstall"},
{"invoke", "status", "/home/myuser/.porter/mixins/exec/exec invoke --action status"},
{"version", "version --output json", "/home/myuser/.porter/mixins/exec/exec version --output json"},
{"build", "build", "/home/myuser/.porter/mixins/exec/exec build\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe build"},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can separate expected output by a newline character, so for this test there are two possible expected outputs which both pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh interesting, might be easier to read if it is separated into array for the want command? maybe it isn't possible, not sure how the testing framework works.

{"install", "install", "/home/myuser/.porter/mixins/exec/exec install\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe install"},
{"upgrade", "upgrade", "/home/myuser/.porter/mixins/exec/exec upgrade\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe upgrade"},
{"uninstall", "uninstall", "/home/myuser/.porter/mixins/exec/exec uninstall\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe uninstall"},
{"invoke", "status", "/home/myuser/.porter/mixins/exec/exec invoke --action status\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe invoke --action status"},
{"version", "version --output json", "/home/myuser/.porter/mixins/exec/exec version --output json\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe version --output json"},
}

for _, tc := range testcases {
Expand Down
6 changes: 6 additions & 0 deletions pkg/pkgmgmt/client/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func TestFileSystem_InstallFromUrl(t *testing.T) {
} else {
require.NoError(t, err)
clientPath := "/home/myuser/.porter/packages/mypkg/mypkg"
if runtime.GOOS == "windows" {
clientPath += ".exe"
}
clientStats, err := p.FileSystem.Stat(clientPath)
require.NoError(t, err)
wantMode := pkg.FileModeExecutable
Expand Down Expand Up @@ -116,6 +119,9 @@ func TestFileSystem_InstallFromFeedUrl(t *testing.T) {
require.NoError(t, err)

clientExists, _ := p.FileSystem.Exists("/home/myuser/.porter/packages/helm/helm")
if runtime.GOOS == "windows" {
clientExists, _ = p.FileSystem.Exists("/home/myuser/.porter/packages/helm/helm.exe")
}
assert.True(t, clientExists)
runtimeExists, _ := p.FileSystem.Exists("/home/myuser/.porter/packages/helm/runtimes/helm-runtime")
assert.True(t, runtimeExists)
Expand Down
2 changes: 1 addition & 1 deletion pkg/pkgmgmt/feed/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (feed *MixinFeed) Generate(ctx context.Context, opts GenerateOptions) error
}
}

mixinRegex := regexp.MustCompile(`(.*/)?(.+)/([a-z0-9-]+)-(linux|windows|darwin)-(amd64|arm64)(\.exe)?`)
mixinRegex := regexp.MustCompile(`(.*[/\\])?(.+)[/\\]([a-z0-9-]+)-(linux|windows|darwin)-(amd64|arm64)(\.exe)?`)

err = feed.FileSystem.Walk(opts.SearchDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/pkgmgmt/feed/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestGenerate_RegexMatch(t *testing.T) {
}{{
name: "no bins",
mixinName: "",
wantError: `failed to traverse the bin directory: open /bin: file does not exist`,
wantError: `file does not exist`,
}, {
name: "valid mixin name",
mixinName: "my-42nd-mixin",
Expand Down
2 changes: 1 addition & 1 deletion pkg/porter/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *Porter) Archive(ctx context.Context, opts ArchiveOptions) error {

dir := filepath.Dir(opts.ArchiveFile)
if _, err := p.Config.FileSystem.Stat(dir); os.IsNotExist(err) {
return log.Error(fmt.Errorf("parent directory %q does not exist", dir))
return log.Error(fmt.Errorf("parent directory %q does not exist", filepath.ToSlash(dir)))
}

bundleRef, err := opts.GetBundleReference(ctx, p)
Expand Down
3 changes: 2 additions & 1 deletion pkg/porter/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package porter

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

"get.porter.sh/porter/pkg"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestArchive_ArchiveDirectory(t *testing.T) {
fs: p.FileSystem,
}

dir, err := ex.createArchiveFolder("examples/test-bundle-0.2.0")
dir, err := ex.createArchiveFolder(filepath.FromSlash("examples/test-bundle-0.2.0"))
require.NoError(t, err)
require.Contains(t, dir, "examples-test-bundle-0.2.0")

Expand Down
50 changes: 32 additions & 18 deletions pkg/porter/cnab_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package porter

import (
"fmt"
"path/filepath"
"testing"

Expand All @@ -23,7 +24,7 @@ func TestSharedOptions_defaultBundleFiles(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, "porter.yaml", opts.File)
assert.Equal(t, ".cnab/bundle.json", opts.CNABFile)
assert.Equal(t, filepath.FromSlash(".cnab/bundle.json"), opts.CNABFile)
}

func TestSharedOptions_defaultBundleFiles_AltManifest(t *testing.T) {
Expand All @@ -37,7 +38,7 @@ func TestSharedOptions_defaultBundleFiles_AltManifest(t *testing.T) {
err := opts.defaultBundleFiles(cxt.Context)
require.NoError(t, err)

assert.Equal(t, ".cnab/bundle.json", opts.CNABFile)
assert.Equal(t, filepath.FromSlash(".cnab/bundle.json"), opts.CNABFile)
}

func TestSharedOptions_defaultBundleFiles_CNABFile(t *testing.T) {
Expand Down Expand Up @@ -88,7 +89,9 @@ func TestSharedOptions_validateBundleJson(t *testing.T) {

if tc.wantError == "" {
require.NoError(t, err)
assert.Equal(t, opts.CNABFile, tc.wantBundleJson)
wantBundleJsonAbs, err := filepath.Abs(tc.wantBundleJson)
require.NoError(t, err)
assert.Equal(t, wantBundleJsonAbs, opts.CNABFile)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantError)
Expand All @@ -98,6 +101,10 @@ func TestSharedOptions_validateBundleJson(t *testing.T) {
}

func Test_bundleFileOptions(t *testing.T) {
absWantFile := absOSFilepath(t, "/"+config.Name)
absWantCNABFile := absOSFilepath(t, "/"+build.LOCAL_BUNDLE)
absPathToBundle := absOSFilepath(t, "/path/to/bundle")

testcases := []struct {
name string
opts BundleDefinitionOptions
Expand All @@ -110,8 +117,8 @@ func Test_bundleFileOptions(t *testing.T) {
name: "no opts",
opts: BundleDefinitionOptions{},
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil },
wantFile: "/" + config.Name,
wantCNABFile: "/" + build.LOCAL_BUNDLE,
wantFile: absWantFile,
wantCNABFile: absWantCNABFile,
wantError: "",
}, {
name: "reference set",
Expand All @@ -125,12 +132,12 @@ func Test_bundleFileOptions(t *testing.T) {
}, {
name: "invalid dir",
opts: BundleDefinitionOptions{
Dir: "path/to/bundle",
Dir: filepath.FromSlash("path/to/bundle"),
},
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil },
wantFile: "",
wantCNABFile: "",
wantError: `"path/to/bundle" is not a valid directory: open /path/to/bundle: file does not exist`,
wantError: fmt.Sprintf("%q is not a valid directory: open %s: file does not exist", filepath.FromSlash("path/to/bundle"), absPathToBundle),
}, {
name: "invalid file",
opts: BundleDefinitionOptions{
Expand All @@ -139,11 +146,11 @@ func Test_bundleFileOptions(t *testing.T) {
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil },
wantFile: "",
wantCNABFile: "",
wantError: "unable to access --file /alternate/porter.yaml: open /alternate/porter.yaml: file does not exist",
wantError: fmt.Sprintf("unable to access --file %s: open %s: file does not exist", absOSFilepath(t, "/alternate/porter.yaml"), absOSFilepath(t, "/alternate/porter.yaml")),
}, {
name: "valid dir",
opts: BundleDefinitionOptions{
Dir: "path/to/bundle",
Dir: absOSFilepath(t, "/path/to/bundle"),
},
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error {
err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, config.Name), pkg.FileModeDirectory)
Expand All @@ -152,8 +159,8 @@ func Test_bundleFileOptions(t *testing.T) {
}
return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory)
},
wantFile: "/path/to/bundle/porter.yaml",
wantCNABFile: "/path/to/bundle/.cnab/bundle.json",
wantFile: absOSFilepath(t, "/path/to/bundle/porter.yaml"),
wantCNABFile: absOSFilepath(t, "/path/to/bundle/.cnab/bundle.json"),
wantError: "",
}, {
name: "valid file",
Expand All @@ -163,14 +170,14 @@ func Test_bundleFileOptions(t *testing.T) {
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error {
return ctx.FileSystem.MkdirAll(opts.File, pkg.FileModeDirectory)
},
wantFile: "/alternate/porter.yaml",
wantCNABFile: "/" + build.LOCAL_BUNDLE,
wantFile: absOSFilepath(t, "/alternate/porter.yaml"),
wantCNABFile: absOSFilepath(t, "/"+build.LOCAL_BUNDLE),
wantError: "",
}, {
name: "valid dir and file",
opts: BundleDefinitionOptions{
Dir: "path/to/bundle",
File: "alternate/porter.yaml",
Dir: absOSFilepath(t, "/path/to/bundle"),
File: filepath.FromSlash("alternate/porter.yaml"),
},
setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error {
err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, opts.File), pkg.FileModeDirectory)
Expand All @@ -179,8 +186,8 @@ func Test_bundleFileOptions(t *testing.T) {
}
return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory)
},
wantFile: "/path/to/bundle/alternate/porter.yaml",
wantCNABFile: "/path/to/bundle/.cnab/bundle.json",
wantFile: absOSFilepath(t, "/path/to/bundle/alternate/porter.yaml"),
wantCNABFile: absOSFilepath(t, "/path/to/bundle/.cnab/bundle.json"),
wantError: "",
}}

Expand Down Expand Up @@ -209,9 +216,16 @@ func Test_bundleFileOptions(t *testing.T) {
if tc.opts.Dir != "" && tc.wantError == "" {
require.Equal(t, tc.opts.Dir, wd)
} else {
require.Equal(t, "/", wd)
path := absOSFilepath(t, "/")
require.Equal(t, path, wd)
}
}
})
}
}

func absOSFilepath(t *testing.T, path string) string {
result, err := filepath.Abs(filepath.FromSlash(path))
require.NoError(t, err)
return result
}
12 changes: 11 additions & 1 deletion pkg/porter/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -302,6 +303,12 @@ func TestCredentialsEdit(t *testing.T) {
p.Setenv("EDITOR", "vi")
p.Setenv(test.ExpectedCommandEnv, "bash -c vi "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml"))

if runtime.GOOS == "windows" {
p.Setenv("SHELL", "cmd")
p.Setenv("EDITOR", "notepad")
p.Setenv(test.ExpectedCommandEnv, "cmd /C notepad "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml"))
}

opts := CredentialEditOptions{Namespace: "dev", Name: "kool-kreds"}

p.TestCredentials.AddTestCredentialsDirectory("testdata/test-creds")
Expand All @@ -315,7 +322,10 @@ func TestCredentialsEditEditorPathWithArgument(t *testing.T) {

p.Setenv("SHELL", "something")
p.Setenv("EDITOR", "C:\\Program Files\\Visual Studio Code\\code.exe --wait")
p.Setenv(test.ExpectedCommandEnv, "something -c C:\\Program Files\\Visual Studio Code\\code.exe --wait "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml"))
p.Setenv(test.ExpectedCommandEnv, fmt.Sprintf("something -c C:\\Program Files\\Visual Studio Code\\code.exe --wait %s", filepath.Join(os.TempDir(), "porter-kool-kreds.yaml")))
if runtime.GOOS == "windows" {
p.Setenv(test.ExpectedCommandEnv, fmt.Sprintf("something /C C:\\Program Files\\Visual Studio Code\\code.exe --wait %s", filepath.Join(os.TempDir(), "porter-kool-kreds.yaml")))
}
opts := CredentialEditOptions{Namespace: "dev", Name: "kool-kreds"}

p.TestCredentials.AddTestCredentialsDirectory("testdata/test-creds")
Expand Down
15 changes: 10 additions & 5 deletions pkg/porter/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package porter
import (
"context"
"errors"
"runtime"
"testing"
"time"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/cache"
Expand Down Expand Up @@ -34,12 +36,10 @@ func TestPublish_Validate_PorterYamlDoesNotExist(t *testing.T) {

opts := PublishOptions{}
err := opts.Validate(p.Config)
require.Error(t, err, "validation should have failed")
assert.EqualError(
require.ErrorContains(
t,
err,
"could not find porter.yaml in the current directory /, make sure you are in the right directory or specify the porter manifest with --file",
"porter.yaml not present so should have failed validation",
"could not find porter.yaml in the current directory",
)
}

Expand All @@ -51,7 +51,7 @@ func TestPublish_Validate_ArchivePath(t *testing.T) {
ArchiveFile: "mybuns.tgz",
}
err := opts.Validate(p.Config)
assert.EqualError(t, err, "unable to access --archive mybuns.tgz: open /mybuns.tgz: file does not exist")
assert.ErrorContains(t, err, "file does not exist")

p.FileSystem.WriteFile("mybuns.tgz", []byte("mybuns"), pkg.FileModeWritable)
err = opts.Validate(p.Config)
Expand Down Expand Up @@ -225,6 +225,11 @@ func TestPublish_RefreshCachedBundle(t *testing.T) {
require.NoError(t, err)
origBunPathTime := file.ModTime()

if runtime.GOOS == "windows" {
// see https://github.com/getporter/porter/issues/2858
time.Sleep(5 * time.Millisecond)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-( we've seen this in k8s too

}

// Should refresh cache
err = p.refreshCachedBundle(bundleRef)
require.NoError(t, err, "should have successfully updated the cache")
Expand Down
7 changes: 6 additions & 1 deletion pkg/portercontext/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package portercontext
import (
"context"
"errors"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,5 +54,9 @@ func TestContext_LogToFile(t *testing.T) {
c.CompareGoldenFile("testdata/expected-logs.txt", string(logContents))

// Compare the human readable logs sent to stderr
c.CompareGoldenFile("testdata/expected-output.txt", c.GetError())
if runtime.GOOS == "windows" {
c.CompareGoldenFile("testdata/expected-output-windows.txt", c.GetError())
} else {
c.CompareGoldenFile("testdata/expected-output.txt", c.GetError())
}
}
4 changes: 4 additions & 0 deletions pkg/portercontext/testdata/expected-output-windows.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Writing logs to C:\.porter\logs\0.json
a thing happened
a weird thing happened
a bad thing happened
2 changes: 1 addition & 1 deletion pkg/secrets/plugins/filesystem/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFileSystem_SetSecretDir(t *testing.T) {
s := filesystem.NewStore(c.Config)
secretDir, err := s.SetSecretDir()
require.NoError(t, err)
require.Equal(t, "/home/myuser/.porter/secrets", secretDir)
require.Equal(t, filepath.FromSlash("/home/myuser/.porter/secrets"), secretDir)
}

func TestFileSystem_DataOperation(t *testing.T) {
Expand Down
Loading