mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-05-06 23:38:02 +00:00
Tests and improve createExtraDirsInRootfs (#111)
This commit is contained in:
parent
8feaf648e0
commit
26140a46d3
pkg/action
@ -44,12 +44,17 @@ func ChrootHook(config *config.Config, hook string, chrootDir string, bindMounts
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createExtraDirsInRootfs(cfg *config.Config, extradirs []string, target string) {
|
func createExtraDirsInRootfs(cfg *config.Config, extradirs []string, target string) {
|
||||||
|
if target == "" {
|
||||||
|
cfg.Logger.Warn("Empty target for extra rootfs dirs, not doing anything")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, d := range extradirs {
|
for _, d := range extradirs {
|
||||||
if exists, _ := fsutils.Exists(cfg.Fs, filepath.Join(target, d)); !exists {
|
if exists, _ := fsutils.Exists(cfg.Fs, filepath.Join(target, d)); !exists {
|
||||||
cfg.Logger.Debugf("Creating extra dir %s under %s", d, target)
|
cfg.Logger.Debugf("Creating extra dir %s under %s", d, target)
|
||||||
err := cfg.Fs.Mkdir(filepath.Join(target, d), cnst.DirPerm)
|
err := fsutils.MkdirAll(cfg.Fs, filepath.Join(target, d), cnst.DirPerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg.Logger.Warnf("Failure creating extra dir %s in rootfs at %s", d, target)
|
cfg.Logger.Warnf("Failure creating extra dir %s under %s", d, target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
125
pkg/action/common_test.go
Normal file
125
pkg/action/common_test.go
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
package action
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
agentConfig "github.com/kairos-io/kairos-agent/v2/pkg/config"
|
||||||
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
||||||
|
v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks"
|
||||||
|
"github.com/kairos-io/kairos-sdk/collector"
|
||||||
|
. "github.com/onsi/ginkgo/v2"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
"github.com/twpayne/go-vfs"
|
||||||
|
"github.com/twpayne/go-vfs/vfst"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Common action tests", func() {
|
||||||
|
Describe("createExtraDirsInRootfs", func() {
|
||||||
|
var config *agentConfig.Config
|
||||||
|
var fs vfs.FS
|
||||||
|
var logger v1.Logger
|
||||||
|
var runner *v1mock.FakeRunner
|
||||||
|
var mounter *v1mock.ErrorMounter
|
||||||
|
var syscall *v1mock.FakeSyscall
|
||||||
|
var client *v1mock.FakeHTTPClient
|
||||||
|
var cloudInit *v1mock.FakeCloudInitRunner
|
||||||
|
var cleanup func()
|
||||||
|
var memLog *bytes.Buffer
|
||||||
|
var extractor *v1mock.FakeImageExtractor
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
runner = v1mock.NewFakeRunner()
|
||||||
|
syscall = &v1mock.FakeSyscall{}
|
||||||
|
mounter = v1mock.NewErrorMounter()
|
||||||
|
client = &v1mock.FakeHTTPClient{}
|
||||||
|
memLog = &bytes.Buffer{}
|
||||||
|
logger = v1.NewBufferLogger(memLog)
|
||||||
|
extractor = v1mock.NewFakeImageExtractor(logger)
|
||||||
|
logger.SetLevel(v1.DebugLevel())
|
||||||
|
var err error
|
||||||
|
fs, cleanup, err = vfst.NewTestFS(map[string]interface{}{})
|
||||||
|
Expect(err).Should(BeNil())
|
||||||
|
|
||||||
|
cloudInit = &v1mock.FakeCloudInitRunner{}
|
||||||
|
config = agentConfig.NewConfig(
|
||||||
|
agentConfig.WithFs(fs),
|
||||||
|
agentConfig.WithRunner(runner),
|
||||||
|
agentConfig.WithLogger(logger),
|
||||||
|
agentConfig.WithMounter(mounter),
|
||||||
|
agentConfig.WithSyscall(syscall),
|
||||||
|
agentConfig.WithClient(client),
|
||||||
|
agentConfig.WithCloudInitRunner(cloudInit),
|
||||||
|
agentConfig.WithImageExtractor(extractor),
|
||||||
|
)
|
||||||
|
config.Install = &agentConfig.Install{}
|
||||||
|
config.Bundles = agentConfig.Bundles{}
|
||||||
|
config.Config = collector.Config{}
|
||||||
|
})
|
||||||
|
|
||||||
|
AfterEach(func() {
|
||||||
|
cleanup()
|
||||||
|
})
|
||||||
|
|
||||||
|
It("creates the dirs", func() {
|
||||||
|
extraDirs := []string{"one", "/two"}
|
||||||
|
rootDir := "/"
|
||||||
|
createExtraDirsInRootfs(config, extraDirs, rootDir)
|
||||||
|
for _, d := range extraDirs {
|
||||||
|
Expect(memLog).To(ContainSubstring(fmt.Sprintf("Creating extra dir %s under %s", d, rootDir)))
|
||||||
|
stat, err := fs.Stat(filepath.Join(rootDir, d))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(stat.IsDir()).To(BeTrue())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
It("doesnt create the dirs if they already exists", func() {
|
||||||
|
extraDirs := []string{"one", "/two"}
|
||||||
|
rootDir := "/"
|
||||||
|
for _, d := range extraDirs {
|
||||||
|
err := fs.Mkdir(filepath.Join(rootDir, d), os.ModeDir)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
stat, err := fs.Stat(filepath.Join(rootDir, d))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(stat.IsDir()).To(BeTrue())
|
||||||
|
}
|
||||||
|
createExtraDirsInRootfs(config, extraDirs, rootDir)
|
||||||
|
for _, d := range extraDirs {
|
||||||
|
// No message of creation
|
||||||
|
Expect(memLog).ToNot(ContainSubstring(fmt.Sprintf("Creating extra dir %s under %s", d, rootDir)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
It("Crates dirs with subdirs", func() {
|
||||||
|
extraDirs := []string{"/one/two/", "/three/four"}
|
||||||
|
rootDir := "/"
|
||||||
|
createExtraDirsInRootfs(config, extraDirs, rootDir)
|
||||||
|
for _, d := range extraDirs {
|
||||||
|
Expect(memLog).To(ContainSubstring(fmt.Sprintf("Creating extra dir %s under %s", d, rootDir)))
|
||||||
|
stat, err := fs.Stat(filepath.Join(rootDir, d))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(stat.IsDir()).To(BeTrue())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
It("Doesnt nothing with an empty target", func() {
|
||||||
|
extraDirs := []string{"/one/two/", "/three/four"}
|
||||||
|
rootDir := ""
|
||||||
|
createExtraDirsInRootfs(config, extraDirs, rootDir)
|
||||||
|
Expect(memLog).To(ContainSubstring("Empty target for extra rootfs dirs, not doing anything"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("Fails with a non valid rootdir", func() {
|
||||||
|
extraDirs := []string{"/one/two/", "/three/four"}
|
||||||
|
rootDir := "@&^$$W#@#$"
|
||||||
|
createExtraDirsInRootfs(config, extraDirs, rootDir)
|
||||||
|
for _, d := range extraDirs {
|
||||||
|
Expect(memLog).To(ContainSubstring(fmt.Sprintf("Creating extra dir %s under %s", d, rootDir)))
|
||||||
|
Expect(memLog).To(ContainSubstring(fmt.Sprintf("Failure creating extra dir %s under %s", d, rootDir)))
|
||||||
|
_, err := fs.Stat(filepath.Join(rootDir, d))
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user