Skip to content

Commit

Permalink
add tests for ddc-node
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 9, 2023
1 parent 958aff1 commit 651ca80
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ sp-std = { version = "4.0.0-dev", default-features = false, git = "https://githu
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
pallet-timestamp = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-tracing = { version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
substrate-test-utils = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
Expand Down
5 changes: 5 additions & 0 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![recursion_limit = "256"]

#[cfg(test)]
pub(crate) mod mock;
#[cfg(test)]
mod tests;

use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, StorageNodePubKey};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
Expand Down
114 changes: 114 additions & 0 deletions pallets/ddc-nodes/src/mock.rs
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);
}
}
183 changes: 183 additions & 0 deletions pallets/ddc-nodes/src/tests.rs
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(),
)
})
}

0 comments on commit 651ca80

Please sign in to comment.