Skip to content

Commit

Permalink
Integration test suite for odo commands we call
Browse files Browse the repository at this point in the history
- remove unused commands from `commands.ts` and unused methods from
  `odo.ts`
  - Notably, remove `ODO.getApplications()`, `ODO.getComponents()`,
    and the in-memory tree representing the cluster state,
    since they don't work properly and we have replacements for them
- rewrite integration tests for `commands.ts` and `odo.ts`
- report test coverage for integration tests
- add the integration tests to the UI test Jenkinsfile workflow

Depends on redhat-developer#2865, redhat-developer#2863, redhat-developer#2862

Fixes redhat-developer#2799

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed May 19, 2023
1 parent 3a18ed6 commit 3883774
Show file tree
Hide file tree
Showing 28 changed files with 954 additions and 2,032 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

[Jenkinsfile]
indent_size = 2

[./package.json]
indent_style = tab
indent_size = 4
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
"radix": 2,
"no-trailing-spaces": "error",
"@typescript-eslint/prefer-regexp-exec": 0,
"quotes": [ "error", "single"]
"quotes": [ "error", "single"],
"@typescript-eslint/no-unused-vars": [ "error", { "vars": "local", "args": "none" }]
},
"overrides": [
{
Expand Down
21 changes: 21 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,28 @@ There are only a few guidelines that we need contributors to follow.

![View Container OpenShift](https://github.com/redhat-developer/vscode-openshift-tools/blob/master/images/view-container-icon.png)

## Running the Integration Test Suite

In order to run the integration test suite, you need access to an OpenShift cluster
where you can create and delete projects.
Unfortunately, this means that OpenShift Developer Sandbox instances won't work.
Also, non-OpenShift Kubernetes distributions, such as `minikube`, will not work, since we test many OpenShift-specific features.
One way to access such a cluster is by using [crc](https://crc.dev/crc/) to run an OpenShift cluster locally on your computer.
The tests will create and delete resources on the cluster,
so please make sure nothing important is running on the cluster.
First, set the following environment variables to point the tests to your cluster:
- `CLUSTER_URL`: the URL pointing to the API of the cluster, defaults to `https://192.168.130.11:6443` (the default `crc` IP address)
- `CLUSTER_USER`: the username to use to login to the cluster, defaults to `developer`
- `CLUSTER_PASSWORD`: the password to use to login to the cluster, default to `developer`
Then, run `npm run test-integration`.
If you would like to generate a coverage report of the integration test suite,
you can run `npm run test-integration:coverage`.
> If you have any questions or run into any problems, please post an issue - we'll be very happy to help.

### Certificate of Origin

By contributing to this project you agree to the Developer Certificate of
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"debuggers",
"K8s",
"Cloud",
"podman"
"podman"
],
"icon": "images/openshift_extension.png",
"main": "./out/src/extension",
Expand Down Expand Up @@ -69,14 +69,15 @@
"dev:run:helm-chart-view": "webpack-dev-server --port 9222 --mode development --config src/webview/helm-chart/webpack.config.js",
"dev:run:create-service-view": "webpack-dev-server --mode development --config src/webview/create-service/webpack.config.js",
"watch": "tsc -watch -p ./",
"clean": "shx rm -rf out/build out/coverage out/src out/test out/tools out/test-resources out/logViewer",
"clean": "shx rm -rf out/build out/coverage out/src out/test out/tools out/test-resources out/*Viewer",
"lint": "eslint . --ext .ts --quiet",
"lint-fix": "eslint . --ext .ts --fix",
"lint-nic": "eslint . --ext .ts --no-inline-config",
"bundle-tools": "node ./out/build/bundle-tools.js --platform",
"todo": "leasot **/*.ts --ignore node_modules -x",
"test": "npm run vscode:prepublish && node ./out/build/run-tests.js unit",
"test-integration": "npm run vscode:prepublish && node ./out/build/run-tests.js integration",
"test-integration:coverage": "npm run vscode:prepublish && npm run test:instrument && node ./out/build/run-tests.js integration",
"test:instrument": "shx rm -rf out/src-orig && shx mv out/src out/src-orig && istanbul instrument --complete-copy --embed-source --output out/src out/src-orig",
"test:coverage": "npm run build && npm run test:instrument && node ./out/build/install-vscode.js redhat.vscode-redhat-account && node ./out/build/run-tests.js unit",
"update-deps": "ncu --upgrade --loglevel verbose --packageFile package.json && npm update",
Expand Down Expand Up @@ -144,6 +145,7 @@
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.12",
"@types/pify": "^5.0.1",
"@types/proxyquire": "^1.3.28",
"@types/react": "^17.0.37",
"@types/react-copy-to-clipboard": "^5.0.4",
"@types/react-dom": "^17.0.11",
Expand Down Expand Up @@ -1079,6 +1081,10 @@
{
"command": "openshift.component.deleteSourceFolder",
"when": "false"
},
{
"command": "openshift.component.openInBrowser",
"when": "false"
}
],
"view/title": [
Expand Down Expand Up @@ -1437,7 +1443,7 @@
"onCommand:openshift.componentType.newComponent"
]
},
{
{
"id": "helmChart",
"title": "Work with Helm Charts",
"description": "Browse the catalog to discover and install Helm Charts.\n[Browse Helm Chart](command:openshift.componentTypesView.registry.openHelmChartsInView)",
Expand Down
52 changes: 22 additions & 30 deletions src/k8s/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import * as _ from 'lodash';
import OpenShiftItem from '../openshift/openshiftItem';
import { ClusterExplorerV1 } from 'vscode-kubernetes-tools-api';
import * as common from './common';
import { ClusterServiceVersionKind, CRDDescription, CustomResourceDefinitionKind } from './olm/types';
import { TreeItem, WebviewPanel, window } from 'vscode';
import { vsCommand, VsCommandError } from '../vscommand';
import CreateServiceViewLoader from '../webview/create-service/createServiceViewLoader';
import { DEFAULT_K8S_SCHEMA, getUISchema, randomString, generateDefaults } from './utils';
import { loadYaml } from '@kubernetes/client-node';
import { JSONSchema7 } from 'json-schema';
import { getInstance, OpenShiftObject } from '../odo';
import { CommandText } from '../base/command';
import * as _ from 'lodash';
import { TreeItem, WebviewPanel, window } from 'vscode';
import { ClusterExplorerV1 } from 'vscode-kubernetes-tools-api';
import { getInstance } from '../odo';
import { Command } from '../odo/command';
import OpenShiftItem from '../openshift/openshiftItem';
import { getOpenAPISchemaFor } from '../util/swagger';
import { VsCommandError, vsCommand } from '../vscommand';
import CreateServiceViewLoader from '../webview/create-service/createServiceViewLoader';
import * as common from './common';
import { CRDDescription, ClusterServiceVersionKind, CustomResourceDefinitionKind } from './olm/types';
import { DEFAULT_K8S_SCHEMA, generateDefaults, getUISchema, randomString } from './utils';

class CsvNode implements ClusterExplorerV1.Node, ClusterExplorerV1.ClusterExplorerExtensionNode {

Expand Down Expand Up @@ -53,8 +53,6 @@ export class ClusterServiceVersion extends OpenShiftItem {
getCsv: (csvName: string): string => `get csv ${csvName}`,
getCrd: (crdName: string): string => `get crd ${crdName}`,
getCreateCommand: (file: string): string => `create -f ${file}`,
getCurrentUserName: (): CommandText => new CommandText('oc whoami'),
getCurrentUserToken: (): CommandText => new CommandText('oc whoami -t'),
};

public static getNodeContributor(): ClusterExplorerV1.NodeContributor {
Expand All @@ -71,7 +69,7 @@ export class ClusterServiceVersion extends OpenShiftItem {
};
}

static createFormMessageListener(app: OpenShiftObject, panel: WebviewPanel) {
static createFormMessageListener(panel: WebviewPanel) {
return async (event: any) => {
if (event.command === 'cancel') {
if (event.changed === true) {
Expand All @@ -85,14 +83,13 @@ export class ClusterServiceVersion extends OpenShiftItem {
if (event.command === 'create') {
// add waiting for Deployment to be created using wait --for=condition
// no need to wait until it is available
const clusters = await getInstance().getClusters();
if (clusters.length === 0) {
if (!await getInstance().getActiveCluster()) {
// could be expired session
return;
}

try {
await OpenShiftItem.odo.createService(app, event.formData);
await OpenShiftItem.odo.createService(event.formData);
window.showInformationMessage(`Service ${event.formData.metadata.name} successfully created.` );
panel.dispose();
} catch (err) {
Expand All @@ -103,31 +100,26 @@ export class ClusterServiceVersion extends OpenShiftItem {
}
}

// oc delete Database database1

@vsCommand('clusters.openshift.csv.create')
static async createNewService(crdOwnedNode: K8sCrdNode): Promise<void> {
const projects = await getInstance().getProjects();
const apps = await getInstance().getApplications(projects[0]);
const app = apps.find(item => item.getName() === 'app');
return ClusterServiceVersion.createNewServiceFromDescriptor(crdOwnedNode.impl.crdDescription, crdOwnedNode.impl.csv, app);
return ClusterServiceVersion.createNewServiceFromDescriptor(crdOwnedNode.impl.crdDescription, crdOwnedNode.impl.csv);
}

static async getAuthToken(): Promise<string> {
const gcuCmd = this.command.getCurrentUserName();
const gcuCmd = Command.getCurrentUserName();
const gcuExecRes = await this.odo.execute(gcuCmd, undefined, false);
if (gcuExecRes.error) {
throw new VsCommandError(gcuExecRes.stderr, `Cannot get current user name. '${gcuCmd}' returned non zero error code.`);
}
const gcutCmd = this.command.getCurrentUserToken();
const gcutCmd = Command.getCurrentUserToken();
const gcutExecRes = await this.odo.execute(gcutCmd, undefined, false);
if (gcutExecRes.error) {
throw new VsCommandError(gcuExecRes.stderr, `Cannot get current user name. '${gcutCmd}' returned non zero error code.`);
}
return gcutExecRes.stdout.trim();
}

static async createNewServiceFromDescriptor(crdDescription: CRDDescription, csv: ClusterServiceVersionKind, application: OpenShiftObject): Promise<void> {
static async createNewServiceFromDescriptor(crdDescription: CRDDescription, csv: ClusterServiceVersionKind): Promise<void> {
const getCrdCmd = ClusterServiceVersion.command.getCrd(crdDescription.name);
let crdResource: CustomResourceDefinitionKind;
let apiVersion: string;
Expand All @@ -142,9 +134,9 @@ export class ClusterServiceVersion extends OpenShiftItem {
openAPIV3SchemaAll = crdResource.spec.versions.find((version) => version.name === crdDescription.version).schema.openAPIV3Schema;
apiVersion = `${crdResource.spec.group}/${crdDescription.version}`;
} else {
const clusters = await this.odo.getClusters();
const activeCluster = await this.odo.getActiveCluster();
const token = await this.getAuthToken();
openAPIV3SchemaAll = await getOpenAPISchemaFor(clusters[0].getName(), token, crdDescription.kind, crdDescription.version);
openAPIV3SchemaAll = await getOpenAPISchemaFor(activeCluster, token, crdDescription.kind, crdDescription.version);
const gvk = _.find(openAPIV3SchemaAll['x-kubernetes-group-version-kind'], ({ group, version, kind }) =>
crdDescription.version === version && crdDescription.kind === kind && group);
apiVersion = `${gvk.group}/${gvk.version}`;
Expand All @@ -163,7 +155,7 @@ export class ClusterServiceVersion extends OpenShiftItem {
crdDescription
);

const panel = await CreateServiceViewLoader.loadView('Create Service', ClusterServiceVersion.createFormMessageListener.bind(undefined, application));
const panel = await CreateServiceViewLoader.loadView('Create Service', ClusterServiceVersion.createFormMessageListener.bind(undefined));

panel.webview.onDidReceiveMessage(async (event)=> {
if(event.command === 'ready') {
Expand All @@ -176,4 +168,4 @@ export class ClusterServiceVersion extends OpenShiftItem {
}
});
}
}
}
Loading

0 comments on commit 3883774

Please sign in to comment.