github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/inventory/inventory_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 inventory 6 7 import ( 8 "context" 9 "testing" 10 11 imock "github.com/choria-io/go-choria/inter/imocks" 12 "github.com/golang/mock/gomock" 13 . "github.com/onsi/ginkgo/v2" 14 . "github.com/onsi/gomega" 15 "github.com/sirupsen/logrus" 16 17 "github.com/choria-io/go-choria/config" 18 "github.com/choria-io/go-choria/protocol" 19 ) 20 21 func TestExternal(t *testing.T) { 22 RegisterFailHandler(Fail) 23 RunSpecs(t, "Providers/Discovery/Inventory") 24 } 25 26 var _ = Describe("Inventory", func() { 27 var ( 28 mockctl *gomock.Controller 29 fw *imock.MockFramework 30 cfg *config.Config 31 inv *Inventory 32 ) 33 34 BeforeEach(func() { 35 logger := logrus.New() 36 logger.SetOutput(GinkgoWriter) 37 38 mockctl = gomock.NewController(GinkgoT()) 39 fw, cfg = imock.NewFrameworkForTests(mockctl, GinkgoWriter) 40 cfg.Choria.InventoryDiscoverySource = "testdata/good-inventory.yaml" 41 inv = New(fw) 42 }) 43 44 AfterEach(func() { 45 mockctl.Finish() 46 }) 47 48 Describe("Discover", func() { 49 It("Should resolve nodes", func() { 50 filter := protocol.NewFilter() 51 nodes, err := inv.Discover(context.Background(), Collective("mt_collective"), Filter(filter)) 52 Expect(err).To(Not(HaveOccurred())) 53 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 54 55 nodes, err = inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 56 Expect(err).To(Not(HaveOccurred())) 57 Expect(nodes).To(Equal([]string{"dev1.example.net", "dev2.example.net"})) 58 59 filter.AddFactFilter("country", "==", "mt") 60 nodes, err = inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 61 Expect(err).To(Not(HaveOccurred())) 62 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 63 }) 64 65 It("Should resolve groups", func() { 66 filter := protocol.NewFilter() 67 filter.AddIdentityFilter("group:malta") 68 69 nodes, err := inv.Discover(context.Background(), Filter(filter)) 70 Expect(err).To(Not(HaveOccurred())) 71 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 72 73 filter = protocol.NewFilter() 74 filter.AddIdentityFilter("group:all") 75 nodes, err = inv.Discover(context.Background(), Filter(filter)) 76 Expect(err).To(Not(HaveOccurred())) 77 Expect(nodes).To(Equal([]string{"dev1.example.net", "dev2.example.net"})) 78 79 filter = protocol.NewFilter() 80 filter.AddIdentityFilter("group:acme") 81 nodes, err = inv.Discover(context.Background(), Filter(filter)) 82 Expect(err).To(Not(HaveOccurred())) 83 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 84 }) 85 86 It("Should resolve multiple groups", func() { 87 filter := protocol.NewFilter() 88 filter.AddIdentityFilter("group:malta") 89 filter.AddIdentityFilter("group:germany") 90 91 nodes, err := inv.Discover(context.Background(), Filter(filter)) 92 Expect(err).To(Not(HaveOccurred())) 93 Expect(nodes).To(Equal([]string{"dev1.example.net", "dev2.example.net"})) 94 }) 95 96 It("Should match facts", func() { 97 filter := protocol.NewFilter() 98 filter.AddFactFilter("customer", "==", "acme") 99 nodes, err := inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 100 Expect(err).To(Not(HaveOccurred())) 101 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 102 }) 103 104 It("Should match classes", func() { 105 filter := protocol.NewFilter() 106 filter.AddClassFilter("one") 107 nodes, err := inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 108 Expect(err).To(Not(HaveOccurred())) 109 Expect(nodes).To(Equal([]string{"dev1.example.net", "dev2.example.net"})) 110 111 filter.AddClassFilter("three") 112 nodes, err = inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 113 Expect(err).To(Not(HaveOccurred())) 114 Expect(nodes).To(Equal([]string{"dev2.example.net"})) 115 }) 116 117 It("Should match agents", func() { 118 filter := protocol.NewFilter() 119 filter.AddAgentFilter("rpcutil") 120 nodes, err := inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 121 Expect(err).To(Not(HaveOccurred())) 122 Expect(nodes).To(Equal([]string{"dev1.example.net", "dev2.example.net"})) 123 124 filter = protocol.NewFilter() 125 filter.AddAgentFilter("rpcutil") 126 filter.AddAgentFilter("other") 127 nodes, err = inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 128 Expect(err).To(Not(HaveOccurred())) 129 Expect(nodes).To(Equal([]string{"dev2.example.net"})) 130 }) 131 132 It("Should match compound filters", func() { 133 filter := protocol.NewFilter() 134 err := filter.AddCompoundFilter(`with("customer=acme") and with("one")`) 135 Expect(err).To(Not(HaveOccurred())) 136 nodes, err := inv.Discover(context.Background(), Collective("mcollective"), Filter(filter)) 137 Expect(err).To(Not(HaveOccurred())) 138 Expect(nodes).To(Equal([]string{"dev1.example.net"})) 139 }) 140 }) 141 })