diff --git a/src/sdl/SDL/SDL.spec.ts b/src/sdl/SDL/SDL.spec.ts index 5e9edb3..ec8fddc 100644 --- a/src/sdl/SDL/SDL.spec.ts +++ b/src/sdl/SDL/SDL.spec.ts @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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]: {} }; @@ -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]: { @@ -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" @@ -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", () => { diff --git a/test/fixtures/groups-basic-snapshot.json b/test/fixtures/groups-basic-snapshot.json new file mode 100644 index 0000000..43c7a79 --- /dev/null +++ b/test/fixtures/groups-basic-snapshot.json @@ -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": [] + } + } + } +] diff --git a/test/fixtures/manifest-basic-snapshot.json b/test/fixtures/manifest-basic-snapshot.json new file mode 100644 index 0000000..901d8e3 --- /dev/null +++ b/test/fixtures/manifest-basic-snapshot.json @@ -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 + } + ] + } +] diff --git a/test/yml.ts b/test/templates.ts similarity index 80% rename from test/yml.ts rename to test/templates.ts index 5818812..c06be3f 100644 --- a/test/yml.ts +++ b/test/templates.ts @@ -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"); @@ -58,3 +62,11 @@ export const readBasicSdl = (variables: BasicSdlTemplateVariables = {}): string return createYML(ymlVars); }; + +const jsonTemplate = + >(template: T) => + (variables: Record = {}) => + merge({}, template, variables); + +export const createManifestWith = jsonTemplate(manifestBasicSnapshot); +export const createGroupsWith = jsonTemplate(groupsBasicSnapshot); diff --git a/tsconfig.json b/tsconfig.json index 48cf78f..d106ee7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -66,7 +66,8 @@ /* Advanced Options */ "skipLibCheck": true /* Skip type checking of declaration files. */, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "resolveJsonModule": true }, "include": ["./src/**/*"], "exclude": ["./examples", "./tests", "./test", "./src/**/*.spec.ts"]