|
| 1 | +package changesets |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/Masterminds/semver/v3" |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 12 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 13 | + cldfoperations "github.com/smartcontractkit/chainlink-deployments-framework/operations" |
| 14 | + cldflogger "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" |
| 15 | + |
| 16 | + "github.com/smartcontractkit/cld-changesets/catalog/operations" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCreateAddressRefChangeset_VerifyPreconditions(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + version := semver.MustParse("1.0.0") |
| 23 | + addressRef1 := cldfdatastore.AddressRef{Address: "0x01", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} |
| 24 | + addressRef2 := cldfdatastore.AddressRef{Address: "0x02", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} |
| 25 | + |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + env cldf.Environment |
| 29 | + input CreateAddressRefChangesetInput |
| 30 | + wantErr string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "success: valid preconditions", |
| 34 | + env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, |
| 35 | + input: CreateAddressRefChangesetInput{ |
| 36 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1}, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "failure: missing datastore", |
| 41 | + env: cldf.Environment{}, |
| 42 | + input: CreateAddressRefChangesetInput{ |
| 43 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1}, |
| 44 | + }, |
| 45 | + wantErr: "missing datastore in environment", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "failure: no address refs given", |
| 49 | + env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, |
| 50 | + input: CreateAddressRefChangesetInput{ |
| 51 | + AddressRefs: []cldfdatastore.AddressRef{}, |
| 52 | + }, |
| 53 | + wantErr: "missing address refs input", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "failure: duplicate entries", |
| 57 | + env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, |
| 58 | + input: CreateAddressRefChangesetInput{ |
| 59 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1, addressRef2}, |
| 60 | + }, |
| 61 | + wantErr: "duplicate address ref entries found in input", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "failure: address ref already exists", |
| 65 | + env: cldf.Environment{DataStore: func() cldfdatastore.DataStore { |
| 66 | + ds := cldfdatastore.NewMemoryDataStore() |
| 67 | + err := ds.Addresses().Add(addressRef1) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + return ds.Seal() |
| 71 | + }()}, |
| 72 | + input: CreateAddressRefChangesetInput{ |
| 73 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1}, |
| 74 | + }, |
| 75 | + wantErr: "address ref for chain selector 1234, type MyContract, version 1.0.0 and qualifier \"q1\" already exists", |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + err := CreateAddressRefChangeset{}.VerifyPreconditions(tt.env, tt.input) |
| 83 | + |
| 84 | + if tt.wantErr == "" { |
| 85 | + require.NoError(t, err) |
| 86 | + } else { |
| 87 | + require.ErrorContains(t, err, tt.wantErr) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestCreateAddressRefChangeset_Apply(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + version := semver.MustParse("1.0.0") |
| 97 | + addressRef1 := cldfdatastore.AddressRef{Address: "0x01", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} |
| 98 | + addressRef2 := cldfdatastore.AddressRef{Address: "0x02", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q2"} |
| 99 | + |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + env cldf.Environment |
| 103 | + input CreateAddressRefChangesetInput |
| 104 | + want cldf.ChangesetOutput |
| 105 | + wantErr string |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "success: adds two entries to address refs", |
| 109 | + env: cldf.Environment{ |
| 110 | + DataStore: testDataStoreWithAddressRefs(t).Seal(), |
| 111 | + OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), |
| 112 | + }, |
| 113 | + input: CreateAddressRefChangesetInput{ |
| 114 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1, addressRef2}, |
| 115 | + }, |
| 116 | + want: cldf.ChangesetOutput{ |
| 117 | + DataStore: testDataStoreWithAddressRefs(t, addressRef1, addressRef2), |
| 118 | + Reports: []cldfoperations.Report[any, any]{{ |
| 119 | + Def: cldfoperations.Definition{ |
| 120 | + ID: "catalog-create-address-ref", |
| 121 | + Version: semver.MustParse("1.0.0"), |
| 122 | + Description: "Add address ref entries to the Catalog service", |
| 123 | + }, |
| 124 | + Input: operations.CreateAddressRefInput{ |
| 125 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1, addressRef2}, |
| 126 | + }, |
| 127 | + Output: operations.CreateAddressRefOutput{ |
| 128 | + DataStore: testDataStoreWithAddressRefs(t, addressRef1, addressRef2), |
| 129 | + }, |
| 130 | + }}, |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "failure: fails to add second entry", |
| 135 | + env: cldf.Environment{ |
| 136 | + DataStore: testDataStoreWithAddressRefs(t, addressRef2).Seal(), |
| 137 | + OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), |
| 138 | + }, |
| 139 | + input: CreateAddressRefChangesetInput{ |
| 140 | + AddressRefs: []cldfdatastore.AddressRef{addressRef1, addressRef2}, |
| 141 | + }, |
| 142 | + wantErr: "failed to create address ref entry 1 in catalog store: " + |
| 143 | + "an address ref with the supplied key already exists", |
| 144 | + }, |
| 145 | + } |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + |
| 150 | + got, err := CreateAddressRefChangeset{}.Apply(tt.env, tt.input) |
| 151 | + |
| 152 | + if tt.wantErr == "" { |
| 153 | + require.NoError(t, err) |
| 154 | + require.Empty(t, |
| 155 | + cmp.Diff(tt.want, got, |
| 156 | + cmpopts.IgnoreFields(cldfoperations.Report[any, any]{}, "ID", "Timestamp"), |
| 157 | + cmpopts.IgnoreUnexported(cldfdatastore.MemoryAddressRefStore{}, cldfdatastore.MemoryChainMetadataStore{}, |
| 158 | + cldfdatastore.MemoryContractMetadataStore{}, cldfdatastore.MemoryEnvMetadataStore{}, |
| 159 | + cldfdatastore.LabelSet{}))) |
| 160 | + } else { |
| 161 | + require.ErrorContains(t, err, tt.wantErr) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// ----- helpers ----- |
| 168 | + |
| 169 | +func testDataStoreWithAddressRefs( |
| 170 | + t *testing.T, addressRefs ...cldfdatastore.AddressRef, |
| 171 | +) cldfdatastore.MutableDataStore { |
| 172 | + t.Helper() |
| 173 | + |
| 174 | + ds := cldfdatastore.NewMemoryDataStore() |
| 175 | + for _, ar := range addressRefs { |
| 176 | + err := ds.Addresses().Add(ar) |
| 177 | + require.NoError(t, err) |
| 178 | + } |
| 179 | + |
| 180 | + return ds |
| 181 | +} |
0 commit comments