test-versioning-providers.sh
1 #!/bin/bash 2 # Test script for S3 and SFTP versioning providers 3 4 # Set up colors for output 5 GREEN='\033[0;32m' 6 RED='\033[0;31m' 7 YELLOW='\033[1;33m' 8 NC='\033[0m' # No Color 9 10 echo -e "${YELLOW}Testing S3 and SFTP Versioning Providers${NC}" 11 echo "======================================" 12 13 # Create a test directory 14 TEST_DIR="./test-versioning-providers" 15 mkdir -p $TEST_DIR 16 echo -e "${GREEN}Created test directory: $TEST_DIR${NC}" 17 18 # Create test files with initial content 19 S3_TEST_FILE="$TEST_DIR/s3-test-file.txt" 20 SFTP_TEST_FILE="$TEST_DIR/sftp-test-file.txt" 21 echo "Initial content - Version 1" > $S3_TEST_FILE 22 echo "Initial content - Version 1" > $SFTP_TEST_FILE 23 echo -e "${GREEN}Created test files: $S3_TEST_FILE, $SFTP_TEST_FILE${NC}" 24 25 # Create configuration files 26 S3_CONFIG_FILE="$TEST_DIR/s3-config.json" 27 SFTP_CONFIG_FILE="$TEST_DIR/sftp-config.json" 28 29 cat > $S3_CONFIG_FILE << EOF 30 { 31 "provider": "s3", 32 "endpoint": "http://localhost:9000", 33 "access_key": "minioadmin", 34 "secret_key": "minioadmin", 35 "region": "us-east-1", 36 "bucket": "test-bucket", 37 "use_ssl": false, 38 "versioning_enabled": true, 39 "max_versions": 10 40 } 41 EOF 42 43 cat > $SFTP_CONFIG_FILE << EOF 44 { 45 "provider": "sftp", 46 "host": "localhost", 47 "port": 22, 48 "username": "test", 49 "password": "password", 50 "versioning_enabled": true, 51 "diff_versioning_enabled": true, 52 "auto_version": true, 53 "max_versions": 10, 54 "diff_threshold": 1024, 55 "full_version_interval": 5, 56 "version_directory": ".versions", 57 "compression_enabled": true, 58 "binary_diff_enabled": true 59 } 60 EOF 61 62 echo -e "${GREEN}Created configuration files: $S3_CONFIG_FILE, $SFTP_CONFIG_FILE${NC}" 63 64 # Test S3 versioning 65 echo -e "${YELLOW}Testing S3 versioning...${NC}" 66 67 # Start a local MinIO server for testing 68 echo -e "${YELLOW}Starting MinIO server...${NC}" 69 docker run -d --name minio-test -p 9000:9000 -e "MINIO_ACCESS_KEY=minioadmin" -e "MINIO_SECRET_KEY=minioadmin" minio/minio server /data 70 if [ $? -ne 0 ]; then 71 echo -e "${RED}Failed to start MinIO server${NC}" 72 echo -e "${YELLOW}Checking if MinIO is already running...${NC}" 73 MINIO_RUNNING=$(docker ps | grep minio-test) 74 if [ -z "$MINIO_RUNNING" ]; then 75 echo -e "${RED}MinIO is not running and could not be started${NC}" 76 exit 1 77 else 78 echo -e "${GREEN}MinIO is already running${NC}" 79 fi 80 else 81 echo -e "${GREEN}MinIO server started${NC}" 82 # Wait for MinIO to start 83 sleep 5 84 85 # Create the test bucket 86 echo -e "${YELLOW}Creating test bucket...${NC}" 87 docker exec minio-test mkdir -p /data/test-bucket 88 if [ $? -ne 0 ]; then 89 echo -e "${RED}Failed to create test bucket${NC}" 90 docker stop minio-test 91 docker rm minio-test 92 exit 1 93 fi 94 echo -e "${GREEN}Test bucket created${NC}" 95 fi 96 97 # Set up environment variables for testing 98 export KEEPSYNC_PROVIDER_TYPE="s3" 99 export KEEPSYNC_PROVIDER_CONFIG="$S3_CONFIG_FILE" 100 export KEEPSYNC_TEST_MODE="true" 101 102 # Build the CLI 103 echo -e "${YELLOW}Building the CLI...${NC}" 104 ./scripts/integrated-build.sh build 105 if [ $? -ne 0 ]; then 106 echo -e "${RED}Failed to build the CLI${NC}" 107 docker stop minio-test 108 docker rm minio-test 109 exit 1 110 fi 111 echo -e "${GREEN}CLI built successfully${NC}" 112 113 # Test S3 version operations 114 echo -e "${YELLOW}Testing S3 version operations...${NC}" 115 116 # Upload the initial file 117 echo -e "${YELLOW}Uploading initial file...${NC}" 118 ./bin/keepsync sync $S3_TEST_FILE s3://test-bucket/test-file.txt 119 if [ $? -ne 0 ]; then 120 echo -e "${RED}Failed to upload initial file${NC}" 121 docker stop minio-test 122 docker rm minio-test 123 exit 1 124 fi 125 echo -e "${GREEN}Initial file uploaded successfully${NC}" 126 127 # List versions (should show one version) 128 echo -e "${YELLOW}Listing versions (should show one version)...${NC}" 129 ./bin/keepsync version list s3://test-bucket/test-file.txt 130 if [ $? -ne 0 ]; then 131 echo -e "${RED}Failed to list versions${NC}" 132 docker stop minio-test 133 docker rm minio-test 134 exit 1 135 fi 136 echo -e "${GREEN}Version list command executed successfully${NC}" 137 138 # Create a second version 139 echo -e "${YELLOW}Creating a second version...${NC}" 140 echo "Modified content - Version 2" > $S3_TEST_FILE 141 ./bin/keepsync sync $S3_TEST_FILE s3://test-bucket/test-file.txt 142 if [ $? -ne 0 ]; then 143 echo -e "${RED}Failed to upload second version${NC}" 144 docker stop minio-test 145 docker rm minio-test 146 exit 1 147 fi 148 echo -e "${GREEN}Second version uploaded successfully${NC}" 149 150 # List versions (should show two versions) 151 echo -e "${YELLOW}Listing versions (should show two versions)...${NC}" 152 ./bin/keepsync version list s3://test-bucket/test-file.txt 153 if [ $? -ne 0 ]; then 154 echo -e "${RED}Failed to list versions${NC}" 155 docker stop minio-test 156 docker rm minio-test 157 exit 1 158 fi 159 echo -e "${GREEN}Version list command executed successfully${NC}" 160 161 # Get the first version 162 echo -e "${YELLOW}Getting the first version...${NC}" 163 FIRST_VERSION=$(./bin/keepsync version list s3://test-bucket/test-file.txt | grep -v "VERSION" | tail -1 | awk '{print $1}') 164 ./bin/keepsync version get s3://test-bucket/test-file.txt $FIRST_VERSION $TEST_DIR/s3-test-file-v1.txt 165 if [ $? -ne 0 ]; then 166 echo -e "${RED}Failed to get first version${NC}" 167 docker stop minio-test 168 docker rm minio-test 169 exit 1 170 fi 171 echo -e "${GREEN}First version retrieved successfully${NC}" 172 173 # Verify the content of the retrieved file 174 CONTENT=$(cat $TEST_DIR/s3-test-file-v1.txt) 175 if [ "$CONTENT" != "Initial content - Version 1" ]; then 176 echo -e "${RED}File content does not match the expected content${NC}" 177 echo -e "${RED}Expected: Initial content - Version 1${NC}" 178 echo -e "${RED}Actual: $CONTENT${NC}" 179 docker stop minio-test 180 docker rm minio-test 181 exit 1 182 fi 183 echo -e "${GREEN}File content matches the expected content${NC}" 184 185 # Restore the first version 186 echo -e "${YELLOW}Restoring the first version...${NC}" 187 ./bin/keepsync version restore s3://test-bucket/test-file.txt $FIRST_VERSION 188 if [ $? -ne 0 ]; then 189 echo -e "${RED}Failed to restore first version${NC}" 190 docker stop minio-test 191 docker rm minio-test 192 exit 1 193 fi 194 echo -e "${GREEN}First version restored successfully${NC}" 195 196 # Download the restored file 197 echo -e "${YELLOW}Downloading the restored file...${NC}" 198 ./bin/keepsync sync s3://test-bucket/test-file.txt $TEST_DIR/s3-test-file-restored.txt 199 if [ $? -ne 0 ]; then 200 echo -e "${RED}Failed to download restored file${NC}" 201 docker stop minio-test 202 docker rm minio-test 203 exit 1 204 fi 205 echo -e "${GREEN}Restored file downloaded successfully${NC}" 206 207 # Verify the content of the restored file 208 CONTENT=$(cat $TEST_DIR/s3-test-file-restored.txt) 209 if [ "$CONTENT" != "Initial content - Version 1" ]; then 210 echo -e "${RED}Restored file content does not match the expected content${NC}" 211 echo -e "${RED}Expected: Initial content - Version 1${NC}" 212 echo -e "${RED}Actual: $CONTENT${NC}" 213 docker stop minio-test 214 docker rm minio-test 215 exit 1 216 fi 217 echo -e "${GREEN}Restored file content matches the expected content${NC}" 218 219 # Clean up S3 test 220 echo -e "${YELLOW}Cleaning up S3 test...${NC}" 221 docker stop minio-test 222 docker rm minio-test 223 echo -e "${GREEN}S3 test cleanup completed${NC}" 224 225 # Test SFTP versioning 226 echo -e "${YELLOW}Testing SFTP versioning...${NC}" 227 228 # Start a local SFTP server for testing 229 echo -e "${YELLOW}Starting SFTP server...${NC}" 230 docker run -d --name sftp-test -p 22:22 -e "SFTP_USERS=test:password:1001" atmoz/sftp 231 if [ $? -ne 0 ]; then 232 echo -e "${RED}Failed to start SFTP server${NC}" 233 echo -e "${YELLOW}Checking if SFTP is already running...${NC}" 234 SFTP_RUNNING=$(docker ps | grep sftp-test) 235 if [ -z "$SFTP_RUNNING" ]; then 236 echo -e "${RED}SFTP is not running and could not be started${NC}" 237 exit 1 238 else 239 echo -e "${GREEN}SFTP is already running${NC}" 240 fi 241 else 242 echo -e "${GREEN}SFTP server started${NC}" 243 # Wait for SFTP to start 244 sleep 5 245 fi 246 247 # Set up environment variables for testing 248 export KEEPSYNC_PROVIDER_TYPE="sftp" 249 export KEEPSYNC_PROVIDER_CONFIG="$SFTP_CONFIG_FILE" 250 251 # Test SFTP version operations 252 echo -e "${YELLOW}Testing SFTP version operations...${NC}" 253 254 # Upload the initial file 255 echo -e "${YELLOW}Uploading initial file...${NC}" 256 ./bin/keepsync sync $SFTP_TEST_FILE sftp://test@localhost/upload/test-file.txt 257 if [ $? -ne 0 ]; then 258 echo -e "${RED}Failed to upload initial file${NC}" 259 docker stop sftp-test 260 docker rm sftp-test 261 exit 1 262 fi 263 echo -e "${GREEN}Initial file uploaded successfully${NC}" 264 265 # List versions (should show one version) 266 echo -e "${YELLOW}Listing versions (should show one version)...${NC}" 267 ./bin/keepsync version list sftp://test@localhost/upload/test-file.txt 268 if [ $? -ne 0 ]; then 269 echo -e "${RED}Failed to list versions${NC}" 270 docker stop sftp-test 271 docker rm sftp-test 272 exit 1 273 fi 274 echo -e "${GREEN}Version list command executed successfully${NC}" 275 276 # Create a second version 277 echo -e "${YELLOW}Creating a second version...${NC}" 278 echo "Modified content - Version 2" > $SFTP_TEST_FILE 279 ./bin/keepsync sync $SFTP_TEST_FILE sftp://test@localhost/upload/test-file.txt 280 if [ $? -ne 0 ]; then 281 echo -e "${RED}Failed to upload second version${NC}" 282 docker stop sftp-test 283 docker rm sftp-test 284 exit 1 285 fi 286 echo -e "${GREEN}Second version uploaded successfully${NC}" 287 288 # List versions (should show two versions) 289 echo -e "${YELLOW}Listing versions (should show two versions)...${NC}" 290 ./bin/keepsync version list sftp://test@localhost/upload/test-file.txt 291 if [ $? -ne 0 ]; then 292 echo -e "${RED}Failed to list versions${NC}" 293 docker stop sftp-test 294 docker rm sftp-test 295 exit 1 296 fi 297 echo -e "${GREEN}Version list command executed successfully${NC}" 298 299 # Get the first version 300 echo -e "${YELLOW}Getting the first version...${NC}" 301 FIRST_VERSION=$(./bin/keepsync version list sftp://test@localhost/upload/test-file.txt | grep -v "VERSION" | tail -1 | awk '{print $1}') 302 ./bin/keepsync version get sftp://test@localhost/upload/test-file.txt $FIRST_VERSION $TEST_DIR/sftp-test-file-v1.txt 303 if [ $? -ne 0 ]; then 304 echo -e "${RED}Failed to get first version${NC}" 305 docker stop sftp-test 306 docker rm sftp-test 307 exit 1 308 fi 309 echo -e "${GREEN}First version retrieved successfully${NC}" 310 311 # Verify the content of the retrieved file 312 CONTENT=$(cat $TEST_DIR/sftp-test-file-v1.txt) 313 if [ "$CONTENT" != "Initial content - Version 1" ]; then 314 echo -e "${RED}File content does not match the expected content${NC}" 315 echo -e "${RED}Expected: Initial content - Version 1${NC}" 316 echo -e "${RED}Actual: $CONTENT${NC}" 317 docker stop sftp-test 318 docker rm sftp-test 319 exit 1 320 fi 321 echo -e "${GREEN}File content matches the expected content${NC}" 322 323 # Restore the first version 324 echo -e "${YELLOW}Restoring the first version...${NC}" 325 ./bin/keepsync version restore sftp://test@localhost/upload/test-file.txt $FIRST_VERSION 326 if [ $? -ne 0 ]; then 327 echo -e "${RED}Failed to restore first version${NC}" 328 docker stop sftp-test 329 docker rm sftp-test 330 exit 1 331 fi 332 echo -e "${GREEN}First version restored successfully${NC}" 333 334 # Download the restored file 335 echo -e "${YELLOW}Downloading the restored file...${NC}" 336 ./bin/keepsync sync sftp://test@localhost/upload/test-file.txt $TEST_DIR/sftp-test-file-restored.txt 337 if [ $? -ne 0 ]; then 338 echo -e "${RED}Failed to download restored file${NC}" 339 docker stop sftp-test 340 docker rm sftp-test 341 exit 1 342 fi 343 echo -e "${GREEN}Restored file downloaded successfully${NC}" 344 345 # Verify the content of the restored file 346 CONTENT=$(cat $TEST_DIR/sftp-test-file-restored.txt) 347 if [ "$CONTENT" != "Initial content - Version 1" ]; then 348 echo -e "${RED}Restored file content does not match the expected content${NC}" 349 echo -e "${RED}Expected: Initial content - Version 1${NC}" 350 echo -e "${RED}Actual: $CONTENT${NC}" 351 docker stop sftp-test 352 docker rm sftp-test 353 exit 1 354 fi 355 echo -e "${GREEN}Restored file content matches the expected content${NC}" 356 357 # Clean up SFTP test 358 echo -e "${YELLOW}Cleaning up SFTP test...${NC}" 359 docker stop sftp-test 360 docker rm sftp-test 361 echo -e "${GREEN}SFTP test cleanup completed${NC}" 362 363 # Clean up test directory 364 echo -e "${YELLOW}Cleaning up test directory...${NC}" 365 rm -rf $TEST_DIR 366 echo -e "${GREEN}Test directory cleanup completed${NC}" 367 368 # Reset environment variables 369 unset KEEPSYNC_PROVIDER_TYPE 370 unset KEEPSYNC_PROVIDER_CONFIG 371 unset KEEPSYNC_TEST_MODE 372 373 echo -e "${GREEN}All tests completed successfully!${NC}"