github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/external/external_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 external
     6  
     7  import (
     8  	"context"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  	"time"
    14  
    15  	imock "github.com/choria-io/go-choria/inter/imocks"
    16  	"github.com/golang/mock/gomock"
    17  	. "github.com/onsi/ginkgo/v2"
    18  	. "github.com/onsi/gomega"
    19  
    20  	"github.com/choria-io/go-choria/config"
    21  	"github.com/choria-io/go-choria/protocol"
    22  )
    23  
    24  func TestExternal(t *testing.T) {
    25  	RegisterFailHandler(Fail)
    26  	RunSpecs(t, "Providers/Discovery/External")
    27  }
    28  
    29  var _ = Describe("External", func() {
    30  	var (
    31  		mockctl *gomock.Controller
    32  		fw      *imock.MockFramework
    33  		cfg     *config.Config
    34  		e       *External
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		mockctl = gomock.NewController(GinkgoT())
    39  		fw, cfg = imock.NewFrameworkForTests(mockctl, GinkgoWriter)
    40  		cfg.Collectives = []string{"mcollective", "test"}
    41  
    42  		e = New(fw)
    43  	})
    44  
    45  	AfterEach(func() {
    46  		mockctl.Finish()
    47  	})
    48  
    49  	Describe("New", func() {
    50  		It("Should initialize timeout to default", func() {
    51  			Expect(e.timeout).To(Equal(2 * time.Second))
    52  			cfg.DiscoveryTimeout = 100
    53  			e = New(fw)
    54  			Expect(e.timeout).To(Equal(100 * time.Second))
    55  		})
    56  	})
    57  
    58  	Describe("Discover", func() {
    59  		It("Should request and return discovered nodes", func() {
    60  			if runtime.GOOS == "windows" {
    61  				Skip("not tested on windows")
    62  			}
    63  
    64  			f := protocol.NewFilter()
    65  			f.AddAgentFilter("rpcutil")
    66  			f.AddFactFilter("country", "==", "mt")
    67  
    68  			wd, _ := os.Getwd()
    69  			cfg.Choria.ExternalDiscoveryCommand = filepath.Join(wd, "testdata/good.rb")
    70  			nodes, err := e.Discover(context.Background(), Filter(f), DiscoveryOptions(map[string]string{"foo": "bar"}))
    71  			Expect(err).ToNot(HaveOccurred())
    72  			Expect(nodes).To(Equal([]string{"one", "two"}))
    73  
    74  			cfg.Choria.ExternalDiscoveryCommand = filepath.Join(wd, "testdata/good_with_argument.rb") + " discover --test"
    75  			nodes, err = e.Discover(context.Background(), Filter(f), DiscoveryOptions(map[string]string{"foo": "bar"}))
    76  			Expect(err).ToNot(HaveOccurred())
    77  			Expect(nodes).To(Equal([]string{"one", "two"}))
    78  		})
    79  
    80  		It("Should support command overrides via options", func() {
    81  			if runtime.GOOS == "windows" {
    82  				Skip("not tested on windows")
    83  			}
    84  
    85  			f := protocol.NewFilter()
    86  			f.AddAgentFilter("rpcutil")
    87  			f.AddFactFilter("country", "==", "mt")
    88  
    89  			wd, _ := os.Getwd()
    90  			cfg.Choria.ExternalDiscoveryCommand = filepath.Join(wd, "testdata/missing.rb")
    91  			cmd := filepath.Join(wd, "testdata/good_with_argument.rb") + " discover --test"
    92  			nodes, err := e.Discover(context.Background(), Filter(f), DiscoveryOptions(map[string]string{"command": cmd, "foo": "bar"}))
    93  			Expect(err).ToNot(HaveOccurred())
    94  			Expect(nodes).To(Equal([]string{"one", "two"}))
    95  		})
    96  	})
    97  })