Add comprehensive test coverage for core functionalities
This commit introduces test cases for the API, archive, store, and filesystem functionalities, as well as a functional test for a full workflow. It ensures robust testing for snapshot operations, archiving, and blob management, significantly improving reliability.
This commit is contained in:
95
hash/hash_test.go
Normal file
95
hash/hash_test.go
Normal file
@ -0,0 +1,95 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCalculateFileHash(t *testing.T) {
|
||||
// Create a temporary directory for tests
|
||||
tempDir, err := os.MkdirTemp("", "agate-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir) // Clean up after test
|
||||
|
||||
// Create a test file with known content
|
||||
testContent := "This is a test file for hashing"
|
||||
testFilePath := filepath.Join(tempDir, "test_file.txt")
|
||||
if err := os.WriteFile(testFilePath, []byte(testContent), 0644); err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// Calculate the expected hash manually
|
||||
hasher := sha256.New()
|
||||
hasher.Write([]byte(testContent))
|
||||
expectedHash := hex.EncodeToString(hasher.Sum(nil))
|
||||
|
||||
// Calculate the hash using the function
|
||||
hash, err := CalculateFileHash(testFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to calculate file hash: %v", err)
|
||||
}
|
||||
|
||||
// Check that the hash matches the expected value
|
||||
if hash != expectedHash {
|
||||
t.Errorf("Hash does not match: got %s, want %s", hash, expectedHash)
|
||||
}
|
||||
|
||||
// Test with a non-existent file
|
||||
_, err = CalculateFileHash(filepath.Join(tempDir, "nonexistent.txt"))
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error when calculating hash of non-existent file, got nil")
|
||||
}
|
||||
|
||||
// Test with a directory
|
||||
dirPath := filepath.Join(tempDir, "test_dir")
|
||||
if err := os.MkdirAll(dirPath, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test directory: %v", err)
|
||||
}
|
||||
_, err = CalculateFileHash(dirPath)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error when calculating hash of a directory, got nil")
|
||||
}
|
||||
|
||||
// Test with an empty file
|
||||
emptyFilePath := filepath.Join(tempDir, "empty_file.txt")
|
||||
if err := os.WriteFile(emptyFilePath, []byte{}, 0644); err != nil {
|
||||
t.Fatalf("Failed to create empty test file: %v", err)
|
||||
}
|
||||
emptyHash, err := CalculateFileHash(emptyFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to calculate hash of empty file: %v", err)
|
||||
}
|
||||
// The SHA-256 hash of an empty string is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
|
||||
expectedEmptyHash := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
if emptyHash != expectedEmptyHash {
|
||||
t.Errorf("Empty file hash does not match: got %s, want %s", emptyHash, expectedEmptyHash)
|
||||
}
|
||||
|
||||
// Test with a large file
|
||||
largeFilePath := filepath.Join(tempDir, "large_file.bin")
|
||||
largeFileSize := 1024 * 1024 // 1 MB
|
||||
largeFile, err := os.Create(largeFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create large test file: %v", err)
|
||||
}
|
||||
// Fill the file with a repeating pattern
|
||||
pattern := []byte("0123456789")
|
||||
for i := 0; i < largeFileSize/len(pattern); i++ {
|
||||
if _, err := largeFile.Write(pattern); err != nil {
|
||||
largeFile.Close()
|
||||
t.Fatalf("Failed to write to large test file: %v", err)
|
||||
}
|
||||
}
|
||||
largeFile.Close()
|
||||
|
||||
// Calculate the hash of the large file
|
||||
_, err = CalculateFileHash(largeFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to calculate hash of large file: %v", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user