Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7b43858

Browse files
committedNov 22, 2023
mantle/kola: Create functions to grep for pattern
1 parent 14b0143 commit 7b43858

File tree

2 files changed

+103
-146
lines changed

2 files changed

+103
-146
lines changed
 

‎mantle/kola/tests/ignition/qemufailure.go

+103-137
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ import (
2929
"github.com/coreos/coreos-assembler/mantle/kola/register"
3030
"github.com/coreos/coreos-assembler/mantle/platform"
3131
"github.com/coreos/coreos-assembler/mantle/platform/conf"
32-
"github.com/coreos/ignition/v2/config/v3_2/types"
3332
"github.com/coreos/coreos-assembler/mantle/util"
33+
"github.com/coreos/ignition/v2/config/v3_2/types"
3434
)
3535

36-
var console bool
37-
3836
func init() {
3937
register.RegisterTest(&register.Test{
4038
Name: "coreos.ignition.failure",
@@ -78,42 +76,60 @@ func runDualBootfsIgnitionFailure(c cluster.TestCluster) {
7876
}
7977
}
8078

81-
func ignitionFailure(c cluster.TestCluster) error {
82-
// We can't create files in / due to the immutable bit OSTree creates, so
83-
// this is a convenient way to test Ignition failure.
84-
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
79+
// Read file and verify if it contains a pattern
80+
// 1. Read file, make sure it exists
81+
// 2. regex for pattern
82+
func fileHasPattern(tempLogFilePath string, searchPattern string) (bool, error) {
83+
file, err := os.ReadFile(tempLogFilePath)
8584
if err != nil {
86-
return errors.Wrapf(err, "creating empty config")
85+
return false, err
8786
}
88-
failConfig.AddFile("/notwritable.txt", "Hello world", 0644)
89-
90-
builder := platform.NewQemuBuilder() // platform.Manhole()
91-
if err != nil {
92-
return err
87+
// File exists but it's empty
88+
if len(file) == 0 {
89+
return false, nil
9390
}
94-
builder.MemoryMiB = 1024
95-
builder.Firmware = kola.QEMUOptions.Firmware
91+
// File has content, but the pattern is not present
92+
match := regexp.MustCompile(searchPattern).Match(file)
93+
if match {
94+
// Pattern found
95+
return true, nil
96+
}
97+
// Pattern not found
98+
return false, nil
99+
}
100+
101+
// Start the VM, take string and grep for it in the temporary console logs
102+
func verifyError(builder *platform.QemuBuilder, searchPattern string) error {
96103
inst, err := builder.Exec()
97104
if err != nil {
98105
return err
99106
}
100107
defer inst.Destroy()
101-
102108
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
109+
103110
defer cancel()
104111

105112
errchan := make(chan error)
106113
go func() {
107-
err := inst.WaitAll(ctx)
108-
if err == nil {
109-
err = fmt.Errorf("Ignition unexpectedly succeeded")
110-
} else if err == platform.ErrInitramfsEmergency {
111-
// The expected case
112-
err = nil
114+
resultingError := inst.WaitAll(ctx)
115+
if resultingError == nil {
116+
resultingError = fmt.Errorf("ignition unexpectedly succeeded")
117+
} else if resultingError == platform.ErrInitramfsEmergency {
118+
// Expectred initramfs failure, checking the console file to insure
119+
// that coreos.ignition.failure failed
120+
found, err := fileHasPattern(builder.ConsoleFile, searchPattern)
121+
if err != nil {
122+
resultingError = errors.Wrapf(err, "failed to find pattern '%s' in file '%s'", searchPattern, builder.ConsoleFile)
123+
} else if !found {
124+
resultingError = fmt.Errorf("regex match NOT found")
125+
} else {
126+
// The expected case
127+
resultingError = nil
128+
}
113129
} else {
114-
err = errors.Wrapf(err, "expected initramfs emergency.target error")
130+
resultingError = errors.Wrapf(resultingError, "expected initramfs emergency.target error")
115131
}
116-
errchan <- err
132+
errchan <- resultingError
117133
}()
118134

119135
select {
@@ -130,16 +146,55 @@ func ignitionFailure(c cluster.TestCluster) error {
130146
}
131147
}
132148

133-
// Verify that there is only one boot filesystem attached to the device
134-
func dualBootfsFailure(c cluster.TestCluster) error {
149+
func ignitionFailure(c cluster.TestCluster) error {
150+
// We can't create files in / due to the immutable bit OSTree creates, so
151+
// this is a convenient way to test Ignition failure.
152+
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
153+
if err != nil {
154+
return errors.Wrapf(err, "creating empty config")
155+
}
156+
failConfig.AddFile("/notwritable.txt", "Hello world", 0644)
157+
135158
builder := platform.NewQemuBuilder()
136159

137-
consoleFile, err := builder.TempFile("console.log")
160+
// Create a temporary log file
161+
ConsoleFile, err := builder.TempFile("console.log")
162+
if err != nil {
163+
return err
164+
}
165+
tempLogFilePath := ConsoleFile.Name()
166+
// Instruct builder to use it
167+
builder.ConsoleFile = tempLogFilePath
168+
defer builder.Close()
169+
builder.SetConfig(failConfig)
170+
err = builder.AddBootDisk(&platform.Disk{
171+
BackingFile: kola.QEMUOptions.DiskImage,
172+
})
138173
if err != nil {
139174
return err
140175
}
141-
builder.ConsoleFile = consoleFile.Name()
142176

177+
builder.MemoryMiB = 1024
178+
builder.Firmware = kola.QEMUOptions.Firmware
179+
180+
searchPattern := "error creating /sysroot/notwritable.txt"
181+
if err := verifyError(builder, searchPattern); err != nil {
182+
return err
183+
}
184+
return nil
185+
}
186+
187+
// Verify that there is only one boot filesystem attached to the device
188+
func dualBootfsFailure(c cluster.TestCluster) error {
189+
builder := platform.NewQemuBuilder()
190+
// Create a temporary log file
191+
ConsoleFile, err := builder.TempFile("console.log")
192+
if err != nil {
193+
return err
194+
}
195+
tempLogFilePath := ConsoleFile.Name()
196+
// Instruct builder to use it
197+
builder.ConsoleFile = tempLogFilePath
143198
// get current path and create tmp dir
144199
fakeBootFile, err := builder.TempFile("fakeBoot")
145200
if err != nil {
@@ -174,85 +229,40 @@ func dualBootfsFailure(c cluster.TestCluster) error {
174229
}
175230
builder.MemoryMiB = 1024
176231
builder.Firmware = kola.QEMUOptions.Firmware
177-
inst, err := builder.Exec()
178-
if err != nil {
179-
return err
180-
} // platform.Manhole()
181-
defer inst.Destroy()
182232

183-
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
184-
defer cancel()
185-
186-
errchan := make(chan error)
187-
go func() {
188-
resultingError := inst.WaitAll(ctx)
189-
190-
if resultingError == nil {
191-
resultingError = fmt.Errorf("bootfs unexpectedly succeeded")
192-
} else if resultingError == platform.ErrInitramfsEmergency {
193-
// Expectred initramfs failure, checking the console file to insure
194-
// that coreos-unique-boot.service failed
195-
b, err := os.ReadFile(builder.ConsoleFile)
196-
if err != nil {
197-
panic(err)
198-
}
199-
isExist, err := regexp.Match("Error: System has 2 devices with a filesystem labeled 'boot'", b)
200-
if err != nil {
201-
panic(err)
202-
}
203-
if isExist {
204-
// The expected case
205-
resultingError = nil
206-
} else {
207-
resultingError = errors.Wrapf(err, "expected coreos-unique-boot.service to fail")
208-
}
209-
} else {
210-
resultingError = errors.Wrapf(err, "expected initramfs emergency.target error")
211-
}
212-
errchan <- resultingError
213-
}()
214-
215-
select {
216-
case <-ctx.Done():
217-
if err := inst.Kill(); err != nil {
218-
return errors.Wrapf(err, "failed to kill the vm instance")
219-
}
220-
return errors.Wrapf(ctx.Err(), "timed out waiting for initramfs error")
221-
case err := <-errchan:
222-
if err != nil {
223-
return err
224-
}
225-
return nil
233+
searchRegexString := "Error: System has 2 devices with a filesystem labeled 'boot'"
234+
if err := verifyError(builder, searchRegexString); err != nil {
235+
return err
226236
}
237+
return nil
227238
}
228239

229240
// Use ignition config to create a second bootfs
230241
// 1 - produce an ignition file that format a disk with a"boot" label.
231242
// 2 - boot the VM with the ignition file and an extra disk.
232-
// 3 - observe the failure
243+
// 3 - observe the failure
233244
func dualBootfsIgnitionFailure(c cluster.TestCluster) error {
234245
builder := platform.NewQemuBuilder()
235-
// inserting log file for troubleshooting
236-
// file located in coreos-assembler directory
237-
238-
// consoleFile, err := builder.TempFile("console.log")
239-
// if err != nil {
240-
// return err
241-
// }
242-
//builder.ConsoleFile = consoleFile.Name()
243-
builder.ConsoleFile = "console.txt"
244246

247+
// Create a temporary log file
248+
ConsoleFile, err := builder.TempFile("console.log")
249+
if err != nil {
250+
return err
251+
}
252+
tempLogFilePath := ConsoleFile.Name()
253+
// Instruct builder to use it
254+
builder.ConsoleFile = tempLogFilePath
245255
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
246256
if err != nil {
247257
return errors.Wrapf(err, "creating empty config")
248258
}
249259

250-
// Craft an ingniton file that format a partition
251-
formaterConfig := types.Config {
260+
// Craft an Ingniton file that formats a partition
261+
formaterConfig := types.Config{
252262
Ignition: types.Ignition{
253263
Version: "3.2.0",
254264
},
255-
Storage: types.Storage {
265+
Storage: types.Storage{
256266
Filesystems: []types.Filesystem{
257267
{
258268
Device: "/dev/disk/by-id/virtio-extra-boot",
@@ -281,54 +291,10 @@ func dualBootfsIgnitionFailure(c cluster.TestCluster) error {
281291

282292
builder.MemoryMiB = 1024
283293
builder.Firmware = kola.QEMUOptions.Firmware
284-
inst, err := builder.Exec()
285-
if err != nil {
286-
return err
287-
}
288-
defer inst.Destroy()
289294

290-
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
291-
defer cancel()
292-
293-
errchan := make(chan error)
294-
go func() {
295-
resultingError := inst.WaitAll(ctx)
296-
297-
if resultingError == nil {
298-
resultingError = fmt.Errorf("bootfs unexpectedly succeeded")
299-
} else if resultingError == platform.ErrInitramfsEmergency {
300-
// Expectred initramfs failure, checking the console file to insure
301-
// that coreos-unique-boot.service failed
302-
b, err := os.ReadFile(builder.ConsoleFile)
303-
if err != nil {
304-
panic(err)
305-
}
306-
isExist, err := regexp.Match("Error: System has 2 devices with a filesystem labeled 'boot'", b)
307-
if err != nil {
308-
panic(err)
309-
}
310-
if isExist {
311-
// The expected case
312-
resultingError = nil
313-
} else {
314-
resultingError = errors.Wrapf(err, "expected coreos-unique-boot.service to fail")
315-
}
316-
} else {
317-
resultingError = errors.Wrapf(err, "expected initramfs emergency.target error")
318-
}
319-
errchan <- resultingError
320-
}()
321-
322-
select {
323-
case <-ctx.Done():
324-
if err := inst.Kill(); err != nil {
325-
return errors.Wrapf(err, "failed to kill the vm instance")
326-
}
327-
return errors.Wrapf(ctx.Err(), "timed out waiting for initramfs error")
328-
case err := <-errchan:
329-
if err != nil {
330-
return err
331-
}
332-
return nil
295+
searchRegexString := "Error: System has 2 devices with a filesystem labeled 'boot'"
296+
if err := verifyError(builder, searchRegexString); err != nil {
297+
return err
333298
}
299+
return nil
334300
}

‎mantle/platform/platform.go

-9
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,3 @@ func WriteJSONInfo(m Machine, w io.Writer) error {
555555
e.SetIndent("", "")
556556
return e.Encode(info)
557557
}
558-
559-
// Create a temporary log file
560-
func CreateTempLogFile(builder *QemuBuilder) string {
561-
ConsoleFile, err := builder.TempFile("console.log")
562-
if err != nil {
563-
return "creating console.log not successfull."
564-
}
565-
return ConsoleFile.Name()
566-
}

0 commit comments

Comments
 (0)
Please sign in to comment.