| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2016 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test migrations auto update prompt" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | test_init_ipfs |
| 12 | |
| 13 | # Remove explicit AutoConf.Enabled=false from test profile to use implicit default |
| 14 | # This allows daemon to work with 'auto' values added by v16-to-17 migration |
| 15 | ipfs config --json AutoConf.Enabled null >/dev/null 2>&1 |
| 16 | |
| 17 | MIGRATION_START=7 |
| 18 | IPFS_REPO_VER=$(<.ipfs/version) |
| 19 | |
| 20 | # Generate mock migration binaries |
| 21 | gen_mock_migrations() { |
| 22 | mkdir bin |
| 23 | i=$((MIGRATION_START)) |
| 24 | until [ $i -ge $IPFS_REPO_VER ] |
| 25 | do |
| 26 | j=$((i+1)) |
| 27 | echo "#!/bin/bash" > bin/fs-repo-${i}-to-${j} |
| 28 | echo "echo fake applying ${i}-to-${j} repo migration" >> bin/fs-repo-${i}-to-${j} |
| 29 | # Update version file to the target version for hybrid migration system |
| 30 | echo "if [ \"\$1\" = \"-path\" ] && [ -n \"\$2\" ]; then" >> bin/fs-repo-${i}-to-${j} |
| 31 | echo " echo $j > \"\$2/version\"" >> bin/fs-repo-${i}-to-${j} |
| 32 | echo "elif [ -n \"\$IPFS_PATH\" ]; then" >> bin/fs-repo-${i}-to-${j} |
| 33 | echo " echo $j > \"\$IPFS_PATH/version\"" >> bin/fs-repo-${i}-to-${j} |
| 34 | echo "fi" >> bin/fs-repo-${i}-to-${j} |
| 35 | chmod +x bin/fs-repo-${i}-to-${j} |
| 36 | ((i++)) |
| 37 | done |
| 38 | } |
| 39 | |
| 40 | # Check for expected output from each migration |
| 41 | check_migration_output() { |
| 42 | out_file="$1" |
| 43 | i=$((MIGRATION_START)) |
| 44 | until [ $i -ge $IPFS_REPO_VER ] |
| 45 | do |
| 46 | j=$((i+1)) |
| 47 | grep "applying ${i}-to-${j} repo migration" "$out_file" > /dev/null |
| 48 | ((i++)) |
| 49 | done |
| 50 | } |
| 51 | |
| 52 | # Create fake migration binaries instead of letting ipfs download from network |
| 53 | # To test downloading and running actual binaries, comment out this test. |
| 54 | test_expect_success "setup mock migrations" ' |
| 55 | gen_mock_migrations && |
| 56 | find bin -name "fs-repo-*-to-*" | wc -l > mock_count && |
| 57 | echo $((IPFS_REPO_VER-MIGRATION_START)) > expect_mock_count && |
| 58 | export PATH="$(pwd)/bin":$PATH && |
| 59 | test_cmp mock_count expect_mock_count |
| 60 | ' |
| 61 | |
| 62 | test_expect_success "manually reset repo version to $MIGRATION_START" ' |
| 63 | echo "$MIGRATION_START" > "$IPFS_PATH"/version |
| 64 | ' |
| 65 | |
| 66 | test_expect_success "ipfs daemon --migrate=false fails" ' |
| 67 | test_expect_code 1 ipfs daemon --migrate=false > false_out 2>&1 |
| 68 | ' |
| 69 | |
| 70 | test_expect_success "output looks good" ' |
| 71 | grep "Kubo repository at .* has version .* and needs to be migrated to version" false_out && |
| 72 | grep "Error: fs-repo requires migration" false_out |
| 73 | ' |
| 74 | |
| 75 | # The migrations will succeed and the daemon will continue running |
| 76 | # since the mock migrations now properly update the repo version number. |
| 77 | test_expect_success "ipfs daemon --migrate=true runs migration" ' |
| 78 | ipfs daemon --migrate=true > true_out 2>&1 & |
| 79 | DAEMON_PID=$! |
| 80 | # Wait for daemon to be ready then shutdown gracefully |
| 81 | sleep 3 && ipfs shutdown 2>/dev/null || kill $DAEMON_PID 2>/dev/null || true |
| 82 | wait $DAEMON_PID 2>/dev/null || true |
| 83 | ' |
| 84 | |
| 85 | test_expect_success "output looks good" ' |
| 86 | check_migration_output true_out && |
| 87 | (grep "Success: fs-repo migrated to version $IPFS_REPO_VER" true_out > /dev/null || |
| 88 | grep "Hybrid migration completed successfully: v$MIGRATION_START → v$IPFS_REPO_VER" true_out > /dev/null) |
| 89 | ' |
| 90 | |
| 91 | test_expect_success "reset repo version for auto-migration test" ' |
| 92 | echo "$MIGRATION_START" > "$IPFS_PATH"/version |
| 93 | ' |
| 94 | |
| 95 | test_expect_success "'ipfs daemon' prompts to auto migrate" ' |
| 96 | test_expect_code 1 ipfs daemon > daemon_out 2>&1 |
| 97 | ' |
| 98 | |
| 99 | test_expect_success "output looks good" ' |
| 100 | grep "Kubo repository at .* has version .* and needs to be migrated to version" daemon_out > /dev/null && |
| 101 | grep "Run migrations now?" daemon_out > /dev/null && |
| 102 | grep "Error: fs-repo requires migration" daemon_out > /dev/null |
| 103 | ' |
| 104 | |
| 105 | test_expect_success "ipfs repo migrate succeed" ' |
| 106 | test_expect_code 0 ipfs repo migrate > migrate_out |
| 107 | ' |
| 108 | |
| 109 | test_expect_success "output looks good" ' |
| 110 | grep "Migrating repository from version" migrate_out > /dev/null && |
| 111 | (grep "Success: fs-repo migrated to version $IPFS_REPO_VER" migrate_out > /dev/null || |
| 112 | grep "Hybrid migration completed successfully: v$MIGRATION_START → v$IPFS_REPO_VER" migrate_out > /dev/null) |
| 113 | ' |
| 114 | |
| 115 | test_expect_success "manually reset repo version to latest" ' |
| 116 | echo "$IPFS_REPO_VER" > "$IPFS_PATH"/version |
| 117 | ' |
| 118 | |
| 119 | test_expect_success "detect repo does not need migration" ' |
| 120 | test_expect_code 0 ipfs repo migrate > migrate_out |
| 121 | ' |
| 122 | |
| 123 | test_expect_success "output looks good" ' |
| 124 | grep "Repository is already at version" migrate_out > /dev/null |
| 125 | ' |
| 126 | |
| 127 | # ensure that we get a lock error if we need to migrate and the daemon is running |
| 128 | test_launch_ipfs_daemon |
| 129 | |
| 130 | test_expect_success "manually reset repo version to $MIGRATION_START" ' |
| 131 | echo "$MIGRATION_START" > "$IPFS_PATH"/version |
| 132 | ' |
| 133 | |
| 134 | test_expect_success "ipfs repo migrate fails" ' |
| 135 | test_expect_code 1 ipfs repo migrate 2> migrate_out |
| 136 | ' |
| 137 | |
| 138 | test_expect_success "output looks good" ' |
| 139 | grep "repo.lock" migrate_out > /dev/null |
| 140 | ' |
| 141 | |
| 142 | test_kill_ipfs_daemon |
| 143 | |
| 144 | test_done |