kairos-agent/internal/agent/validate_test.go
Dimitris Karakasilis e7807dd66c Change module path according to Go docs (#1220)
https://go.dev/doc/modules/major-version

This way we can bump the kairos dependency on the provider-kairos repo

which otherwise produced the error:

```
~/workspace/kairos/provider-kairos (main)*$ go get -u github.com/kairos-io/kairos@v2.0.0-alpha3
go: github.com/kairos-io/kairos@v2.0.0-alpha3: invalid version: module contains a go.mod file, so module path must match major version ("github.com/kairos-io/kairos/v2")
```

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
Co-authored-by: Itxaka <itxaka.garcia@spectrocloud.com>
2023-03-30 14:18:53 +03:00

108 lines
2.7 KiB
Go

package agent_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
. "github.com/kairos-io/kairos/v2/internal/agent"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Validate", func() {
Context("JSONSchema", func() {
It("returns a schema with a url to the given version", func() {
out, err := JSONSchema("0.0.0")
Expect(err).ToNot(HaveOccurred())
Expect(strings.Contains(out, `$schema": "https://kairos.io/0.0.0/cloud-config.json"`)).To(BeTrue())
})
})
Context("Validate", func() {
var yaml string
Context("With a really long config string", func() {
BeforeEach(func() {
yaml = `#cloud-config
users:
- name: kairos
passwd: kairos
vpn:
network_token: "dssdnfjkldashfkjhasdkhfkasjdhfkjhasdjkfhaksjdhfkjashjdkfhioreqwhfuihqweruifhuewrbfhuewrfuyequfhuiehuifheqrihfuiqrehfuirqheiufhreqiuhfuiqheiufhqeuihfuiqrehfiuhqreuifrhiuqehfiuhqeirhfiuewhrfhqwehfriuewhfuihewiuhfruewhrifhwiuehrfiuhweiurfhwueihrfuiwehufhweuihrfuiwerhfuihewruifhewuihfiouwehrfiouhwei"
`
})
It("validates", func() {
Expect(Validate(yaml)).ToNot(HaveOccurred())
})
})
Context("with a valid config", func() {
BeforeEach(func() {
yaml = `#cloud-config
users:
- name: kairos
passwd: kairos`
})
It("is successful reading it from file", func() {
f, err := ioutil.TempDir("", "tests")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(f)
path := filepath.Join(f, "config.yaml")
err = os.WriteFile(path, []byte(yaml), 0655)
Expect(err).ToNot(HaveOccurred())
err = Validate(path)
Expect(err).ToNot(HaveOccurred())
})
It("is successful reading it from a string", func() {
Expect(Validate(yaml)).ToNot(HaveOccurred())
})
})
Context("without a header", func() {
BeforeEach(func() {
yaml = `users:
- name: kairos
passwd: kairos`
})
It("is fails", func() {
f, err := ioutil.TempDir("", "tests")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(f)
path := filepath.Join(f, "config.yaml")
err = os.WriteFile(path, []byte(yaml), 0655)
Expect(err).ToNot(HaveOccurred())
err = Validate(path)
Expect(err).To(MatchError("missing #cloud-config header"))
})
})
Context("with an invalid rule", func() {
BeforeEach(func() {
yaml = `#cloud-config
users:
- name: 007
passwd: kairos`
})
It("is fails", func() {
f, err := ioutil.TempDir("", "tests")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(f)
path := filepath.Join(f, "config.yaml")
err = os.WriteFile(path, []byte(yaml), 0655)
Expect(err).ToNot(HaveOccurred())
err = Validate(path)
Expect(err.Error()).To(MatchRegexp("expected string, but got number"))
})
})
})
})