github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/flatfile/flatfile_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 flatfile
     6  
     7  import (
     8  	"context"
     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 TestFlatfile(t *testing.T) {
    18  	RegisterFailHandler(Fail)
    19  	RunSpecs(t, "Providers/Discovery/Flatfile")
    20  }
    21  
    22  var _ = Describe("Flatfile", func() {
    23  	It("Should expect a source", func() {
    24  		ff := &FlatFile{}
    25  		n, err := ff.Discover(context.Background())
    26  		Expect(n).To(BeEmpty())
    27  		Expect(err).To(MatchError("source file not specified"))
    28  	})
    29  
    30  	It("Should validate the filter", func() {
    31  		filter := protocol.NewFilter()
    32  		filter.AddAgentFilter("test")
    33  		ff := &FlatFile{}
    34  		n, err := ff.Discover(context.Background(), Filter(filter), File("testdata/nodes.txt"))
    35  		Expect(n).To(BeEmpty())
    36  		Expect(err).To(MatchError("only identity filters are supported"))
    37  	})
    38  
    39  	It("Should support flat files", func() {
    40  		ff := &FlatFile{}
    41  		n, err := ff.Discover(context.Background(), File("testdata/nodes.txt"), Format(TextFormat))
    42  		Expect(n).To(Equal([]string{"one", "two", "three", "123-abc.example.net", "4bb280ea283e"}))
    43  		Expect(err).ToNot(HaveOccurred())
    44  	})
    45  
    46  	It("Should support json files", func() {
    47  		ff := &FlatFile{}
    48  		n, err := ff.Discover(context.Background(), File("testdata/nodes.json"), Format(JSONFormat))
    49  		Expect(err).ToNot(HaveOccurred())
    50  		Expect(n).To(Equal([]string{"one.json", "two.json", "three.json"}))
    51  	})
    52  
    53  	It("Should support yaml files", func() {
    54  		ff := &FlatFile{}
    55  		n, err := ff.Discover(context.Background(), File("testdata/nodes.yaml"), Format(YAMLFormat))
    56  		Expect(err).ToNot(HaveOccurred())
    57  		Expect(n).To(Equal([]string{"one.yaml", "two.yaml", "three.yaml"}))
    58  	})
    59  
    60  	It("Should support choria response files", func() {
    61  		ff := &FlatFile{}
    62  		n, err := ff.Discover(context.Background(), File("testdata/choria.json"), Format(ChoriaResponsesFormat))
    63  		Expect(err).ToNot(HaveOccurred())
    64  		Expect(n).To(Equal([]string{"n1.example.net", "n2.example.net", "n3.example.net"}))
    65  	})
    66  
    67  	It("Should support identity filters", func() {
    68  		filter := protocol.NewFilter()
    69  		filter.AddIdentityFilter("n1.example.net")
    70  		filter.AddIdentityFilter("/n2/")
    71  
    72  		ff := &FlatFile{}
    73  		n, err := ff.Discover(context.Background(), File("testdata/choria.json"), Format(ChoriaResponsesFormat), Filter(filter))
    74  		Expect(err).ToNot(HaveOccurred())
    75  		Expect(n).To(Equal([]string{"n1.example.net", "n2.example.net"}))
    76  	})
    77  
    78  	It("Should validate node names", func() {
    79  		ff := &FlatFile{}
    80  		n, err := ff.Discover(context.Background(), File("testdata/badnodes.txt"), Format(TextFormat))
    81  		Expect(err).To(MatchError(`invalid identity string "foo bar"`))
    82  		Expect(n).To(BeEmpty())
    83  	})
    84  
    85  	It("Should support gjson matches", func() {
    86  		ff := &FlatFile{}
    87  		n, err := ff.Discover(context.Background(), File("testdata/choria.json"), Format(JSONFormat), DiscoveryOptions(map[string]string{
    88  			"filter": "replies.#.sender",
    89  		}))
    90  		Expect(err).ToNot(HaveOccurred())
    91  		Expect(n).To(Equal([]string{"n1.example.net", "n2.example.net", "n3.example.net"}))
    92  	})
    93  
    94  	It("Should support file overrides", func() {
    95  		ff := &FlatFile{}
    96  		n, err := ff.Discover(context.Background(), File("testdata/choria.json"), DiscoveryOptions(map[string]string{
    97  			"format": "text",
    98  			"file":   "testdata/nodes.txt",
    99  		}))
   100  		Expect(err).ToNot(HaveOccurred())
   101  		Expect(n).To(Equal([]string{"one", "two", "three", "123-abc.example.net", "4bb280ea283e"}))
   102  	})
   103  })