-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
304 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
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,114 @@ | ||
//! Test utilities | ||
#![allow(dead_code)] | ||
|
||
use crate::{self as pallet_ddc_nodes, *}; | ||
use frame_support::{ | ||
construct_runtime, parameter_types, | ||
traits::{ConstU32, ConstU64, Everything}, | ||
weights::constants::RocksDbWeight, | ||
}; | ||
use frame_system::mocking::{MockBlock, MockUncheckedExtrinsic}; | ||
use sp_core::H256; | ||
use sp_io::TestExternalities; | ||
use sp_runtime::{ | ||
testing::Header, | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
}; | ||
|
||
/// The AccountId alias in this test module. | ||
pub(crate) type AccountId = u64; | ||
pub(crate) type AccountIndex = u64; | ||
pub(crate) type BlockNumber = u64; | ||
pub(crate) type Balance = u128; | ||
|
||
type UncheckedExtrinsic = MockUncheckedExtrinsic<Test>; | ||
type Block = MockBlock<Test>; | ||
|
||
construct_runtime!( | ||
pub enum Test where | ||
Block = Block, | ||
NodeBlock = Block, | ||
UncheckedExtrinsic = UncheckedExtrinsic, | ||
{ | ||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, | ||
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, | ||
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>}, | ||
DdcNodes: pallet_ddc_nodes::{Pallet, Call, Storage, Event<T>}, | ||
} | ||
); | ||
|
||
parameter_types! { | ||
pub static ExistentialDeposit: Balance = 1; | ||
} | ||
|
||
impl frame_system::Config for Test { | ||
type BaseCallFilter = Everything; | ||
type BlockWeights = (); | ||
type BlockLength = (); | ||
type DbWeight = RocksDbWeight; | ||
type RuntimeOrigin = RuntimeOrigin; | ||
type Index = AccountIndex; | ||
type BlockNumber = BlockNumber; | ||
type RuntimeCall = RuntimeCall; | ||
type Hash = H256; | ||
type Hashing = BlakeTwo256; | ||
type AccountId = AccountId; | ||
type Lookup = IdentityLookup<Self::AccountId>; | ||
type Header = Header; | ||
type RuntimeEvent = RuntimeEvent; | ||
type BlockHashCount = ConstU64<250>; | ||
type Version = (); | ||
type PalletInfo = PalletInfo; | ||
type AccountData = pallet_balances::AccountData<Balance>; | ||
type OnNewAccount = (); | ||
type OnKilledAccount = (); | ||
type SystemWeightInfo = (); | ||
type SS58Prefix = (); | ||
type OnSetCode = (); | ||
type MaxConsumers = ConstU32<16>; | ||
} | ||
|
||
impl pallet_balances::Config for Test { | ||
type MaxLocks = ConstU32<1024>; | ||
type MaxReserves = (); | ||
type ReserveIdentifier = [u8; 8]; | ||
type Balance = Balance; | ||
type RuntimeEvent = RuntimeEvent; | ||
type DustRemoval = (); | ||
type ExistentialDeposit = ExistentialDeposit; | ||
type AccountStore = System; | ||
type WeightInfo = (); | ||
} | ||
|
||
impl pallet_timestamp::Config for Test { | ||
type Moment = u64; | ||
type OnTimestampSet = (); | ||
type MinimumPeriod = ConstU64<5>; | ||
type WeightInfo = (); | ||
} | ||
|
||
impl crate::pallet::Config for Test { | ||
type RuntimeEvent = RuntimeEvent; | ||
} | ||
|
||
pub(crate) type TestRuntimeCall = <Test as frame_system::Config>::RuntimeCall; | ||
|
||
pub struct ExtBuilder; | ||
|
||
impl ExtBuilder { | ||
fn build(self) -> TestExternalities { | ||
sp_tracing::try_init_simple(); | ||
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap(); | ||
|
||
let _ = pallet_balances::GenesisConfig::<Test> { balances: vec![(1, 100), (2, 100)] } | ||
.assimilate_storage(&mut storage); | ||
|
||
TestExternalities::new(storage) | ||
} | ||
pub fn build_and_execute(self, test: impl FnOnce() -> ()) { | ||
sp_tracing::try_init_simple(); | ||
let mut ext = self.build(); | ||
ext.execute_with(test); | ||
} | ||
} |
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,183 @@ | ||
//! Tests for the module. | ||
use super::{mock::*, *}; | ||
use crate::{cdn_node::CDNNodeParams, storage_node::StorageNodeParams}; | ||
use ddc_primitives::NodePubKey; | ||
use frame_support::{assert_noop, assert_ok}; | ||
use sp_runtime::AccountId32; | ||
|
||
#[test] | ||
fn create_node_works() { | ||
ExtBuilder.build_and_execute(|| { | ||
System::set_block_number(1); | ||
let bytes = [0u8; 32]; | ||
let node_pub_key = AccountId32::from(bytes); | ||
let cdn_node_params = CDNNodeParams { | ||
host: vec![1u8, 255], | ||
http_port: 35000u16, | ||
grpc_port: 25000u16, | ||
p2p_port: 15000u16, | ||
}; | ||
|
||
// Node params are not valid | ||
assert_noop!( | ||
DdcNodes::create_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::StorageParams(StorageNodeParams { | ||
host: vec![1u8, 255], | ||
http_port: 35000u16, | ||
grpc_port: 25000u16, | ||
p2p_port: 15000u16, | ||
}) | ||
), | ||
Error::<Test>::InvalidNodeParams | ||
); | ||
|
||
// Node created | ||
assert_ok!(DdcNodes::create_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
)); | ||
|
||
// Node already exists | ||
assert_noop!( | ||
DdcNodes::create_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params) | ||
), | ||
Error::<Test>::NodeAlreadyExists | ||
); | ||
|
||
// Checking that event was emitted | ||
assert_eq!(System::events().len(), 1); | ||
System::assert_last_event( | ||
Event::NodeCreated { node_pub_key: NodePubKey::CDNPubKey(node_pub_key) }.into(), | ||
) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn set_node_params_works() { | ||
ExtBuilder.build_and_execute(|| { | ||
System::set_block_number(1); | ||
let bytes = [0u8; 32]; | ||
let node_pub_key = AccountId32::from(bytes); | ||
let storage_node_params = StorageNodeParams { | ||
host: vec![1u8, 255], | ||
http_port: 35000u16, | ||
grpc_port: 25000u16, | ||
p2p_port: 15000u16, | ||
}; | ||
let cdn_node_params = CDNNodeParams { | ||
host: vec![1u8, 255], | ||
http_port: 35000u16, | ||
grpc_port: 25000u16, | ||
p2p_port: 15000u16, | ||
}; | ||
|
||
// Node doesn't exist | ||
assert_noop!( | ||
DdcNodes::set_node_params( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
), | ||
Error::<Test>::NodeDoesNotExist | ||
); | ||
|
||
// Node created | ||
assert_ok!(DdcNodes::create_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
)); | ||
|
||
// Set node params | ||
assert_ok!(DdcNodes::set_node_params( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
)); | ||
|
||
// Node params are not valid | ||
assert_noop!( | ||
DdcNodes::set_node_params( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::StorageParams(storage_node_params) | ||
), | ||
Error::<Test>::InvalidNodeParams | ||
); | ||
|
||
// Only node provider can set params | ||
assert_noop!( | ||
DdcNodes::set_node_params( | ||
RuntimeOrigin::signed(2), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
), | ||
Error::<Test>::OnlyNodeProvider | ||
); | ||
|
||
// Checking that event was emitted | ||
assert_eq!(System::events().len(), 2); | ||
System::assert_last_event( | ||
Event::NodeParamsChanged { node_pub_key: NodePubKey::CDNPubKey(node_pub_key) }.into(), | ||
) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn set_delete_node_works() { | ||
ExtBuilder.build_and_execute(|| { | ||
System::set_block_number(1); | ||
let bytes = [0u8; 32]; | ||
let node_pub_key = AccountId32::from(bytes); | ||
let cdn_node_params = CDNNodeParams { | ||
host: vec![1u8, 255], | ||
http_port: 35000u16, | ||
grpc_port: 25000u16, | ||
p2p_port: 15000u16, | ||
}; | ||
|
||
// Node doesn't exist | ||
assert_noop!( | ||
DdcNodes::delete_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()) | ||
), | ||
Error::<Test>::NodeDoesNotExist | ||
); | ||
|
||
// Create node | ||
assert_ok!(DdcNodes::create_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
NodeParams::CDNParams(cdn_node_params.clone()) | ||
)); | ||
|
||
// Only node provider can delete | ||
assert_noop!( | ||
DdcNodes::delete_node( | ||
RuntimeOrigin::signed(2), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()) | ||
), | ||
Error::<Test>::OnlyNodeProvider | ||
); | ||
|
||
// Delete node | ||
assert_ok!(DdcNodes::delete_node( | ||
RuntimeOrigin::signed(1), | ||
NodePubKey::CDNPubKey(node_pub_key.clone()), | ||
)); | ||
|
||
// Checking that event was emitted | ||
assert_eq!(System::events().len(), 2); | ||
System::assert_last_event( | ||
Event::NodeDeleted { node_pub_key: NodePubKey::CDNPubKey(node_pub_key) }.into(), | ||
) | ||
}) | ||
} |