Skip to content

Commit

Permalink
Merge pull request #509 from kairos-io/add-test-and-fix-recovery-upgrade
Browse files Browse the repository at this point in the history
Add test and fix recovery upgrade
  • Loading branch information
jimmykarily authored Sep 4, 2024
2 parents f56b7a9 + 774547e commit 76899b7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ require (

require (
github.com/distribution/reference v0.6.0
github.com/google/go-github/v64 v64.0.0
github.com/google/go-github/v64 v64.0.0
github.com/twpayne/go-vfs/v5 v5.0.4
github.com/twpayne/go-vfs/v5 v5.0.4
github.com/google/go-github/v63 v63.0.0
github.com/twpayne/go-vfs/v4 v4.3.0
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ github.com/google/go-containerregistry v0.20.2 h1:B1wPJ1SN/S7pB+ZAimcciVD+r+yV/l
github.com/google/go-containerregistry v0.20.2/go.mod h1:z38EKdKh4h7IP2gSfUUqEvalZBqs6AoLeWfUy34nQC8=
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
github.com/google/go-github/v64 v64.0.0/go.mod h1:xB3vqMQNdHzilXBiO2I+M7iEFtHf+DP/omBOv6tQzVo=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
Expand Down Expand Up @@ -533,7 +532,6 @@ github.com/tredoe/osutil v1.5.0 h1:UGVxbbHRoZi8xXVmbNZ2vgG6XoJ15ndE4LniiQ3rJKg=
github.com/tredoe/osutil v1.5.0/go.mod h1:TEzphzUUunysbdDRfdOgqkg10POQbnfIPV50ynqOfIg=
github.com/twpayne/go-vfs/v4 v4.3.0 h1:rTqFzzOQ/6ESKTSiwVubHlCBedJDOhQyVSnw8rQNZhU=
github.com/twpayne/go-vfs/v4 v4.3.0/go.mod h1:tq2UVhnUepesc0lSnPJH/jQ8HruGhzwZe2r5kDFpEIw=
github.com/twpayne/go-vfs/v5 v5.0.4/go.mod h1:zTPFJUbgsEMFNSWnWQlLq9wh4AN83edZzx3VXbxrS1w=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
Expand Down
59 changes: 59 additions & 0 deletions pkg/action/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ package action_test

import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/kairos-io/kairos-sdk/collector"
sdkTypes "github.com/kairos-io/kairos-sdk/types"

"github.com/kairos-io/kairos-agent/v2/internal/agent"
agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"

Expand Down Expand Up @@ -50,8 +55,11 @@ var _ = Describe("Runtime Actions", func() {
var memLog *bytes.Buffer
var ghwTest v1mock.GhwMock
var extractor *v1mock.FakeImageExtractor
var dummySourceFile string
var dummySourceSizeMb int64

BeforeEach(func() {
dummySourceSizeMb = 20
runner = v1mock.NewFakeRunner()
syscall = &v1mock.FakeSyscall{}
mounter = v1mock.NewErrorMounter()
Expand Down Expand Up @@ -80,6 +88,7 @@ var _ = Describe("Runtime Actions", func() {

AfterEach(func() {
cleanup()
os.RemoveAll(dummySourceFile)
})

Describe("Upgrade Action", Label("upgrade"), func() {
Expand Down Expand Up @@ -142,6 +151,34 @@ var _ = Describe("Runtime Actions", func() {
AfterEach(func() {
ghwTest.Clean()
})
It("calculates the recovery source size correctly", func() {
dummySourceFile = createDummyFile(fs, dummySourceSizeMb)
upgradeConfig := agent.ExtraConfigUpgrade{}
upgradeConfig.Upgrade.Entry = constants.BootEntryRecovery
upgradeConfig.Upgrade.RecoverySystem.URI = fmt.Sprintf("file:%s", dummySourceFile)
d, err := json.Marshal(upgradeConfig)
Expect(err).ToNot(HaveOccurred())
cliConfig := string(d)

config, err := agentConfig.Scan(collector.Readers(strings.NewReader(cliConfig)))
Expect(err).ToNot(HaveOccurred())

agentConfig.WithFs(fs)(config)
agentConfig.WithRunner(runner)(config)
agentConfig.WithLogger(logger)(config)
agentConfig.WithMounter(mounter)(config)
agentConfig.WithSyscall(syscall)(config)
agentConfig.WithClient(client)(config)
agentConfig.WithCloudInitRunner(cloudInit)(config)
agentConfig.WithImageExtractor(extractor)(config)
agentConfig.WithPlatform("linux/amd64")(config)
config.ImageExtractor = extractor

spec, err = agentConfig.NewUpgradeSpec(config)
Expect(err).ShouldNot(HaveOccurred())
Expect(spec.Entry).To(Equal(constants.BootEntryRecovery))
Expect(spec.Recovery.Size).To(Equal(uint(100 + dummySourceSizeMb))) // We adding 100Mb on top
})
Describe(fmt.Sprintf("Booting from %s", constants.ActiveLabel), Label("active_label"), func() {
var err error
BeforeEach(func() {
Expand Down Expand Up @@ -623,3 +660,25 @@ var _ = Describe("Runtime Actions", func() {
})
})
})

func createDummyFile(fs vfs.FS, sizeMb int64) string {
fileSize := int64(sizeMb * 1024 * 1024)

tmpFile, err := os.CreateTemp("", "dummyfile_*.tmp")
Expect(err).ToNot(HaveOccurred())
tmpName := tmpFile.Name()
tmpFile.Close()
os.RemoveAll(tmpName)

dir := filepath.Dir(tmpName)
err = fs.Mkdir(dir, os.ModePerm)
Expect(err).ToNot(HaveOccurred())

f, err := fs.Create(tmpName)
Expect(err).ShouldNot(HaveOccurred())
err = f.Truncate(fileSize)
Expect(err).ShouldNot(HaveOccurred())
f.Close()

return tmpName
}
5 changes: 3 additions & 2 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,10 @@ func GetSourceSize(config *Config, source *v1.ImageSource) (int64, error) {

case source.IsFile():
file, err := config.Fs.Stat(source.Value())
if err == nil {
size = file.Size()
if err != nil {
return size, err
}
size = file.Size()
}
// Normalize size to Mb before returning and add 100Mb to round the size from bytes to mb+extra files like grub stuff
if size != 0 {
Expand Down

0 comments on commit 76899b7

Please sign in to comment.