Simplify code and fix tests

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis 2024-08-28 13:09:27 +03:00
parent 9a06c5a2a7
commit 00597f5ecf
No known key found for this signature in database
GPG Key ID: 286DCAFD2C97DDE3
2 changed files with 45 additions and 16 deletions

View File

@ -19,6 +19,7 @@ package action_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
sdkTypes "github.com/kairos-io/kairos-sdk/types"
@ -76,6 +77,16 @@ var _ = Describe("Runtime Actions", func() {
agentConfig.WithImageExtractor(extractor),
agentConfig.WithPlatform("linux/amd64"),
)
source := v1.NewFileSrc(createDummyFile(10))
config.Install.Recovery = v1.Image{
File: "",
Size: constants.ImgSize,
Label: constants.ActiveLabel,
FS: constants.LinuxImgFs,
MountPoint: constants.TransitionDir,
Source: source,
}
})
AfterEach(func() {
@ -623,3 +634,28 @@ var _ = Describe("Runtime Actions", func() {
})
})
})
func createDummyFile(sizeMb int64) string {
fileSize := int64(sizeMb * 1024 * 1024)
tmpFile, err := os.CreateTemp("", "dummyfile_*.tmp")
Expect(err).ToNot(HaveOccurred())
defer tmpFile.Close()
dummyData := []byte("1234567890ABCDEF")
dummyLength := int64(len(dummyData))
var written int64
for written < fileSize {
bytesToWrite := dummyLength
if written+bytesToWrite > fileSize {
bytesToWrite = fileSize - written
}
n, err := tmpFile.Write(dummyData[:bytesToWrite])
Expect(err).ToNot(HaveOccurred())
written += int64(n)
}
return tmpFile.Name()
}

View File

@ -277,16 +277,11 @@ func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) {
recMnt = constants.TransitionDir
}
upgradeRecoverySystemUri, err := cfg.Query("upgrade.\"recovery-system\".uri")
upgradeRecoverySystemUri = strings.TrimRight(upgradeRecoverySystemUri, "\n")
if err != nil {
return nil, fmt.Errorf("failed to found recovery upgrade source: %w", err)
recoverySrc := cfg.Install.Recovery.Source
if recoverySrc == nil {
recoverySrc = v1.NewEmptySrc()
}
recoverySrc, err := v1.NewSrcFromURI(upgradeRecoverySystemUri)
if err != nil {
return nil, fmt.Errorf("failed to parse recovery upgrade source uri: %w", err)
}
recovery = v1.Image{
File: filepath.Join(ep.Recovery.MountPoint, "cOS", constants.TransitionImgFile),
Size: constants.ImgSize,
@ -301,25 +296,23 @@ func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) {
if ep.State.MountPoint == "" {
ep.State.MountPoint = constants.StateDir
}
upgradeSystemUri, err := cfg.Query("upgrade.system.uri")
upgradeSystemUri = strings.TrimRight(upgradeSystemUri, "\n")
systemSrc := cfg.Install.Active.Source
if systemSrc == nil {
systemSrc = v1.NewEmptySrc()
}
if err != nil {
return nil, fmt.Errorf("failed to found upgrade source: %w", err)
}
src, err := v1.NewSrcFromURI(upgradeSystemUri)
if err != nil {
return nil, fmt.Errorf("failed to parse upgrade source uri: %w", err)
}
active = v1.Image{
File: filepath.Join(ep.State.MountPoint, "cOS", constants.TransitionImgFile),
Size: constants.ImgSize,
Label: constants.ActiveLabel,
FS: constants.LinuxImgFs,
MountPoint: constants.TransitionDir,
Source: src,
Source: systemSrc,
}
passive = v1.Image{