Skip to content

Commit

Permalink
Move all tests to collector
Browse files Browse the repository at this point in the history
Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
  • Loading branch information
mauromorales committed Mar 23, 2023
1 parent 0d33a62 commit 4143874
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 216 deletions.
76 changes: 74 additions & 2 deletions pkg/config/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ options:
config_url: http://127.0.0.1:%d/remote_config_5.yaml
local_key_2: local_value_2
`, port)), os.ModePerm)
err = os.WriteFile(path.Join(tmpDir2, "local_config_3.yaml"), []byte(`#cloud-config
local_key_3: local_value_3
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(path.Join(serverDir, "remote_config_5.yaml"), []byte(fmt.Sprintf(`#cloud-config
Expand Down Expand Up @@ -281,6 +284,8 @@ options:
Expect(k).To(Equal("local_value_1"))
k = (*c)["local_key_2"].(string)
Expect(k).To(Equal("local_value_2"))
k = (*c)["local_key_3"].(string)
Expect(k).To(Equal("local_value_3"))
k = (*c)["remote_key_1"].(string)
Expect(k).To(Equal("remote_value_1"))
k = (*c)["remote_key_2"].(string)
Expand Down Expand Up @@ -401,6 +406,9 @@ name: Mario
tmpDir, err = os.MkdirTemp("", "config")
Expect(err).ToNot(HaveOccurred())

err = os.WriteFile(filepath.Join(tmpDir, "b"), []byte(`zz.foo="baa" options.foo=bar`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

err = os.WriteFile(path.Join(tmpDir, "local_config.yaml"), []byte(`#cloud-config
local_key_1: local_value_1
some:
Expand All @@ -412,7 +420,10 @@ some:

It("can query for keys", func() {
o := &Options{}
err := o.Apply(Directories(tmpDir))

err = o.Apply(MergeBootLine, Directories(tmpDir),
WithBootCMDLineFile(filepath.Join(tmpDir, "b")),
)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(o)
Expand All @@ -421,10 +432,71 @@ some:
v, err := c.Query("local_key_1")
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal("local_value_1\n"))
// TODO: there's a bug when trying to dig some.other.key, so making the test pass this way for now, since that was not tested before
v, err = c.Query("some")
Expect(err).ToNot(HaveOccurred())
// TODO: there's a bug when trying to dig some.other.key, so making the test pass this way for now, since that was not tested before
Expect(v).To(Equal("other:\n key: 3\n"))
Expect(c.Query("options")).To(Equal("foo: bar\n"))
})
})

Describe("FindYAMLWithKey", func() {
var c1Path, c2Path, tmpDir string
var err error

BeforeEach(func() {
var c1 = `
a: 1
b:
c: foo
d:
e: bar
`

var c2 = `
b:
c: foo2
`
tmpDir, err = os.MkdirTemp("", "config")
Expect(err).ToNot(HaveOccurred())

c1Path = filepath.Join(tmpDir, "c1.yaml")
c2Path = filepath.Join(tmpDir, "c2.yaml")

err := os.WriteFile(c1Path, []byte(c1), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(c2Path, []byte(c2), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
// closeFunc()
err := os.RemoveAll(tmpDir)
Expect(err).ToNot(HaveOccurred())
})

It("can find a top level key", func() {
r, err := FindYAMLWithKey("a", Directories(tmpDir))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(Equal([]string{c1Path}))
})

It("can find a nested key", func() {
r, err := FindYAMLWithKey("d.e", Directories(tmpDir))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(Equal([]string{c1Path}))
})

It("returns multiple files when key exists in them", func() {
r, err := FindYAMLWithKey("b.c", Directories(tmpDir))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(ContainElements(c1Path, c2Path))
})

It("return an empty list when key is not found", func() {
r, err := FindYAMLWithKey("does.not.exist", Directories(tmpDir))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(BeEmpty())
})
})
})
Expand Down
1 change: 0 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/kairos-io/kairos-sdk/bundles"
"github.com/kairos-io/kairos/pkg/config/collector"
schema "github.com/kairos-io/kairos/pkg/config/schemas"
"github.com/kairos-io/kairos/sdk/bundles"
yip "github.com/mudler/yip/pkg/schema"

"gopkg.in/yaml.v3"
Expand Down
215 changes: 2 additions & 213 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
package config_test

import (
"fmt"
"os"
"path/filepath"

. "github.com/kairos-io/kairos/pkg/config"
"github.com/kairos-io/kairos/pkg/config/collector"
// . "github.com/kairos-io/kairos/pkg/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"
// . "github.com/onsi/gomega"
)

type TConfig struct {
Expand All @@ -45,211 +41,4 @@ var _ = Describe("Config", func() {
os.RemoveAll(d)
}
})

