Skip to content

Commit

Permalink
Update code to match linter rules
Browse files Browse the repository at this point in the history
Issue: ZENKO-4882
  • Loading branch information
williamlardier committed Sep 6, 2024
1 parent a841e58 commit c1ec6b7
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 28 deletions.
4 changes: 2 additions & 2 deletions tests/ctst/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ export const s3FunctionExtraParams: { [key: string]: Record<string, unknown>[] }
}],
};

export function safeJsonParse<T>(jsonString: string): { ok: boolean, result: T | null } {
export function safeJsonParse<T>(jsonString: string): { ok: boolean, result: T | null, error?: Error | null } {
let result: T;
try {
result = JSON.parse(jsonString) as T;
} catch (err) {
return { ok: false, result: null };
return { ok: false, result: null, error: (err as Error) };
}
return { ok: true, result };
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ctst/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default tseslint.config(
'**/build',
'**/coverage',
'**/dist',
'**/node_modules',
'**/node_modules/**/*',
'**/package',
'**/reports',
'**/eslint.config.mjs',
Expand Down
13 changes: 9 additions & 4 deletions tests/ctst/features/resource-policies/regen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* When applying the script, make sure to have the changes in a separate commit.
* Usage: node regen.js
*/
const fs = require('fs');
import fs from 'fs';

const targetFiles = [
'./AssumeRole.feature',
Expand Down Expand Up @@ -152,21 +152,26 @@ const output = scenarios.map(scenario => {
const paddedBucketPolicyApplies = scenario.bucketPolicyApplies.padEnd(longest.bucketPolicyApplies);
const paddedBucketPolicyEffect = scenario.bucketPolicyEffect.padEnd(longest.bucketPolicyEffect);

// eslint-disable-next-line max-len
return ` | ${paddedAction} | ${paddedBucketPolicyExists} | ${paddedBucketPolicyApplies} | ${paddedBucketPolicyEffect} | ${paddedIamPolicyExists} | ${paddedIamPolicyApplies} | ${paddedIamPolicyEffect} |`;
}).join('\n');

targetFiles.forEach((file) => {
targetFiles.forEach(file => {
const filePath = `${__dirname}/${file}`;
const fileContent = fs.readFileSync(filePath, 'utf-8');
const startIndex = fileContent.indexOf('Everything below is generated');
const startIndexNextLine = fileContent.indexOf('\n', startIndex);
const endIndex = fileContent.length;

if (startIndex !== -1 && endIndex !== -1) {
const newContent = fileContent.substring(0, startIndexNextLine) + '\n' + output + '\n' + fileContent.substring(endIndex);
const newContent =
`${fileContent.substring(0, startIndexNextLine) }\n${ output }\n${ fileContent.substring(endIndex)}`;
fs.writeFileSync(filePath, newContent, 'utf-8');
// eslint-disable-next-line no-console
console.log(`Content in ${file} replaced.`);
} else {
console.error(`Couldn't find the specified markers in ${file}. Make sure the file contains the markers as specified.`);
// eslint-disable-next-line no-console
console.error(
`Couldn't find the specified markers in ${file}. Make sure the file contains the markers as specified.`);
}
});
5 changes: 3 additions & 2 deletions tests/ctst/steps/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ When('i subscribe to {string} notifications for destination {int}',
notificationConfig.QueueConfigurations.push(destinationConfig.QueueConfigurations[0]);
this.addCommandParameter({ notificationConfiguration: `'${JSON.stringify(notificationConfig)}'` });
} catch (error) {
this.logger.debug('Error parsing notification configuration', { error });
// Put new config if old doesn't exist
this.addCommandParameter({ notificationConfiguration: `'${JSON.stringify(destinationConfig)}'` });
}
Expand Down Expand Up @@ -210,6 +211,7 @@ When('i subscribe to {string} notifications for destination {int} with {string}
notificationConfig.QueueConfigurations.push(destinationConfig.QueueConfigurations[0]);
this.addCommandParameter({ notificationConfiguration: `'${JSON.stringify(notificationConfig)}'` });
} catch (error) {
this.logger.debug('Error putting notification configuration', { error });
// Put new config it old doesn't exist
this.addCommandParameter({ notificationConfiguration: `'${JSON.stringify(destinationConfig)}'` });
}
Expand All @@ -218,7 +220,6 @@ When('i subscribe to {string} notifications for destination {int} with {string}
await Utils.sleep(10000);
});

// eslint-disable-next-line new-cap
When('i unsubscribe from {string} notifications for destination {int}',
async function (this: Zenko, notificationType: string, destination: number) {
this.resetCommand();
Expand Down Expand Up @@ -251,7 +252,6 @@ When('i unsubscribe from {string} notifications for destination {int}',
await Utils.sleep(10000);
});

// eslint-disable-next-line new-cap
When('a {string} event is triggered {string} {string}',
async function (this: Zenko, notificationType: string, enable: string, filterType: string) {
this.resetCommand();
Expand Down Expand Up @@ -327,6 +327,7 @@ Then('i should {string} a notification for {string} event in destination {int}',
}
return false;
} catch (error) {
this.logger.debug('error when parsing notification message', { error });
return false;
}
},
Expand Down
3 changes: 2 additions & 1 deletion tests/ctst/steps/pra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ async function waitForPhase(
parsedStatus = json.result;
break;
}
} catch (e) {
} catch (err) {
world.logger.debug('Failed to parse DR status line', { line, err });
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ctst/steps/quotas/quotas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-case-declarations */
import fs from 'fs';
import lockFile from 'proper-lockfile';
import { Given, When, ITestCaseHookParameter } from '@cucumber/cucumber';
Expand Down Expand Up @@ -37,6 +36,7 @@ export async function prepareQuotaScenarios(world: Zenko, scenarioConfiguration:
try {
releaseLock = await lockFile.lock(filePath, { stale: Constants.DEFAULT_TIMEOUT / 2 });
} catch (err) {
world.logger.error('Unable to acquire lock', { err });
releaseLock = false;
}
}
Expand Down
39 changes: 24 additions & 15 deletions tests/ctst/steps/utils/kubernetes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { KubernetesHelper, Utils } from 'cli-testing';
import Zenko from 'world/Zenko';
import {
Expand All @@ -9,6 +8,9 @@ import {
V1Deployment,
AppsApi,
CustomObjectsApi,
V1PersistentVolumeClaim,
CoreV1Api,
BatchV1Api,
} from '@kubernetes/client-node';

type ZenkoStatusValue = {
Expand All @@ -21,46 +23,52 @@ type ZenkoStatusValue = {

type ZenkoStatus = ZenkoStatusValue[];

export function createKubeBatchClient(world: Zenko) {
export function createKubeBatchClient(world: Zenko): BatchV1Api {
if (!KubernetesHelper.clientBatch) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.clientBatch!;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.clientBatch;
}

export function createKubeCoreClient(world: Zenko) {
export function createKubeCoreClient(world: Zenko): CoreV1Api {
if (!KubernetesHelper.clientBatch) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.clientCore!;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.clientCore;
}

export function createKubeWatchClient(world: Zenko) {
export function createKubeWatchClient(world: Zenko): Watch {
if (!KubernetesHelper.clientWatch) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.clientWatch as Watch;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.clientWatch;
}

export function createKubeAppsV1Client(world: Zenko) {
export function createKubeAppsV1Client(world: Zenko): AppsV1Api {
if (!KubernetesHelper.clientAppsV1) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.clientAppsV1 as AppsV1Api;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.clientAppsV1;
}

export function createKubeAppsClient(world: Zenko) {
export function createKubeAppsClient(world: Zenko): AppsApi {
if (!KubernetesHelper.clientApps) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.clientApps as AppsApi;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.clientApps;
}

export function createKubeCustomObjectClient(world: Zenko) {
export function createKubeCustomObjectClient(world: Zenko): CustomObjectsApi {
if (!KubernetesHelper.customObject) {
KubernetesHelper.init(world.parameters);
}
return KubernetesHelper.customObject as CustomObjectsApi;
// @ts-expect-error kube client class is not stable yet
return KubernetesHelper.customObject;
}

export async function createJobAndWaitForCompletion(world: Zenko, jobName: string, customMetadata?: string) {
Expand Down Expand Up @@ -107,7 +115,7 @@ export async function createJobAndWaitForCompletion(world: Zenko, jobName: strin
} else if (watchObj.object?.status?.failed) {
world.logger.debug('job failed', {
job: job.metadata,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
object: watchObj.object,
});
reject(new Error('job failed'));
Expand Down Expand Up @@ -359,7 +367,7 @@ export async function getPVCFromLabel(world: Zenko, label: string, value: string
const coreClient = createKubeCoreClient(world);

const pvcList = await coreClient.listNamespacedPersistentVolumeClaim(namespace);
const pvc = pvcList.body.items.find(pvc => pvc.metadata?.labels?.[label] === value);
const pvc = pvcList.body.items.find((pvc: V1PersistentVolumeClaim) => pvc.metadata?.labels?.[label] === value);

return pvc;
}
Expand Down Expand Up @@ -387,6 +395,7 @@ export async function createSecret(
world.logger.debug('Secret does not exist, creating new', {
secretName,
namespace,
err,
});
}

Expand Down
1 change: 1 addition & 0 deletions tests/ctst/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
"skipLibCheck": true,

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
Expand Down
6 changes: 4 additions & 2 deletions tests/ctst/world/Zenko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ export default class Zenko extends World<ZenkoWorldParameters> {
decision = false;
}
} catch (err) {
CacheHelper.logger.debug('Error when parsing JSON', {
err,
stdout: res.stdout,
});
decision = res.stdout === '';
}
});
Expand Down Expand Up @@ -384,7 +388,6 @@ export default class Zenko extends World<ZenkoWorldParameters> {
clientId: string,
grantType: string,
): Promise<string> {
this.parameters;
const baseUrl = this.parameters.ssl === false ? 'http://' : 'https://';
const data = qs.stringify({
username,
Expand Down Expand Up @@ -822,7 +825,6 @@ export default class Zenko extends World<ZenkoWorldParameters> {
* Cleanup function for the Zenko world
* @returns {undefined}
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
static async teardown() { }

async metadataSearchResponseCode(userCredentials: AWSCredentials, bucketName: string) {
Expand Down

0 comments on commit c1ec6b7

Please sign in to comment.