Skip to content

Commit

Permalink
test(sdl): for each spec expect full groups and manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed May 10, 2024
1 parent 03af646 commit 0211266
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 73 deletions.
158 changes: 87 additions & 71 deletions src/sdl/SDL/SDL.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { faker } from "@faker-js/faker";

import { readBasicSdl } from "../../../test/yml";
import { createGroupsWith, createManifestWith, readBasicSdl } from "../../../test/templates";
import { SdlValidationError } from "../../error";
import { SDL } from "./SDL";
import { v2ServiceImageCredentials } from "../types";
import { omit } from "lodash";
import omit from "lodash/omit";
import { AKT_DENOM, SANDBOX_ID, USDC_IBC_DENOMS } from "../../config/network";

describe("SDL", () => {
Expand All @@ -13,18 +13,21 @@ describe("SDL", () => {
const yml = readBasicSdl({ denom });
const sdl = SDL.fromString(yml, "beta3", "sandbox");

expect(sdl.groups()).toMatchObject([
{
resources: [
{
price: {
denom: denom,
amount: "1000"
expect(sdl.manifest()).toMatchObject(createManifestWith());
expect(sdl.groups()).toMatchObject(
createGroupsWith([
{
resources: [
{
price: {
denom: denom,
amount: "1000"
}
}
}
]
}
]);
]
}
])
);
});

it("should throw an error when denomination is invalid", () => {
Expand All @@ -39,53 +42,57 @@ describe("SDL", () => {

describe("endpoints", () => {
it("should resolve with valid endpoints", () => {
const endpointName = faker.lorem.word();
const endpointName = faker.lorem.word({ length: { min: 3, max: 10 } });
const endpoint = {
[endpointName]: {
kind: "ip"
}
};
const yml = readBasicSdl({ endpoint });
const yml = readBasicSdl({ endpoint, denom: "uakt" });
const sdl = SDL.fromString(yml, "beta3", "sandbox");

expect(sdl.manifest()).toMatchObject([
{
services: [
{
resources: {
endpoints: {
1: {
kind: 2,
sequence_number: 1
expect(sdl.manifest()).toMatchObject(
createManifestWith([
{
services: [
{
resources: {
endpoints: {
1: {
kind: 2,
sequence_number: 1
}
}
}
},
expose: [
{
ip: endpointName,
endpointSequenceNumber: 1
}
]
}
]
}
]);
expect(sdl.groups()).toMatchObject([
{
resources: [
{
resource: {
endpoints: {
1: {
kind: 2,
sequence_number: 1
},
expose: [
{
ip: endpointName,
endpointSequenceNumber: 1
}
]
}
]
}
])
);
expect(sdl.groups()).toMatchObject(
createGroupsWith([
{
resources: [
{
resource: {
endpoints: {
1: {
kind: 2,
sequence_number: 1
}
}
}
}
}
]
}
]);
]
}
])
);
});

it("should throw provided an invalid endpoint name", () => {
Expand All @@ -101,7 +108,7 @@ describe("SDL", () => {
});

it("should throw provided no endpoint kind", () => {
const endpointName = faker.lorem.word();
const endpointName = faker.lorem.word({ length: { min: 3, max: 10 } });
const endpoint = {
[endpointName]: {}
};
Expand All @@ -111,7 +118,7 @@ describe("SDL", () => {
});

it("should throw provided invalid endpoint kind", () => {
const endpointName = faker.lorem.word();
const endpointName = faker.lorem.word({ length: { min: 3, max: 10 } });
const endpointKind = faker.lorem.word();
const endpoint = {
[endpointName]: {
Expand All @@ -126,7 +133,7 @@ describe("SDL", () => {
});

it("should throw when endpoint is unused", () => {
const endpointName = faker.lorem.word();
const endpointName = faker.lorem.word({ length: { min: 3, max: 10 } });
const endpoint = {
[endpointName]: {
kind: "ip"
Expand All @@ -145,28 +152,37 @@ describe("SDL", () => {
username: faker.internet.userName(),
password: faker.internet.password()
};
const sdl = SDL.fromString(readBasicSdl({ credentials }), "beta3", "sandbox");

expect(sdl.manifest()).toMatchObject([
{
services: [
{
credentials
}
]
}
]);
const sdl = SDL.fromString(readBasicSdl({ credentials, denom: "uakt" }), "beta3", "sandbox");

expect(sdl.manifest()).toMatchObject(
createManifestWith([
{
services: [
{
credentials
}
]
}
])
);
expect(sdl.groups()).toMatchObject(createGroupsWith());
});

it("should resolve a service without credentials", () => {
const sdl = SDL.fromString(readBasicSdl(), "beta3", "sandbox");
const group = sdl.manifest()[0];

if (!("services" in group)) {
throw new Error("No services found in group");
}

expect(group.services[0].credentials).toBeNull();
const sdl = SDL.fromString(readBasicSdl({ denom: "uakt" }), "beta3", "sandbox");

expect(sdl.manifest()).toMatchObject(
createManifestWith([
{
services: [
{
credentials: null
}
]
}
])
);
expect(sdl.groups()).toMatchObject(createGroupsWith());
});

describe("invalid credentials errors", () => {
Expand Down
78 changes: 78 additions & 0 deletions test/fixtures/groups-basic-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[
{
"name": "dcloud",
"resources": [
{
"resource": {
"endpoints": [
{
"sequence_number": 0
}
],
"cpu": {
"units": {
"val": {
"0": 53,
"1": 48,
"2": 48
}
}
},
"memory": {
"quantity": {
"val": {
"0": 53,
"1": 51,
"2": 54,
"3": 56,
"4": 55,
"5": 48,
"6": 57,
"7": 49,
"8": 50
}
}
},
"storage": [
{
"name": "default",
"quantity": {
"val": {
"0": 53,
"1": 51,
"2": 54,
"3": 56,
"4": 55,
"5": 48,
"6": 57,
"7": 49,
"8": 50
}
}
}
],
"gpu": {
"units": {
"val": {
"0": 48
}
}
},
"id": 1
},
"price": {
"denom": "uakt",
"amount": "1000"
},
"count": 1
}
],
"requirements": {
"attributes": [],
"signedBy": {
"allOf": [],
"anyOf": []
}
}
}
]
68 changes: 68 additions & 0 deletions test/fixtures/manifest-basic-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[
{
"name": "dcloud",
"services": [
{
"name": "web",
"image": "akashlytics/hello-akash-world:0.2.0",
"command": null,
"args": null,
"env": null,
"resources": {
"id": 1,
"cpu": {
"units": {
"val": "500"
}
},
"memory": {
"size": {
"val": 536870912
}
},
"storage": [
{
"name": "default",
"size": {
"val": 536870912
}
}
],
"endpoints": [
{
"sequence_number": 0
}
],
"gpu": {
"units": {
"val": 0
}
}
},
"count": 1,
"expose": [
{
"port": 3000,
"externalPort": 80,
"proto": "TCP",
"service": "",
"global": true,
"hosts": null,
"httpOptions": {
"maxBodySize": 1048576,
"readTimeout": 60000,
"sendTimeout": 60000,
"nextTries": 3,
"nextTimeout": 0,
"nextCases": ["error", "timeout"]
},
"ip": "",
"endpointSequenceNumber": 0
}
],
"params": null,
"credentials": null
}
]
}
]
14 changes: 13 additions & 1 deletion test/yml.ts → test/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { dump } from "js-yaml";
import { faker } from "@faker-js/faker";
import template from "lodash/template";
import memoize from "lodash/memoize";
import pick from "lodash/pick";

import { AKT_DENOM } from "../src/config/network";
import { SANDBOX_ID, USDC_IBC_DENOMS } from "../src/config/network";
import { pick } from "lodash";
import groupsBasicSnapshot from "./fixtures/groups-basic-snapshot.json";
import manifestBasicSnapshot from "./fixtures/manifest-basic-snapshot.json";
import { merge } from "lodash";

export const readYml = (name: string): string => {
return fs.readFileSync(path.resolve(__dirname, `./fixtures/${name}.yml`), "utf-8");
Expand Down Expand Up @@ -58,3 +62,11 @@ export const readBasicSdl = (variables: BasicSdlTemplateVariables = {}): string

return createYML(ymlVars);
};

const jsonTemplate =
<T extends Record<string, any>>(template: T) =>
(variables: Record<string, any> = {}) =>
merge({}, template, variables);

export const createManifestWith = jsonTemplate(manifestBasicSnapshot);
export const createGroupsWith = jsonTemplate(groupsBasicSnapshot);
Loading

0 comments on commit 0211266

Please sign in to comment.