@cryptotaxi247 / kubo / commits / 37673c85c

add a test for auto migrations cli interface

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Jul 6, 2016 at 15:22 UTC 37673c85c700d5e4c9b9e1e89c7dff38c9b95c14
4 files changed +62 -3
cmd/ipfs/daemon.go
+1
@@ -225,6 +225,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
225 return
226 case fsrepo.ErrNeedMigration:
227 domigrate, found, _ := req.Option(migrateKwd).Bool()
228 + fmt.Println("Found old repo version, migrations need to be run.")
229
230 if !found {
231 err = migrate.TryMigrating(fsrepo.RepoVersion)
repo/fsrepo/migrations/mfsr.go
+6 -2
@@ -61,7 +61,7 @@ func (v VersionFileNotFound) Error() string {
61 }
62
63 func TryMigrating(tovers int) error {
64 - if !YesNoPrompt("run migrations automatically? [y/n]") {
64 + if !YesNoPrompt("Run migrations automatically? [y/N]") {
65 return fmt.Errorf("please run the migrations manually")
66 }
67
@@ -70,7 +70,7 @@ func TryMigrating(tovers int) error {
70
71 func YesNoPrompt(prompt string) bool {
72 var s string
73 - for {
73 + for i := 0; i < 3; i++ {
74 fmt.Printf("%s ", prompt)
75 fmt.Scanf("%s", &s)
76 switch s {
@@ -78,7 +78,11 @@ func YesNoPrompt(prompt string) bool {
78 return true
79 case "n", "N":
80 return false
81 + case "":
82 + return false
83 }
84 fmt.Println("Please press either 'y' or 'n'")
85 }
86 +
87 + return false
88 }
repo/fsrepo/migrations/migrations.go
+3 -1
@@ -27,7 +27,9 @@ const migrations = "fs-repo-migrations"
27 func RunMigration(newv int) error {
28 migrateBin := "fs-repo-migrations"
29 fmt.Println(" => checking for migrations binary...")
30 - _, err := exec.LookPath(migrateBin)
30 +
31 + var err error
32 + migrateBin, err = exec.LookPath(migrateBin)
33 if err == nil {
34 // check to make sure migrations binary supports our target version
35 err = verifyMigrationSupportsVersion(migrateBin, newv)
test/sharness/t0066-migration.sh new
+52
@@ -0,0 +1,52 @@
1 +#!/bin/sh
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 +test_expect_success "setup mock migrations" '
14 + mkdir bin &&
15 + echo "#!/bin/bash" > bin/fs-repo-migrations &&
16 + echo "echo 4" >> bin/fs-repo-migrations &&
17 + chmod +x bin/fs-repo-migrations &&
18 + export PATH="$(pwd)/bin":$PATH
19 +'
20 +
21 +test_expect_success "manually reset repo version to 3" '
22 + echo "3" > "$IPFS_PATH"/version
23 +'
24 +
25 +test_expect_success "ipfs daemon --migrate=false fails" '
26 + test_expect_code 1 ipfs daemon --migrate=false 2> false_out
27 +'
28 +
29 +test_expect_success "output looks good" '
30 + grep "ipfs repo needs migration" false_out
31 +'
32 +
33 +test_expect_success "ipfs daemon --migrate=true runs migration" '
34 + test_expect_code 1 ipfs daemon --migrate=true > true_out
35 +'
36 +
37 +test_expect_success "output looks good" '
38 + grep "running migration" true_out > /dev/null &&
39 + grep "binary completed successfully" true_out > /dev/null
40 +'
41 +
42 +test_expect_success "'ipfs daemon' prompts to auto migrate" '
43 + test_expect_code 1 ipfs daemon > daemon_out 2> daemon_err
44 +'
45 +
46 +test_expect_success "output looks good" '
47 + grep "Found old repo version" daemon_out > /dev/null &&
48 + grep "Run migrations automatically?" daemon_out > /dev/null &&
49 + grep "please run the migrations manually" daemon_err > /dev/null
50 +'
51 +
52 +test_done