Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BEAM-13878] [Playground] Increase test coverage for the fs_tool package #16912

Merged
merged 4 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions playground/backend/internal/fs_tool/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,29 @@ const (
destinationDir = "destinationDir"
testFileMode = 0755
pipelinesFolder = "executable_files"
fileName = "file.txt"
directoryName = "incorrect"
)

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, directoryName), testFileMode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we prepare files in the directory with the name "incorrect"? This might be confusing, can we think about a better naming?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if err != nil {
return err
}
err = os.Mkdir(destinationDir, testFileMode)
if err != nil {
return err
}
filePath := filepath.Join(sourceDir, "file.txt")
filePath := filepath.Join(sourceDir, fileName)
_, err = os.Create(filePath)
return err
}
Expand All @@ -66,11 +77,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
Expand All @@ -87,7 +93,7 @@ func TestLifeCycle_CopyFile(t *testing.T) {
wantErr bool
}{
{
name: "file doesn't exist",
name: "File doesn't exist",
fields: fields{
folderGlobs: nil,
},
Expand All @@ -99,17 +105,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: directoryName,
sourceDir: sourceDir,
destinationDir: destinationDir,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -176,15 +194,15 @@ 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",
},
}, wantErr: true,
},
{
name: "source file folder path exists",
name: "Source file folder path exists",
fields: fields{
Paths: LifeCyclePaths{
AbsoluteSourceFileFolderPath: filepath.Join(baseFileFolder, "src"),
Expand Down Expand Up @@ -339,6 +357,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{
Expand Down
2 changes: 1 addition & 1 deletion playground/backend/internal/fs_tool/go_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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),
Expand Down
17 changes: 14 additions & 3 deletions playground/backend/internal/fs_tool/java_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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),
Expand Down Expand Up @@ -94,10 +94,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")
Expand All @@ -115,7 +126,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()),
Expand Down
93 changes: 93 additions & 0 deletions playground/backend/internal/fs_tool/path_checker_test.go
Original file line number Diff line number Diff line change
@@ -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: "All success",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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: "All success",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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)
}
})
}
}
2 changes: 1 addition & 1 deletion playground/backend/internal/fs_tool/python_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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),
Expand Down