From e5d8f99c6071c09aa7bca4e79d28b26f95dc7716 Mon Sep 17 00:00:00 2001 From: Nicolas Lacasse Date: Wed, 18 Jul 2018 15:44:34 -0700 Subject: runsc: Fixes to CheckpointRestoreTest. We must delete the output file at the beginning of the test, otherwise the test fails immediately. Also some minor cleanups in readOutputFile. PiperOrigin-RevId: 205150525 Change-Id: I6bae1acd5b315320a2c6e25a59afcfc06267fb17 --- runsc/container/container_test.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 72b115628..fc441e353 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -121,12 +121,12 @@ func createWriteableOutputFile(path string) (*os.File, error) { return outputFile, nil } -func readOutputNum(outputFile *os.File, path string, first bool) (int, error) { +func readOutputNum(f *os.File, first bool) (int, error) { var num int time.Sleep(1 * time.Second) - // Check that outputFile exists and contains counting data. - fileInfo, err := os.Stat(path) + // Check that f exists and contains counting data. + fileInfo, err := f.Stat() if err != nil { return 0, fmt.Errorf("error creating output file: %v", err) } @@ -136,15 +136,15 @@ func readOutputNum(outputFile *os.File, path string, first bool) (int, error) { } // Read the first number in the new file - outputFileContent, err := ioutil.ReadAll(outputFile) + b, err := ioutil.ReadAll(f) if err != nil { return 0, fmt.Errorf("error reading file: %v", err) } - if len(outputFileContent) == 0 { + if len(b) == 0 { return 0, fmt.Errorf("error no content was read") } - nums := strings.Split(string(outputFileContent), "\n") + nums := strings.Split(string(b), "\n") if first { num, err = strconv.Atoi(nums[0]) @@ -487,6 +487,9 @@ func TestExec(t *testing.T) { // be the next consecutive number after the last number from the checkpointed container. func TestCheckpointRestore(t *testing.T) { outputPath := filepath.Join(os.TempDir(), "output") + // Make sure it does not already exist. + os.Remove(outputPath) + outputFile, err := createWriteableOutputFile(outputPath) if err != nil { t.Fatalf("error creating output file: %v", err) @@ -538,7 +541,7 @@ func TestCheckpointRestore(t *testing.T) { } defer os.RemoveAll(imagePath) - lastNum, err := readOutputNum(outputFile, outputPath, false) + lastNum, err := readOutputNum(outputFile, false) if err != nil { t.Fatalf("error with outputFile: %v", err) } @@ -563,7 +566,7 @@ func TestCheckpointRestore(t *testing.T) { t.Fatalf("error starting container: %v", err) } - firstNum, err := readOutputNum(outputFile2, outputPath, true) + firstNum, err := readOutputNum(outputFile2, true) if err != nil { t.Fatalf("error with outputFile: %v", err) } @@ -594,7 +597,7 @@ func TestCheckpointRestore(t *testing.T) { t.Fatalf("error starting container: %v", err) } - firstNum2, err := readOutputNum(outputFile3, outputPath, true) + firstNum2, err := readOutputNum(outputFile3, true) if err != nil { t.Fatalf("error with outputFile: %v", err) } -- cgit v1.2.3