| 1 |
// Copyright 2016 CNI authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package invoke_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"strings" |
| 22 |
|
| 23 |
. "github.com/onsi/ginkgo/v2" |
| 24 |
. "github.com/onsi/gomega" |
| 25 |
|
| 26 |
"github.com/containernetworking/cni/pkg/invoke" |
| 27 |
) |
| 28 |
|
| 29 |
var _ = Describe("FindInPath", func() { |
| 30 |
var ( |
| 31 |
multiplePaths []string |
| 32 |
pluginName string |
| 33 |
plugin2NameWithExt string |
| 34 |
plugin2NameWithoutExt string |
| 35 |
pluginDir string |
| 36 |
anotherTempDir string |
| 37 |
) |
| 38 |
|
| 39 |
BeforeEach(func() { |
| 40 |
tempDir, err := os.MkdirTemp("", "cni-find") |
| 41 |
Expect(err).NotTo(HaveOccurred()) |
| 42 |
|
| 43 |
plugin, err := os.CreateTemp(tempDir, "a-cni-plugin") |
| 44 |
Expect(err).NotTo(HaveOccurred()) |
| 45 |
|
| 46 |
plugin2Name := "a-plugin-with-extension" + invoke.ExecutableFileExtensions[0] |
| 47 |
plugin2, err := os.Create(filepath.Join(tempDir, plugin2Name)) |
| 48 |
Expect(err).NotTo(HaveOccurred()) |
| 49 |
|
| 50 |
anotherTempDir, err = os.MkdirTemp("", "nothing-here") |
| 51 |
Expect(err).NotTo(HaveOccurred()) |
| 52 |
|
| 53 |
multiplePaths = []string{anotherTempDir, tempDir} |
| 54 |
pluginDir, pluginName = filepath.Split(plugin.Name()) |
| 55 |
_, plugin2NameWithExt = filepath.Split(plugin2.Name()) |
| 56 |
plugin2NameWithoutExt = strings.Split(plugin2NameWithExt, ".")[0] |
| 57 |
}) |
| 58 |
|
| 59 |
AfterEach(func() { |
| 60 |
os.RemoveAll(pluginDir) |
| 61 |
os.RemoveAll(anotherTempDir) |
| 62 |
}) |
| 63 |
|
| 64 |
Context("when multiple paths are provided", func() { |
| 65 |
It("returns only the path to the plugin", func() { |
| 66 |
pluginPath, err := invoke.FindInPath(pluginName, multiplePaths) |
| 67 |
Expect(err).NotTo(HaveOccurred()) |
| 68 |
Expect(pluginPath).To(Equal(filepath.Join(pluginDir, pluginName))) |
| 69 |
}) |
| 70 |
}) |
| 71 |
|
| 72 |
Context("when a plugin name without its file name extension is provided", func() { |
| 73 |
It("returns the path to the plugin, including its extension", func() { |
| 74 |
pluginPath, err := invoke.FindInPath(plugin2NameWithoutExt, multiplePaths) |
| 75 |
Expect(err).NotTo(HaveOccurred()) |
| 76 |
Expect(pluginPath).To(Equal(filepath.Join(pluginDir, plugin2NameWithExt))) |
| 77 |
}) |
| 78 |
}) |
| 79 |
|
| 80 |
Context("when an error occurs", func() { |
| 81 |
Context("when no paths are provided", func() { |
| 82 |
It("returns an error noting no paths were provided", func() { |
| 83 |
_, err := invoke.FindInPath(pluginName, []string{}) |
| 84 |
Expect(err).To(MatchError("no paths provided")) |
| 85 |
}) |
| 86 |
}) |
| 87 |
|
| 88 |
Context("when no plugin is provided", func() { |
| 89 |
It("returns an error noting the plugin name wasn't found", func() { |
| 90 |
_, err := invoke.FindInPath("", multiplePaths) |
| 91 |
Expect(err).To(MatchError("no plugin name provided")) |
| 92 |
}) |
| 93 |
}) |
| 94 |
|
| 95 |
Context("when the plugin cannot be found", func() { |
| 96 |
It("returns an error noting the path", func() { |
| 97 |
pathsWithNothing := []string{anotherTempDir} |
| 98 |
_, err := invoke.FindInPath(pluginName, pathsWithNothing) |
| 99 |
Expect(err).To(MatchError(fmt.Sprintf("failed to find plugin %q in path %s", pluginName, pathsWithNothing))) |
| 100 |
}) |
| 101 |
}) |
| 102 |
|
| 103 |
Context("When the plugin contains a directory separator", func() { |
| 104 |
It("returns an error", func() { |
| 105 |
bogusPlugin := ".." + string(os.PathSeparator) + "pluginname" |
| 106 |
_, err := invoke.FindInPath(bogusPlugin, []string{anotherTempDir}) |
| 107 |
Expect(err).To(MatchError("invalid plugin name: " + bogusPlugin)) |
| 108 |
}) |
| 109 |
}) |
| 110 |
}) |
| 111 |
}) |