github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_sync_test.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package main 6 7 import ( 8 "os" 9 "testing" 10 11 "github.com/attic-labs/noms/go/datas" 12 "github.com/attic-labs/noms/go/nbs" 13 "github.com/attic-labs/noms/go/spec" 14 "github.com/attic-labs/noms/go/types" 15 "github.com/attic-labs/noms/go/util/clienttest" 16 "github.com/stretchr/testify/suite" 17 ) 18 19 func TestSync(t *testing.T) { 20 suite.Run(t, &nomsSyncTestSuite{}) 21 } 22 23 type nomsSyncTestSuite struct { 24 clienttest.ClientTestSuite 25 } 26 27 func (s *nomsSyncTestSuite) TestSyncValidation() { 28 sourceDB := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize)) 29 source1 := sourceDB.GetDataset("src") 30 source1, err := sourceDB.CommitValue(source1, types.Number(42)) 31 s.NoError(err) 32 source1HeadRef := source1.Head().Hash() 33 source1.Database().Close() 34 sourceSpecMissingHashSymbol := spec.CreateValueSpecString("nbs", s.DBDir, source1HeadRef.String()) 35 36 sinkDatasetSpec := spec.CreateValueSpecString("nbs", s.DBDir2, "dest") 37 38 defer func() { 39 err := recover() 40 s.Equal(clienttest.ExitError{Code: 1}, err) 41 }() 42 43 s.MustRun(main, []string{"sync", sourceSpecMissingHashSymbol, sinkDatasetSpec}) 44 } 45 46 func (s *nomsSyncTestSuite) TestSync() { 47 defer s.NoError(os.RemoveAll(s.DBDir2)) 48 49 sourceDB := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize)) 50 source1 := sourceDB.GetDataset("src") 51 source1, err := sourceDB.CommitValue(source1, types.Number(42)) 52 s.NoError(err) 53 source1HeadRef := source1.Head().Hash() // Remember first head, so we can sync to it. 54 source1, err = sourceDB.CommitValue(source1, types.Number(43)) 55 s.NoError(err) 56 sourceDB.Close() 57 58 // Pull from a hash to a not-yet-existing dataset in a new DB 59 sourceSpec := spec.CreateValueSpecString("nbs", s.DBDir, "#"+source1HeadRef.String()) 60 sinkDatasetSpec := spec.CreateValueSpecString("nbs", s.DBDir2, "dest") 61 sout, _ := s.MustRun(main, []string{"sync", sourceSpec, sinkDatasetSpec}) 62 s.Regexp("Synced", sout) 63 64 db := datas.NewDatabase(nbs.NewLocalStore(s.DBDir2, clienttest.DefaultMemTableSize)) 65 dest := db.GetDataset("dest") 66 s.True(types.Number(42).Equals(dest.HeadValue())) 67 db.Close() 68 69 // Pull from a dataset in one DB to an existing dataset in another 70 sourceDataset := spec.CreateValueSpecString("nbs", s.DBDir, "src") 71 sout, _ = s.MustRun(main, []string{"sync", sourceDataset, sinkDatasetSpec}) 72 s.Regexp("Synced", sout) 73 74 db = datas.NewDatabase(nbs.NewLocalStore(s.DBDir2, clienttest.DefaultMemTableSize)) 75 dest = db.GetDataset("dest") 76 s.True(types.Number(43).Equals(dest.HeadValue())) 77 db.Close() 78 79 // Pull when sink dataset is already up to date 80 sout, _ = s.MustRun(main, []string{"sync", sourceDataset, sinkDatasetSpec}) 81 s.Regexp("up to date", sout) 82 83 // Pull from a source dataset to a not-yet-existing dataset in another DB, BUT all the needed chunks already exists in the sink. 84 sinkDatasetSpec = spec.CreateValueSpecString("nbs", s.DBDir2, "dest2") 85 sout, _ = s.MustRun(main, []string{"sync", sourceDataset, sinkDatasetSpec}) 86 s.Regexp("Created", sout) 87 88 db = datas.NewDatabase(nbs.NewLocalStore(s.DBDir2, clienttest.DefaultMemTableSize)) 89 dest = db.GetDataset("dest2") 90 s.True(types.Number(43).Equals(dest.HeadValue())) 91 db.Close() 92 } 93 94 func (s *nomsSyncTestSuite) TestSync_Issue2598() { 95 defer s.NoError(os.RemoveAll(s.DBDir2)) 96 97 sourceDB := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize)) 98 // Create dataset "src1", which has a lineage of two commits. 99 source1 := sourceDB.GetDataset("src1") 100 source1, err := sourceDB.CommitValue(source1, types.Number(42)) 101 s.NoError(err) 102 source1, err = sourceDB.CommitValue(source1, types.Number(43)) 103 s.NoError(err) 104 105 // Create dataset "src2", with a lineage of one commit. 106 source2 := sourceDB.GetDataset("src2") 107 source2, err = sourceDB.CommitValue(source2, types.Number(1)) 108 s.NoError(err) 109 110 sourceDB.Close() // Close Database backing both Datasets 111 112 // Sync over "src1" 113 sourceDataset := spec.CreateValueSpecString("nbs", s.DBDir, "src1") 114 sinkDatasetSpec := spec.CreateValueSpecString("nbs", s.DBDir2, "dest") 115 sout, _ := s.MustRun(main, []string{"sync", sourceDataset, sinkDatasetSpec}) 116 117 db := datas.NewDatabase(nbs.NewLocalStore(s.DBDir2, clienttest.DefaultMemTableSize)) 118 dest := db.GetDataset("dest") 119 s.True(types.Number(43).Equals(dest.HeadValue())) 120 db.Close() 121 122 // Now, try syncing a second dataset. This crashed in issue #2598 123 sourceDataset2 := spec.CreateValueSpecString("nbs", s.DBDir, "src2") 124 sinkDatasetSpec2 := spec.CreateValueSpecString("nbs", s.DBDir2, "dest2") 125 sout, _ = s.MustRun(main, []string{"sync", sourceDataset2, sinkDatasetSpec2}) 126 127 db = datas.NewDatabase(nbs.NewLocalStore(s.DBDir2, clienttest.DefaultMemTableSize)) 128 dest = db.GetDataset("dest2") 129 s.True(types.Number(1).Equals(dest.HeadValue())) 130 db.Close() 131 132 sout, _ = s.MustRun(main, []string{"sync", sourceDataset, sinkDatasetSpec}) 133 s.Regexp("up to date", sout) 134 } 135 136 func (s *nomsSyncTestSuite) TestRewind() { 137 var err error 138 sourceDB := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize)) 139 src := sourceDB.GetDataset("foo") 140 src, err = sourceDB.CommitValue(src, types.Number(42)) 141 s.NoError(err) 142 rewindRef := src.HeadRef().TargetHash() 143 src, err = sourceDB.CommitValue(src, types.Number(43)) 144 s.NoError(err) 145 sourceDB.Close() // Close Database backing both Datasets 146 147 sourceSpec := spec.CreateValueSpecString("nbs", s.DBDir, "#"+rewindRef.String()) 148 sinkDatasetSpec := spec.CreateValueSpecString("nbs", s.DBDir, "foo") 149 s.MustRun(main, []string{"sync", sourceSpec, sinkDatasetSpec}) 150 151 db := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize)) 152 dest := db.GetDataset("foo") 153 s.True(types.Number(42).Equals(dest.HeadValue())) 154 db.Close() 155 }