Skip to content

Commit d2c677f

Browse files
committed
test: Created sample file and reading contents
1 parent 857743c commit d2c677f

File tree

3 files changed

+104
-48
lines changed

3 files changed

+104
-48
lines changed

mantle/cmd/kola/testiso.go

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
662662
time.Sleep(timeout)
663663
errchan <- fmt.Errorf("timed out after %v", timeout)
664664
}()
665+
665666
if !console {
666667
go func() {
667668
errBuf, err := inst.WaitIgnitionError(ctx)
@@ -679,22 +680,60 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
679680
errchan <- err
680681
}
681682
}()
682-
// check for console badness
683-
errBuf, err := inst.CheckConsoleForBadness(ctx)
684-
if err == nil {
685-
if errBuf != "" {
686-
plog.Info("entered emergency.target in initramfs")
687-
path := filepath.Join(outdir, "ignition-virtio-dump.txt")
688-
if err := os.WriteFile(path, []byte(errBuf), 0644); err != nil {
689-
plog.Errorf("Failed to write journal: %v", err)
683+
684+
go func() {
685+
errBuf, err := inst.CheckConsoleForBadness(ctx)
686+
if err == nil {
687+
if errBuf != "" {
688+
plog.Info("Badness identified")
689+
path := filepath.Join(outdir, "badness.txt")
690+
if err := os.WriteFile(path, []byte(errBuf), 0644); err != nil {
691+
plog.Errorf("Failed to write journal: %v", err)
692+
}
693+
err = platform.ErrInitramfsEmergency
690694
}
691-
err = platform.ErrInitramfsEmergency
692695
}
693-
}
696+
if err != nil {
697+
errchan <- err
698+
}
699+
700+
}()
701+
702+
go func() {
703+
// figure out how to get console file from instance
704+
// Run a sample Log File func
705+
filename := inst.CreateLogFile()
706+
// Read file
707+
fmt.Printf("\nTesting file:\n%v\n", filename)
708+
// Open file for reading
709+
file, err := os.Open(filename)
694710
if err != nil {
695-
errchan <- err
711+
fmt.Println("Error opening file:", err)
712+
return
696713
}
697-
}
714+
defer file.Close() // When done, close file
715+
// Read and print contents
716+
scanner := bufio.NewScanner(file)
717+
for scanner.Scan() {
718+
fmt.Printf("File contents:\n%v\n",scanner.Text())
719+
fmt.Println("")
720+
}
721+
if err := scanner.Err(); err != nil {
722+
fmt.Printf("Error reading %v.\nError: %v",filename, err)
723+
}
724+
725+
726+
// below something like
727+
// inst.builder.ConsoleFile
728+
729+
// zz, err := inst.CheckConsoleForBadness(ctx)
730+
// fmt.Println(zz,err)
731+
732+
//plus that
733+
// ReadFile(path/tofile...)
734+
// path := filepath.Base(outdir) // returns test name / dir
735+
}()
736+
698737
go func() {
699738
err := inst.Wait()
700739
// only one Wait() gets process data, so also manually check for signal
@@ -708,6 +747,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
708747
time.Sleep(1 * time.Minute)
709748
errchan <- fmt.Errorf("QEMU exited; timed out waiting for completion")
710749
}()
750+
711751
go func() {
712752
r := bufio.NewReader(qchan)
713753
for _, exp := range expected {
@@ -735,6 +775,7 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
735775
// OK!
736776
errchan <- nil
737777
}()
778+
738779
go func() {
739780
//check for error when switching boot order
740781
if booterrchan != nil {
@@ -743,6 +784,8 @@ func awaitCompletion(ctx context.Context, inst *platform.QemuInstance, outdir st
743784
}
744785
}
745786
}()
787+
788+
}
746789
err := <-errchan
747790
return time.Since(start), err
748791
}

