diff --git a/its/samples/sample-helm/Chart.yaml b/its/samples/sample-helm/Chart.yaml new file mode 100644 index 000000000..120c07b36 --- /dev/null +++ b/its/samples/sample-helm/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v2 +name: test-limit-ranges +version: 0.1.0 diff --git a/its/samples/sample-helm/templates/limit-range.yaml b/its/samples/sample-helm/templates/limit-range.yaml new file mode 100644 index 000000000..08fc68c6f --- /dev/null +++ b/its/samples/sample-helm/templates/limit-range.yaml @@ -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 + diff --git a/its/samples/sample-helm/templates/pod.yaml b/its/samples/sample-helm/templates/pod.yaml new file mode 100644 index 000000000..7d85ea0f1 --- /dev/null +++ b/its/samples/sample-helm/templates/pod.yaml @@ -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 diff --git a/its/samples/sample-helm/values.yaml b/its/samples/sample-helm/values.yaml new file mode 100644 index 000000000..14ba35110 --- /dev/null +++ b/its/samples/sample-helm/values.yaml @@ -0,0 +1,2 @@ +name: example +namespace: example-limit-range diff --git a/its/samples/workspace-helm.code-workspace b/its/samples/workspace-helm.code-workspace new file mode 100644 index 000000000..b43fd5c16 --- /dev/null +++ b/its/samples/workspace-helm.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "./sample-helm" + } + ], + "settings": {} +} diff --git a/its/src/test/helmSuite/helm.test.ts b/its/src/test/helmSuite/helm.test.ts new file mode 100644 index 000000000..4418574b0 --- /dev/null +++ b/its/src/test/helmSuite/helm.test.ts @@ -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); + +}); diff --git a/its/src/test/helmSuite/index.ts b/its/src/test/helmSuite/index.ts new file mode 100644 index 000000000..6ffa645b4 --- /dev/null +++ b/its/src/test/helmSuite/index.ts @@ -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); + } + }); +} diff --git a/its/src/test/runTest.ts b/its/src/test/runTest.ts index 755b35e3f..565e85b9c 100644 --- a/its/src/test/runTest.ts +++ b/its/src/test/runTest.ts @@ -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'); diff --git a/its/src/test/suite/extension.test.ts b/its/src/test/suite/extension.test.ts index 3a9ececb8..b8607823e 100644 --- a/its/src/test/suite/extension.test.ts +++ b/its/src/test/suite/extension.test.ts @@ -15,6 +15,8 @@ import { waitForSonarLintDiagnostics, dumpLogOutput } from '../common/util'; const sampleFolderLocation = '../../../samples/'; +const ONE_MINUTE_MS = 60_000; + suite('Extension Test Suite', () => { suiteSetup(() => { @@ -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')!; @@ -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); });