Skip to content

Commit

Permalink
test: Created sample file and reading contents
Browse files Browse the repository at this point in the history
  • Loading branch information
c4rt0 committed Sep 17, 2024
1 parent 857743c commit d2c677f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 48 deletions.
67 changes: 55 additions & 12 deletions mantle/cmd/kola/testiso.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
time.Sleep(timeout)
errchan <- fmt.Errorf("timed out after %v", timeout)
}()

if !console {
go func() {
errBuf, err := inst.WaitIgnitionError(ctx)
Expand All @@ -679,22 +680,60 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
errchan <- err
}
}()
// check for console badness
errBuf, err := inst.CheckConsoleForBadness(ctx)
if err == nil {
if errBuf != "" {
plog.Info("entered emergency.target in initramfs")
path := filepath.Join(outdir, "ignition-virtio-dump.txt")
if err := os.WriteFile(path, []byte(errBuf), 0644); err != nil {
plog.Errorf("Failed to write journal: %v", err)

go func() {
errBuf, err := inst.CheckConsoleForBadness(ctx)
if err == nil {
if errBuf != "" {
plog.Info("Badness identified")
path := filepath.Join(outdir, "badness.txt")
if err := os.WriteFile(path, []byte(errBuf), 0644); err != nil {
plog.Errorf("Failed to write journal: %v", err)
}
err = platform.ErrInitramfsEmergency
}
err = platform.ErrInitramfsEmergency
}
}
if err != nil {
errchan <- err
}

}()

go func() {
// figure out how to get console file from instance
// Run a sample Log File func
filename := inst.CreateLogFile()
// Read file
fmt.Printf("\nTesting file:\n%v\n", filename)
// Open file for reading
file, err := os.Open(filename)
if err != nil {
errchan <- err
fmt.Println("Error opening file:", err)
return
}
}
defer file.Close() // When done, close file
// Read and print contents
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Printf("File contents:\n%v\n",scanner.Text())
fmt.Println("")
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading %v.\nError: %v",filename, err)
}


// below something like
// inst.builder.ConsoleFile

// zz, err := inst.CheckConsoleForBadness(ctx)
// fmt.Println(zz,err)

//plus that
// ReadFile(path/tofile...)
// path := filepath.Base(outdir) // returns test name / dir
}()

go func() {
err := inst.Wait()
// only one Wait() gets process data, so also manually check for signal
Expand All @@ -708,6 +747,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
time.Sleep(1 * time.Minute)
errchan <- fmt.Errorf("QEMU exited; timed out waiting for completion")
}()

go func() {
r := bufio.NewReader(qchan)
for _, exp := range expected {
Expand Down Expand Up @@ -735,6 +775,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
// OK!
errchan <- nil
}()

go func() {
//check for error when switching boot order
if booterrchan != nil {
Expand All @@ -743,6 +784,8 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
}
}
}()

}
err := <-errchan
return time.Since(start), err
}
Expand Down
52 changes: 16 additions & 36 deletions mantle/kola/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -1919,33 +1919,30 @@ func ScpKolet(machines []platform.Machine) error {


// CheckConsoleText checks console output for badness
func CheckConsoleText(badlines []string) (bool, []string) {
badness := false
newBadlines := []string{}
// input : the console content
// output true if badlines were found and the bad lines.
func CheckConsoleText(input []byte) (bool, []string) {
warnOnly := true
badlines := []string{}

// Ensure there are enough badlines to check
if len(badlines) < 2 {
return badness, badlines // Return early if not enough lines
}
for _, check := range consoleChecks {
if check.skipFlag != nil {
continue
}
match := check.match.FindStringSubmatch(badlines[1])
match := check.match.FindSubmatch(input)
if match != nil {
badline := check.desc
if len(match) > 1 {
// include first subexpression
badline += fmt.Sprintf(" (%v)", match[1])
}
newBadlines = append(newBadlines, badline)
badlines = append(badlines, badline)
if !check.warnOnly {
badness = false
warnOnly = false
}
}
}
badlines = append(badlines, newBadlines...)
return badness, badlines
return warnOnly, badlines
}

// CheckConsole checks some console output for badness and returns short
Expand All @@ -1956,33 +1953,16 @@ func CheckConsoleText(badlines []string) (bool, []string) {
// rerun success.
func CheckConsole(output []byte, t *register.Test) (bool, []string) {
var badlines []string
warnOnly, allowRerunSuccess := true, true
for _, check := range consoleChecks {
if check.skipFlag != nil && t != nil && t.HasFlag(*check.skipFlag) {
continue
}
match := check.match.FindSubmatch(output)
if match != nil {
badline := check.desc
if len(match) > 1 {
// include first subexpression
badline += fmt.Sprintf(" (%s)", match[1])
}
badlines = append(badlines, badline)
if !check.warnOnly {
warnOnly = false
}
if !check.allowRerunSuccess {
allowRerunSuccess = false
}
is_it_err,checkConsole := CheckConsoleText([]string(badlines))
fmt.Printf("%v\n,%v", checkConsole, is_it_err)
}
}
var badness bool
allowRerunSuccess := true

//here pass output to checkconsoleText
badness, badlines = CheckConsoleText(output)

if len(badlines) > 0 && allowRerunSuccess && t != nil {
markTestForRerunSuccess(t, "CheckConsole:")
}
return warnOnly, badlines
return badness, badlines
}

func SetupOutputDir(outputDir, platform string) (string, error) {
Expand Down
33 changes: 33 additions & 0 deletions mantle/platform/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,40 @@ func (inst *QemuInstance) WaitIgnitionError(ctx context.Context) (string, error)
}
return r.String(), nil
}
// Create a sample, log file for testing purposes
func (inst *QemuInstance) CreateLogFile() string {
// Create some dir and check if it exists
dir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting home directory:", err)
return ""
}
fullDir := filepath.Join(dir, "log", "journal", "test")

err = os.MkdirAll(fullDir, os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
return ""
}

// Create sample log file
filePath := filepath.Join(fullDir, "sample.log")
file, err := os.Create(filePath)
if err != nil {
fmt.Println("Error creating file:", err)
return ""
}
defer file.Close() // Ensure the file is closed when done

// Write "Hello!" to the file
_, err = file.WriteString("Test file\n")
if err != nil {
fmt.Println("Error writing to file:", err)
return ""
}

return filePath
}
// Copy of WaitIgnitionError -> CheckConsoleForBadness
// CheckConsoleForBadness will only return if the instance
// failed inside the initramfs. The resulting string will
Expand Down

0 comments on commit d2c677f

Please sign in to comment.