-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime(v2)): add sanity checks on store upgrades (#21748)
- Loading branch information
1 parent
081b90e
commit c9f0e2e
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package runtime | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
corestore "cosmossdk.io/core/store" | ||
) | ||
|
||
func TestCheckStoreUpgrade(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
storeUpgrades *corestore.StoreUpgrades | ||
errMsg string | ||
}{ | ||
{ | ||
name: "Nil StoreUpgrades", | ||
storeUpgrades: nil, | ||
errMsg: "store upgrades cannot be nil", | ||
}, | ||
{ | ||
name: "Valid StoreUpgrades", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2"}, | ||
Deleted: []string{"store3", "store4"}, | ||
}, | ||
}, | ||
{ | ||
name: "Duplicate key in Added", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2", "store1"}, | ||
Deleted: []string{"store3"}, | ||
}, | ||
errMsg: "store upgrade has duplicate key store1 in added", | ||
}, | ||
{ | ||
name: "Duplicate key in Deleted", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1"}, | ||
Deleted: []string{"store2", "store3", "store2"}, | ||
}, | ||
errMsg: "store upgrade has duplicate key store2 in deleted", | ||
}, | ||
{ | ||
name: "Key in both Added and Deleted", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2"}, | ||
Deleted: []string{"store2", "store3"}, | ||
}, | ||
errMsg: "store upgrade has key store2 in both added and deleted", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := checkStoreUpgrade(tt.storeUpgrades) | ||
if tt.errMsg == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, tt.errMsg) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package runtime | ||
|
||
import ( | ||
"testing" | ||
|
||
corestore "cosmossdk.io/core/store" | ||
) | ||
|
||
func TestCheckStoreUpgrade(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
storeUpgrades *corestore.StoreUpgrades | ||
wantErr bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "Nil StoreUpgrades", | ||
storeUpgrades: nil, | ||
wantErr: true, | ||
errMsg: "store upgrades cannot be nil", | ||
}, | ||
{ | ||
name: "Valid StoreUpgrades", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2"}, | ||
Deleted: []string{"store3", "store4"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Duplicate key in Added", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2", "store1"}, | ||
Deleted: []string{"store3"}, | ||
}, | ||
wantErr: true, | ||
errMsg: "store upgrade has duplicate key store1 in added", | ||
}, | ||
{ | ||
name: "Duplicate key in Deleted", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1"}, | ||
Deleted: []string{"store2", "store3", "store2"}, | ||
}, | ||
wantErr: true, | ||
errMsg: "store upgrade has duplicate key store2 in deleted", | ||
}, | ||
{ | ||
name: "Key in both Added and Deleted", | ||
storeUpgrades: &corestore.StoreUpgrades{ | ||
Added: []string{"store1", "store2"}, | ||
Deleted: []string{"store2", "store3"}, | ||
}, | ||
wantErr: true, | ||
errMsg: "store upgrade has key store2 in both added and deleted", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := checkStoreUpgrade(tt.storeUpgrades) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("checkStoreUpgrade() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if err != nil && err.Error() != tt.errMsg { | ||
t.Errorf("checkStoreUpgrade() error message = %v, want %v", err.Error(), tt.errMsg) | ||
} | ||
}) | ||
} | ||
} |