Context("directory", func() {
headerCheck := func(confStr string) {
Expect(collector.HasValidHeader(confStr)).To(BeTrue())
}

It("reads from bootargs and can query", func() {
err := os.WriteFile(filepath.Join(d, "b"), []byte(`zz.foo="baa" options.foo=bar`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.MergeBootLine,
collector.WithBootCMDLineFile(filepath.Join(d, "b")),
collector.NoLogs, collector.StrictValidation(false))
Expect(err).ToNot(HaveOccurred())

headerCheck(c.String())

Expect(c.Options["foo"]).To(Equal("bar"))
Expect(c.Query("options")).To(Equal("foo: bar\n"))
Expect(c.Query("options.foo")).To(Equal("bar\n"))
})

It("reads multiple config files", func() {
var cc = `#kairos-config
baz: bar
kairos:
network_token: foo
`
var c2 = `
b: f
c: d
`

err := os.WriteFile(filepath.Join(d, "test.yaml"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

err = os.WriteFile(filepath.Join(d, "b.yaml"), []byte(c2), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.Directories(d), collector.NoLogs, collector.StrictValidation(false))
Expect(err).ToNot(HaveOccurred())
Expect(c).ToNot(BeNil())
providerCfg := &TConfig{}
err = c.Unmarshal(providerCfg)
Expect(err).ToNot(HaveOccurred())
Expect(providerCfg.Kairos).ToNot(BeNil())
Expect(providerCfg.Kairos.NetworkToken).To(Equal("foo"))
all := map[string]string{}
yaml.Unmarshal([]byte(c.String()), &all)
Expect(all["b"]).To(Equal("f"))
Expect(all["baz"]).To(Equal("bar"))
})

It("reads config file greedly", func() {

var cc = `#kairos-config
baz: bar
kairos:
network_token: foo
`

err := os.WriteFile(filepath.Join(d, "test.yaml"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(filepath.Join(d, "b.yaml"), []byte(`
fooz:
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

err = os.WriteFile(filepath.Join(d, "more-kairos.yaml"), []byte(`#cloud-config
kairos:
other_key: test
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.Directories(d), collector.NoLogs, collector.StrictValidation(false))
Expect(err).ToNot(HaveOccurred())
Expect(c).ToNot(BeNil())
providerCfg := &TConfig{}
err = c.Unmarshal(providerCfg)
Expect(err).ToNot(HaveOccurred())
Expect(providerCfg.Kairos).ToNot(BeNil())
Expect(providerCfg.Kairos.NetworkToken).To(Equal("foo"))
Expect(providerCfg.Kairos.OtherKey).To(Equal("test"))
expectedString := `#cloud-config
baz: bar
kairos:
network_token: foo
other_key: test
`
Expect(c.String()).To(Equal(expectedString), c.String(), cc)
})

FIt("merges with bootargs", func() {

var cc = `#kairos-config
kairos:
network_token: "foo"
bb:
nothing: "foo"
`

err := os.WriteFile(filepath.Join(d, "test.yaml"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(filepath.Join(d, "b"), []byte(`zz.foo="baa" options.foo=bar`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.Directories(d), collector.MergeBootLine,
collector.WithBootCMDLineFile(filepath.Join(d, "b")),
collector.NoLogs, collector.StrictValidation(false))
fmt.Printf("c = %T , %#v", c, c)
Expect(err).ToNot(HaveOccurred())
Expect(c.Options["foo"]).To(Equal("bar"))

providerCfg := &TConfig{}
err = c.Unmarshal(providerCfg)
Expect(err).ToNot(HaveOccurred())
Expect(providerCfg.Kairos).ToNot(BeNil())
Expect(providerCfg.Kairos.NetworkToken).To(Equal("foo"))
_, exists := c.Data()["zz"]
Expect(exists).To(BeFalse())
})

It("reads config file from url", func() {
var cc = `#cloud-config
config_url: "https://gist.githubusercontent.com/mudler/ab26e8dd65c69c32ab292685741ca09c/raw/bafae390eae4e6382fb1b68293568696823b3103/test.yaml"
`

err := os.WriteFile(filepath.Join(d, "test.yaml"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.Directories(d), collector.NoLogs, collector.StrictValidation(false))
Expect(err).ToNot(HaveOccurred())
Expect(c).ToNot(BeNil())
Expect(len(c.Bundles)).To(Equal(1))
Expect(c.Bundles[0].Targets[0]).To(Equal("package:utils/edgevpn"))
Expect(c.String()).ToNot(Equal(cc))
})

It("keeps header", func() {
var cc = `#cloud-config
config_url: "https://gist.githubusercontent.com/mudler/7e3d0426fce8bfaaeb2644f83a9bfe0c/raw/77ded58aab3ee2a8d4117db95e078f81fd08dfde/testgist.yaml"
`

err := os.WriteFile(filepath.Join(d, "test.yaml"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(collector.Directories(d), collector.NoLogs, collector.StrictValidation(false))
Expect(err).ToNot(HaveOccurred())
Expect(c).ToNot(BeNil())
Expect(len(c.Bundles)).To(Equal(1))
Expect(c.Bundles[0].Targets[0]).To(Equal("package:utils/edgevpn"))
Expect(c.String()).ToNot(Equal(cc))

headerCheck(c.String())
})
})

Describe("FindYAMLWithKey", func() {
var c1Path, c2Path string

BeforeEach(func() {
var c1 = `
a: 1
b:
c: foo
d:
e: bar
`

var c2 = `
b:
c: foo2
`
c1Path = filepath.Join(d, "c1.yaml")
c2Path = filepath.Join(d, "c2.yaml")

err := os.WriteFile(c1Path, []byte(c1), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(c2Path, []byte(c2), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
})

It("can find a top level key", func() {
r, err := FindYAMLWithKey("a", collector.Directories(d))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(Equal([]string{c1Path}))
})

It("can find a nested key", func() {
r, err := FindYAMLWithKey("d.e", collector.Directories(d))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(Equal([]string{c1Path}))
})

It("returns multiple files when key exists in them", func() {
r, err := FindYAMLWithKey("b.c", collector.Directories(d))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(ContainElements(c1Path, c2Path))
})

It("return an empty list when key is not found", func() {
r, err := FindYAMLWithKey("does.not.exist", collector.Directories(d))
Expect(err).ToNot(HaveOccurred())
Expect(r).To(BeEmpty())
})
})
})

0 comments on commit 4143874

Please sign in to comment.