Skip to content

Commit

Permalink
feat: add datacenter and resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamsartem committed Jan 17, 2025
1 parent 33720de commit e349051
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package/src/commands/provider/offer-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export default class OfferInfo extends BaseCommand<typeof OfferInfo> {
const { flags } = await initCli(this, await this.parse(OfferInfo));
const offers = await resolveCreatedOffers(flags);
const offerInfoResult = await getOffersInfo(offers);
commandObj.logToStderr(await offersInfoToString(offerInfoResult));
commandObj.log(await offersInfoToString(offerInfoResult));
}
}
9 changes: 9 additions & 0 deletions packages/cli/package/src/lib/dealClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export async function getContracts() {
return { contracts, providerOrWallet };
}

export async function getContractsByPrivKey(privKey: string) {
const providerOrWallet = await getWallet(privKey);

return {
providerOrWallet,
contracts: await createContracts(providerOrWallet),
};
}

export async function getSignerAddress() {
const { providerOrWallet } = await getContracts();

Expand Down
157 changes: 146 additions & 11 deletions packages/cli/package/test/tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@
import assert from "node:assert";
import { join } from "node:path";

import { describe } from "vitest";
import { describe, expect } from "vitest";

import { LOCAL_NET_DEFAULT_ACCOUNTS } from "../../src/common.js";
import {
LOCAL_NET_DEFAULT_ACCOUNTS,
LOCAL_NET_DEFAULT_WALLET_KEY,
} from "../../src/common.js";
import { getConfigInitFunction } from "../../src/lib/configs/initConfigNew.js";
import { options as providerConfigOptions } from "../../src/lib/configs/project/provider/provider.js";
import {
initProviderConfig,
options as providerConfigOptions,
} from "../../src/lib/configs/project/provider/provider.js";
import { dataCenterToHumanReadableString } from "../../src/lib/configs/project/provider/provider4.js";
import {
OFFER_FLAG_NAME,
PRIV_KEY_FLAG_NAME,
PROVIDER_CONFIG_FULL_FILE_NAME,
} from "../../src/lib/const.js";
import {
getContractsByPrivKey,
getEventValue,
} from "../../src/lib/dealClient.js";
import { stringifyUnknown } from "../../src/lib/helpers/stringifyUnknown.js";
import { numToStr } from "../../src/lib/helpers/typesafeStringify.js";
import { fluence } from "../helpers/commonWithSetupTests.js";
Expand All @@ -39,13 +50,22 @@ const PRIV_KEY_1 = {
[PRIV_KEY_FLAG_NAME]: LOCAL_NET_DEFAULT_ACCOUNTS[1].privateKey,
};

async function initProviderConfigWithPath(path: string) {
return getConfigInitFunction({
async function initProviderConfigWithPath(
path: string,
): Promise<NonNullable<Awaited<ReturnType<typeof initProviderConfig>>>> {
const providerConfig = await getConfigInitFunction({
...providerConfigOptions,
getConfigPath() {
return join(path, PROVIDER_CONFIG_FULL_FILE_NAME);
},
})();

assert(
providerConfig !== null,
"Provider config must already exists in a quickstart template",
);

return providerConfig;
}

describe("provider tests", () => {
Expand All @@ -54,14 +74,8 @@ describe("provider tests", () => {
async () => {
const cwd = join("test", "tmp", "fullLifeCycle");
await initializeTemplate(cwd);

const providerConfig = await initProviderConfigWithPath(cwd);

assert(
providerConfig !== null,
"Provider config must already exists in a quickstart template",
);

// add extra capacity commitments and compute peers not used in any offer
providerConfig.capacityCommitments = Object.fromEntries(
Object.values(providerConfig.capacityCommitments).map((config, i) => {
Expand Down Expand Up @@ -165,6 +179,127 @@ describe("provider tests", () => {
});
},
);

wrappedTest(
"create offer with newly added datacenter, update datacenter",
async () => {
const cwd = join("test", "tmp", "addDatacenter");
await initializeTemplate(cwd);

const { contracts } = await getContractsByPrivKey(
LOCAL_NET_DEFAULT_WALLET_KEY,
);

const countryCode = "BY";
const cityCode = "MNSK";
const cityIndex = "0";
const tier = 3;
const NO_CERTIFICATIONS = "NO_CERTIFICATIONS";
const certifications = [NO_CERTIFICATIONS];

const createDatacenterTxReceipt = await (
await contracts.diamond.createDatacenter({
countryCode,
cityCode,
index: cityIndex,
tier,
certifications,
})
).wait();

assert(createDatacenterTxReceipt !== null, "Tx receipt must exist");

const createdDatacenterId = await getEventValue({
txReceipt: createDatacenterTxReceipt,
contract: contracts.diamond,
eventName: "DatacenterCreated",
value: "id",
});

assert(
typeof createdDatacenterId === "string",
"Datacenter id must be a string",
);

const providerConfig = await initProviderConfigWithPath(cwd);
const [defaultOffer] = Object.keys(providerConfig.offers);

assert(
defaultOffer !== undefined &&
providerConfig.offers[defaultOffer] !== undefined,
"Default offer must exist in the provider config",
);

providerConfig.offers[defaultOffer].dataCenterName =
dataCenterToHumanReadableString({
countryCode,
cityCode,
cityIndex,
});

await providerConfig.$commit();

await fluence({
args: ["provider", "offer-create"],
flags: {
...PRIV_KEY_1,
[OFFER_FLAG_NAME]: defaultOffer,
},
cwd,
});

const offerInfoWithNewDatacenter = await fluence({
args: ["provider", "offer-info"],
flags: {
...PRIV_KEY_1,
[OFFER_FLAG_NAME]: defaultOffer,
},
cwd,
});

expect(offerInfoWithNewDatacenter).toEqual(
expect.stringContaining(`countryCode: ${countryCode}`),
);

expect(offerInfoWithNewDatacenter).toEqual(
expect.stringContaining(`tier: "${numToStr(tier)}"`),
);

expect(offerInfoWithNewDatacenter).toEqual(
expect.stringContaining(NO_CERTIFICATIONS),
);

const NEW_CERTIFICATIONS = "ALL_CERTIFICATIONS";
const newTier = 4;

await (
await contracts.diamond.updateDatacenter(createdDatacenterId, newTier, [
NEW_CERTIFICATIONS,
])
).wait();

const offerInfoWithUpdatedDatacenter = await fluence({
args: ["provider", "offer-info"],
flags: {
...PRIV_KEY_1,
[OFFER_FLAG_NAME]: defaultOffer,
},
cwd,
});

expect(offerInfoWithUpdatedDatacenter).toEqual(
expect.stringContaining(`countryCode: ${countryCode}`),
);

expect(offerInfoWithUpdatedDatacenter).toEqual(
expect.stringContaining(`tier: "${numToStr(newTier)}"`),
);

expect(offerInfoWithUpdatedDatacenter).toEqual(
expect.stringContaining(NEW_CERTIFICATIONS),
);
},
);
});

async function checkProviderNameIsCorrect(cwd: string, providerName: string) {
Expand Down

0 comments on commit e349051

Please sign in to comment.