From 50463f995a6b3e62554374a3a452da4fef5ba3ae Mon Sep 17 00:00:00 2001 From: satyazzz123 Date: Wed, 3 Jan 2024 23:03:16 +0530 Subject: [PATCH 1/4] added tests for delta.go functions Signed-off-by: satyazzz123 --- filesystem/delta_test.go | 128 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 filesystem/delta_test.go diff --git a/filesystem/delta_test.go b/filesystem/delta_test.go new file mode 100644 index 000000000..44eb2c8f9 --- /dev/null +++ b/filesystem/delta_test.go @@ -0,0 +1,128 @@ +/* + * Copyright IBM Corporation 2021 + * + * Licensed 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 filesystem + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestGenerateDelta(t *testing.T) { + t.Run("this test scenario covers the creation of source and destination directory and successful execution of function", func(t *testing.T) { + + tempDir, err := ioutil.TempDir("", "test_generate_delta") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tempDir) + + // Create source and destination directories + sourceDir := filepath.Join(tempDir, "source") + destinationDir := filepath.Join(tempDir, "destination") + if err := os.Mkdir(sourceDir, 0755); err != nil { + t.Fatalf("Failed to create source directory: %v", err) + } + if err := os.Mkdir(destinationDir, 0755); err != nil { + t.Fatalf("Failed to create destination directory: %v", err) + } + + err = GenerateDelta(sourceDir, destinationDir, tempDir) + + // Check for errors + if err != nil { + t.Errorf("Unexpected error during GenerateDelta: %v", err) + } + + }) + + t.Run("this test covers the scenario for the presence of a modifications file and its content after calling the function", func(t *testing.T) { + + tempDir, err := ioutil.TempDir("", "test_generate_delta") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tempDir) + + sourceDir := filepath.Join(tempDir, "source") + destinationDir := filepath.Join(tempDir, "destination") + if err := os.Mkdir(sourceDir, 0755); err != nil { + t.Fatalf("Failed to create source directory: %v", err) + } + if err := os.Mkdir(destinationDir, 0755); err != nil { + t.Fatalf("Failed to create destination directory: %v", err) + } + + sourceFilePath := filepath.Join(sourceDir, "file.txt") + if err := ioutil.WriteFile(sourceFilePath, []byte("content"), 0644); err != nil { + t.Fatalf("Failed to create source file: %v", err) + } + + err = GenerateDelta(sourceDir, destinationDir, tempDir) + + if err != nil { + t.Errorf("Unexpected error during GenerateDelta: %v", err) + } + + // Check for modifications file + modificationsFilePath := filepath.Join(tempDir, "modifications", "file.txt") + if _, err := os.Stat(modificationsFilePath); os.IsNotExist(err) { + t.Errorf("Expected modifications file to be created, but it doesn't exist") + } + }) +} + +func TestGenerateDeltaAdditionCallBack(t *testing.T) { + t.Run("this test covers the scenario where function correctly replicates a source file to the additions directory based on the provided configuration and if the replicated file is created in the expected location", func(t *testing.T) { + + tempDir, err := ioutil.TempDir("", "test_generate_delta_addition") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tempDir) + + // Create source and destination files + sourceFile := filepath.Join(tempDir, "source.txt") + destinationDir := filepath.Join(tempDir, "destination") + if err := os.Mkdir(destinationDir, 0755); err != nil { + t.Fatalf("Failed to create destination directory: %v", err) + } + + // Source file contents present + content := []byte("Hello, move2kubee!") + if err := ioutil.WriteFile(sourceFile, content, 0644); err != nil { + t.Fatalf("Failed to create source file: %v", err) + } + + config := generateDeltaConfig{ + store: tempDir, + sourceDirectory: tempDir, + destinationDirectory: destinationDir, + } + + err = generateDeltaAdditionCallBack(sourceFile, filepath.Join(destinationDir, "newfile.txt"), config) + + if err != nil { + t.Errorf("Unexpected error during generateDeltaAdditionCallBack: %v", err) + } + + replicatedFilePath := filepath.Join(tempDir, additionsDir, "newfile.txt") + if _, err := os.Stat(replicatedFilePath); os.IsNotExist(err) { + t.Errorf("Replicated file not found at expected path: %s", replicatedFilePath) + } + }) +} From 8c62d1973f97dd2816272dcf52fa17ccb7deb996 Mon Sep 17 00:00:00 2001 From: satyazzz123 Date: Mon, 8 Jan 2024 16:01:24 +0530 Subject: [PATCH 2/4] added checks for destinatioDir and file contents Signed-off-by: satyazzz123 --- filesystem/delta_test.go | 95 +++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 16 deletions(-) diff --git a/filesystem/delta_test.go b/filesystem/delta_test.go index 44eb2c8f9..99e939b14 100644 --- a/filesystem/delta_test.go +++ b/filesystem/delta_test.go @@ -16,42 +16,79 @@ package filesystem import ( + "bytes" "io/ioutil" "os" "path/filepath" + "reflect" "testing" ) func TestGenerateDelta(t *testing.T) { - t.Run("this test scenario covers the creation of source and destination directory and successful execution of function", func(t *testing.T) { + t.Run("This test scenario covers the creation of source and destination directory and successful execution of function", func(t *testing.T) { + sourceDir, err := ioutil.TempDir("", "source") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(sourceDir) - tempDir, err := ioutil.TempDir("", "test_generate_delta") + destinationDir, err := ioutil.TempDir("", "destination") if err != nil { - t.Fatalf("Failed to create temporary directory: %v", err) + t.Fatal(err) } - defer os.RemoveAll(tempDir) + defer os.RemoveAll(destinationDir) - // Create source and destination directories - sourceDir := filepath.Join(tempDir, "source") - destinationDir := filepath.Join(tempDir, "destination") - if err := os.Mkdir(sourceDir, 0755); err != nil { - t.Fatalf("Failed to create source directory: %v", err) + storeDir, err := ioutil.TempDir("", "store") + if err != nil { + t.Fatal(err) } - if err := os.Mkdir(destinationDir, 0755); err != nil { - t.Fatalf("Failed to create destination directory: %v", err) + defer os.RemoveAll(storeDir) + + // Add a file to the source directory that isn't in the destination + sourceOnlyFile := filepath.Join(sourceDir, "source_only.txt") + if err := ioutil.WriteFile(sourceOnlyFile, []byte("source only"), 0644); err != nil { + t.Fatal(err) } - err = GenerateDelta(sourceDir, destinationDir, tempDir) + // Add a file to both directories and then modify it in the source + commonFile := "common.txt" + sourceCommonFilePath := filepath.Join(sourceDir, commonFile) + destCommonFilePath := filepath.Join(destinationDir, commonFile) + if err := ioutil.WriteFile(sourceCommonFilePath, []byte("original"), 0644); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(destCommonFilePath, []byte("original"), 0644); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(sourceCommonFilePath, []byte("modified"), 0644); err != nil { + t.Fatal(err) + } - // Check for errors + // Call GenerateDelta + err = GenerateDelta(sourceDir, destinationDir, storeDir) if err != nil { - t.Errorf("Unexpected error during GenerateDelta: %v", err) + t.Errorf("GenerateDelta returned an error: %v", err) + } + + // Check that the destination directory has the expected contents + expectedFiles := []string{commonFile} + files, err := ioutil.ReadDir(destinationDir) + if err != nil { + t.Fatal(err) + } + + var destFiles []string + for _, file := range files { + destFiles = append(destFiles, file.Name()) + } + + if !reflect.DeepEqual(expectedFiles, destFiles) { + t.Errorf("destination directory contents mismatch. got: %v, want: %v", destFiles, expectedFiles) } }) t.Run("this test covers the scenario for the presence of a modifications file and its content after calling the function", func(t *testing.T) { - tempDir, err := ioutil.TempDir("", "test_generate_delta") if err != nil { t.Fatalf("Failed to create temporary directory: %v", err) @@ -78,12 +115,24 @@ func TestGenerateDelta(t *testing.T) { t.Errorf("Unexpected error during GenerateDelta: %v", err) } - // Check for modifications file + // Check for modifications file existence modificationsFilePath := filepath.Join(tempDir, "modifications", "file.txt") if _, err := os.Stat(modificationsFilePath); os.IsNotExist(err) { t.Errorf("Expected modifications file to be created, but it doesn't exist") } + + // Check modifications file contents + contents, err := ioutil.ReadFile(modificationsFilePath) + if err != nil { + t.Errorf("Failed to read modifications file: %v", err) + } + + expectedContents := []byte("content") + if !bytes.Equal(contents, expectedContents) { + t.Errorf("Modifications file contents mismatch. got: %v, want: %v", contents, expectedContents) + } }) + } func TestGenerateDeltaAdditionCallBack(t *testing.T) { @@ -124,5 +173,19 @@ func TestGenerateDeltaAdditionCallBack(t *testing.T) { if _, err := os.Stat(replicatedFilePath); os.IsNotExist(err) { t.Errorf("Replicated file not found at expected path: %s", replicatedFilePath) } + + // Check the contents of the replicated file + replicatedContent, err := ioutil.ReadFile(replicatedFilePath) + if err != nil { + t.Fatalf("Failed to read replicated file: %v", err) + } + + // Define the expected content + expectedContent := []byte("Hello, move2kubee!") + + // Compare the contents + if !bytes.Equal(replicatedContent, expectedContent) { + t.Errorf("Replicated file content mismatch. got: %s, want: %s", replicatedContent, expectedContent) + } }) } From 99bc3c5dd784b4d4ed0635f9019390b02bd4a8ca Mon Sep 17 00:00:00 2001 From: satyazzz123 Date: Mon, 8 Jan 2024 16:55:19 +0530 Subject: [PATCH 3/4] added checks for destinatioDir and file contents Signed-off-by: satyazzz123 --- filesystem/delta_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/filesystem/delta_test.go b/filesystem/delta_test.go index 99e939b14..f99e8b5d7 100644 --- a/filesystem/delta_test.go +++ b/filesystem/delta_test.go @@ -28,26 +28,26 @@ func TestGenerateDelta(t *testing.T) { t.Run("This test scenario covers the creation of source and destination directory and successful execution of function", func(t *testing.T) { sourceDir, err := ioutil.TempDir("", "source") if err != nil { - t.Fatal(err) + t.Fatalf("failed to create source directory") } defer os.RemoveAll(sourceDir) destinationDir, err := ioutil.TempDir("", "destination") if err != nil { - t.Fatal(err) + t.Fatalf("failed to create destination directory") } defer os.RemoveAll(destinationDir) storeDir, err := ioutil.TempDir("", "store") if err != nil { - t.Fatal(err) + t.Fatalf("failed to create store directory") } defer os.RemoveAll(storeDir) // Add a file to the source directory that isn't in the destination sourceOnlyFile := filepath.Join(sourceDir, "source_only.txt") if err := ioutil.WriteFile(sourceOnlyFile, []byte("source only"), 0644); err != nil { - t.Fatal(err) + t.Fatalf("Failed to add to source only file: %v", err) } // Add a file to both directories and then modify it in the source @@ -55,13 +55,16 @@ func TestGenerateDelta(t *testing.T) { sourceCommonFilePath := filepath.Join(sourceDir, commonFile) destCommonFilePath := filepath.Join(destinationDir, commonFile) if err := ioutil.WriteFile(sourceCommonFilePath, []byte("original"), 0644); err != nil { - t.Fatal(err) + t.Fatalf("Failed to write original content to source common file: %v", err) + } if err := ioutil.WriteFile(destCommonFilePath, []byte("original"), 0644); err != nil { - t.Fatal(err) + t.Fatalf("Failed to write original content to destinatio common file: %v", err) + } if err := ioutil.WriteFile(sourceCommonFilePath, []byte("modified"), 0644); err != nil { - t.Fatal(err) + t.Fatalf("Failed to modify common file in source directory: %v", err) + } // Call GenerateDelta @@ -74,7 +77,7 @@ func TestGenerateDelta(t *testing.T) { expectedFiles := []string{commonFile} files, err := ioutil.ReadDir(destinationDir) if err != nil { - t.Fatal(err) + t.Errorf("destination directory contents mismatch. got: %v, want: %v", destFiles, expectedFiles) } var destFiles []string From c48391b4869611b79bed1843876c54072d9e4f58 Mon Sep 17 00:00:00 2001 From: satyazzz123 Date: Mon, 8 Jan 2024 17:14:26 +0530 Subject: [PATCH 4/4] added checks for destinatioDir and file contents Signed-off-by: satyazzz123 --- filesystem/delta_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/filesystem/delta_test.go b/filesystem/delta_test.go index f99e8b5d7..46d41e58d 100644 --- a/filesystem/delta_test.go +++ b/filesystem/delta_test.go @@ -40,7 +40,7 @@ func TestGenerateDelta(t *testing.T) { storeDir, err := ioutil.TempDir("", "store") if err != nil { - t.Fatalf("failed to create store directory") + t.Fatal("failed to create source directory") } defer os.RemoveAll(storeDir) @@ -56,15 +56,12 @@ func TestGenerateDelta(t *testing.T) { destCommonFilePath := filepath.Join(destinationDir, commonFile) if err := ioutil.WriteFile(sourceCommonFilePath, []byte("original"), 0644); err != nil { t.Fatalf("Failed to write original content to source common file: %v", err) - } if err := ioutil.WriteFile(destCommonFilePath, []byte("original"), 0644); err != nil { - t.Fatalf("Failed to write original content to destinatio common file: %v", err) - + t.Fatalf("Failed to write original content to destination common file: %v", err) } if err := ioutil.WriteFile(sourceCommonFilePath, []byte("modified"), 0644); err != nil { t.Fatalf("Failed to modify common file in source directory: %v", err) - } // Call GenerateDelta @@ -77,7 +74,7 @@ func TestGenerateDelta(t *testing.T) { expectedFiles := []string{commonFile} files, err := ioutil.ReadDir(destinationDir) if err != nil { - t.Errorf("destination directory contents mismatch. got: %v, want: %v", destFiles, expectedFiles) + t.Fatalf("failed to read destination directory %v", err) } var destFiles []string