From 36a22467d996ab6ef2b90bcdffdd7137df82cf8a Mon Sep 17 00:00:00 2001 From: Gabriel Rosenhouse Date: Sun, 22 Oct 2017 23:21:08 -0700 Subject: [PATCH] Windows compatibility: all tests pass! --- libcni/api_test.go | 2 +- libcni/backwards_compatibility_test.go | 4 +++ libcni/libcni_suite_test.go | 26 ++++++++----------- pkg/invoke/get_version_integration_test.go | 4 +++ pkg/version/legacy_examples/examples.go | 4 +++ .../legacy_examples/legacy_examples_test.go | 7 ++++- pkg/version/testhelpers/testhelpers_test.go | 4 +++ plugins/test/noop/noop_test.go | 2 +- 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/libcni/api_test.go b/libcni/api_test.go index 0616bf4..711ffc1 100644 --- a/libcni/api_test.go +++ b/libcni/api_test.go @@ -72,7 +72,7 @@ func newPluginInfo(configValue, prevResult string, injectDebugFilePath bool, res config += fmt.Sprintf(`, "prevResult": %s`, prevResult) } if injectDebugFilePath { - config += fmt.Sprintf(`, "debugFile": "%s"`, debugFilePath) + config += fmt.Sprintf(`, "debugFile": %q`, debugFilePath) } if len(capabilities) > 0 { config += `, "capabilities": {` diff --git a/libcni/backwards_compatibility_test.go b/libcni/backwards_compatibility_test.go index a1fcce5..4f4f65f 100644 --- a/libcni/backwards_compatibility_test.go +++ b/libcni/backwards_compatibility_test.go @@ -19,6 +19,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "github.com/containernetworking/cni/libcni" @@ -55,6 +56,9 @@ var _ = Describe("Backwards compatibility", func() { }) It("correctly handles the request from a runtime with an older libcni", func() { + if runtime.GOOS == "windows" { + Skip("cannot build old runtime on windows") + } example := legacy_examples.V010_Runtime binPath, err := example.Build() diff --git a/libcni/libcni_suite_test.go b/libcni/libcni_suite_test.go index 22ab4d7..057567f 100644 --- a/libcni/libcni_suite_test.go +++ b/libcni/libcni_suite_test.go @@ -15,9 +15,8 @@ package libcni_test import ( - "fmt" + "encoding/json" "path/filepath" - "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -31,7 +30,7 @@ func TestLibcni(t *testing.T) { RunSpecs(t, "Libcni Suite") } -var plugins = map[string]string{ +var pluginPackages = map[string]string{ "noop": "github.com/containernetworking/cni/plugins/test/noop", } @@ -39,24 +38,21 @@ var pluginPaths map[string]string var pluginDirs []string // array of plugin dirs var _ = SynchronizedBeforeSuite(func() []byte { - dirs := make([]string, 0, len(plugins)) - for name, packagePath := range plugins { + paths := map[string]string{} + for name, packagePath := range pluginPackages { execPath, err := gexec.Build(packagePath) Expect(err).NotTo(HaveOccurred()) - dirs = append(dirs, fmt.Sprintf("%s=%s", name, execPath)) + paths[name] = execPath } + crossNodeData, err := json.Marshal(paths) + Expect(err).NotTo(HaveOccurred()) - return []byte(strings.Join(dirs, ":")) + return crossNodeData }, func(crossNodeData []byte) { - pluginPaths = make(map[string]string) - for _, str := range strings.Split(string(crossNodeData), ":") { - kvs := strings.SplitN(str, "=", 2) - if len(kvs) != 2 { - Fail("Invalid inter-node data...") - } - pluginPaths[kvs[0]] = kvs[1] - pluginDirs = append(pluginDirs, filepath.Dir(kvs[1])) + Expect(json.Unmarshal(crossNodeData, &pluginPaths)).To(Succeed()) + for _, pluginPath := range pluginPaths { + pluginDirs = append(pluginDirs, filepath.Dir(pluginPath)) } }) diff --git a/pkg/invoke/get_version_integration_test.go b/pkg/invoke/get_version_integration_test.go index 7e58a9b..4792829 100644 --- a/pkg/invoke/get_version_integration_test.go +++ b/pkg/invoke/get_version_integration_test.go @@ -18,6 +18,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "github.com/containernetworking/cni/pkg/invoke" "github.com/containernetworking/cni/pkg/version" @@ -38,6 +39,9 @@ var _ = Describe("GetVersion, integration tests", func() { pluginDir, err := ioutil.TempDir("", "plugins") Expect(err).NotTo(HaveOccurred()) pluginPath = filepath.Join(pluginDir, "test-plugin") + if runtime.GOOS == "windows" { + pluginPath += ".exe" + } }) AfterEach(func() { diff --git a/pkg/version/legacy_examples/examples.go b/pkg/version/legacy_examples/examples.go index 1bf406b..787d83d 100644 --- a/pkg/version/legacy_examples/examples.go +++ b/pkg/version/legacy_examples/examples.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "net" "path/filepath" + "runtime" "sync" "github.com/containernetworking/cni/pkg/types" @@ -61,6 +62,9 @@ func (e Example) Build() (string, error) { } outBinPath := filepath.Join(buildDir, e.Name) + if runtime.GOOS == "windows" { + outBinPath += ".exe" + } if err := testhelpers.BuildAt([]byte(e.PluginSource), e.CNIRepoGitRef, outBinPath); err != nil { return "", err diff --git a/pkg/version/legacy_examples/legacy_examples_test.go b/pkg/version/legacy_examples/legacy_examples_test.go index 4115105..3490eda 100644 --- a/pkg/version/legacy_examples/legacy_examples_test.go +++ b/pkg/version/legacy_examples/legacy_examples_test.go @@ -17,6 +17,7 @@ package legacy_examples_test import ( "os" "path/filepath" + "runtime" "github.com/containernetworking/cni/pkg/version/legacy_examples" . "github.com/onsi/ginkgo" @@ -29,7 +30,11 @@ var _ = Describe("The v0.1.0 Example", func() { pluginPath, err := example.Build() Expect(err).NotTo(HaveOccurred()) - Expect(filepath.Base(pluginPath)).To(Equal(example.Name)) + expectedBaseName := example.Name + if runtime.GOOS == "windows" { + expectedBaseName += ".exe" + } + Expect(filepath.Base(pluginPath)).To(Equal(expectedBaseName)) Expect(os.RemoveAll(pluginPath)).To(Succeed()) }) diff --git a/pkg/version/testhelpers/testhelpers_test.go b/pkg/version/testhelpers/testhelpers_test.go index 3473cd5..4fe070f 100644 --- a/pkg/version/testhelpers/testhelpers_test.go +++ b/pkg/version/testhelpers/testhelpers_test.go @@ -18,6 +18,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "github.com/containernetworking/cni/pkg/version/testhelpers" . "github.com/onsi/ginkgo" @@ -46,6 +47,9 @@ func main() { skel.PluginMain(c, c) } outputDir, err = ioutil.TempDir("", "bin") Expect(err).NotTo(HaveOccurred()) outputFilePath = filepath.Join(outputDir, "some-binary") + if runtime.GOOS == "windows" { + outputFilePath += ".exe" + } }) AfterEach(func() { diff --git a/plugins/test/noop/noop_test.go b/plugins/test/noop/noop_test.go index bf92dab..4658fc0 100644 --- a/plugins/test/noop/noop_test.go +++ b/plugins/test/noop/noop_test.go @@ -140,7 +140,7 @@ var _ = Describe("No-op plugin", func() { // Remove the DEBUG option from CNI_ARGS and regular args newArgs := "FOO=BAR" cmd.Env[len(cmd.Env)-1] = "CNI_ARGS=" + newArgs - newStdin := fmt.Sprintf(`{"some":"stdin-json", "cniVersion": "0.3.1", "debugFile": "%s"}`, debugFileName) + newStdin := fmt.Sprintf(`{"some":"stdin-json", "cniVersion": "0.3.1", "debugFile": %q}`, debugFileName) cmd.Stdin = strings.NewReader(newStdin) expectedCmdArgs.Args = newArgs expectedCmdArgs.StdinData = []byte(newStdin) -- 2.44.0