diff --git a/src/test/mocks/cockpit/cockpitFile.ts b/src/test/mocks/cockpit/cockpitFile.ts new file mode 100644 index 00000000..a20eb4e8 --- /dev/null +++ b/src/test/mocks/cockpit/cockpitFile.ts @@ -0,0 +1,48 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import path from 'path'; + +import { mockGetBlueprints } from '../../fixtures/blueprints'; +import { mockComposes } from '../../fixtures/composes'; + +const readBlueprint = (id: string): Promise => { + for (const bp of mockGetBlueprints.data) { + if (bp.id === id) { + return new Promise((resolve) => { + resolve(JSON.stringify(bp)); + }); + } + } + return new Promise((resolve) => { + resolve('{}'); + }); +}; + +const readCompose = (id: string): Promise => { + for (const compose of mockComposes) { + if (compose.id === id) { + return new Promise((resolve) => { + resolve(JSON.stringify(compose.request)); + }); + } + } + return new Promise((resolve) => { + resolve('{}'); + }); +}; + +export const cockpitFile = (filepath: string) => { + return { + read: (): Promise => { + const file = path.parse(filepath); + const dir = path.parse(file.dir); + + // if the directory matches the file it's a blueprint + if (file.name === dir.name) { + return readBlueprint(file.name); + } + return readCompose(file.name); + }, + close: () => {}, + replace: (contents: string) => {}, + }; +}; diff --git a/src/test/mocks/cockpit/cockpitHTTP.ts b/src/test/mocks/cockpit/cockpitHTTP.ts new file mode 100644 index 00000000..3a0fdb55 --- /dev/null +++ b/src/test/mocks/cockpit/cockpitHTTP.ts @@ -0,0 +1,28 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import path from 'path'; + +import type { Method, Headers, Params } from '../../../store/cockpit/types'; +import { mockStatus } from '../../fixtures/composes'; + +type requestOptions = { + path: string; + method: Method; + body: unknown; + headers: Headers | undefined; + params: Params | undefined; +}; + +export const cockpitHTTP = (address: string, attr: object) => { + return { + request: (request: requestOptions): Promise => { + if (request.path.startsWith('/api/image-builder-composer/v2/composes/')) { + return new Promise((resolve) => { + resolve(JSON.stringify(mockStatus(path.parse(request.path).name))); + }); + } + return new Promise((resolve) => { + resolve(''); + }); + }, + }; +}; diff --git a/src/test/mocks/cockpit/fsinfo.ts b/src/test/mocks/cockpit/fsinfo.ts index 856c39ab..75a870b8 100644 --- a/src/test/mocks/cockpit/fsinfo.ts +++ b/src/test/mocks/cockpit/fsinfo.ts @@ -1,15 +1,75 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ +import path from 'path'; + +import { mockBlueprintIds } from '../../fixtures/blueprints'; +import { mockComposes } from '../../fixtures/composes'; + +const bpDir = '/default/.cache/cockpit-image-builder/'; + type fileinfo = { entries?: Record; mtime: number; }; +interface FSInfoMap { + [id: string]: fileinfo; +} + +export const listBlueprints = (): Promise => { + const result: FSInfoMap = {}; + for (const e of Object.values(mockBlueprintIds)) { + result[e] = { + mtime: 1, + }; + } + return new Promise((resolve) => { + resolve({ + entries: result, + mtime: 1, + }); + }); +}; + +export const listComposes = (): Promise => { + const result: FSInfoMap = {}; + let count = 0; + for (const e of mockComposes) { + // hack + if (count === 10) { + break; + } + count += 1; + result[e.id] = { + mtime: new Date(e.created_at).getTime() / 1000, + }; + } + return new Promise((resolve) => { + resolve({ + entries: result, + mtime: 1, + }); + }); +}; + export const fsinfo = ( - path: string, + filepath: string, attributes: (keyof fileinfo)[], options: object ): Promise => { + if (filepath === bpDir) { + return listBlueprints(); + } + + // HACK: the composes in fixture don't have blueprints attached, so + // abuse one blueprint to return all composes. + if ( + filepath === + '/default/.cache/cockpit-image-builder/b3ff8307-18bd-418a-9a91-836ce039b035' + ) { + return listComposes(); + } + return new Promise((resolve) => { resolve({ entries: {}, diff --git a/src/test/mocks/cockpit/index.ts b/src/test/mocks/cockpit/index.ts index 18b8fe67..36f2ab48 100644 --- a/src/test/mocks/cockpit/index.ts +++ b/src/test/mocks/cockpit/index.ts @@ -1,18 +1,14 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import type { Method, Headers, Params } from '../../../store/cockpit/types'; + +import path from 'path'; + +import { cockpitFile } from './cockpitFile'; +import { cockpitHTTP } from './cockpitHTTP'; type userinfo = { home: string; }; -type requestOptions = { - path: string; - method: Method; - body: unknown; - headers: Headers | undefined; - params: Params | undefined; -}; - export default { transport: { host: '', @@ -21,43 +17,15 @@ export default { user: (): Promise => { return new Promise((resolve) => { resolve({ - home: '', + home: '/default', }); }); }, - file: (path: string) => { - return { - read: (): Promise => { - return new Promise((resolve) => { - resolve(''); - }); - }, - close: () => {}, - replace: (contents: string): Promise => { - return new Promise((resolve) => { - resolve(); - }); - }, - }; - }, + file: cockpitFile, spawn: (args: string[], attributes: object): Promise => { return new Promise((resolve) => { resolve(''); }); }, - http: (address: string, options: object) => { - return { - get: (path?: string, headers?: object): string => { - return ''; - }, - post: (path: string, data: object, headers?: object): string => { - return ''; - }, - request: (request: requestOptions): Promise => { - return new Promise((resolve) => { - resolve(''); - }); - }, - }; - }, + http: cockpitHTTP, };