From 3482ad3d3258ec9533389a7413c0ebe60ebb2818 Mon Sep 17 00:00:00 2001 From: Martin Mulholland Date: Wed, 10 Feb 2021 11:55:51 -0500 Subject: [PATCH 1/2] Restructure test directories and improve temp dir error handling --- .gitignore | 2 +- .../parserTest/parser_api_test.go} | 2 +- tests/{ => v2}/utils/command_test_utils.go | 0 tests/{ => v2}/utils/component_test_utils.go | 0 tests/{ => v2}/utils/endpoint-test-utils.go | 0 tests/{ => v2}/utils/test_utils.go | 40 ++++++++++++++----- 6 files changed, 31 insertions(+), 13 deletions(-) rename tests/{api/parser_v200_verify_test.go => v2/parserTest/parser_api_test.go} (99%) rename tests/{ => v2}/utils/command_test_utils.go (100%) rename tests/{ => v2}/utils/component_test_utils.go (100%) rename tests/{ => v2}/utils/endpoint-test-utils.go (100%) rename tests/{ => v2}/utils/test_utils.go (91%) diff --git a/.gitignore b/.gitignore index 2e12ef21..8b08cc53 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,6 @@ main .vscode/ # File created running tests -tests/tmp/ +tests/**/tmp/ .DS_Store diff --git a/tests/api/parser_v200_verify_test.go b/tests/v2/parserTest/parser_api_test.go similarity index 99% rename from tests/api/parser_v200_verify_test.go rename to tests/v2/parserTest/parser_api_test.go index b47b5d29..ead6bb53 100644 --- a/tests/api/parser_v200_verify_test.go +++ b/tests/v2/parserTest/parser_api_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/devfile/library/tests/utils" + "github.com/devfile/library/tests/v2/utils" schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" ) diff --git a/tests/utils/command_test_utils.go b/tests/v2/utils/command_test_utils.go similarity index 100% rename from tests/utils/command_test_utils.go rename to tests/v2/utils/command_test_utils.go diff --git a/tests/utils/component_test_utils.go b/tests/v2/utils/component_test_utils.go similarity index 100% rename from tests/utils/component_test_utils.go rename to tests/v2/utils/component_test_utils.go diff --git a/tests/utils/endpoint-test-utils.go b/tests/v2/utils/endpoint-test-utils.go similarity index 100% rename from tests/utils/endpoint-test-utils.go rename to tests/v2/utils/endpoint-test-utils.go diff --git a/tests/utils/test_utils.go b/tests/v2/utils/test_utils.go similarity index 91% rename from tests/utils/test_utils.go rename to tests/v2/utils/test_utils.go index d4f4b09a..fa1b26be 100644 --- a/tests/utils/test_utils.go +++ b/tests/v2/utils/test_utils.go @@ -23,13 +23,15 @@ import ( ) const ( - tmpDir = "../tmp/" - logErrorOnly = false - logFileName = "test.log" + defaultTempDir = "./tmp/" + logFileName = "test.log" // logToFileOnly - If set to false the log output will also be output to the console logToFileOnly = true // If set to false the log output will also be output to the console ) +// tmpDir temporary directory in use +var tmpDir string + var ( testLogger *log.Logger ) @@ -38,11 +40,14 @@ var ( // - the temporary directory used by the test to store logs and generated devfiles. // - the log file func init() { + tmpDir = defaultTempDir if _, err := os.Stat(tmpDir); !os.IsNotExist(err) { os.RemoveAll(tmpDir) } - os.Mkdir(tmpDir, 0755) - + if err := os.Mkdir(tmpDir, 0755); err != nil { + fmt.Printf("Failed to create temp directory, will use current directory : %v ", err) + tmpDir = "./" + } f, err := os.OpenFile(filepath.Join(tmpDir, logFileName), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { fmt.Printf("Error creating Log file : %v", err) @@ -55,14 +60,23 @@ func init() { } testLogger.Println("Test Starting:") } + } -// createTempDir creates a specified sub directory under the temp directory if it does not exist. +// CreateTempDir creates a specified sub directory under the temp directory if it does not exist. // Returns the name of the created directory. -func createTempDir(subdir string) string { +func CreateTempDir(subdir string) string { tempDir := tmpDir + subdir + "/" if _, err := os.Stat(tempDir); os.IsNotExist(err) { - os.Mkdir(tempDir, 0755) + if err = os.Mkdir(tempDir, 0755); err != nil { + LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err)) + // if cannot create subdirectory just use the base tmp directory + tempDir = tmpDir + } + } else if err != nil { + // if cannot create subdirectory just use the base tmp directory + LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err)) + tempDir = tmpDir } return tempDir } @@ -78,9 +92,9 @@ func GetDevFileName() string { testFile := filepath.Base(fn) testFileExtension := filepath.Ext(testFile) subdir := testFile[0 : len(testFile)-len(testFileExtension)] - destDir := createTempDir(subdir) + destDir := CreateTempDir(subdir) callerName := runtime.FuncForPC(pc).Name() - pos1 := strings.LastIndex(callerName, "/tests/api.") + len("/tests/api.") + pos1 := strings.LastIndex(callerName, "/parserTest.") + len("/parserTest.") devfileName := destDir + callerName[pos1:len(callerName)] + ".yaml" LogInfoMessage(fmt.Sprintf("GetDevFileName : %s", devfileName)) @@ -99,7 +113,11 @@ func AddSuffixToFileName(fileName string, suffix string) string { // LogMessage logs the specified message and returns the message logged func LogMessage(message string) string { - testLogger.Println(message) + if testLogger != nil { + testLogger.Println(message) + } else { + fmt.Printf("Logger not available: %s", message) + } return message } From 79f4b1d43264e9c9f1c18bd1d4824a8536b299ab Mon Sep 17 00:00:00 2001 From: Martin Mulholland Date: Wed, 10 Feb 2021 17:02:54 -0500 Subject: [PATCH 2/2] Modify tmp dir logic --- tests/v2/utils/test_utils.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/v2/utils/test_utils.go b/tests/v2/utils/test_utils.go index fa1b26be..bcd04654 100644 --- a/tests/v2/utils/test_utils.go +++ b/tests/v2/utils/test_utils.go @@ -67,13 +67,11 @@ func init() { // Returns the name of the created directory. func CreateTempDir(subdir string) string { tempDir := tmpDir + subdir + "/" - if _, err := os.Stat(tempDir); os.IsNotExist(err) { - if err = os.Mkdir(tempDir, 0755); err != nil { - LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err)) - // if cannot create subdirectory just use the base tmp directory - tempDir = tmpDir - } - } else if err != nil { + var err error + if _, err = os.Stat(tempDir); os.IsNotExist(err) { + err = os.Mkdir(tempDir, 0755) + } + if err != nil { // if cannot create subdirectory just use the base tmp directory LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err)) tempDir = tmpDir