Files
szci/test/test.ts
2025-12-14 01:42:14 +00:00

160 lines
5.0 KiB
TypeScript

import { assertEquals, assertExists } from '@std/assert';
import * as path from '@std/path';
import * as smartpath from '@push.rocks/smartpath';
// Set up test environment with the NEW SZCI environment variables
Deno.env.set('SZCI_TEST', 'true');
Deno.env.set('SZCI_URL_CLOUDLY', 'localhost');
Deno.env.set('CI_REPOSITORY_URL', 'https://yyyyyy:xxxxxxxx@gitlab.com/mygroup/myrepo.git');
Deno.env.set('CI_BUILD_TOKEN', 'kjlkjfiudofiufs');
Deno.env.set('SZCI_LOGIN_DOCKER', 'docker.io|someuser|somepass');
Deno.env.set('SZCI_SSHKEY_1', 'hostString|somePrivKey|##');
// Get the test assets directory
const testAssetsDir = path.join(smartpath.get.dirnameFromImportMetaUrl(import.meta.url), 'assets/');
// Save original cwd and change to test assets
const originalCwd = Deno.cwd();
Deno.chdir(testAssetsDir);
import type { Dockerfile } from '../ts/manager.docker/mod.classes.dockerfile.ts';
import { Szci } from '../ts/szci.classes.szci.ts';
import * as DockerfileModule from '../ts/manager.docker/mod.classes.dockerfile.ts';
// ======
// Docker
// ======
let dockerfile1: Dockerfile;
let dockerfile2: Dockerfile;
let sortableArray: Dockerfile[];
Deno.test('should return valid Dockerfiles', async () => {
const szciInstance = new Szci();
await szciInstance.start();
dockerfile1 = new DockerfileModule.Dockerfile(szciInstance.dockerManager, {
filePath: './Dockerfile',
read: true,
});
dockerfile2 = new DockerfileModule.Dockerfile(szciInstance.dockerManager, {
filePath: './Dockerfile_sometag1',
read: true,
});
assertEquals(dockerfile1.version, 'latest');
assertEquals(dockerfile2.version, 'sometag1');
});
Deno.test('should read a directory of Dockerfiles', async () => {
const szciInstance = new Szci();
await szciInstance.start();
const readDockerfilesArray = await DockerfileModule.Dockerfile.readDockerfiles(
szciInstance.dockerManager
);
sortableArray = readDockerfilesArray;
// The test assets directory should have multiple Dockerfiles
assertExists(readDockerfilesArray, 'readDockerfilesArray should exist');
assertEquals(readDockerfilesArray.length > 0, true, 'Should find at least one Dockerfile');
// Find the sometag1 dockerfile
const sometag1Dockerfile = readDockerfilesArray.find(df => df.version === 'sometag1');
assertExists(sometag1Dockerfile, 'Should find Dockerfile_sometag1');
assertEquals(sometag1Dockerfile?.version, 'sometag1');
});
Deno.test('should sort an array of Dockerfiles', async () => {
// Use the sortableArray from previous test, or create a new one if empty
if (!sortableArray || sortableArray.length === 0) {
const szciInstance = new Szci();
await szciInstance.start();
sortableArray = await DockerfileModule.Dockerfile.readDockerfiles(szciInstance.dockerManager);
}
const sortedArray = await DockerfileModule.Dockerfile.sortDockerfiles(sortableArray);
assertExists(sortedArray, 'sortedArray should exist');
console.log('Sorted dockerfiles:', sortedArray.map(df => df.cleanTag));
});
Deno.test({
name: 'should build all Dockerfiles',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'build'],
});
},
});
Deno.test({
name: 'should test all Dockerfiles',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'test'],
});
},
});
Deno.test({
name: 'should login docker daemon',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'login'],
});
},
});
// ===
// SSH
// ===
Deno.test('should prepare SSH keys', async () => {
// Ensure test mode is set so we don't actually write to disk
Deno.env.set('SZCI_TEST', 'true');
const szciModSsh = await import('../ts/mod_ssh/index.ts');
await szciModSsh.handleCli({
_: ['ssh', 'prepare'],
});
});
// ====
// node
// ====
Deno.test({
name: 'should install a certain version of node',
// Allow resource leaks for this test since nvm creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.nodejsManager.handleCli({
_: ['node', 'install', 'stable'],
});
await szciInstance.nodejsManager.handleCli({
_: ['node', 'install', 'lts'],
});
await szciInstance.nodejsManager.handleCli({
_: ['node', 'install', 'legacy'],
});
},
});
// Restore original working directory after all tests
Deno.test('reset paths', () => {
Deno.chdir(originalCwd);
});