github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/ddlresolver/filesystem_resolver_test.go (about) 1 // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package ddlresolver 6 7 import ( 8 "context" 9 "os" 10 11 "github.com/choria-io/go-choria/config" 12 imock "github.com/choria-io/go-choria/inter/imocks" 13 agentDDL "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent" 14 "github.com/golang/mock/gomock" 15 . "github.com/onsi/ginkgo/v2" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("FileSystemDDLResolver", func() { 20 var ( 21 res *FileSystemDDLResolver 22 fw *imock.MockFramework 23 cfg *config.Config 24 mockctl *gomock.Controller 25 ) 26 27 BeforeEach(func() { 28 mockctl = gomock.NewController(GinkgoT()) 29 fw, cfg = imock.NewFrameworkForTests(mockctl, GinkgoWriter) 30 res = &FileSystemDDLResolver{} 31 cfg.LibDir = []string{"testdata/dir1"} 32 cfg.Choria.RubyLibdir = []string{"testdata/dir2"} 33 }) 34 35 AfterEach(func() { 36 mockctl.Finish() 37 }) 38 39 Describe("DDL", func() { 40 It("Should handle failures", func() { 41 err := res.DDL(context.Background(), "foo", "bar", nil, fw) 42 Expect(err).To(MatchError("unsupported ddl type \"foo\"")) 43 }) 44 45 It("Should correctly find and unmarshal into target", func() { 46 ddl := &agentDDL.DDL{} 47 err := res.DDL(context.Background(), "agent", "four", ddl, fw) 48 Expect(err).ToNot(HaveOccurred()) 49 Expect(ddl.Metadata.Name).To(Equal("four")) 50 }) 51 }) 52 53 Describe("DDLBytes", func() { 54 It("Should handle failures", func() { 55 _, err := res.DDLBytes(context.Background(), "foo", "bar", fw) 56 Expect(err).To(MatchError("unsupported ddl type \"foo\"")) 57 }) 58 59 It("Should find the correct DDL", func() { 60 b, err := res.DDLBytes(context.Background(), "agent", "four", fw) 61 Expect(err).ToNot(HaveOccurred()) 62 expected, err := os.ReadFile("testdata/dir2/mcollective/agent/four.json") 63 Expect(err).ToNot(HaveOccurred()) 64 Expect(expected).ToNot(BeEmpty()) 65 Expect(b).To(Equal(expected)) 66 }) 67 }) 68 69 Describe("DDLNames", func() { 70 It("Should handle failures", func() { 71 _, err := res.DDLBytes(context.Background(), "foo", "bar", fw) 72 Expect(err).To(MatchError("unsupported ddl type \"foo\"")) 73 }) 74 75 It("Should find the correct names", func() { 76 names, err := res.DDLNames(context.Background(), "agent", fw) 77 Expect(err).ToNot(HaveOccurred()) 78 Expect(names).To(Equal([]string{"four", "one", "three", "two"})) 79 }) 80 }) 81 })