gitlab.com/jokerrs1/Sia@v1.3.2/modules/host/persist_compat_1.2.0_test.go (about)

     1  package host
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/build"
     9  	"github.com/NebulousLabs/Sia/modules"
    10  	"github.com/NebulousLabs/Sia/modules/consensus"
    11  	"github.com/NebulousLabs/Sia/modules/gateway"
    12  	"github.com/NebulousLabs/Sia/modules/transactionpool"
    13  	"github.com/NebulousLabs/Sia/modules/wallet"
    14  	"github.com/NebulousLabs/Sia/persist"
    15  )
    16  
    17  const (
    18  	// v112StorageManagerOne names the first legacy storage manager that can be
    19  	// used to test upgrades.
    20  	v112Host = "v112Host.tar.gz"
    21  )
    22  
    23  // loadExistingHostWithNewDeps will create all of the dependencies for a host, then load
    24  // the host on top of the given directory.
    25  func loadExistingHostWithNewDeps(modulesDir, hostDir string) (modules.Host, error) {
    26  	testdir := build.TempDir(modules.HostDir, modulesDir)
    27  
    28  	// Create the host dependencies.
    29  	g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// Create the host.
    47  	h, err := newHost(&modules.ProductionDependencies{}, cs, tp, w, "localhost:0", hostDir)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return h, nil
    52  }
    53  
    54  // TestV112StorageManagerUpgrade creates a host with a legacy storage manager,
    55  // and then attempts to upgrade the storage manager.
    56  func TestV112StorageManagerUpgrade(t *testing.T) {
    57  	if testing.Short() {
    58  		t.SkipNow()
    59  	}
    60  
    61  	// Copy the testdir legacy storage manager to the temp directory.
    62  	source := filepath.Join("testdata", v112Host)
    63  	legacyHost := build.TempDir(modules.HostDir, t.Name(), modules.HostDir)
    64  	err := build.ExtractTarGz(source, legacyHost)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	// Patch the storagemanager.json to point to the new storage folder
    70  	// location.
    71  	smPersist := new(v112StorageManagerPersist)
    72  	err = persist.LoadJSON(v112StorageManagerMetadata, smPersist, filepath.Join(legacyHost, v112StorageManagerDir, v112StorageManagerPersistFilename))
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	smPersist.StorageFolders[0].Path = filepath.Join(legacyHost, "storageFolderOne")
    77  	smPersist.StorageFolders[1].Path = filepath.Join(legacyHost, "storageFolderTwo")
    78  	err = persist.SaveJSON(v112StorageManagerMetadata, smPersist, filepath.Join(legacyHost, v112StorageManagerDir, v112StorageManagerPersistFilename))
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	oldCapacity := smPersist.StorageFolders[0].Size + smPersist.StorageFolders[1].Size
    83  	oldCapacityRemaining := smPersist.StorageFolders[0].SizeRemaining + smPersist.StorageFolders[1].SizeRemaining
    84  	oldUsed := oldCapacity - oldCapacityRemaining
    85  
    86  	// Create the symlink to point to the storage folder.
    87  	err = os.Symlink(filepath.Join(legacyHost, "storageFolderOne"), filepath.Join(legacyHost, v112StorageManagerDir, "66"))
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	err = os.Symlink(filepath.Join(legacyHost, "storageFolderTwo"), filepath.Join(legacyHost, v112StorageManagerDir, "04"))
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	// Patching complete. Proceed to create the host and verify that the
    97  	// upgrade went smoothly.
    98  	host, err := loadExistingHostWithNewDeps(filepath.Join(t.Name(), "newDeps"), legacyHost)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	storageFolders := host.StorageFolders()
   104  	if len(storageFolders) != 2 {
   105  		t.Fatal("Storage manager upgrade was unsuccessful.")
   106  	}
   107  
   108  	// The amount of data reported should match the previous amount of data
   109  	// that was stored.
   110  	capacity := storageFolders[0].Capacity + storageFolders[1].Capacity
   111  	capacityRemaining := storageFolders[0].CapacityRemaining + storageFolders[1].CapacityRemaining
   112  	capacityUsed := capacity - capacityRemaining
   113  	if capacity != oldCapacity {
   114  		t.Error("new storage folders don't have the same size as the old storage folders")
   115  	}
   116  	if capacityRemaining != oldCapacityRemaining {
   117  		t.Error("capacity remaining statistics do not match up", capacityRemaining/modules.SectorSize, oldCapacityRemaining/modules.SectorSize)
   118  	}
   119  	if oldUsed != capacityUsed {
   120  		t.Error("storage folders have different usage values", capacityUsed/modules.SectorSize, oldUsed/modules.SectorSize)
   121  	}
   122  }