mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-06-03 01:44:53 +00:00
Skip dirs in copyArtifactSetRole and replace only basename
otherwise we get the error: ``` panic: open /efi/EFI/kairos/passive.efi.extra.d: is a directory ``` and we may replace parts of the path that we shouldn't. Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
parent
fd2f83dc55
commit
046f6879ff
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
||||||
@ -35,9 +36,17 @@ func overwriteArtifactSetRole(fs v1.FS, dir, oldRole, newRole string, logger sdk
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copy the source file but rename the base name to as
|
// copy the source file but rename the base name to as
|
||||||
func copyArtifact(source, oldRole, newRole string) (string, error) {
|
func copyArtifact(fs v1.FS, source, oldRole, newRole string) (string, error) {
|
||||||
newName := strings.ReplaceAll(source, oldRole, newRole)
|
dir := filepath.Dir(source)
|
||||||
return newName, copyFile(source, newName)
|
base := filepath.Base(source)
|
||||||
|
|
||||||
|
// Replace the substring in the base name
|
||||||
|
newBase := strings.ReplaceAll(base, oldRole, newRole)
|
||||||
|
|
||||||
|
// Join the directory and the new base name
|
||||||
|
newName := filepath.Join(dir, newBase)
|
||||||
|
|
||||||
|
return newName, fsutils.Copy(fs, source, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeArtifactSetWithRole(fs v1.FS, artifactDir, role string) error {
|
func removeArtifactSetWithRole(fs v1.FS, artifactDir, role string) error {
|
||||||
@ -55,11 +64,16 @@ func copyArtifactSetRole(fs v1.FS, artifactDir, oldRole, newRole string, logger
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(info.Name(), oldRole) {
|
if !strings.HasPrefix(info.Name(), oldRole) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
newPath, err := copyArtifact(path, oldRole, newRole)
|
newPath, err := copyArtifact(fs, path, oldRole, newRole)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("copying artifact from %s to %s: %w", path, newPath, err)
|
return fmt.Errorf("copying artifact from %s to %s: %w", path, newPath, err)
|
||||||
}
|
}
|
||||||
|
64
pkg/uki/common_test.go
Normal file
64
pkg/uki/common_test.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package uki
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
cnst "github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
||||||
|
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
|
||||||
|
sdkTypes "github.com/kairos-io/kairos-sdk/types"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo/v2"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
"github.com/twpayne/go-vfs/v4"
|
||||||
|
"github.com/twpayne/go-vfs/v4/vfst"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Common functions tests", func() {
|
||||||
|
Describe("copyArtifactSetRole", func() {
|
||||||
|
var fs vfs.FS
|
||||||
|
var err error
|
||||||
|
var memLog *bytes.Buffer
|
||||||
|
var logger sdkTypes.KairosLogger
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
fs, _, err = vfst.NewTestFS(map[string]interface{}{})
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
logger = sdkTypes.NewBufferLogger(memLog)
|
||||||
|
logger.SetLevel("debug")
|
||||||
|
|
||||||
|
Expect(fsutils.MkdirAll(fs, "/active", cnst.DirPerm)).ToNot(HaveOccurred())
|
||||||
|
Expect(fsutils.MkdirAll(fs, "/other", cnst.DirPerm)).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
f, err := fs.Create("/other/active.efi")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
_, err = os.Stat(f.Name())
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
f, err = fs.Create("/other/other.efi")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
_, err = os.Stat(f.Name())
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("skips directories", func() {
|
||||||
|
err = copyArtifactSetRole(fs, "/", "active", "passive", logger)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("replaces only the base file name", func() {
|
||||||
|
err = copyArtifactSetRole(fs, "/other", "other", "newother", logger)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
glob, _ := fs.Glob("/other/*")
|
||||||
|
Expect(glob).To(HaveExactElements([]string{
|
||||||
|
"/other/active.efi",
|
||||||
|
"/other/newother.efi",
|
||||||
|
"/other/other.efi",
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
13
pkg/uki/suite_test.go
Normal file
13
pkg/uki/suite_test.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package uki_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo/v2"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestActionSuite(t *testing.T) {
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "Uki test suite")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user