mirror of
https://github.com/kairos-io/kcrypt.git
synced 2025-09-16 23:29:11 +00:00
Create parition info parsing library
to be used both here (when trying to find the partition UUID using a label) and on the kairos side when updating the file after calling kcrypt to encrypt a partition (which causes the UUID to change). Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
102
pkg/partition_info/partition_info_test.go
Normal file
102
pkg/partition_info/partition_info_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
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))
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user