Skip to content

Commit

Permalink
More config tests (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Jul 21, 2023
1 parent 922d35b commit fa8d212
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/elementalConfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ func ReadConfigRunFromAgentConfig(c *agentConfig.Config) (*v1.RunConfig, error)
return cfg, err
}

// readSpecFromCloudConfig returns a v1.Spec for the given spec
func readSpecFromCloudConfig(r *v1.RunConfig, spec string) (v1.Spec, error) {
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
func ReadSpecFromCloudConfig(r *v1.RunConfig, spec string) (v1.Spec, error) {
var sp v1.Spec
var err error

Expand Down Expand Up @@ -614,7 +614,7 @@ func readConfigAndSpecFromAgentConfig(c *agentConfig.Config, action string) (*v1
if err != nil {
return nil, nil, err
}
spec, err := readSpecFromCloudConfig(runConfig, action)
spec, err := ReadSpecFromCloudConfig(runConfig, action)
if err != nil {
return nil, nil, err
}
Expand Down
131 changes: 131 additions & 0 deletions pkg/elementalConfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ limitations under the License.
package elementalConfig_test

import (
"fmt"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/sanity-io/litter"
"k8s.io/mount-utils"
"os"
"path/filepath"

"github.com/jaypipes/ghw/pkg/block"
Expand Down Expand Up @@ -397,5 +402,131 @@ var _ = Describe("Types", Label("types", "config"), func() {
})
})
})
Describe("Config from cloudconfig", Label("cloud-config"), func() {
var bootedFrom string
var dir string
var ghwTest v1mock.GhwMock

BeforeEach(func() {
bootedFrom = ""
runner.SideEffect = func(cmd string, args ...string) ([]byte, error) {
switch cmd {
case "cat":
return []byte(bootedFrom), nil
default:
return []byte{}, nil
}
}

dir, err = os.MkdirTemp("", "test-config")
Expect(err).ToNot(HaveOccurred())
ccdata := []byte(`#cloud-config
debug: true
install:
grub-entry-name: "MyCustomOS"
system:
size: 666
reset:
reset-persistent: true
reset-oem: true
passive:
label: MY_LABEL
upgrade:
recovery: true
system:
uri: docker:test/image:latest
cloud-init-paths:
- /what
`)
err = os.WriteFile(filepath.Join(dir, "cc.yaml"), ccdata, os.ModePerm)
Expect(err).ToNot(HaveOccurred())

mainDisk := block.Disk{
Name: "device",
Partitions: []*block.Partition{
{
Name: "device1",
FilesystemLabel: constants.EfiLabel,
Type: "vfat",
},
{
Name: "device2",
FilesystemLabel: constants.OEMLabel,
Type: "ext4",
},
{
Name: "device3",
FilesystemLabel: constants.RecoveryLabel,
Type: "ext4",
},
{
Name: "device4",
FilesystemLabel: constants.StateLabel,
Type: "ext4",
},
{
Name: "device5",
FilesystemLabel: constants.PersistentLabel,
Type: "ext4",
},
},
}
ghwTest = v1mock.GhwMock{}
ghwTest.AddDisk(mainDisk)
ghwTest.CreateDevices()
})

AfterEach(func() {
os.RemoveAll(dir)
ghwTest.Clean()
})
It("Reads properly the cloud config for install", func() {
cfg, err := config.Scan(collector.Directories([]string{dir}...))
Expect(err).ToNot(HaveOccurred())
installConfig, installSpec, err := elementalConfig.ReadInstallConfigFromAgentConfig(cfg)
Expect(err).ToNot(HaveOccurred())
Expect(installConfig.Debug).To(BeTrue())
Expect(installSpec.GrubDefEntry).To(Equal("MyCustomOS"))
Expect(installSpec.Active.Size).To(Equal(uint(666)))
Expect(installConfig.CloudInitPaths).To(ContainElement("/what"))

})
It("Reads properly the cloud config for reset", func() {
bootedFrom = constants.SystemLabel
cfg, err := config.Scan(collector.Directories([]string{dir}...))
runconfig, err := elementalConfig.ReadConfigRunFromAgentConfig(cfg)
Expect(err).ToNot(HaveOccurred())
// Override the runconfig with our test params
runconfig.Runner = runner
runconfig.Logger = logger
runconfig.Fs = fs
runconfig.Mounter = mounter
runconfig.CloudInitRunner = ci
spec, err := elementalConfig.ReadSpecFromCloudConfig(runconfig, "reset")
Expect(err).ToNot(HaveOccurred())
resetSpec := spec.(*v1.ResetSpec)
fmt.Print(litter.Sdump(resetSpec))
Expect(resetSpec.FormatPersistent).To(BeTrue())
Expect(resetSpec.FormatOEM).To(BeTrue())
Expect(resetSpec.Passive.Label).To(Equal("MY_LABEL"))
})
It("Reads properly the cloud config for upgrade", func() {
cfg, err := config.Scan(collector.Directories([]string{dir}...))
runconfig, err := elementalConfig.ReadConfigRunFromAgentConfig(cfg)
Expect(err).ToNot(HaveOccurred())
// Override the runconfig with our test params
runconfig.Runner = runner
runconfig.Logger = logger
runconfig.Fs = fs
runconfig.Mounter = mounter
runconfig.CloudInitRunner = ci
spec, err := elementalConfig.ReadSpecFromCloudConfig(runconfig, "upgrade")
Expect(err).ToNot(HaveOccurred())
upgradeSpec := spec.(*v1.UpgradeSpec)
fmt.Print(litter.Sdump(upgradeSpec.Active.Source))
Expect(upgradeSpec.RecoveryUpgrade).To(BeTrue())
})

})
})
})

0 comments on commit fa8d212

Please sign in to comment.