Skip to content

Commit e6f7e4c

Browse files
committed
Move truncate to native golang
1 parent 0bc49aa commit e6f7e4c

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

mantle/kola/tests/ignition/qemufailure.go

+36-58
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ func init() {
4141
Tags: []string{"ignition"},
4242
})
4343
register.RegisterTest(&register.Test{
44-
Name: "coreos.ignition.uniquebootfs",
45-
Description: "Verify there is only one bootfs.",
44+
Name: "coreos.unique.boot.failure",
45+
Description: "Verify boot fails if there are multiple boot filesystems.",
4646
Run: runBootfsFailure,
47-
ClusterSize: 0,
48-
Platforms: []string{"qemu"},
49-
Tags: []string{"ignition"},
5047
})
5148
}
5249

@@ -119,51 +116,50 @@ func ignitionFailure(c cluster.TestCluster) error {
119116
}
120117
}
121118

122-
// get current path and create tmp dir
123-
func GetTempdir(dirname string) (string, error) {
119+
func bootfsFailure(c cluster.TestCluster) error {
120+
// Create fakeboot disk
121+
fakeConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
122+
if err != nil {
123+
return errors.Wrapf(err, "creating empty config")
124+
}
125+
// get current path and create tmp dir
126+
dirname := "tmp"
124127
dir, err := os.Getwd()
125128
if err != nil {
126-
return "", err
129+
return err
127130
}
128-
path := filepath.Join(dir, dirname)
129-
return path, nil
130-
}
131-
132-
func bootfsFailure(c cluster.TestCluster) error {
133-
// We can't create files in / due to the immutable bit OSTree creates, so
134-
// this is a convenient way to test Ignition failure.
135-
136-
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
131+
tempDir := filepath.Join(dir, dirname)
132+
// Change the current working directory
133+
if err := os.Chdir(tempDir); err != nil {
134+
return err
135+
}
136+
bootName := "fakeboot"
137+
createBoot, err := os.Create(bootName)
137138
if err != nil {
138-
return errors.Wrapf(err, "creating empty config")
139+
return err
139140
}
140-
141-
// Config which theoretically aims to reference fakeboot disk
142-
fakeConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
141+
// Truncate the file to 1 gigabyte
142+
// 1<<30 is a way to represent 1gb in terms of bytes
143+
// (bitwise left shift operation)
144+
const oneGB = 1 << 30
145+
err = os.Truncate(createBoot.Name(), oneGB)
143146
if err != nil {
144-
return errors.Wrapf(err, "creating empty config")
145-
} else {
146-
147-
tempDir, err := GetTempdir("tmp")
148-
if err != nil {
149-
fmt.Println("Error getting temp directory:", err)
150-
}
151-
fmt.Println("Temp directory:", tempDir)
152-
cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf(`set -euo pipefail;
153-
mkdir -p %s
154-
truncate -s 1G fakeboot
155-
mkfs.ext4 -L boot fakeboot
156-
`, tempDir))
157-
cmd.Stdout = os.Stdout
158-
cmd.Stderr = os.Stderr
159-
if err := cmd.Run(); err != nil {
160-
c.Fatal(err)
161-
}
147+
return err
148+
}
149+
cmd := exec.Command("mkfs.ext4", "-L", "boot", createBoot.Name())
150+
cmd.Stdout = os.Stdout
151+
cmd.Stderr = os.Stderr
152+
if err := cmd.Run(); err != nil {
153+
c.Fatal(err)
162154
}
163155

164156
builder := platform.NewQemuBuilder()
165157
defer builder.Close()
166-
builder.SetConfig(failConfig)
158+
builder.SetConfig(fakeConfig)
159+
builder.AddDisk(&platform.Disk{
160+
BackingFile: tempDir,
161+
BackingFormat: "raw",
162+
})
167163
err = builder.AddBootDisk(&platform.Disk{
168164
BackingFile: kola.QEMUOptions.DiskImage,
169165
})
@@ -179,24 +175,6 @@ func bootfsFailure(c cluster.TestCluster) error {
179175
}
180176
defer inst.Destroy()
181177

182-
builder2 := platform.NewQemuBuilder()
183-
defer builder2.Close()
184-
builder2.SetConfig(fakeConfig) // how to fakeboot?
185-
err = builder2.AddDisk(&platform.Disk{
186-
BackingFile: kola.QEMUOptions.DiskImage,
187-
})
188-
189-
if err != nil {
190-
return err
191-
}
192-
builder2.MemoryMiB = 1024
193-
builder2.Firmware = kola.QEMUOptions.Firmware
194-
inst2, err := builder2.Exec()
195-
if err != nil {
196-
return err
197-
}
198-
defer inst2.Destroy()
199-
200178
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
201179
defer cancel()
202180

0 commit comments

Comments
 (0)