Skip to content

Commit

Permalink
feat: [KTD-3526] add pitfile not create directory location path check…
Browse files Browse the repository at this point in the history
…ing logic plus unit tests update
  • Loading branch information
damwu1 committed May 13, 2024
1 parent 9103d34 commit 292849b
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 40 deletions.
36 changes: 34 additions & 2 deletions k8s-deployer/src/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ const isExecutable = async (filePath: string) => {
}
}

/*
Description: function isDirectoryExists: the purpose of this function is for checking the application directory is whether exists or not
Usage example:
// Assuming we have a project root directory /workspace, and we have 1 sub directory under /workspace, which is /workspace/project-directory
// eg: /workspace
// - project-directory
const isApplicationDirectoryExists = await isDirectoryExists('/workspace/project-directory') // return as true and it means application directory exists and accessible
const isApplicationDirectoryExists = await isDirectoryExists('/workspace/some-other-directory') // return as false because some-other-directory does not exist and it means application directory does not exist and not accessible
*/
const isDirectoryExists = async (filePath: string): Promise<boolean> => {
try {
await fs.promises.access(filePath, fs.constants.F_OK)
return true
} catch (e) {
logger.error(`There is no ${filePath} or it is not accessible.`, { cause: e })
return false
}
}

export const cloneFromGit = async (appId: string, location: Schema.Location, targetDirectory: string): Promise<CommitSha> => {
logger.info("The '%s' will be copied from '%s' into %s' using '%s'", appId, location.gitRepository, targetDirectory, location.gitRef)
const fullCommand = `k8s-deployer/scripts/git-co.sh ${ location.gitRepository } ${ location.gitRef } ${ targetDirectory }`
Expand Down Expand Up @@ -164,6 +185,15 @@ const undeployApplication = async (
}
}

// setAppDirectory is used to set app directory value based on the result of isDirectoryExists function
const setAppDirectory = async (appDir: string, specId: string): Promise<string> => {
if (!appDir || !specId) throw new Error('appDir or specId value is missing')

const isApplicationDirectoryExists = await isDirectoryExists(appDir)

return isApplicationDirectoryExists ? specId : '.'
}

export const deployLockManager = async (config: Config, workspace: string, namespace: Namespace) => {
const spec = getLockManagerConfig()
const appName = spec.id
Expand Down Expand Up @@ -215,7 +245,8 @@ export const deployComponent = async (
appDir = spec.location.path
logger.info("The application directory will be taken from 'location.path' attribute: '%s' of '%s'", appDir, spec.name)
} else {
appDir = spec.id
appDir = await setAppDirectory(appDir, spec.id)

logger.info("The application directory will be taken from 'id' attribute: '%s' of '%s'", appDir, spec.name)
}
commitSha = await Shell.exec(`cd ${ appDir } && git log --pretty=format:"%h" -1`)
Expand All @@ -236,7 +267,8 @@ export const undeployComponent = async (workspace: string, namespace: Namespace,
appDir = spec.location.path
logger.info("The application directory will be taken from 'location.path' attribute: '%s' of '%s'", appDir, spec.name)
} else {
appDir = spec.id
appDir = await setAppDirectory(appDir, spec.id)

logger.info("The application directory will be taken from 'id' attribute: '%s' of '%s'", appDir, spec.name)
}
}
Expand Down
6 changes: 4 additions & 2 deletions k8s-deployer/src/k8s.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Shell from "./shell-facade.js"
import * as cfg from "./config.js"
import { logger } from "./logger.js"
import { Namespace } from "./model.js"
import * as cfg from "./config.js"
import * as Shell from "./shell-facade.js"

const pad = (v: string | number, len: number = 2): string => {
let result = `${ v }`
Expand Down Expand Up @@ -29,8 +29,10 @@ export const createNamespace = async (workspace: string, rootNamespace: Namespac
const logFile = `${ workspace }/logs/create-ns-${ namespace }.log`

logger.info("Creating namespace: '%s'", namespace)

const command = `k8s-deployer/scripts/k8s-manage-namespace.sh ${ rootNamespace } create "${ namespace }" ${ timeoutSeconds }`
const timeoutMs = timeoutSeconds * 1_000

await Shell.exec(command, { logFileName: logFile, tailTarget: logger.info, timeoutMs })

logger.info("Namespace created: '%s'", namespace)
Expand Down
14 changes: 7 additions & 7 deletions k8s-deployer/src/test-suite-handler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as fs from "fs"

import { Config } from "./config.js"
import * as Deployer from "./deployer.js"
import * as K8s from "./k8s.js"
import { LOG_SEPARATOR_LINE, logger } from "./logger.js"
import { DeployedComponent, DeployedTestSuite, GraphDeploymentResult, Namespace, Prefix, Schema } from "./model.js"
import * as Deployer from "./deployer.js"
import { Config } from "./config.js"
import * as PifFileLoader from "./pitfile/pitfile-loader.js"
import * as K8s from "./k8s.js"
import * as TestRunner from "./test-app-client/test-runner.js"
import * as Shell from "./shell-facade.js"
import { PodLogTail } from "./pod-log-tail.js"
import * as Shell from "./shell-facade.js"
import * as TestRunner from "./test-app-client/test-runner.js"

export const generatePrefix = (env: string): Prefix => {
return generatePrefixByDate(new Date(), env)
Expand Down Expand Up @@ -137,7 +137,7 @@ const deployLocal = async (
logger.info("process.env.MOCK_NS=%s", process.env.MOCK_NS)
}

logger.info("NAMEPSACE IN USE=%s, process.env.MOCK_NS=%s", ns, process.env.MOCK_NS)
logger.info("NAMESPACE IN USE=%s, process.env.MOCK_NS=%s", ns, process.env.MOCK_NS)

await deployLockManager(config, workspace, ns, pitfile.lockManager.enabled, testSuite.id)
logger.info("")
Expand Down Expand Up @@ -234,7 +234,7 @@ export const processTestSuite = async (
// By default assume processing strategy to be "deploy all then run tests one by one"

logger.info("")
logger.info("--------------- Processig %s ---------------", testSuite.id)
logger.info("--------------- Processing %s ---------------", testSuite.id)
logger.info("")
const list = await deployAll(prefix, config, pitfile, seqNumber, testSuite)

Expand Down
4 changes: 2 additions & 2 deletions k8s-deployer/test/pitfile/pitfile-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as sinon from "sinon"
import * as chai from "chai"
import chaiAsPromised from 'chai-as-promised'
import * as sinon from "sinon"
chai.use(chaiAsPromised)

import * as PifFileLoader from "../../src/pitfile/pitfile-loader.js"
Expand Down Expand Up @@ -47,7 +47,7 @@ describe("Loads pitfile from disk", () => {
})

it("should throw if location has no gitRemote", async () => {
const errorMessage = `Invalid configiuration for 'suite-1'. The 'location.gitRepository' is required when location.type is REMOTE`
const errorMessage = `Invalid configuration for 'suite-1'. The 'location.gitRepository' is required when location.type is REMOTE`
await chai.expect(PifFileLoader.loadFromFile("dist/test/pitfile/test-pitfile-valid-3-invalid.yml")).eventually.rejectedWith(errorMessage)
})

Expand Down
Loading

0 comments on commit 292849b

Please sign in to comment.