Skip to content

Commit ed73def

Browse files
committed
Adding unit tests for EmbedKargsIntoBootImage function
1 parent 464c769 commit ed73def

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

pkg/isoeditor/kargs_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package isoeditor
22

33
import (
44
"errors"
5+
"os"
6+
"path/filepath"
57

68
. "github.com/onsi/ginkgo"
79
. "github.com/onsi/gomega"
@@ -153,3 +155,139 @@ menuentry 'Fedora CoreOS (Live)' --class fedora --class gnu-linux --class gnu --
153155
})
154156
})
155157
})
158+
159+
// Tests for EmbedKargsIntoBootImage
160+
var _ = Describe("EmbedKargsIntoBootImage", func() {
161+
var (
162+
baseDir string // acts as baseIsoPath (where /coreos/kargs.json is read from)
163+
stagingDir string // acts as stagingIsoPath (where files are written)
164+
)
165+
166+
writeBaseKargsJSON := func(json string) {
167+
p := filepath.Join(baseDir, "coreos", "kargs.json")
168+
Expect(os.MkdirAll(filepath.Dir(p), 0755)).To(Succeed())
169+
Expect(os.WriteFile(p, []byte(json), 0644)).To(Succeed())
170+
}
171+
172+
// helper to create a target file inside staging dir with a given size (filled with zeros)
173+
createStagingFile := func(rel string, size int) string {
174+
full := filepath.Join(stagingDir, rel)
175+
Expect(os.MkdirAll(filepath.Dir(full), 0755)).To(Succeed())
176+
buf := make([]byte, size)
177+
Expect(os.WriteFile(full, buf, 0644)).To(Succeed())
178+
return full
179+
}
180+
181+
BeforeEach(func() {
182+
var err error
183+
baseDir, err = os.MkdirTemp("", "iso-base")
184+
Expect(err).ToNot(HaveOccurred())
185+
stagingDir, err = os.MkdirTemp("", "iso-staging")
186+
Expect(err).ToNot(HaveOccurred())
187+
})
188+
189+
AfterEach(func() {
190+
os.RemoveAll(baseDir)
191+
os.RemoveAll(stagingDir)
192+
})
193+
194+
It("fails when /coreos/kargs.json cannot be read from base ISO path", func() {
195+
// Do NOT create base coreos/kargs.json
196+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "any")
197+
Expect(err).To(HaveOccurred())
198+
Expect(err.Error()).To(ContainSubstring("failed to read kargs config"))
199+
})
200+
201+
It("fails when /coreos/kargs.json is malformed", func() {
202+
writeBaseKargsJSON(`{ not valid json }`)
203+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "newKargs")
204+
Expect(err).To(HaveOccurred())
205+
Expect(err.Error()).To(ContainSubstring("failed to parse"))
206+
})
207+
208+
It("fails when no kargs file entries are present", func() {
209+
writeBaseKargsJSON(`{"default":"abc","files":[],"size":10}`)
210+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "extra")
211+
Expect(err).To(HaveOccurred())
212+
Expect(err.Error()).To(ContainSubstring("no kargs file entries"))
213+
})
214+
215+
It("fails when a listed staging file does not exist", func() {
216+
writeBaseKargsJSON(`{
217+
"default": "abc",
218+
"files": [{"path":"cdboot.img","offset":10}],
219+
"size": 100
220+
}`)
221+
// Don't create cdboot.img in staging
222+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "zzz")
223+
Expect(err).To(HaveOccurred())
224+
Expect(err.Error()).To(ContainSubstring("does not exist"))
225+
})
226+
227+
It("fails when kargs length exceeds configured Size", func() {
228+
// default=3 chars, custom=9 chars -> total 12 > size 10
229+
writeBaseKargsJSON(`{
230+
"default": "abc",
231+
"files": [{"path":"cdboot.img","offset":0}],
232+
"size": 10
233+
}`)
234+
_ = createStagingFile("cdboot.img", 32)
235+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "toolonggg")
236+
Expect(err).To(HaveOccurred())
237+
Expect(err.Error()).To(ContainSubstring("exceeds available field size"))
238+
})
239+
240+
It("fails when size is not provided but available space (by file length) is insufficient", func() {
241+
// Size=0 means use file size heuristic:
242+
// file size 8, offset=4, default len=3 -> append offset = 7, remaining = 1
243+
// total needed default+custom = 3 + 3 = 6 > 1 -> error
244+
writeBaseKargsJSON(`{
245+
"default": "abc",
246+
"files": [{"path":"cdboot.img","offset":4}],
247+
"size": 0
248+
}`)
249+
_ = createStagingFile("cdboot.img", 8)
250+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "xyz")
251+
Expect(err).To(HaveOccurred())
252+
Expect(err.Error()).To(ContainSubstring("exceeds available field size"))
253+
})
254+
255+
It("fails when the staging path exists but is a directory (open for write fails)", func() {
256+
writeBaseKargsJSON(`{
257+
"default": "abc",
258+
"files": [{"path":"cdboot.img","offset":5}],
259+
"size": 100
260+
}`)
261+
// Create a directory named cdboot.img
262+
Expect(os.MkdirAll(filepath.Join(stagingDir, "cdboot.img"), 0755)).To(Succeed())
263+
err := EmbedKargsIntoBootImage(baseDir, stagingDir, "ok")
264+
Expect(err).To(HaveOccurred())
265+
Expect(err.Error()).To(ContainSubstring("failed to open target file"))
266+
})
267+
268+
It("successfully embeds kargs into TWO different files with different offsets", func() {
269+
// default is "abc" (len=3)
270+
writeBaseKargsJSON(`{
271+
"default": "abc",
272+
"files": [
273+
{"path":"cdboot.img","offset":10},
274+
{"path":"coreos/kargs.json","offset":20}
275+
],
276+
"size": 1024
277+
}`)
278+
cdboot := createStagingFile("cdboot.img", 256)
279+
kargsBin := createStagingFile("coreos/kargs.json", 256)
280+
281+
custom := "dual-file=ok"
282+
Expect(EmbedKargsIntoBootImage(baseDir, stagingDir, custom)).To(Succeed())
283+
284+
// Verify writes at offset + len(default)
285+
cd, err := os.ReadFile(cdboot)
286+
Expect(err).ToNot(HaveOccurred())
287+
Expect(string(cd[10+3 : 10+3+len(custom)])).To(Equal(custom))
288+
289+
kb, err := os.ReadFile(kargsBin)
290+
Expect(err).ToNot(HaveOccurred())
291+
Expect(string(kb[20+3 : 20+3+len(custom)])).To(Equal(custom))
292+
})
293+
})

0 commit comments

Comments
 (0)