Browse Source

network-tests: BasicFixture

Mokhtar Naamani 4 years ago
parent
commit
559b032163

+ 2 - 2
tests/network-tests/.env

@@ -1,9 +1,9 @@
 # Address of the Joystream node.
 NODE_URL = ws://127.0.0.1:9944
-# Path to the database for shared keys and nonce
-DB_PATH = .tmp/db.json
 # Account which is expected to provide sufficient funds to test accounts.
 TREASURY_ACCOUNT_URI = //Alice
+# Sudo Account
+SUDO_ACCOUNT_URI = //Alice
 # Amount of members able to buy membership in membership creation test.
 MEMBERSHIP_CREATION_N = 2
 # ID of the membership paid terms used in membership creation test.

+ 8 - 0
tests/network-tests/src/Api.ts

@@ -856,6 +856,14 @@ export class Api {
     return this.getBlockDuration().muln(durationInBlocks).toNumber()
   }
 
+  public expectMemberRegisteredEvent(events: EventRecord[]): MemberId {
+    const record = events.find((record) => record.event.method && record.event.method.toString() === 'MemberRegistered')
+    if (!record) {
+      throw new Error('Expected Event Not Found')
+    }
+    return record.event.data[0] as MemberId
+  }
+
   public expectProposalCreatedEvent(events: EventRecord[]): ProposalId {
     const record = events.find((record) => record.event.method && record.event.method.toString() === 'ProposalCreated')
     if (!record) {

+ 30 - 0
tests/network-tests/src/Fixture.ts

@@ -0,0 +1,30 @@
+import { Api } from './Api'
+
+export interface Fixture {
+  runner(expectFailure: boolean): Promise<void>
+}
+
+// Fixture that measures start and end blocks
+// ensures fixture only runs once
+export class BaseFixture implements Fixture {
+  protected api: Api
+  private ran = false
+
+  constructor(api: Api) {
+    this.api = api
+    // record starting block
+  }
+
+  public async runner(expectFailure: boolean): Promise<void> {
+    if (this.ran) {
+      return
+    }
+    this.ran = true
+    return this.execute(expectFailure)
+    // record end blocks
+  }
+
+  protected async execute(expectFailure: boolean): Promise<void> {
+    return
+  }
+}

+ 0 - 3
tests/network-tests/src/IFixture.ts

@@ -1,3 +0,0 @@
-export interface Fixture {
-  runner(expectFailure: boolean): Promise<void>
-}

+ 1 - 1
tests/network-tests/src/fixtures/councilElectionHappyCase.ts

@@ -1,4 +1,4 @@
-import { Fixture } from '../IFixture'
+import { Fixture } from '../Fixture'
 import { ElectCouncilFixture } from './councilElectionModule'
 import { Api } from '../Api'
 import BN from 'bn.js'

+ 1 - 1
tests/network-tests/src/fixtures/councilElectionModule.ts

@@ -4,7 +4,7 @@ import { assert } from 'chai'
 import { Seat } from '@joystream/types/council'
 import { v4 as uuid } from 'uuid'
 import { Utils } from '../utils'
-import { Fixture } from '../IFixture'
+import { Fixture } from '../Fixture'
 
 export class ElectCouncilFixture implements Fixture {
   private api: Api

+ 22 - 19
tests/network-tests/src/fixtures/membershipModule.ts

@@ -1,21 +1,29 @@
 import { Api } from '../Api'
 import BN from 'bn.js'
 import { assert } from 'chai'
-import { Fixture } from '../IFixture'
-import { PaidTermId } from '@joystream/types/members'
+import { Fixture, BaseFixture } from '../Fixture'
+import { PaidTermId, MemberId } from '@joystream/types/members'
+import Debugger from 'debug'
 
-export class BuyMembershipHappyCaseFixture implements Fixture {
-  private api: Api
+export class BuyMembershipHappyCaseFixture extends BaseFixture {
   private accounts: string[]
   private paidTerms: PaidTermId
+  private debug: Debugger.Debugger
+  private memberIds: MemberId[] = []
 
   public constructor(api: Api, accounts: string[], paidTerms: PaidTermId) {
-    this.api = api
+    super(api)
     this.accounts = accounts
     this.paidTerms = paidTerms
+    this.debug = Debugger('fixture:BuyMembershipHappyCaseFixture')
   }
 
-  public async runner(expectFailure: boolean): Promise<void> {
+  public getCreatedMembers(): MemberId[] {
+    return this.memberIds.slice()
+  }
+
+  public async execute(expectFailure: boolean): Promise<void> {
+    this.debug(`Registering ${this.accounts.length} new members`)
     // Fee estimation and transfer
     const membershipFee: BN = await this.api.getMembershipFee(this.paidTerms)
     const membershipTransactionFee: BN = this.api.estimateBuyMembershipFee(
@@ -25,20 +33,15 @@ export class BuyMembershipHappyCaseFixture implements Fixture {
     )
     this.api.treasuryTransferBalanceToAccounts(this.accounts, membershipTransactionFee.add(new BN(membershipFee)))
 
-    // Buying membership
-    await Promise.all(
-      this.accounts.map(async (account) => {
-        await this.api.buyMembership(account, this.paidTerms, `member${account.substring(0, 14)}`)
-      })
-    )
-
-    // Assertions
-    this.accounts.forEach((account) =>
-      this.api
-        .getMemberIds(account)
-        .then((membership) => assert(membership.length > 0, `Account ${account} is not a member`))
-    )
+    this.memberIds = (
+      await Promise.all(
+        this.accounts.map((account) =>
+          this.api.buyMembership(account, this.paidTerms, `member${account.substring(0, 14)}`)
+        )
+      )
+    ).map(({ events }) => this.api.expectMemberRegisteredEvent(events))
 
+    this.debug(`New member ids: ${this.memberIds}`)
     if (expectFailure) {
       throw new Error('Successful fixture run while expecting failure')
     }

+ 1 - 1
tests/network-tests/src/fixtures/proposalsModule.ts

@@ -2,7 +2,7 @@ import { Api, WorkingGroups } from '../Api'
 import { v4 as uuid } from 'uuid'
 import BN from 'bn.js'
 import { ProposalId } from '@joystream/types/proposals'
-import { Fixture } from '../IFixture'
+import { Fixture } from '../Fixture'
 import { assert } from 'chai'
 import { ApplicationId, OpeningId } from '@joystream/types/hiring'
 import { WorkerId } from '@joystream/types/working-group'

+ 1 - 1
tests/network-tests/src/fixtures/sudoHireLead.ts

@@ -1,4 +1,4 @@
-import { Fixture } from '../IFixture'
+import { Fixture } from '../Fixture'
 import {
   SudoAddLeaderOpeningFixture,
   ApplyForOpeningFixture,

+ 1 - 1
tests/network-tests/src/fixtures/workingGroupModule.ts

@@ -7,7 +7,7 @@ import { RewardRelationship } from '@joystream/types/recurring-rewards'
 import { Application, ApplicationIdToWorkerIdMap, Worker, WorkerId } from '@joystream/types/working-group'
 import { Utils } from '../utils'
 import { ApplicationId, Opening as HiringOpening, OpeningId } from '@joystream/types/hiring'
-import { Fixture } from '../IFixture'
+import { Fixture } from '../Fixture'
 
 export class AddWorkerOpeningFixture implements Fixture {
   private api: Api

+ 4 - 4
tests/network-tests/src/flows/membership/creatingMemberships.ts

@@ -7,16 +7,15 @@ import { PaidTermId } from '@joystream/types/members'
 import BN from 'bn.js'
 import Debugger from 'debug'
 
-const debug = Debugger('flow:memberships')
+export default async function membershipCreation(api: Api, env: NodeJS.ProcessEnv) {
+  const debug = Debugger('flow:memberships')
+  debug('started')
 
-// Membership creation scenario
-export default async function memberShipCreation(api: Api, env: NodeJS.ProcessEnv) {
   const N: number = +env.MEMBERSHIP_CREATION_N!
   const nAccounts = api.createKeyPairs(N).map((key) => key.address)
   const aAccount = api.createKeyPairs(1)[0].address
   const paidTerms: PaidTermId = api.createPaidTermId(new BN(+env.MEMBERSHIP_PAID_TERMS!))
 
-  debug(`creating ${N} new members`)
   const happyCaseFixture = new BuyMembershipHappyCaseFixture(api, nAccounts, paidTerms)
   // Buy membeship is accepted with sufficient funds
   await happyCaseFixture.runner(false)
@@ -33,4 +32,5 @@ export default async function memberShipCreation(api: Api, env: NodeJS.ProcessEn
 
   // Account A was able to buy the membership with sufficient funds
   await buyMembershipAfterAccountTopUp.runner(false)
+  debug('finished')
 }

+ 3 - 4
tests/network-tests/src/flows/proposals/councilSetup.ts

@@ -8,7 +8,6 @@ import { assert } from 'chai'
 
 const debug = Debugger('flow:councilSetup')
 
-// Electing council scenario
 export default async function councilSetup(api: Api, env: NodeJS.ProcessEnv) {
   // Skip creating council if already elected
   if ((await api.getCouncil()).length) {
@@ -17,8 +16,8 @@ export default async function councilSetup(api: Api, env: NodeJS.ProcessEnv) {
   }
 
   const numberOfApplicants = (await api.getCouncilSize()).toNumber() * 2
-  const voters = api.createKeyPairs(5).map((key) => key.address)
   const applicants = api.createKeyPairs(numberOfApplicants).map((key) => key.address)
+  const voters = api.createKeyPairs(5).map((key) => key.address)
 
   const paidTerms: PaidTermId = api.createPaidTermId(new BN(+env.MEMBERSHIP_PAID_TERMS!))
   const K: number = +env.COUNCIL_ELECTION_K!
@@ -33,8 +32,8 @@ export default async function councilSetup(api: Api, env: NodeJS.ProcessEnv) {
   // that is smaller than the council size if not enough members apply.
   const councilElectionHappyCaseFixture = new CouncilElectionHappyCaseFixture(
     api,
-    voters,
-    applicants,
+    voters, // should be member ids
+    applicants, // should be member ids
     K,
     greaterStake,
     lesserStake

+ 2 - 3
tests/network-tests/src/flows/workingGroup/manageWorkerAsLead.ts

@@ -54,7 +54,7 @@ export default async function manageWorker(api: Api, env: NodeJS.ProcessEnv, gro
     group
   )
   await applyForWorkerOpeningFixture.runner(false)
-  debug('Applications:', applyForWorkerOpeningFixture.getApplicationIds())
+
   const applicationIdsToHire = applyForWorkerOpeningFixture.getApplicationIds().slice(0, 2)
 
   // Begin application review
@@ -65,7 +65,6 @@ export default async function manageWorker(api: Api, env: NodeJS.ProcessEnv, gro
   )
   await beginApplicationReviewFixture.runner(false)
 
-  debug('Filling with ids:', applicationIdsToHire)
   // Fill worker opening
   const fillOpeningFixture = new FillOpeningFixture(
     api,
@@ -77,7 +76,7 @@ export default async function manageWorker(api: Api, env: NodeJS.ProcessEnv, gro
     group
   )
   await fillOpeningFixture.runner(false)
-  debug('Hired worker ids', fillOpeningFixture.getWorkerIds())
+
   const firstWorkerId = fillOpeningFixture.getWorkerIds()[0]
 
   const decreaseStakeFixture = new DecreaseStakeFixture(api, firstWorkerId, group)

+ 5 - 2
tests/network-tests/src/scenarios/full.ts

@@ -27,11 +27,13 @@ const scenario = async () => {
   // Connect api to the chain
   const nodeUrl: string = env.NODE_URL || 'ws://127.0.0.1:9944'
   const provider = new WsProvider(nodeUrl)
-  const api: Api = await Api.create(provider, env.TREASURY_ACCOUNT_URI || '//Alice', '//Alice')
+  const api: Api = await Api.create(provider, env.TREASURY_ACCOUNT_URI || '//Alice', env.SUDO_ACCOUNT_URI || '//Alice')
 
   await Promise.all([creatingMemberships(api, env), councilSetup(api, env)])
 
-  // MaxActiveProposalLimit = 5
+  // Runtime is configured for MaxActiveProposalLimit = 5
+  // So we should ensure we don't exceed that number of active proposals
+  // which limits the number of concurrent tests that create proposals
   await Promise.all([
     electionParametersProposal(api, env),
     spendingProposal(api, env),
@@ -51,6 +53,7 @@ const scenario = async () => {
     leaderSetup(api, env, WorkingGroups.ContentDirectoryWorkingGroup),
   ])
 
+  // All tests below require an active Lead for each group
   // Test bug only on one instance of working group is sufficient
   await atLeastValueBug(api, env)