From 50bb5c958c686948fd05cf24d7236e4fcf09c36a Mon Sep 17 00:00:00 2001 From: Pavel Avilov Date: Thu, 3 Mar 2022 22:57:15 +0300 Subject: [PATCH] Merge pull request #16912 from [BEAM-13878] [Playground] Increase test coverage for the fs_tool package * Increase test coverage for the fs_tool package * Rename folder * Remove useless variable * Update test names --- .../backend/internal/fs_tool/fs_test.go | 63 ++++++++++--- .../backend/internal/fs_tool/go_fs_test.go | 2 +- .../backend/internal/fs_tool/java_fs_test.go | 17 +++- .../internal/fs_tool/path_checker_test.go | 93 +++++++++++++++++++ .../internal/fs_tool/python_fs_test.go | 2 +- 5 files changed, 160 insertions(+), 17 deletions(-) create mode 100644 playground/backend/internal/fs_tool/path_checker_test.go diff --git a/playground/backend/internal/fs_tool/fs_test.go b/playground/backend/internal/fs_tool/fs_test.go index 8ff4b0d456e2..df499bb3fc69 100644 --- a/playground/backend/internal/fs_tool/fs_test.go +++ b/playground/backend/internal/fs_tool/fs_test.go @@ -31,10 +31,21 @@ const ( destinationDir = "destinationDir" testFileMode = 0755 pipelinesFolder = "executable_files" + fileName = "file.txt" + emptyFolder = "emptyFolder" ) +func TestMain(m *testing.M) { + err := prepareFiles() + if err != nil { + panic(fmt.Errorf("error during test setup: %s", err.Error())) + } + defer teardownFiles() + m.Run() +} + func prepareFiles() error { - err := os.Mkdir(sourceDir, testFileMode) + err := os.MkdirAll(filepath.Join(sourceDir, emptyFolder), testFileMode) if err != nil { return err } @@ -42,7 +53,7 @@ func prepareFiles() error { if err != nil { return err } - filePath := filepath.Join(sourceDir, "file.txt") + filePath := filepath.Join(sourceDir, fileName) _, err = os.Create(filePath) return err } @@ -67,11 +78,6 @@ func teardownFolders(baseFileFolder string) error { } func TestLifeCycle_CopyFile(t *testing.T) { - if err := prepareFiles(); err != nil { - t.Fatalf("Error during preparing files for test: %s", err) - } - defer teardownFiles() - type fields struct { folderGlobs []string Paths LifeCyclePaths @@ -88,7 +94,7 @@ func TestLifeCycle_CopyFile(t *testing.T) { wantErr bool }{ { - name: "file doesn't exist", + name: "File doesn't exist", fields: fields{ folderGlobs: nil, }, @@ -100,17 +106,29 @@ func TestLifeCycle_CopyFile(t *testing.T) { wantErr: true, }, { - name: "file exists", + name: "File exists", fields: fields{ folderGlobs: nil, }, args: args{ - fileName: "file.txt", + fileName: fileName, sourceDir: sourceDir, destinationDir: destinationDir, }, wantErr: false, }, + { + name: "Copy directory instead of file", + fields: fields{ + folderGlobs: nil, + }, + args: args{ + fileName: emptyFolder, + sourceDir: sourceDir, + destinationDir: destinationDir, + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -177,7 +195,7 @@ func TestLifeCycle_CreateSourceCodeFile(t *testing.T) { wantErr bool }{ { - name: "source file folder path doesn't exist", + name: "Source file folder path doesn't exist", fields: fields{ Paths: LifeCyclePaths{ AbsoluteSourceFileFolderPath: "src", @@ -185,7 +203,7 @@ func TestLifeCycle_CreateSourceCodeFile(t *testing.T) { }, wantErr: true, }, { - name: "source file folder path exists", + name: "Source file folder path exists", fields: fields{ Paths: LifeCyclePaths{ AbsoluteSourceFileFolderPath: filepath.Join(baseFileFolder, "src"), @@ -343,6 +361,27 @@ func TestNewLifeCycle(t *testing.T) { }, }, }, + { + name: "SCIO LifeCycle", + args: args{ + sdk: pb.Sdk_SDK_SCIO, + pipelineId: pipelineId, + pipelinesFolder: pipelinesFolder, + }, + want: &LifeCycle{ + folderGlobs: []string{baseFileFolder}, + Paths: LifeCyclePaths{ + SourceFileName: fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension), + AbsoluteSourceFileFolderPath: baseFileFolder, + AbsoluteSourceFilePath: filepath.Join(baseFileFolder, fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension)), + ExecutableFileName: fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension), + AbsoluteExecutableFileFolderPath: baseFileFolder, + AbsoluteExecutableFilePath: filepath.Join(baseFileFolder, fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension)), + AbsoluteBaseFolderPath: baseFileFolder, + AbsoluteLogFilePath: filepath.Join(baseFileFolder, logFileName), + }, + }, + }, { name: "Unavailable SDK", args: args{ diff --git a/playground/backend/internal/fs_tool/go_fs_test.go b/playground/backend/internal/fs_tool/go_fs_test.go index a0211751d16c..8021e1c906b6 100644 --- a/playground/backend/internal/fs_tool/go_fs_test.go +++ b/playground/backend/internal/fs_tool/go_fs_test.go @@ -42,7 +42,7 @@ func Test_newGoLifeCycle(t *testing.T) { { // Test case with calling newGoLifeCycle method with correct pipelineId and workingDir. // As a result, want to receive an expected go life cycle. - name: "newGoLifeCycle", + name: "NewGoLifeCycle", args: args{ pipelineId: pipelineId, pipelinesFolder: filepath.Join(workingDir, pipelinesFolder), diff --git a/playground/backend/internal/fs_tool/java_fs_test.go b/playground/backend/internal/fs_tool/java_fs_test.go index d51e7c031846..ac2cfb6970c1 100644 --- a/playground/backend/internal/fs_tool/java_fs_test.go +++ b/playground/backend/internal/fs_tool/java_fs_test.go @@ -43,7 +43,7 @@ func Test_newJavaLifeCycle(t *testing.T) { { // Test case with calling newJavaLifeCycle method with correct pipelineId and workingDir. // As a result, want to receive an expected java life cycle. - name: "newJavaLifeCycle", + name: "NewJavaLifeCycle", args: args{ pipelineId: pipelineId, pipelinesFolder: filepath.Join(workingDir, pipelinesFolder), @@ -96,10 +96,21 @@ func Test_executableName(t *testing.T) { want string wantErr bool }{ + { + // Test case with calling sourceFileName method with empty directory. + // As a result, want to receive an error. + name: "Directory is empty", + prepare: func() {}, + args: args{ + executableFolder: filepath.Join(workDir, pipelinesFolder, pipelineId.String(), "bin"), + }, + want: "", + wantErr: true, + }, { // Test case with calling sourceFileName method with correct pipelineId and workingDir. // As a result, want to receive a name that should be executed - name: "get executable name", + name: "Get executable name", prepare: func() { compiled := filepath.Join(workDir, pipelinesFolder, pipelineId.String(), compiledFolderName) filePath := filepath.Join(compiled, "temp.class") @@ -117,7 +128,7 @@ func Test_executableName(t *testing.T) { { // Test case with calling sourceFileName method with wrong directory. // As a result, want to receive an error. - name: "directory doesn't exist", + name: "Directory doesn't exist", prepare: func() {}, args: args{ executableFolder: filepath.Join(workDir, pipelineId.String()), diff --git a/playground/backend/internal/fs_tool/path_checker_test.go b/playground/backend/internal/fs_tool/path_checker_test.go new file mode 100644 index 000000000000..5a7a4e28bd93 --- /dev/null +++ b/playground/backend/internal/fs_tool/path_checker_test.go @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fs_tool + +import ( + "fmt" + "path/filepath" + "testing" +) + +func TestWrongExtension_Error(t *testing.T) { + errorMessage := "MOCK_ERROR" + type fields struct { + error string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Get error of the WrongExtension", + fields: fields{error: errorMessage}, + want: fmt.Sprintf("File has wrong extension: %v", errorMessage), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &WrongExtension{ + error: tt.fields.error, + } + if got := e.Error(); got != tt.want { + t.Errorf("Error() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCheckPathIsValid(t *testing.T) { + type args struct { + args []interface{} + } + tests := []struct { + name string + args args + want bool + wantErr bool + }{ + { + name: "File not exist", + args: args{args: []interface{}{"filePath.txt", ".txt"}}, + want: false, + wantErr: true, + }, + { + name: "Incorrect extension", + args: args{args: []interface{}{filepath.Join(sourceDir, fileName), JavaSourceFileExtension}}, + want: false, + wantErr: true, + }, + { + name: "CheckPathIsValid worked successfully", + args: args{args: []interface{}{filepath.Join(sourceDir, fileName), ".txt"}}, + want: true, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := CheckPathIsValid(tt.args.args...) + if (err != nil) != tt.wantErr { + t.Errorf("CheckPathIsValid() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("CheckPathIsValid() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/playground/backend/internal/fs_tool/python_fs_test.go b/playground/backend/internal/fs_tool/python_fs_test.go index 98ee39e9805d..c881d4640c54 100644 --- a/playground/backend/internal/fs_tool/python_fs_test.go +++ b/playground/backend/internal/fs_tool/python_fs_test.go @@ -40,7 +40,7 @@ func Test_newPythonLifeCycle(t *testing.T) { { // Test case with calling newPythonLifeCycle method with correct pipelineId and workingDir. // As a result, want to receive an expected python life cycle. - name: "newPythonLifeCycle", + name: "NewPythonLifeCycle", args: args{ pipelineId: pipelineId, pipelinesFolder: filepath.Join(workingDir, pipelinesFolder),