github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/registration/file_content_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 registration
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/tidwall/gjson"
    14  
    15  	. "github.com/onsi/ginkgo/v2"
    16  	. "github.com/onsi/gomega"
    17  	log "github.com/sirupsen/logrus"
    18  
    19  	"github.com/choria-io/go-choria/config"
    20  	"github.com/choria-io/go-choria/server/data"
    21  )
    22  
    23  func Test(t *testing.T) {
    24  	os.Setenv("MCOLLECTIVE_CERTNAME", "rip.mcollective")
    25  	RegisterFailHandler(Fail)
    26  	RunSpecs(t, "Registration")
    27  }
    28  
    29  var _ = Describe("RegistrationData", func() {
    30  	var (
    31  		reg  *FileContent
    32  		c    *config.Config
    33  		err  error
    34  		msgs chan *data.RegistrationItem
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		c = config.NewConfigForTests()
    39  
    40  		reg = &FileContent{}
    41  		log.SetLevel(log.ErrorLevel)
    42  
    43  		msgs = make(chan *data.RegistrationItem, 1)
    44  
    45  		os.Chtimes("testdata/sample.json", time.Unix(1511865541, 0), time.Unix(1511865541, 0))
    46  	})
    47  
    48  	It("Should return err when the data file is missing", func() {
    49  		c.Choria.FileContentRegistrationData = "/nonexisting"
    50  		reg.Init(c, log.WithFields(log.Fields{}))
    51  
    52  		err := reg.publish(msgs)
    53  		Expect(err).To(MatchError("could not find data file /nonexisting"))
    54  	})
    55  
    56  	It("Should return err when the data file is empty", func() {
    57  		tmpfile, err := os.CreateTemp("", "file_content_registration")
    58  		Expect(err).ToNot(HaveOccurred())
    59  		tmpfile.Close()
    60  		defer os.Remove(tmpfile.Name())
    61  
    62  		c.Choria.FileContentRegistrationData = tmpfile.Name()
    63  		reg.Init(c, log.WithFields(log.Fields{}))
    64  
    65  		err = reg.publish(msgs)
    66  		Expect(err).To(MatchError(fmt.Sprintf("data file %s is empty", tmpfile.Name())))
    67  	})
    68  
    69  	It("Should read the file and publish it to default location", func() {
    70  		c.Choria.FileContentRegistrationData = "testdata/sample.json"
    71  		reg.Init(c, log.WithFields(log.Fields{}))
    72  
    73  		err = reg.publish(msgs)
    74  		Expect(err).ToNot(HaveOccurred())
    75  
    76  		msg := <-msgs
    77  		Expect(string(msg.Data)).To(Equal(`{"mtime":1511865541,"file":"testdata/sample.json","updated":false,"protocol":"choria:registration:filecontent:1","zcontent":"H4sIAAAAAAAA/6pWSsvMSVWyUigpKk2tBQAAAP//AQAA//9QwpuPDgAAAA=="}`))
    78  		Expect(msg.TargetAgent).To(Equal("registration"))
    79  	})
    80  
    81  	It("Should support custom targets", func() {
    82  		c.Choria.FileContentRegistrationData = "testdata/sample.json"
    83  		c.Choria.FileContentRegistrationTarget = "my.cmdb"
    84  
    85  		reg.Init(c, log.WithFields(log.Fields{}))
    86  
    87  		err = reg.publish(msgs)
    88  		Expect(err).ToNot(HaveOccurred())
    89  
    90  		msg := <-msgs
    91  		Expect(string(msg.Data)).To(Equal(`{"mtime":1511865541,"file":"testdata/sample.json","updated":false,"protocol":"choria:registration:filecontent:1","zcontent":"H4sIAAAAAAAA/6pWSsvMSVWyUigpKk2tBQAAAP//AQAA//9QwpuPDgAAAA=="}`))
    92  		Expect(msg.TargetAgent).To(Equal(""))
    93  		Expect(msg.Destination).To(Equal("my.cmdb"))
    94  	})
    95  
    96  	It("Should support disabling compression", func() {
    97  		c.Choria.FileContentRegistrationData = "testdata/sample.json"
    98  		c.Choria.FileContentCompression = false
    99  		reg.Init(c, log.WithFields(log.Fields{}))
   100  
   101  		err = reg.publish(msgs)
   102  		Expect(err).ToNot(HaveOccurred())
   103  
   104  		msg := <-msgs
   105  		Expect(string(msg.Data)).To(Equal(`{"mtime":1511865541,"file":"testdata/sample.json","updated":false,"protocol":"choria:registration:filecontent:1","content":"eyJmaWxlIjogdHJ1ZX0="}`))
   106  		Expect(msg.TargetAgent).To(Equal("registration"))
   107  	})
   108  
   109  	It("Should detect file updates", func() {
   110  		c.Choria.FileContentRegistrationData = "testdata/sample.json"
   111  		c.Choria.FileContentCompression = false
   112  		reg.Init(c, log.WithFields(log.Fields{}))
   113  
   114  		err = reg.publish(msgs)
   115  		Expect(err).ToNot(HaveOccurred())
   116  
   117  		msg := <-msgs
   118  		Expect(gjson.GetBytes(msg.Data, "updated").Bool()).To(BeFalse())
   119  
   120  		err = os.Chtimes("testdata/sample.json", time.Now(), time.Now())
   121  		Expect(err).ToNot(HaveOccurred())
   122  
   123  		err = reg.publish(msgs)
   124  		Expect(err).ToNot(HaveOccurred())
   125  
   126  		msg = <-msgs
   127  
   128  		Expect(gjson.GetBytes(msg.Data, "updated").Bool()).To(BeTrue())
   129  	})
   130  })