mirror of
https://github.com/kairos-io/kcrypt.git
synced 2025-07-11 14:23:06 +00:00
103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
|
package partition_info_test
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
|
||
|
. "github.com/onsi/ginkgo/v2"
|
||
|
. "github.com/onsi/gomega"
|
||
|
|
||
|
pi "github.com/kairos-io/kcrypt/pkg/partition_info"
|
||
|
)
|
||
|
|
||
|
var _ = Describe("Partition Info file parsing", func() {
|
||
|
Describe("ParsePartitionInfoFile", func() {
|
||
|
var file string
|
||
|
|
||
|
BeforeEach(func() {
|
||
|
file = "../../tests/assets/partition_info.yaml"
|
||
|
})
|
||
|
|
||
|
It("parses the file correctly", func() {
|
||
|
info, err := pi.ParsePartitionInfoFile(file)
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
Expect(len(info)).To(Equal(2))
|
||
|
Expect(info["COS_PERSISTENT"]).To(Equal("some_uuid_1"))
|
||
|
Expect(info["COS_OTHER"]).To(Equal("some_uuid_2"))
|
||
|
})
|
||
|
})
|
||
|
|
||
|
Describe("UpdatePartitionInfoFile", func() {
|
||
|
var file *os.File
|
||
|
var err error
|
||
|
|
||
|
BeforeEach(func() {
|
||
|
file, err = ioutil.TempFile("", "parition-info.*.yaml")
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
fileContents := `
|
||
|
- label: TO_DELETE
|
||
|
uuid: old_uuid_1
|
||
|
- label: TO_BE_UPDATED
|
||
|
uuid: old_uuid_2
|
||
|
`
|
||
|
_, err = file.Write([]byte(fileContents))
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
})
|
||
|
|
||
|
AfterEach(func() {
|
||
|
os.Remove(file.Name())
|
||
|
})
|
||
|
|
||
|
It("Updates the partition file correctly", func() {
|
||
|
partitionInfo := pi.PartitionInfo{
|
||
|
"TO_BE_UPDATED": "new_uuid_1",
|
||
|
"NEW_PARTITION": "new_uuid_2",
|
||
|
}
|
||
|
|
||
|
err = pi.UpdatePartitionInfoFile(partitionInfo, file.Name())
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
dat, err := os.ReadFile(file.Name())
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
expectedContent := `NEW_PARTITION: new_uuid_2
|
||
|
TO_BE_UPDATED: new_uuid_1
|
||
|
`
|
||
|
Expect(string(dat)).To(Equal(expectedContent))
|
||
|
})
|
||
|
})
|
||
|
|
||
|
Describe("UpdatePartitionLabelMapping", func() {
|
||
|
var file *os.File
|
||
|
var err error
|
||
|
|
||
|
BeforeEach(func() {
|
||
|
file, err = ioutil.TempFile("", "parition-info.*.yaml")
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
_, err = file.Write([]byte("TO_KEEP: old_uuid_1"))
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
})
|
||
|
|
||
|
AfterEach(func() {
|
||
|
os.Remove(file.Name())
|
||
|
})
|
||
|
|
||
|
It("Updates the file correctly from a `kcrypt encrypt` return value", func() {
|
||
|
partitionData := "TO_BE_ADDED:some_name:new_uuid"
|
||
|
|
||
|
err = pi.UpdatePartitionLabelMapping(partitionData, file.Name())
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
dat, err := os.ReadFile(file.Name())
|
||
|
Expect(err).ToNot(HaveOccurred())
|
||
|
|
||
|
expectedContent := `TO_BE_ADDED: new_uuid
|
||
|
TO_KEEP: old_uuid_1
|
||
|
`
|
||
|
Expect(string(dat)).To(Equal(expectedContent))
|
||
|
})
|
||
|
})
|
||
|
})
|