mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-17 07:17:41 +00:00
More config tests (#97)
This commit is contained in:
@@ -573,8 +573,8 @@ func ReadConfigRunFromAgentConfig(c *agentConfig.Config) (*v1.RunConfig, error)
|
|||||||
return cfg, err
|
return cfg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// readSpecFromCloudConfig returns a v1.Spec for the given spec
|
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
|
||||||
func readSpecFromCloudConfig(r *v1.RunConfig, spec string) (v1.Spec, error) {
|
func ReadSpecFromCloudConfig(r *v1.RunConfig, spec string) (v1.Spec, error) {
|
||||||
var sp v1.Spec
|
var sp v1.Spec
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -614,7 +614,7 @@ func readConfigAndSpecFromAgentConfig(c *agentConfig.Config, action string) (*v1
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
spec, err := readSpecFromCloudConfig(runConfig, action)
|
spec, err := ReadSpecFromCloudConfig(runConfig, action)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,12 @@ limitations under the License.
|
|||||||
package elementalConfig_test
|
package elementalConfig_test
|
||||||
|
|
||||||
import (
|
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"
|
"k8s.io/mount-utils"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/jaypipes/ghw/pkg/block"
|
"github.com/jaypipes/ghw/pkg/block"
|
||||||
@@ -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())
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user