Skip to content

Commit

Permalink
test: add tests for replicateProcessFileCallBack (#1066)
Browse files Browse the repository at this point in the history
* test: add tests for replicateProcessFileCallBack
* refactor: add IBM licence header, update test names, check copied file contents, use static empty file for testing

Signed-off-by: prakharagarwal1 <prakharagarwal3031@gmail.com>
  • Loading branch information
Prakhar-Agarwal-byte committed Aug 1, 2023
1 parent 8aa892e commit f62294e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
139 changes: 139 additions & 0 deletions filesystem/replicator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 (
"bytes"
"io/ioutil"
"os"
"testing"
)

func TestReplicateProcessFileCallBack(t *testing.T) {
t.Run("test for source file does not exist, should return an error", func(t *testing.T) {
sourceFilePath := "test_data/replicator_test/non_existent_file.txt"
destinationFilePath := "test_data/replicator_test/destination.txt"
err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err == nil {
t.Errorf("Expected error for non-existent source file, got nil")
}
})

t.Run("test for destination file doesn't exist, should copy the source file", func(t *testing.T) {
sourceFilePath := "test_data/replicator_test/emptyfile.txt"
destinationFilePath := "test_data/replicator_test/destination_file.txt"

err := replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

os.Remove(destinationFilePath)
})

t.Run("test for destination file exists but is different, should copy the source file", func(t *testing.T) {
sourceFilePath := "test_data/replicator_test/source_file.txt"
destinationFilePath := "test_data/replicator_test/destination_file.txt"

sourceFile, err := os.Create(sourceFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
sourceFile.Close()
os.Remove(sourceFilePath)
}()

_, err = sourceFile.WriteString("hello, world!")
if err != nil {
t.Fatal(err)
}

destinationFile, err := os.Create(destinationFilePath)
if err != nil {
t.Fatal(err)
}
defer func() {
destinationFile.Close()
os.Remove(destinationFilePath)
}()

_, err = destinationFile.WriteString("hello, go!")
if err != nil {
t.Fatal(err)
}

err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

// Check if the source file content matches the destination file content after copying
sourceContent, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
t.Fatal(err)
}

destinationContent, err := ioutil.ReadFile(destinationFilePath)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(sourceContent, destinationContent) {
t.Errorf("Expected destination content to be equal to source content, but they are different.")
}
})

t.Run("test for destination file exists and is the same, should return nil without copying", func(t *testing.T) {
sourceFilePath := "test_data/replicator_test/source_same.txt"
destinationFilePath := "test_data/replicator_test/destination_same.txt"

// Store the destination file's modification time before calling replicateProcessFileCallBack
info, err := os.Stat(destinationFilePath)
if err != nil {
t.Errorf("Destination file should exist, but got an error: %s", err)
}
originalModTime := info.ModTime()
err = replicateProcessFileCallBack(sourceFilePath, destinationFilePath, nil)
if err != nil {
t.Errorf("Expected nil error, got: %s", err)
}

// Check if the destination file still exists after calling replicateProcessFileCallBack
info, err = os.Stat(destinationFilePath)
if err != nil {
t.Errorf("Destination file should exist, but got an error: %s", err)
}

// Check if the destination file's modification time hasn't changed
updatedModTime := info.ModTime()
if !updatedModTime.Equal(originalModTime) {
t.Errorf("Destination file's modification time has changed, expected: %v, got: %v", originalModTime, updatedModTime)
}

// Check if the destination file content remains unchanged
destinationContent, err := ioutil.ReadFile(destinationFilePath)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(destinationContent, []byte("hello, world!")) {
t.Errorf("Expected destination content to remain unchanged, but got different content.")
}
})
}

1 change: 1 addition & 0 deletions filesystem/test_data/replicator_test/destination_same.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!
Empty file.
1 change: 1 addition & 0 deletions filesystem/test_data/replicator_test/source_same.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!

0 comments on commit f62294e

Please sign in to comment.