Skip to content

Commit

Permalink
feat: implement hasDir and hasFile
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
  • Loading branch information
leninmehedy committed Jan 15, 2024
1 parent f90450c commit 66a4817
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
32 changes: 27 additions & 5 deletions fullstack-network-manager/src/core/kubectl2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class Kubectl2 {
errStream,
null,
false,
({status}) => {
({ status }) => {
if (status === 'Failure' || errStream.size()) {
reject(new FullstackTestingError(`Error - details: \n ${errStream.getContentsAsString()}`))
}
Expand All @@ -326,17 +326,39 @@ export class Kubectl2 {
}

/**
* Check if a file path exists in the container
* Check if a filepath exists in the container
* @param podName pod name
* @param containerName container name
* @param destPath path inside the container
* @return {Promise<boolean>}
*/
async hasPath (podName, containerName, destPath) {
const entries = await this.listDir(podName, containerName, destPath)
async hasFile (podName, containerName, destPath) {
const parentDir = path.dirname(destPath)
const fileName = path.basename(destPath)
const entries = await this.listDir(podName, containerName, parentDir)

for (const item of entries) {
if (item.name === destPath) {
if (item.name === fileName && !item.directory) {
return true
}
}

return false
}

/**
* Check if a directory path exists in the container
* @param podName pod name
* @param containerName container name
* @param destPath path inside the container
* @return {Promise<boolean>}
*/
async hasDir (podName, containerName, destPath) {
const parentDir = path.dirname(destPath)
const dirName = path.basename(destPath)
const entries = await this.listDir(podName, containerName, parentDir)
for (const item of entries) {
if (item.name === dirName && item.directory) {
return true
}
}
Expand Down
9 changes: 7 additions & 2 deletions fullstack-network-manager/test/e2e/core/kubectl_e2e.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { describe, expect, it } from '@jest/globals'
import fs from 'fs'
import os from 'os'
import path from 'path'
import { v4 as uuidv4 } from 'uuid'
import { FullstackTestingError } from '../../../src/core/errors.mjs'
import { constants, Templates } from '../../../src/core/index.mjs'
import { Kubectl2 } from '../../../src/core/kubectl2.mjs'
import { v4 as uuidv4 } from 'uuid'

describe('Kubectl', () => {
const kubectl = new Kubectl2()
Expand Down Expand Up @@ -48,6 +48,11 @@ describe('Kubectl', () => {
await expect(kubectl.getClusterIP('INVALID')).rejects.toThrow(FullstackTestingError)
})

it('should be able to check if a path is directory inside a container', async () => {
const podName = Templates.renderNetworkPodName('node0')
await expect(kubectl.hasDir(podName, constants.ROOT_CONTAINER, constants.HEDERA_HAPI_PATH)).resolves.toBeTruthy()
})

it('should be able to copy a file to and from a container', async () => {
const podName = Templates.renderNetworkPodName('node0')
const containerName = constants.ROOT_CONTAINER
Expand All @@ -60,7 +65,7 @@ describe('Kubectl', () => {
fs.writeFileSync(tmpFile, 'TEST')

await expect(kubectl.copyTo(podName, containerName, tmpFile, destDir)).resolves.toBeTruthy()
await expect(kubectl.hasPath(podName, containerName, destPath)).resolves.toBeTruthy()
await expect(kubectl.hasFile(podName, containerName, destPath)).resolves.toBeTruthy()

await expect(kubectl.copyFrom(podName, containerName, destPath, tmpDir2)).resolves.toBeTruthy()
expect(fs.existsSync(`${tmpDir2}/${testFileName}`))
Expand Down

0 comments on commit 66a4817

Please sign in to comment.