|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 2 | +import { OsRelease } from "../../essentials/OsRelease.js" |
| 3 | +import { SshHost } from "../../index.js" |
| 4 | +import { connectTestHosts, loadEnvVars, TestHost } from "./e2eTestBase.js" |
| 5 | + |
| 6 | +const toolsToCheck: string[] = [ |
| 7 | + "ls", |
| 8 | + "cat", |
| 9 | + "neofetch", |
| 10 | + "vim", |
| 11 | + "nano", |
| 12 | + "git", |
| 13 | + "curl", |
| 14 | + "wget", |
| 15 | + "ssh", |
| 16 | + "scp", |
| 17 | + "tar", |
| 18 | + "gzip", |
| 19 | + "zip", |
| 20 | + "unzip", |
| 21 | + "7z", |
| 22 | + "unrar", |
| 23 | + "btop", |
| 24 | + "htop", |
| 25 | + "top", |
| 26 | + "ufw", |
| 27 | +] |
| 28 | + |
| 29 | +let testSshHosts: [TestHost, SshHost][] = [] |
| 30 | + |
| 31 | +describe("e2e concurrent exec", () => { |
| 32 | + beforeEach(async () => { |
| 33 | + const hostData = loadEnvVars() |
| 34 | + if (hostData.length == 0) { |
| 35 | + throw new Error("No hosts configured!") |
| 36 | + } |
| 37 | + |
| 38 | + testSshHosts = await connectTestHosts(hostData) |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + await Promise.all( |
| 43 | + testSshHosts.map( |
| 44 | + ([testHost, host]) => host.disconnect() |
| 45 | + ) |
| 46 | + ) |
| 47 | + }) |
| 48 | + |
| 49 | + test("execute sleep tasks", async () => { |
| 50 | + await Promise.all( |
| 51 | + testSshHosts.map( |
| 52 | + ([testHost, host]) => executeSleepWorkload(host) |
| 53 | + ) |
| 54 | + ) |
| 55 | + }, 1000 * 20) |
| 56 | + |
| 57 | + test("execute host info tasks", async () => { |
| 58 | + for (const testHost of testSshHosts) { |
| 59 | + const info = await collectHostInfo( |
| 60 | + testHost[1], |
| 61 | + toolsToCheck, |
| 62 | + ) |
| 63 | + |
| 64 | + // osRelease |
| 65 | + expect(info.osRelease.distroName.length).toBeGreaterThan(0) |
| 66 | + expect(info.osRelease.distroVersion.length).toBeGreaterThan(0) |
| 67 | + expect(info.toolesInstalled.length).toBeGreaterThan(0) |
| 68 | + |
| 69 | + expect(info.homefiles.length).toBeGreaterThan(0) |
| 70 | + expect(info.homedir.length).toBeGreaterThan(0) |
| 71 | + } |
| 72 | + }, 1000 * 20) |
| 73 | +}) |
| 74 | + |
| 75 | + |
| 76 | +export interface HostInfo { |
| 77 | + sshHost: SshHost, |
| 78 | + osRelease: OsRelease, |
| 79 | + toolesInstalled: string[], |
| 80 | + neofetch: string | undefined, |
| 81 | + homefiles: string[], |
| 82 | + homedir: string, |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Collects information about a host and returns it in the form of a HostInfo object. |
| 87 | + * |
| 88 | + * The function fetches the host's OS release, checks for the presence of the given tools, |
| 89 | + * lists the files in the user's home directory, and determines the user's home directory path. |
| 90 | + * |
| 91 | + * @param host The SshHost object to collect information about. |
| 92 | + * @param toolsToCheck An array of strings, each containing the name of a command-line tool to check for. |
| 93 | + * @returns A Promise resolving to a HostInfo object containing the collected host information. |
| 94 | + */ |
| 95 | +export async function collectHostInfo(host: SshHost, toolsToCheck: string[]): Promise<HostInfo> { |
| 96 | + const hostInfo: HostInfo = { |
| 97 | + sshHost: host, |
| 98 | + osRelease: await host.fetchOsRelease(), |
| 99 | + toolesInstalled: [], |
| 100 | + neofetch: undefined, |
| 101 | + homefiles: [], |
| 102 | + homedir: await host.homeDir(), |
| 103 | + } |
| 104 | + |
| 105 | + hostInfo.homefiles = (await host.sftp.readdir(hostInfo.homedir)).map( |
| 106 | + (stat) => stat.filename, |
| 107 | + ) |
| 108 | + |
| 109 | + for (const tool of toolsToCheck) { |
| 110 | + if (await host.cmdExists(tool)) { |
| 111 | + hostInfo.toolesInstalled.push(tool) |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + return hostInfo |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Executes a series of commands on a given SSH host, alternating between |
| 121 | + * listing directory contents and sleeping for increasing durations. |
| 122 | + * |
| 123 | + * The function runs the "ls -al" command to list all files in the current |
| 124 | + * directory, then pauses execution with the "sleep" command for 1 second, |
| 125 | + * 2 seconds, and 3 seconds, respectively, between each directory listing. |
| 126 | + * |
| 127 | + * @param host The SshHost object on which to execute the commands. |
| 128 | + * @returns A Promise that resolves when all commands have been executed. |
| 129 | + */ |
| 130 | +export async function executeSleepWorkload( |
| 131 | + host: SshHost, |
| 132 | +): Promise<void> { |
| 133 | + await host.exec("ls -al") |
| 134 | + await host.exec("sleep 1") |
| 135 | + await host.exec("ls -al") |
| 136 | + await host.exec("sleep 2") |
| 137 | + await host.exec("ls -al") |
| 138 | + await host.exec("sleep 3") |
| 139 | + await host.exec("ls -al") |
| 140 | +} |
0 commit comments