github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/puppetdb/puppetdb_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 puppetdb 6 7 import ( 8 "fmt" 9 "testing" 10 11 . "github.com/onsi/ginkgo/v2" 12 . "github.com/onsi/gomega" 13 14 "github.com/choria-io/go-choria/protocol" 15 ) 16 17 func TestPuppetDB(t *testing.T) { 18 RegisterFailHandler(Fail) 19 RunSpecs(t, "Providers/Discovery/PuppetDB") 20 } 21 22 var _ = Describe("PuppetDB", func() { 23 var ( 24 discovery PuppetDB 25 ) 26 27 BeforeEach(func() { 28 discovery = PuppetDB{} 29 }) 30 31 Describe("stringRegex", func() { 32 It("Should correctly create case insensitive regex", func() { 33 Expect(discovery.stringRegex("a1_$-2bZ")).To(Equal("[aA]1_$-2[bB][zZ]")) 34 }) 35 }) 36 37 Describe("capitalizePuppetResource", func() { 38 It("Should correctly capitalize resources", func() { 39 Expect(discovery.capitalizePuppetResource("foo")).To(Equal("Foo")) 40 Expect(discovery.capitalizePuppetResource("Foo")).To(Equal("Foo")) 41 Expect(discovery.capitalizePuppetResource("foo::bar")).To(Equal("Foo::Bar")) 42 Expect(discovery.capitalizePuppetResource("Foo::Bar")).To(Equal("Foo::Bar")) 43 }) 44 }) 45 46 Describe("isNumeric", func() { 47 It("Should correctly detect numbers", func() { 48 Expect(discovery.isNumeric("100")).To(BeTrue()) 49 Expect(discovery.isNumeric("100.2")).To(BeTrue()) 50 Expect(discovery.isNumeric("100.2a")).To(BeFalse()) 51 }) 52 }) 53 54 Describe("discoverNodes", func() { 55 It("Should discover nodes correctly", func() { 56 has := discovery.discoverNodes([]string{"/x/", "y", `pql: nodes[certname] { facts_environment = "production" }`}) 57 expected := `certname ~ "[xX]" or certname = "y" or certname in nodes[certname] { facts_environment = "production" }` 58 59 Expect(has).To(Equal(expected)) 60 }) 61 }) 62 63 Describe("discoverClasses", func() { 64 It("Should correctly discover classes", func() { 65 Expect(discovery.discoverClasses([]string{"/foo/", "foo::bar"})).To(Equal(`resources {type = "Class" and title ~ "[fF][oO][oO]"} and resources {type = "Class" and title = "Foo::Bar"}`)) 66 }) 67 }) 68 69 Describe("discoverAgents", func() { 70 It("Should search for correct classes", func() { 71 has := discovery.discoverAgents([]string{"rpcutil", "rspec1", "/rs/"}) 72 expected := `(resources {type = "Class" and title = "Choria::Service"} or resources {type = "Class" and title = "Mcollective::Service"}) and resources {type = "File" and tag = "mcollective_agent_rspec1_server"} and resources {type = "File" and tag ~ "mcollective_agent_.*?[rR][sS].*?_server"}` 73 Expect(has).To(Equal(expected)) 74 }) 75 }) 76 77 Describe("discoverCollective", func() { 78 It("Should search in facts", func() { 79 Expect(discovery.discoverCollective("rspec_collective")).To(Equal(`certname in inventory[certname] { facts.mcollective.server.collectives.match("\d+") = "rspec_collective" }`)) 80 }) 81 }) 82 83 Describe("discoverFacts", func() { 84 It("Should support all operators", func() { 85 cases := []struct { 86 Filter []protocol.FactFilter 87 Expects string 88 Error error 89 }{ 90 {Filter: []protocol.FactFilter{{Fact: "f", Operator: "=~", Value: "v"}}, Expects: `inventory {facts.f ~ "[vV]"}`}, 91 {Filter: []protocol.FactFilter{{Fact: "f", Operator: "==", Value: "v"}}, Expects: `inventory {facts.f = "v"}`}, 92 {Filter: []protocol.FactFilter{{Fact: "f", Operator: "!=", Value: "v"}}, Expects: `inventory {!(facts.f = "v")}`}, 93 {Filter: []protocol.FactFilter{{Fact: "f", Operator: ">=", Value: "v"}}, Error: fmt.Errorf("'>=' operator supports only numeric values")}, 94 {Filter: []protocol.FactFilter{{Fact: "f", Operator: ">=", Value: "1"}}, Expects: `inventory {facts.f >= 1}`}, 95 } 96 97 for _, tc := range cases { 98 if tc.Error == nil { 99 Expect(discovery.discoverFacts(tc.Filter)).To(Equal(tc.Expects)) 100 } else { 101 _, err := discovery.discoverFacts(tc.Filter) 102 Expect(err).To(MatchError(tc.Error)) 103 } 104 } 105 }) 106 }) 107 })