github.com/decred/dcrlnd@v0.7.6/channeldb/migration23/migration_test.go (about)

     1  package migration23
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrlnd/channeldb/migtest"
     7  	"github.com/decred/dcrlnd/kvdb"
     8  )
     9  
    10  var (
    11  	hexStr = migtest.Hex
    12  
    13  	hash1Str   = "02acee76ebd53d00824410cf6adecad4f50334dac702bd5a2d3ba01b91709f0e"
    14  	hash1      = hexStr(hash1Str)
    15  	paymentID1 = hexStr("0000000000000001")
    16  	attemptID1 = hexStr("0000000000000001")
    17  	attemptID2 = hexStr("0000000000000002")
    18  
    19  	hash2Str   = "62eb3f0a48f954e495d0c14ac63df04a67cefa59dafdbcd3d5046d1f5647840c"
    20  	hash2      = hexStr(hash2Str)
    21  	paymentID2 = hexStr("0000000000000002")
    22  	attemptID3 = hexStr("0000000000000003")
    23  
    24  	hash3Str = "99eb3f0a48f954e495d0c14ac63df04af8cefa59dafdbcd3d5046d1f564784d1"
    25  	hash3    = hexStr(hash3Str)
    26  
    27  	// failing1 will fail because all payment hashes should point to sub
    28  	// buckets containing payment data.
    29  	failing1 = map[string]interface{}{
    30  		hash1: "bogus",
    31  	}
    32  
    33  	// failing2 will fail because the "payment-htlcs-bucket" key must point
    34  	// to an actual bucket or be non-existent, but never point to a value.
    35  	failing2 = map[string]interface{}{
    36  		hash1: map[string]interface{}{
    37  			"payment-htlcs-bucket": "bogus",
    38  		},
    39  	}
    40  
    41  	// failing3 will fail because each attempt ID inside the
    42  	// "payment-htlcs-bucket" must point to a sub-bucket.
    43  	failing3 = map[string]interface{}{
    44  		hash1: map[string]interface{}{
    45  			"payment-creation-info": "aaaa",
    46  			"payment-fail-info":     "bbbb",
    47  			"payment-htlcs-bucket": map[string]interface{}{
    48  				attemptID1: map[string]interface{}{
    49  					"htlc-attempt-info": "cccc",
    50  					"htlc-fail-info":    "dddd",
    51  				},
    52  				attemptID2: "bogus",
    53  			},
    54  			"payment-sequence-key": paymentID1,
    55  		},
    56  	}
    57  
    58  	// pre is a sample snapshot (with fake values) before migration.
    59  	pre = map[string]interface{}{
    60  		hash1: map[string]interface{}{
    61  			"payment-creation-info": "aaaa",
    62  			"payment-fail-info":     "bbbb",
    63  			"payment-htlcs-bucket": map[string]interface{}{
    64  				attemptID1: map[string]interface{}{
    65  					"htlc-attempt-info": "cccc",
    66  					"htlc-fail-info":    "dddd",
    67  				},
    68  			},
    69  			"payment-sequence-key": paymentID1,
    70  		},
    71  		hash2: map[string]interface{}{
    72  			"payment-creation-info": "eeee",
    73  			"payment-htlcs-bucket": map[string]interface{}{
    74  				attemptID2: map[string]interface{}{
    75  					"htlc-attempt-info": "ffff",
    76  					"htlc-fail-info":    "gggg",
    77  				},
    78  				attemptID3: map[string]interface{}{
    79  					"htlc-attempt-info": "hhhh",
    80  					"htlc-settle-info":  "iiii",
    81  				},
    82  			},
    83  			"payment-sequence-key": paymentID2,
    84  		},
    85  		hash3: map[string]interface{}{
    86  			"payment-creation-info": "aaaa",
    87  			"payment-fail-info":     "bbbb",
    88  			"payment-sequence-key":  paymentID1,
    89  		},
    90  	}
    91  
    92  	// post is the expected data after migration.
    93  	post = map[string]interface{}{
    94  		hash1: map[string]interface{}{
    95  			"payment-creation-info": "aaaa",
    96  			"payment-fail-info":     "bbbb",
    97  			"payment-htlcs-bucket": map[string]interface{}{
    98  				"ai" + attemptID1: "cccc",
    99  				"fi" + attemptID1: "dddd",
   100  			},
   101  			"payment-sequence-key": paymentID1,
   102  		},
   103  		hash2: map[string]interface{}{
   104  			"payment-creation-info": "eeee",
   105  			"payment-htlcs-bucket": map[string]interface{}{
   106  				"ai" + attemptID2: "ffff",
   107  				"fi" + attemptID2: "gggg",
   108  				"ai" + attemptID3: "hhhh",
   109  				"si" + attemptID3: "iiii",
   110  			},
   111  			"payment-sequence-key": paymentID2,
   112  		},
   113  		hash3: map[string]interface{}{
   114  			"payment-creation-info": "aaaa",
   115  			"payment-fail-info":     "bbbb",
   116  			"payment-sequence-key":  paymentID1,
   117  		},
   118  	}
   119  )
   120  
   121  // TestMigrateHtlcAttempts tests that migration htlc attempts to the flattened
   122  // structure succeeds.
   123  func TestMigrateHtlcAttempts(t *testing.T) {
   124  	var paymentsRootBucket = []byte("payments-root-bucket")
   125  	tests := []struct {
   126  		name       string
   127  		shouldFail bool
   128  		pre        map[string]interface{}
   129  		post       map[string]interface{}
   130  	}{
   131  		{
   132  			name:       "migration ok",
   133  			shouldFail: false,
   134  			pre:        pre,
   135  			post:       post,
   136  		},
   137  		{
   138  			name:       "non-bucket payments-root-bucket",
   139  			shouldFail: true,
   140  			pre:        failing1,
   141  			post:       failing1,
   142  		},
   143  		{
   144  			name:       "non-bucket payment-htlcs-bucket",
   145  			shouldFail: true,
   146  			pre:        failing2,
   147  			post:       failing2,
   148  		},
   149  		{
   150  			name:       "non-bucket htlc attempt",
   151  			shouldFail: true,
   152  			pre:        failing3,
   153  			post:       failing3,
   154  		},
   155  	}
   156  
   157  	for _, test := range tests {
   158  		test := test
   159  
   160  		migtest.ApplyMigration(
   161  			t,
   162  			func(tx kvdb.RwTx) error {
   163  				return migtest.RestoreDB(
   164  					tx, paymentsRootBucket, test.pre,
   165  				)
   166  			},
   167  			func(tx kvdb.RwTx) error {
   168  				return migtest.VerifyDB(
   169  					tx, paymentsRootBucket, test.post,
   170  				)
   171  			},
   172  			MigrateHtlcAttempts,
   173  			test.shouldFail,
   174  		)
   175  	}
   176  }