github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/config/config_test.go (about) 1 // Copyright (c) 2018-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package config 6 7 import ( 8 "os" 9 "path/filepath" 10 "runtime" 11 "testing" 12 "time" 13 14 "github.com/choria-io/go-choria/build" 15 . "github.com/onsi/ginkgo/v2" 16 . "github.com/onsi/gomega" 17 ) 18 19 func TestChoria(t *testing.T) { 20 os.Setenv("MCOLLECTIVE_CERTNAME", "rip.mcollective") 21 RegisterFailHandler(Fail) 22 RunSpecs(t, "Config") 23 } 24 25 var _ = Describe("Choria/Config", func() { 26 Describe("NewConfig", func() { 27 AfterEach(func() { 28 build.DefaultCollectives = "mcollective" 29 }) 30 31 It("Should switch to choria collective when not configured and the choria security system is in use", func() { 32 c := &Config{Choria: &ChoriaPluginConfig{SecurityProvider: "choria"}} 33 err := c.normalize() 34 Expect(err).ToNot(HaveOccurred()) 35 Expect(c.Collectives).To(Equal([]string{"choria"})) 36 37 c = &Config{Choria: &ChoriaPluginConfig{SecurityProvider: "puppet"}} 38 err = c.normalize() 39 Expect(err).ToNot(HaveOccurred()) 40 Expect(c.Collectives).To(Equal([]string{"mcollective"})) 41 42 build.DefaultCollectives = "foo" 43 c = &Config{Choria: &ChoriaPluginConfig{SecurityProvider: "puppet"}} 44 err = c.normalize() 45 Expect(err).ToNot(HaveOccurred()) 46 Expect(c.Collectives).To(Equal([]string{"foo"})) 47 48 c = &Config{Choria: &ChoriaPluginConfig{}, Collectives: []string{"x", "y"}} 49 err = c.normalize() 50 Expect(err).ToNot(HaveOccurred()) 51 Expect(c.Collectives).To(Equal([]string{"x", "y"})) 52 Expect(c.MainCollective).To(Equal("x")) 53 }) 54 55 It("Should get collectives from build settings", func() { 56 c := &Config{Choria: &ChoriaPluginConfig{}} 57 build.DefaultCollectives = "g1 , g2" 58 err := c.normalize() 59 Expect(err).To(Not(HaveOccurred())) 60 Expect(c.Collectives).To(Equal([]string{"g1", "g2"})) 61 Expect(c.MainCollective).To(Equal("g1")) 62 }) 63 64 It("Should correctly parse config files", func() { 65 var c *Config 66 var err error 67 68 forceDotParse = true 69 if runtime.GOOS == "windows" { 70 c, err = NewConfig("testdata/choria_windows.cfg") 71 } else { 72 c, err = NewConfig("testdata/choria.cfg") 73 } 74 Expect(err).ToNot(HaveOccurred()) 75 forceDotParse = false 76 77 Expect(c.Choria.NetworkWriteDeadline).To(Equal(10 * time.Second)) 78 Expect(c.Registration).To(Equal([]string{"foo"})) 79 Expect(c.RegisterInterval).To(Equal(10)) 80 Expect(c.RegistrationSplay).To(BeTrue()) 81 Expect(c.Collectives).To(Equal([]string{"c_1", "c_2", "c_3"})) 82 Expect(c.MainCollective).To(Equal("c_1")) 83 Expect(c.LibDir).To(Equal([]string{"/dir1", "/dir2", "/dir3", "/dir4"})) 84 Expect(c.DefaultDiscoveryOptions).To(Equal([]string{"one", "two"})) 85 86 if runtime.GOOS == "windows" { 87 Expect(c.Color).To(BeFalse()) 88 } else { 89 Expect(c.Color).To(BeTrue()) 90 } 91 92 Expect(c.Choria.PrivilegedUsers).To(Equal([]string{ 93 "\\.privileged.mcollective$", 94 "\\.privileged.choria$", 95 })) 96 Expect(c.Choria.CertnameAllowList).To(Equal([]string{ 97 "\\.mcollective$", 98 "\\.choria$", 99 })) 100 101 Expect(c.Option("plugin.package.setting", "default")).To(Equal("1")) 102 Expect(c.Option("plugin.package.other_setting", "default")).To(Equal("default")) 103 104 c.SetOption("plugin.package.other_setting", "override") 105 Expect(c.Option("plugin.package.setting", "default")).To(Equal("1")) 106 Expect(c.Option("plugin.package.other_setting", "default")).To(Equal("override")) 107 }) 108 }) 109 110 Context("Projects", func() { 111 It("Should find the project configs", func() { 112 c, err := ProjectConfigurationFiles("testdata/project") 113 Expect(err).ToNot(HaveOccurred()) 114 pwd, _ := os.Getwd() 115 Expect(c).ToNot(BeEmpty()) 116 Expect(c[len(c)-1]).To(Equal(filepath.Join(pwd, "testdata", "project", "choria.conf"))) 117 }) 118 119 It("Should load project configs for users", func() { 120 pwd, _ := os.Getwd() 121 Expect(os.Chdir(filepath.Join(pwd, "testdata", "project"))).ToNot(HaveOccurred()) 122 defer os.Chdir(pwd) 123 124 cfg, err := NewConfig(filepath.Join("..", "choria.cfg")) 125 Expect(err).ToNot(HaveOccurred()) 126 Expect(cfg.Option("plugin.project.test", "")).To(Equal("1")) 127 }) 128 129 It("Should not load project configs for system components", func() { 130 pwd, _ := os.Getwd() 131 Expect(os.Chdir(filepath.Join(pwd, "testdata", "project"))).ToNot(HaveOccurred()) 132 defer os.Chdir(pwd) 133 134 cfg, err := NewSystemConfig(filepath.Join("..", "choria.cfg"), false) 135 Expect(err).ToNot(HaveOccurred()) 136 Expect(cfg.Option("plugin.project.test", "")).To(Equal("0")) 137 }) 138 }) 139 })