mantle/kola/harness.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,33 +1919,30 @@ func ScpKolet(machines []platform.Machine) error {
19191919

19201920

19211921
// CheckConsoleText checks console output for badness
1922-
func CheckConsoleText(badlines []string) (bool, []string) {
1923-
badness := false
1924-
newBadlines := []string{}
1922+
// input : the console content
1923+
// output true if badlines were found and the bad lines.
1924+
func CheckConsoleText(input []byte) (bool, []string) {
1925+
warnOnly := true
1926+
badlines := []string{}
19251927

1926-
// Ensure there are enough badlines to check
1927-
if len(badlines) < 2 {
1928-
return badness, badlines // Return early if not enough lines
1929-
}
19301928
for _, check := range consoleChecks {
19311929
if check.skipFlag != nil {
19321930
continue
19331931
}
1934-
match := check.match.FindStringSubmatch(badlines[1])
1932+
match := check.match.FindSubmatch(input)
19351933
if match != nil {
19361934
badline := check.desc
19371935
if len(match) > 1 {
19381936
// include first subexpression
19391937
badline += fmt.Sprintf(" (%v)", match[1])
19401938
}
1941-
newBadlines = append(newBadlines, badline)
1939+
badlines = append(badlines, badline)
19421940
if !check.warnOnly {
1943-
badness = false
1941+
warnOnly = false
19441942
}
19451943
}
19461944
}
1947-
badlines = append(badlines, newBadlines...)
1948-
return badness, badlines
1945+
return warnOnly, badlines
19491946
}
19501947

19511948
// CheckConsole checks some console output for badness and returns short
@@ -1956,33 +1953,16 @@ func CheckConsoleText(badlines []string) (bool, []string) {
19561953
// rerun success.
19571954
func CheckConsole(output []byte, t *register.Test) (bool, []string) {
19581955
var badlines []string
1959-
warnOnly, allowRerunSuccess := true, true
1960-
for _, check := range consoleChecks {
1961-
if check.skipFlag != nil && t != nil && t.HasFlag(*check.skipFlag) {
1962-
continue
1963-
}
1964-
match := check.match.FindSubmatch(output)
1965-
if match != nil {
1966-
badline := check.desc
1967-
if len(match) > 1 {
1968-
// include first subexpression
1969-
badline += fmt.Sprintf(" (%s)", match[1])
1970-
}
1971-
badlines = append(badlines, badline)
1972-
if !check.warnOnly {
1973-
warnOnly = false
1974-
}
1975-
if !check.allowRerunSuccess {
1976-
allowRerunSuccess = false
1977-
}
1978-
is_it_err,checkConsole := CheckConsoleText([]string(badlines))
1979-
fmt.Printf("%v\n,%v", checkConsole, is_it_err)
1980-
}
1981-
}
1956+
var badness bool
1957+
allowRerunSuccess := true
1958+
1959+
//here pass output to checkconsoleText
1960+
badness, badlines = CheckConsoleText(output)
1961+
19821962
if len(badlines) > 0 && allowRerunSuccess && t != nil {
19831963
markTestForRerunSuccess(t, "CheckConsole:")
19841964
}
1985-
return warnOnly, badlines
1965+
return badness, badlines
19861966
}
19871967

19881968
func SetupOutputDir(outputDir, platform string) (string, error) {

mantle/platform/qemu.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,40 @@ func (inst *QemuInstance) WaitIgnitionError(ctx context.Context) (string, error)
254254
}
255255
return r.String(), nil
256256
}
257+
// Create a sample, log file for testing purposes
258+
func (inst *QemuInstance) CreateLogFile() string {
259+
// Create some dir and check if it exists
260+
dir, err := os.UserHomeDir()
261+
if err != nil {
262+
fmt.Println("Error getting home directory:", err)
263+
return ""
264+
}
265+
fullDir := filepath.Join(dir, "log", "journal", "test")
266+
267+
err = os.MkdirAll(fullDir, os.ModePerm)
268+
if err != nil {
269+
fmt.Println("Error creating directory:", err)
270+
return ""
271+
}
272+
273+
// Create sample log file
274+
filePath := filepath.Join(fullDir, "sample.log")
275+
file, err := os.Create(filePath)
276+
if err != nil {
277+
fmt.Println("Error creating file:", err)
278+
return ""
279+
}
280+
defer file.Close() // Ensure the file is closed when done
281+
282+
// Write "Hello!" to the file
283+
_, err = file.WriteString("Test file\n")
284+
if err != nil {
285+
fmt.Println("Error writing to file:", err)
286+
return ""
287+
}
257288

289+
return filePath
290+
}
258291
// Copy of WaitIgnitionError -> CheckConsoleForBadness
259292
// CheckConsoleForBadness will only return if the instance
260293
// failed inside the initramfs. The resulting string will

0 commit comments

Comments
 (0)