File: find_test.go

package info (click to toggle)
golang-github-appc-cni 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,132 kB
  • sloc: sh: 154; makefile: 7
file content (111 lines) | stat: -rw-r--r-- 3,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package invoke_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/containernetworking/cni/pkg/invoke"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("FindInPath", func() {
	var (
		multiplePaths         []string
		pluginName            string
		plugin2NameWithExt    string
		plugin2NameWithoutExt string
		pluginDir             string
		anotherTempDir        string
	)

	BeforeEach(func() {
		tempDir, err := ioutil.TempDir("", "cni-find")
		Expect(err).NotTo(HaveOccurred())

		plugin, err := ioutil.TempFile(tempDir, "a-cni-plugin")
		Expect(err).NotTo(HaveOccurred())

		plugin2Name := "a-plugin-with-extension" + invoke.ExecutableFileExtensions[0]
		plugin2, err := os.Create(filepath.Join(tempDir, plugin2Name))
		Expect(err).NotTo(HaveOccurred())

		anotherTempDir, err = ioutil.TempDir("", "nothing-here")
		Expect(err).NotTo(HaveOccurred())

		multiplePaths = []string{anotherTempDir, tempDir}
		pluginDir, pluginName = filepath.Split(plugin.Name())
		_, plugin2NameWithExt = filepath.Split(plugin2.Name())
		plugin2NameWithoutExt = strings.Split(plugin2NameWithExt, ".")[0]
	})

	AfterEach(func() {
		os.RemoveAll(pluginDir)
		os.RemoveAll(anotherTempDir)
	})

	Context("when multiple paths are provided", func() {
		It("returns only the path to the plugin", func() {
			pluginPath, err := invoke.FindInPath(pluginName, multiplePaths)
			Expect(err).NotTo(HaveOccurred())
			Expect(pluginPath).To(Equal(filepath.Join(pluginDir, pluginName)))
		})
	})

	Context("when a plugin name without its file name extension is provided", func() {
		It("returns the path to the plugin, including its extension", func() {
			pluginPath, err := invoke.FindInPath(plugin2NameWithoutExt, multiplePaths)
			Expect(err).NotTo(HaveOccurred())
			Expect(pluginPath).To(Equal(filepath.Join(pluginDir, plugin2NameWithExt)))
		})
	})

	Context("when an error occurs", func() {
		Context("when no paths are provided", func() {
			It("returns an error noting no paths were provided", func() {
				_, err := invoke.FindInPath(pluginName, []string{})
				Expect(err).To(MatchError("no paths provided"))
			})
		})

		Context("when no plugin is provided", func() {
			It("returns an error noting the plugin name wasn't found", func() {
				_, err := invoke.FindInPath("", multiplePaths)
				Expect(err).To(MatchError("no plugin name provided"))
			})
		})

		Context("when the plugin cannot be found", func() {
			It("returns an error noting the path", func() {
				pathsWithNothing := []string{anotherTempDir}
				_, err := invoke.FindInPath(pluginName, pathsWithNothing)
				Expect(err).To(MatchError(fmt.Sprintf("failed to find plugin %q in path %s", pluginName, pathsWithNothing)))
			})
		})

		Context("When the plugin contains a directory separator", func() {
			It("returns an error", func() {
				bogusPlugin := ".." + string(os.PathSeparator) + "pluginname"
				_, err := invoke.FindInPath(bogusPlugin, []string{anotherTempDir})
				Expect(err).To(MatchError("invalid plugin name: " + bogusPlugin))
			})
		})
	})
})