github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/server/discovery/discovery_test.go (about)

     1  // Copyright (c) 2017-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package discovery
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/choria-io/go-choria/inter"
    11  	imock "github.com/choria-io/go-choria/inter/imocks"
    12  	v1 "github.com/choria-io/go-choria/protocol/v1"
    13  	"github.com/golang/mock/gomock"
    14  
    15  	"github.com/choria-io/go-choria/filter/classes"
    16  	"github.com/choria-io/go-choria/filter/facts"
    17  	"github.com/choria-io/go-choria/protocol"
    18  	"github.com/choria-io/go-choria/providers/data/ddl"
    19  
    20  	"github.com/choria-io/go-choria/config"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  func Test(t *testing.T) {
    27  	RegisterFailHandler(Fail)
    28  	RunSpecs(t, "Server/Discovery")
    29  }
    30  
    31  var _ = Describe("Server/Discovery", func() {
    32  	var (
    33  		fw     inter.Framework
    34  		cfg    *config.Config
    35  		mgr    *Manager
    36  		req    protocol.Request
    37  		filter *protocol.Filter
    38  		si     *MockServerInfoSource
    39  		ctrl   *gomock.Controller
    40  	)
    41  
    42  	BeforeEach(func() {
    43  		ctrl = gomock.NewController(GinkgoT())
    44  		fw, cfg = imock.NewFrameworkForTests(ctrl, GinkgoWriter)
    45  		si = NewMockServerInfoSource(ctrl)
    46  
    47  		mgr = New(cfg, si, fw.Logger(""))
    48  		rid, err := fw.NewRequestID()
    49  		Expect(err).ToNot(HaveOccurred())
    50  
    51  		req, err = v1.NewRequest("test", "testid", "callerid", 60, rid, "mcollective")
    52  		Expect(err).ToNot(HaveOccurred())
    53  
    54  		filter = req.NewFilter()
    55  		req.SetFilter(filter)
    56  
    57  		klasses, err := classes.ReadClasses("testdata/classes.txt")
    58  		Expect(err).ToNot(HaveOccurred())
    59  		factsj, err := facts.JSON("testdata/facts.yaml", fw.Logger(""))
    60  		Expect(err).ToNot(HaveOccurred())
    61  
    62  		si.EXPECT().Identity().Return("test.example.net").AnyTimes()
    63  		si.EXPECT().Classes().Return(klasses).AnyTimes()
    64  		si.EXPECT().Facts().Return(factsj).AnyTimes()
    65  	})
    66  
    67  	AfterEach(func() {
    68  		ctrl.Finish()
    69  	})
    70  
    71  	It("Should match on empty filters", func() {
    72  		si.EXPECT().KnownAgents().Return([]string{}).AnyTimes()
    73  		Expect(mgr.ShouldProcess(req)).To(BeTrue())
    74  	})
    75  
    76  	It("Should match if all filters matched", func() {
    77  		filter.AddAgentFilter("apache")
    78  		filter.AddClassFilter("role::testing")
    79  		filter.AddClassFilter("/test/")
    80  		filter.AddFactFilter("nested.string", "=~", "/hello/")
    81  		filter.AddIdentityFilter("/test/")
    82  
    83  		si.EXPECT().KnownAgents().Return([]string{"apache", "rpcutil"}).AnyTimes()
    84  		Expect(mgr.ShouldProcess(req)).To(BeTrue())
    85  	})
    86  
    87  	It("Should fail if some filters matched", func() {
    88  		filter.AddAgentFilter("apache")
    89  		filter.AddClassFilter("role::test")
    90  		filter.AddFactFilter("nested.string", "=~", "/meh/")
    91  		si.EXPECT().KnownAgents().Return([]string{"apache", "rpcutil"}).AnyTimes()
    92  		Expect(mgr.ShouldProcess(req)).To(BeFalse())
    93  	})
    94  
    95  	It("Should handle compound filters", func() {
    96  		filter.AddCompoundFilter("with('apache') and with('/testing/') and with('fnumber=1.2') and fact('nested.string') matches('h?llo') and include(fact('sarray'), '1') and include(fact('iarray'), 1)")
    97  		si.EXPECT().DataFuncMap().Return(ddl.FuncMap{}, nil).AnyTimes()
    98  		si.EXPECT().KnownAgents().Return([]string{"apache", "rpcutil"}).AnyTimes()
    99  		Expect(mgr.ShouldProcess(req)).To(BeTrue())
   100  	})
   101  })