github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/config/migrate/migrate_test.go (about)

     1  package migrate_test
     2  
     3  import (
     4  	"archive/zip"
     5  	"context"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/qri-io/ioes"
    14  	"github.com/qri-io/qri/config"
    15  	"github.com/qri-io/qri/config/migrate"
    16  	"github.com/qri-io/qri/repo/buildrepo"
    17  )
    18  
    19  func TestOneToTwo(t *testing.T) {
    20  	t.Skipf("This is an expensive test that downloads IPFS migration binaries. Should be run by hand")
    21  
    22  	// setup a repo in the v1 arrangement
    23  	dir, err := ioutil.TempDir("", "testOneToTwo")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer os.RemoveAll(dir)
    28  	repoPath := filepath.Join(dir, "qri")
    29  	vOneIPFSPath := filepath.Join(dir, "ipfs")
    30  	os.MkdirAll(repoPath, 0774)
    31  	os.MkdirAll(vOneIPFSPath, 0774)
    32  	os.Setenv("IPFS_PATH", vOneIPFSPath)
    33  
    34  	input, err := ioutil.ReadFile("testdata/one_to_two/qri_config.yaml")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	ioutil.WriteFile(filepath.Join(repoPath, "config.yaml"), input, 0774)
    40  	unzipFile("testdata/one_to_two/empty_ipfs_repo_v0.4.21.zip", filepath.Join(dir, "ipfs"))
    41  
    42  	cfg, err := config.ReadFromFile(filepath.Join(repoPath, "config.yaml"))
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	// call OneToTwo
    48  	if err := migrate.RunMigrations(ioes.NewDiscardIOStreams(), cfg, func() bool { return true }, false); err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	ctx, cancel := context.WithCancel(context.Background())
    53  	defer cancel()
    54  	buildrepo.New(ctx, repoPath, cfg)
    55  }
    56  
    57  func TestTwoToThree(t *testing.T) {
    58  	// setup a repo in the v2 arrangement
    59  	dir, err := ioutil.TempDir("", "testTwoToThree")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer os.RemoveAll(dir)
    64  	repoPath := filepath.Join(dir, "qri")
    65  	os.MkdirAll(repoPath, 0774)
    66  
    67  	input, err := ioutil.ReadFile("testdata/two_to_three/qri_config.yaml")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	ioutil.WriteFile(filepath.Join(repoPath, "config.yaml"), input, 0774)
    73  
    74  	cfg, err := config.ReadFromFile(filepath.Join(repoPath, "config.yaml"))
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	// call TwoToThree
    80  	if err := migrate.RunMigrations(ioes.NewStdIOStreams(), cfg, func() bool { return true }, false); err != nil {
    81  		t.Error(err)
    82  	}
    83  
    84  	expect := []string{
    85  		"/ip4/1.2.3.4/tcp/4001/ipfs/QmTestPersistingManuallyAddedBootstrappers",            // should persist unknown
    86  		"/ip4/35.231.230.13/tcp/4001/ipfs/QmdpGkbqDYRPCcwLYnEm8oYGz2G9aUZn9WwPjqvqw3XUAc",  // red
    87  		"/ip4/34.75.40.163/tcp/4001/ipfs/QmTRqTLbKndFC2rp6VzpyApxHCLrFV35setF1DQZaRWPVf",   // orange
    88  		"/ip4/35.237.172.74/tcp/4001/ipfs/QmegNYmwHUQFc3v3eemsYUVf3WiSg4RcMrh3hovA5LncJ2",  // yellow
    89  		"/ip4/35.231.155.111/tcp/4001/ipfs/QmessbA6uGLJ7HTwbUJ2niE49WbdPfzi27tdYXdAaGRB4G", // green
    90  		"/ip4/35.237.232.64/tcp/4001/ipfs/Qmc353gHY5Wx5iHKHPYj3QDqHP4hVA1MpoSsT6hwSyVx3r",  // blue
    91  		"/ip4/35.185.20.61/tcp/4001/ipfs/QmT9YHJF2YkysLqWhhiVTL5526VFtavic3bVueF9rCsjVi",   // indigo
    92  		"/ip4/35.231.246.50/tcp/4001/ipfs/QmQS2ryqZrjJtPKDy9VTkdPwdUSpTi1TdpGUaqAVwfxcNh",  // violet
    93  	}
    94  
    95  	if diff := cmp.Diff(cfg.P2P.QriBootstrapAddrs, expect); diff != "" {
    96  		t.Errorf("config.p2p.QriBootstrapAddrs result mismatch. (-want +got):%s\n", diff)
    97  	}
    98  }
    99  
   100  func unzipFile(sourceZip, destDir string) {
   101  	r, err := zip.OpenReader(sourceZip)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  	defer r.Close()
   106  
   107  	for _, f := range r.File {
   108  		rc, err := f.Open()
   109  		if err != nil {
   110  			panic(err)
   111  		}
   112  		defer rc.Close()
   113  
   114  		fpath := filepath.Join(destDir, f.Name)
   115  		if f.FileInfo().IsDir() {
   116  			os.MkdirAll(fpath, os.ModePerm)
   117  		} else {
   118  			if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
   119  				panic(err)
   120  			}
   121  			outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
   122  			if err != nil {
   123  				panic(err)
   124  			}
   125  			_, err = io.Copy(outFile, rc)
   126  			outFile.Close()
   127  		}
   128  	}
   129  }