| 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 |
"context" |
| 19 |
"encoding/json" |
| 20 |
"errors" |
| 21 |
|
| 22 |
. "github.com/onsi/ginkgo/v2" |
| 23 |
. "github.com/onsi/gomega" |
| 24 |
|
| 25 |
"github.com/containernetworking/cni/pkg/invoke" |
| 26 |
"github.com/containernetworking/cni/pkg/invoke/fakes" |
| 27 |
current "github.com/containernetworking/cni/pkg/types/100" |
| 28 |
"github.com/containernetworking/cni/pkg/version" |
| 29 |
) |
| 30 |
|
| 31 |
var _ = Describe("Executing a plugin, unit tests", func() { |
| 32 |
var ( |
| 33 |
pluginExec invoke.Exec |
| 34 |
rawExec *fakes.RawExec |
| 35 |
versionDecoder *fakes.VersionDecoder |
| 36 |
|
| 37 |
pluginPath string |
| 38 |
netconf []byte |
| 39 |
cniargs *fakes.CNIArgs |
| 40 |
ctx context.Context |
| 41 |
) |
| 42 |
|
| 43 |
BeforeEach(func() { |
| 44 |
rawExec = &fakes.RawExec{} |
| 45 |
rawExec.ExecPluginCall.Returns.ResultBytes = []byte(`{ "cniVersion": "0.3.1", "ips": [ { "version": "4", "address": "1.2.3.4/24" } ] }`) |
| 46 |
|
| 47 |
versionDecoder = &fakes.VersionDecoder{} |
| 48 |
versionDecoder.DecodeCall.Returns.PluginInfo = version.PluginSupports("0.42.0") |
| 49 |
|
| 50 |
pluginExec = &struct { |
| 51 |
*fakes.RawExec |
| 52 |
*fakes.VersionDecoder |
| 53 |
}{ |
| 54 |
RawExec: rawExec, |
| 55 |
VersionDecoder: versionDecoder, |
| 56 |
} |
| 57 |
pluginPath = "/some/plugin/path" |
| 58 |
netconf = []byte(`{ "some": "stdin", "cniVersion": "0.3.1" }`) |
| 59 |
cniargs = &fakes.CNIArgs{} |
| 60 |
cniargs.AsEnvCall.Returns.Env = []string{"SOME=ENV"} |
| 61 |
ctx = context.TODO() |
| 62 |
}) |
| 63 |
|
| 64 |
Describe("returning a result", func() { |
| 65 |
It("unmarshals the result bytes into the Result type", func() { |
| 66 |
r, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 67 |
Expect(err).NotTo(HaveOccurred()) |
| 68 |
|
| 69 |
result, err := current.GetResult(r) |
| 70 |
Expect(err).NotTo(HaveOccurred()) |
| 71 |
Expect(result.IPs).To(HaveLen(1)) |
| 72 |
Expect(result.IPs[0].Address.IP.String()).To(Equal("1.2.3.4")) |
| 73 |
}) |
| 74 |
|
| 75 |
It("passes its arguments through to the rawExec", func() { |
| 76 |
_, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 77 |
Expect(err).NotTo(HaveOccurred()) |
| 78 |
Expect(rawExec.ExecPluginCall.Received.PluginPath).To(Equal(pluginPath)) |
| 79 |
Expect(rawExec.ExecPluginCall.Received.StdinData).To(Equal(netconf)) |
| 80 |
Expect(rawExec.ExecPluginCall.Received.Environ).To(Equal([]string{"SOME=ENV"})) |
| 81 |
}) |
| 82 |
|
| 83 |
Context("when the rawExec fails", func() { |
| 84 |
BeforeEach(func() { |
| 85 |
rawExec.ExecPluginCall.Returns.Error = errors.New("banana") |
| 86 |
}) |
| 87 |
It("returns the error", func() { |
| 88 |
_, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 89 |
Expect(err).To(MatchError("banana")) |
| 90 |
}) |
| 91 |
}) |
| 92 |
|
| 93 |
It("returns an error using the default exec interface", func() { |
| 94 |
// pluginPath should not exist on-disk so we expect an error. |
| 95 |
// This test simply tests that the default exec handler |
| 96 |
// is run when the exec interface is nil. |
| 97 |
_, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, nil) |
| 98 |
Expect(err).To(HaveOccurred()) |
| 99 |
}) |
| 100 |
|
| 101 |
It("assumes config version if result version is missing", func() { |
| 102 |
rawExec.ExecPluginCall.Returns.ResultBytes = []byte(`{ "ips": [ { "version": "4", "address": "1.2.3.4/24" } ] }`) |
| 103 |
r, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 104 |
Expect(err).NotTo(HaveOccurred()) |
| 105 |
Expect(r.Version()).To(Equal("0.3.1")) |
| 106 |
|
| 107 |
result, err := current.GetResult(r) |
| 108 |
Expect(err).NotTo(HaveOccurred()) |
| 109 |
Expect(result.IPs).To(HaveLen(1)) |
| 110 |
Expect(result.IPs[0].Address.IP.String()).To(Equal("1.2.3.4")) |
| 111 |
}) |
| 112 |
|
| 113 |
It("assumes config version if result version is empty", func() { |
| 114 |
rawExec.ExecPluginCall.Returns.ResultBytes = []byte(`{ "cniVersion": "", "ips": [ { "version": "4", "address": "1.2.3.4/24" } ] }`) |
| 115 |
r, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 116 |
Expect(err).NotTo(HaveOccurred()) |
| 117 |
Expect(r.Version()).To(Equal("0.3.1")) |
| 118 |
|
| 119 |
result, err := current.GetResult(r) |
| 120 |
Expect(err).NotTo(HaveOccurred()) |
| 121 |
Expect(result.IPs).To(HaveLen(1)) |
| 122 |
Expect(result.IPs[0].Address.IP.String()).To(Equal("1.2.3.4")) |
| 123 |
}) |
| 124 |
|
| 125 |
It("assumes 0.1.0 if config and result version are empty", func() { |
| 126 |
netconf = []byte(`{ "some": "stdin" }`) |
| 127 |
rawExec.ExecPluginCall.Returns.ResultBytes = []byte(`{ "some": "version-info" }`) |
| 128 |
r, err := invoke.ExecPluginWithResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 129 |
Expect(err).NotTo(HaveOccurred()) |
| 130 |
Expect(r.Version()).To(Equal("0.1.0")) |
| 131 |
}) |
| 132 |
}) |
| 133 |
|
| 134 |
Describe("without returning a result", func() { |
| 135 |
It("passes its arguments through to the rawExec", func() { |
| 136 |
err := invoke.ExecPluginWithoutResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 137 |
Expect(err).NotTo(HaveOccurred()) |
| 138 |
Expect(rawExec.ExecPluginCall.Received.PluginPath).To(Equal(pluginPath)) |
| 139 |
Expect(rawExec.ExecPluginCall.Received.StdinData).To(Equal(netconf)) |
| 140 |
Expect(rawExec.ExecPluginCall.Received.Environ).To(Equal([]string{"SOME=ENV"})) |
| 141 |
}) |
| 142 |
|
| 143 |
Context("when the rawExec fails", func() { |
| 144 |
BeforeEach(func() { |
| 145 |
rawExec.ExecPluginCall.Returns.Error = errors.New("banana") |
| 146 |
}) |
| 147 |
It("returns the error", func() { |
| 148 |
err := invoke.ExecPluginWithoutResult(ctx, pluginPath, netconf, cniargs, pluginExec) |
| 149 |
Expect(err).To(MatchError("banana")) |
| 150 |
}) |
| 151 |
}) |
| 152 |
|
| 153 |
It("returns an error using the default exec interface", func() { |
| 154 |
// pluginPath should not exist on-disk so we expect an error. |
| 155 |
// This test simply tests that the default exec handler |
| 156 |
// is run when the exec interface is nil. |
| 157 |
err := invoke.ExecPluginWithoutResult(ctx, pluginPath, netconf, cniargs, nil) |
| 158 |
Expect(err).To(HaveOccurred()) |
| 159 |
}) |
| 160 |
}) |
| 161 |
|
| 162 |
Describe("discovering the plugin version", func() { |
| 163 |
BeforeEach(func() { |
| 164 |
rawExec.ExecPluginCall.Returns.ResultBytes = []byte(`{ "some": "version-info" }`) |
| 165 |
}) |
| 166 |
|
| 167 |
It("execs the plugin with the command VERSION", func() { |
| 168 |
_, err := invoke.GetVersionInfo(ctx, pluginPath, pluginExec) |
| 169 |
Expect(err).NotTo(HaveOccurred()) |
| 170 |
Expect(rawExec.ExecPluginCall.Received.PluginPath).To(Equal(pluginPath)) |
| 171 |
Expect(rawExec.ExecPluginCall.Received.Environ).To(ContainElement("CNI_COMMAND=VERSION")) |
| 172 |
expectedStdin, _ := json.Marshal(map[string]string{"cniVersion": version.Current()}) |
| 173 |
Expect(rawExec.ExecPluginCall.Received.StdinData).To(MatchJSON(expectedStdin)) |
| 174 |
}) |
| 175 |
|
| 176 |
It("decodes and returns the version info", func() { |
| 177 |
versionInfo, err := invoke.GetVersionInfo(ctx, pluginPath, pluginExec) |
| 178 |
Expect(err).NotTo(HaveOccurred()) |
| 179 |
Expect(versionInfo.SupportedVersions()).To(Equal([]string{"0.42.0"})) |
| 180 |
Expect(versionDecoder.DecodeCall.Received.JSONBytes).To(MatchJSON(`{ "some": "version-info" }`)) |
| 181 |
}) |
| 182 |
|
| 183 |
Context("when the rawExec fails", func() { |
| 184 |
BeforeEach(func() { |
| 185 |
rawExec.ExecPluginCall.Returns.Error = errors.New("banana") |
| 186 |
}) |
| 187 |
It("returns the error", func() { |
| 188 |
_, err := invoke.GetVersionInfo(ctx, pluginPath, pluginExec) |
| 189 |
Expect(err).To(MatchError("banana")) |
| 190 |
}) |
| 191 |
}) |
| 192 |
|
| 193 |
Context("when the plugin is too old to recognize the VERSION command", func() { |
| 194 |
BeforeEach(func() { |
| 195 |
rawExec.ExecPluginCall.Returns.Error = errors.New("unknown CNI_COMMAND: VERSION") |
| 196 |
}) |
| 197 |
|
| 198 |
It("interprets the error as a 0.1.0 version", func() { |
| 199 |
versionInfo, err := invoke.GetVersionInfo(ctx, pluginPath, pluginExec) |
| 200 |
Expect(err).NotTo(HaveOccurred()) |
| 201 |
Expect(versionInfo.SupportedVersions()).To(ConsistOf("0.1.0")) |
| 202 |
}) |
| 203 |
|
| 204 |
It("sets dummy values for env vars required by very old plugins", func() { |
| 205 |
_, err := invoke.GetVersionInfo(ctx, pluginPath, pluginExec) |
| 206 |
Expect(err).NotTo(HaveOccurred()) |
| 207 |
|
| 208 |
env := rawExec.ExecPluginCall.Received.Environ |
| 209 |
Expect(env).To(ContainElement("CNI_NETNS=dummy")) |
| 210 |
Expect(env).To(ContainElement("CNI_IFNAME=dummy")) |
| 211 |
Expect(env).To(ContainElement("CNI_PATH=dummy")) |
| 212 |
}) |
| 213 |
}) |
| 214 |
}) |
| 215 |
}) |