func debugBehavior(args *skel.CmdArgs, command string) error { if !strings.HasPrefix(args.Args, "DEBUG=") { fmt.Printf(`{}`) os.Stderr.WriteString("CNI_ARGS empty, no debug behavior\n") return nil } debugFilePath := strings.TrimPrefix(args.Args, "DEBUG=") debug, err := debug.ReadDebug(debugFilePath) if err != nil { return err } debug.CmdArgs = *args debug.Command = command err = debug.WriteDebug(debugFilePath) if err != nil { return err } if debug.ReportError != "" { return errors.New(debug.ReportError) } else { os.Stdout.WriteString(debug.ReportResult) } return nil }
os.Remove(debugFileName) }) It("responds to ADD using the ReportResult debug field", func() { session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session).Should(gexec.Exit(0)) Expect(session.Out.Contents()).To(MatchJSON(reportResult)) }) It("records all the args provided by skel.PluginMain", func() { session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session).Should(gexec.Exit(0)) debug, err := noop_debug.ReadDebug(debugFileName) Expect(err).NotTo(HaveOccurred()) Expect(debug.Command).To(Equal("ADD")) Expect(debug.CmdArgs).To(Equal(expectedCmdArgs)) }) Context("when the ReportError debug field is set", func() { BeforeEach(func() { debug.ReportError = "banana" Expect(debug.WriteDebug(debugFileName)).To(Succeed()) }) It("returns an error to skel.PluginMain, causing the process to exit code 1", func() { session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session).Should(gexec.Exit(1))
Describe("AddNetwork", func() { It("executes the plugin with command ADD", func() { result, err := cniConfig.AddNetwork(netConfig, runtimeConfig) Expect(err).NotTo(HaveOccurred()) Expect(result).To(Equal(&types.Result{ IP4: &types.IPConfig{ IP: net.IPNet{ IP: net.ParseIP("10.1.2.3"), Mask: net.IPv4Mask(255, 255, 255, 0), }, }, })) debug, err := noop_debug.ReadDebug(debugFilePath) Expect(err).NotTo(HaveOccurred()) Expect(debug.Command).To(Equal("ADD")) Expect(debug.CmdArgs).To(Equal(expectedCmdArgs)) }) Context("when finding the plugin fails", func() { BeforeEach(func() { netConfig.Network.Type = "does-not-exist" }) It("returns the error", func() { _, err := cniConfig.AddNetwork(netConfig, runtimeConfig) Expect(err).To(MatchError(ContainSubstring(`failed to find plugin "does-not-exist"`))) }) })