Skip to content

Commit

Permalink
SLVSCODE-799 Add integration test for Helm chart
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Aug 9, 2024
1 parent 5b50cd7 commit 0ce5812
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 2 deletions.
3 changes: 3 additions & 0 deletions its/samples/sample-helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v2
name: test-limit-ranges
version: 0.1.0
21 changes: 21 additions & 0 deletions its/samples/sample-helm/templates/limit-range.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: LimitRange
metadata:
name: {{ .Values.name }}
namespace: {{ .Values.namespace }}
spec:
limits:
- type: Container
default:
memory: 512Mi
cpu: 1
defaultRequest:
memory: 512Mi
cpu: 1
min:
memory: 256Mi
cpu: 0.5
max:
memory: 1Gi
cpu: 2

10 changes: 10 additions & 0 deletions its/samples/sample-helm/templates/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: example-pod
namespace: {{ .Values.namespace }}
spec:
containers:
- name: nginx-ns-compliant
image: nginx:1.27.0
automountServiceAccountToken: false
2 changes: 2 additions & 0 deletions its/samples/sample-helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: example
namespace: example-limit-range
8 changes: 8 additions & 0 deletions its/samples/workspace-helm.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "./sample-helm"
}
],
"settings": {}
}
41 changes: 41 additions & 0 deletions its/src/test/helmSuite/helm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* --------------------------------------------------------------------------------------------
* SonarLint for VisualStudio Code
* Copyright (C) 2017-2024 SonarSource SA
* sonarlint@sonarsource.com
* Licensed under the LGPLv3 License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as assert from 'assert';
import * as path from 'path';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';

import { activateAndShowOutput, dumpLogOutput, waitForSonarLintDiagnostics } from '../common/util';
import { expect } from 'chai';

const sampleHelmFolderLocation = '../../../samples/sample-helm/';

suite('Helm Test Suite', () => {

test('should report issues on Helm chart', async function () {
const ext = vscode.extensions.getExtension('sonarsource.sonarlint-vscode')!;
await ext.activate();

vscode.commands.executeCommand('SonarLint.ShowSonarLintOutput');

const fileUri = vscode.Uri.file(path.join(__dirname, sampleHelmFolderLocation, 'templates', 'pod.yaml'));
const document = await vscode.workspace.openTextDocument(fileUri);
await vscode.window.showTextDocument(document);

const diags = await waitForSonarLintDiagnostics(fileUri);

assert.strictEqual(diags.length, 2);
assert.strictEqual(diags[0].message, "Specify a storage limit for this container.");
assert.strictEqual(diags[1].message, "Specify a storage request for this container.");

vscode.commands.executeCommand('workbench.action.closeActiveEditor');

}).timeout(60_000);

});
42 changes: 42 additions & 0 deletions its/src/test/helmSuite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* --------------------------------------------------------------------------------------------
* SonarLint for VisualStudio Code
* Copyright (C) 2017-2024 SonarSource SA
* sonarlint@sonarsource.com
* Licensed under the LGPLv3 License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as glob from 'glob';
import * as Mocha from 'mocha';
import * as path from 'path';

export function run(testsRoot: string, cb: (error: any, failures?: number) => void): void {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
reporter: 'mocha-multi-reporters',
reporterOptions: {
reporterEnabled: 'spec, xunit',
xunitReporterOptions: {
output: path.resolve(__dirname, '..', '..', 'helm-tests.xml')
}
},
color: true
});

glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return cb(err);
}

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
return cb(null, failures);
});
} catch (runErr) {
return cb(runErr);
}
});
}
1 change: 1 addition & 0 deletions its/src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function main() {

// run the integration tests
await runTestSuite('./suite');
await runTestSuite('./helmSuite', 'workspace-helm.code-workspace');
await runTestSuite('./secretsSuite', 'workspace-secrets.code-workspace');
await runTestSuite('./pythonSuite', 'workspace-python.code-workspace');
await runTestSuite('./cfamilySuite', 'workspace-cfamily.code-workspace');
Expand Down
6 changes: 4 additions & 2 deletions its/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { waitForSonarLintDiagnostics, dumpLogOutput } from '../common/util';

const sampleFolderLocation = '../../../samples/';

const ONE_MINUTE_MS = 60_000;

suite('Extension Test Suite', () => {

suiteSetup(() => {
Expand Down Expand Up @@ -47,7 +49,7 @@ suite('Extension Test Suite', () => {
assert.equal(diags[0].message, "Remove the declaration of the unused 'i' variable.");

vscode.commands.executeCommand('workbench.action.closeActiveEditor');
}).timeout(60 * 1000);
}).timeout(ONE_MINUTE_MS);

test('should report issue on single yaml file', async function () {
const ext = vscode.extensions.getExtension('sonarsource.sonarlint-vscode')!;
Expand All @@ -65,5 +67,5 @@ suite('Extension Test Suite', () => {
assert.strictEqual(diags[0].message, "Remove the declaration of the unused 'x' variable.");

vscode.commands.executeCommand('workbench.action.closeActiveEditor');
}).timeout(60 * 1000);
}).timeout(ONE_MINUTE_MS);
});

0 comments on commit 0ce5812

Please sign in to comment.