Skip to content

Commit

Permalink
add a test for auto migrations cli interface
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Jul 10, 2016
1 parent a540755 commit 5a66520
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
return
case fsrepo.ErrNeedMigration:
domigrate, found, _ := req.Option(migrateKwd).Bool()
fmt.Println("Found old repo version, migrations need to be run.")

if !found {
err = migrate.TryMigrating(fsrepo.RepoVersion)
Expand Down
8 changes: 6 additions & 2 deletions repo/fsrepo/migrations/mfsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (v VersionFileNotFound) Error() string {
}

func TryMigrating(tovers int) error {
if !YesNoPrompt("run migrations automatically? [y/n]") {
if !YesNoPrompt("Run migrations automatically? [y/N]") {
return fmt.Errorf("please run the migrations manually")
}

Expand All @@ -70,15 +70,19 @@ func TryMigrating(tovers int) error {

func YesNoPrompt(prompt string) bool {
var s string
for {
for i := 0; i < 3; i++ {
fmt.Printf("%s ", prompt)
fmt.Scanf("%s", &s)
switch s {
case "y", "Y":
return true
case "n", "N":
return false
case "":
return false
}
fmt.Println("Please press either 'y' or 'n'")
}

return false
}
4 changes: 3 additions & 1 deletion repo/fsrepo/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const migrations = "fs-repo-migrations"
func RunMigration(newv int) error {
migrateBin := "fs-repo-migrations"
fmt.Println(" => checking for migrations binary...")
_, err := exec.LookPath(migrateBin)

var err error
migrateBin, err = exec.LookPath(migrateBin)
if err == nil {
// check to make sure migrations binary supports our target version
err = verifyMigrationSupportsVersion(migrateBin, newv)
Expand Down
52 changes: 52 additions & 0 deletions test/sharness/t0066-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh
#
# Copyright (c) 2016 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test migrations auto update prompt"

. lib/test-lib.sh

test_init_ipfs

test_expect_success "setup mock migrations" '
mkdir bin &&
echo "#!/bin/bash" > bin/fs-repo-migrations &&
echo "echo 4" >> bin/fs-repo-migrations &&
chmod +x bin/fs-repo-migrations &&
export PATH="$(pwd)/bin":$PATH
'

test_expect_success "manually reset repo version to 3" '
echo "3" > "$IPFS_PATH"/version
'

test_expect_success "ipfs daemon --migrate=false fails" '
test_expect_code 1 ipfs daemon --migrate=false 2> false_out
'

test_expect_success "output looks good" '
grep "ipfs repo needs migration" false_out
'

test_expect_success "ipfs daemon --migrate=true runs migration" '
test_expect_code 1 ipfs daemon --migrate=true > true_out
'

test_expect_success "output looks good" '
grep "running migration" true_out > /dev/null &&
grep "binary completed successfully" true_out > /dev/null
'

test_expect_success "'ipfs daemon' prompts to auto migrate" '
test_expect_code 1 ipfs daemon > daemon_out 2> daemon_err
'

test_expect_success "output looks good" '
grep "Found old repo version" daemon_out > /dev/null &&
grep "Run migrations automatically?" daemon_out > /dev/null &&
grep "please run the migrations manually" daemon_err > /dev/null
'

test_done

0 comments on commit 5a66520

Please sign in to comment.