github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_migrate_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/holochain/holochain-proto/hash"
     6  	peer "github.com/libp2p/go-libp2p-peer"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"testing"
     9  )
    10  
    11  // ActionMigrate
    12  
    13  func TestMigrateName(t *testing.T) {
    14  	Convey("migrate action should have the right name", t, func() {
    15  		a := ActionMigrate{}
    16  		So(a.Name(), ShouldEqual, "migrate")
    17  	})
    18  }
    19  
    20  func TestMigrateEntry(t *testing.T) {
    21  	Convey("empty migrate action Entry() should be retreive a serialized JSON of an empty entry in a GobEntry", t, func() {
    22  		action := ActionMigrate{}
    23  		So(action.Entry(), ShouldResemble, &GobEntry{C: "{\"Type\":\"\",\"DNAHash\":\"\",\"Key\":\"\",\"Data\":\"\"}"})
    24  	})
    25  
    26  	Convey("entries with vals work with Entry()", t, func() {
    27  		dnaHash, err := genTestStringHash()
    28  		So(err, ShouldBeNil)
    29  
    30  		key, err := genTestStringHash()
    31  		So(err, ShouldBeNil)
    32  
    33  		entry := MigrateEntry{DNAHash: dnaHash, Key: key}
    34  		action := ActionMigrate{entry: entry}
    35  
    36  		So(action.Entry(), ShouldResemble, &GobEntry{C: "{\"Type\":\"\",\"DNAHash\":\"" + dnaHash.String() + "\",\"Key\":\"" + key.String() + "\",\"Data\":\"\"}"})
    37  	})
    38  }
    39  
    40  func TestMigrateEntryType(t *testing.T) {
    41  	action := ActionMigrate{}
    42  	Convey("migrate action EntryType() should return the correct type", t, func() {
    43  		So(action.EntryType(), ShouldEqual, MigrateEntryType)
    44  	})
    45  }
    46  
    47  func TestMigrateHeaderSetGet(t *testing.T) {
    48  	Convey("empty migrate action should have empty header", t, func() {
    49  		action := ActionMigrate{}
    50  		So(action.GetHeader(), ShouldEqual, nil)
    51  	})
    52  
    53  	Convey("migrate action should be able to set and get header", t, func() {
    54  		action := ActionMigrate{}
    55  		header, err := genTestHeader()
    56  		So(err, ShouldBeNil)
    57  
    58  		So(action.GetHeader(), ShouldEqual, nil)
    59  		action.SetHeader(header)
    60  		So(action.GetHeader(), ShouldEqual, header)
    61  		action.SetHeader(nil)
    62  		So(action.GetHeader(), ShouldEqual, nil)
    63  	})
    64  }
    65  
    66  func TestMigrateCallShare(t *testing.T) {
    67  	n := 3
    68  	mt := setupMultiNodeTesting(n)
    69  	ringConnect(t, mt.ctx, mt.nodes, n)
    70  	defer mt.cleanupMultiNodeTesting()
    71  
    72  	Convey("ActionMigrate should share as a PUT on the DHT and roundtrip as JSON", t, func() {
    73  		var err error
    74  		header, err := genTestHeader()
    75  		entry, err := genTestMigrateEntry()
    76  		So(err, ShouldBeNil)
    77  
    78  		action := ActionMigrate{header: header, entry: entry}
    79  
    80  		// Can share from some node
    81  		var dhtHash Hash
    82  		fn := &APIFnMigrate{action: action}
    83  		callResponse, err := fn.Call(mt.nodes[0])
    84  		dhtHash, ok := callResponse.(Hash)
    85  		So(ok, ShouldBeTrue)
    86  		So(err, ShouldBeNil)
    87  
    88  		// Can get the PUT MigrateEntry from any node
    89  		for i := 0; i < n; i++ {
    90  			fmt.Printf("\nTesting retrieval of MigrateEntry PUT from node %d\n", i)
    91  
    92  			request := GetReq{H: dhtHash, StatusMask: StatusLive, GetMask: GetMaskEntry}
    93  			response, err := callGet(mt.nodes[i], request, &GetOptions{GetMask: request.GetMask})
    94  			r, ok := response.(GetResp)
    95  
    96  			So(ok, ShouldBeTrue)
    97  			So(err, ShouldBeNil)
    98  
    99  			So(&r.Entry, ShouldResemble, action.Entry())
   100  		}
   101  	})
   102  }
   103  
   104  func TestMigrateActionSysValidation(t *testing.T) {
   105  	d, _, h := PrepareTestChain("test")
   106  	defer CleanupTestChain(h, d)
   107  
   108  	Convey("it should invalidate DNAEntryDef", t, func() {
   109  		action := ActionMigrate{}
   110  		err := action.SysValidation(h, DNAEntryDef, nil, []peer.ID{h.nodeID})
   111  		So(err, ShouldEqual, ErrEntryDefInvalid)
   112  	})
   113  
   114  	Convey("ActionMigrate SysValidation should return an ErrActionMissingHeader error if header is missing", t, func() {
   115  		action := ActionMigrate{}
   116  		err := action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
   117  		So(err, ShouldEqual, ErrActionMissingHeader)
   118  	})
   119  
   120  	Convey("ActionMigrate SysValidation should validate the entry", t, func() {
   121  		header, err := genTestHeader()
   122  		So(err, ShouldBeNil)
   123  
   124  		action := ActionMigrate{header: header}
   125  		err = action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
   126  		// the entry is empty so there should be validation complaints
   127  		So(err.Error(), ShouldEqual, "Validation Failed: Error (input isn't valid multihash) when decoding DNAHash value ''")
   128  
   129  		action.entry, err = genTestMigrateEntry()
   130  		So(err, ShouldBeNil)
   131  
   132  		err = action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
   133  		So(err, ShouldBeNil)
   134  	})
   135  }
   136  
   137  func TestMigrateCheckValidationRequest(t *testing.T) {
   138  	Convey("MigrateAction CheckValidationRequest should always pass", t, func() {
   139  		action := ActionMigrate{}
   140  		So(action.CheckValidationRequest(action.entry.Def()), ShouldBeNil)
   141  	})
   142  }
   143  
   144  func TestMigrateReceive(t *testing.T) {
   145  	mt := setupMultiNodeTesting(1)
   146  	defer mt.cleanupMultiNodeTesting()
   147  	h := mt.nodes[0]
   148  
   149  	Convey("MigrateAction Receive is always an error", t, func() {
   150  		action := ActionMigrate{}
   151  		msg := h.node.NewMessage(PUT_REQUEST, HoldReq{})
   152  		response, err := action.Receive(h.dht, msg)
   153  		So(err.Error(), ShouldEqual, "Action receive is invalid")
   154  		So(response, ShouldBeNil)
   155  	})
   156  }
   157  
   158  // APIFnMigrate
   159  
   160  func TestAPIFnMigrateName(t *testing.T) {
   161  	Convey("migrate action function should have the right name", t, func() {
   162  		fn := &APIFnMigrate{}
   163  		So(fn.Name(), ShouldEqual, "migrate")
   164  	})
   165  }
   166  
   167  func TestAPIFnMigrateArgs(t *testing.T) {
   168  	Convey("APIFnMigrate should have the correct args", t, func() {
   169  		fn := &APIFnMigrate{}
   170  		expected := []Arg{{Name: "migrationType",
   171  			Type: StringArg},
   172  			{Name: "DNAHash",
   173  				Type: HashArg},
   174  			{Name: "Key",
   175  				Type: HashArg},
   176  			{Name: "data",
   177  				Type: StringArg}}
   178  		So(fn.Args(), ShouldResemble, expected)
   179  	})
   180  }