Skip to content

Commit

Permalink
refactor: add test for multiple kubernetes schema urls
Browse files Browse the repository at this point in the history
  • Loading branch information
tricktron committed Mar 24, 2023
1 parent ac1692c commit ef7c724
Showing 1 changed file with 44 additions and 19 deletions.
63 changes: 44 additions & 19 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,11 @@ obj:
});

describe('Test with custom kubernetes schemas', function () {
afterEach(() => {
// remove Kubernetes setting not to affect next tests
languageService.configure(languageSettingsSetup.withKubernetes(false).languageSettings);
yamlSettings.specificValidatorPaths = [];
});
it('Test that properties that match multiple enums get validated properly', (done) => {
languageService.configure(languageSettingsSetup.withKubernetes().languageSettings);
yamlSettings.specificValidatorPaths = ['*.yml', '*.yaml'];
Expand Down Expand Up @@ -1193,6 +1198,45 @@ obj:
expect(result[0].message).to.eq('Missing property "spec".');
expect(result[1].message).to.eq('Property foo is not allowed.');
});

it('custom kubernetes schema version and openshift custom resource definition (CRD) should return validation errors', async () => {
const customKubernetesSchemaVersion =
'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.1-standalone-strict/all.json';
const customOpenshiftSchemaVersion =
'https://raw.githubusercontent.com/tricktron/CRDs-catalog/f-openshift-v4.11/openshift.io/v4.11/all.json';
yamlSettings.kubernetesSchemaUrls = [customKubernetesSchemaVersion, customOpenshiftSchemaVersion];
const settingsHandler = new SettingsHandler({} as Connection, languageService, yamlSettings, validationHandler, telemetry);
const initialSettings = languageSettingsSetup.withKubernetes(true).languageSettings;
const kubernetesSettings = settingsHandler.configureSchemas(
customKubernetesSchemaVersion,
['*k8s.yml'],
undefined,
initialSettings,
SchemaPriority.SchemaAssociation
);
const openshiftSettings = settingsHandler.configureSchemas(
customOpenshiftSchemaVersion,
['*oc.yml'],
undefined,
initialSettings,
SchemaPriority.SchemaAssociation
);
languageService.configure(kubernetesSettings);
languageService.configure(openshiftSettings);
const kubernetes = `apiVersion: apps/v1\nkind: Deployment\nfoo: bar`;
const openshift = `apiVersion: route.openshift.io/v1\nkind: Route\nbaz: abc`;

const kubernetesResult = await parseSetup(kubernetes, 'invalid-k8s.yml');

expect(kubernetesResult.length).to.eq(1);
expect(kubernetesResult[0].message).to.eq('Property foo is not allowed.');

const openshiftResult = await parseSetup(openshift, 'invalid-oc.yml');

expect(openshiftResult.length).to.eq(2);
expect(openshiftResult[0].message).to.eq('Missing property "spec".');
expect(openshiftResult[1].message).to.eq('Property baz is not allowed.');
});
});

// https://github.com/redhat-developer/yaml-language-server/issues/118
Expand Down Expand Up @@ -1945,24 +1989,5 @@ obj:
expect(result[0].message).to.eq('Matches multiple schemas when only one must validate.');
expect(telemetry.messages).to.be.empty;
});

it('single custom kubernetes schema should return validation errors', async () => {
const customKubernetesSchemaVersion = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.1-standalone-strict/all.json';
yamlSettings.kubernetesSchemaUrls = [customKubernetesSchemaVersion];
const settingsHandler = new SettingsHandler({} as Connection, languageService, yamlSettings, validationHandler, telemetry);
const initialSettings = languageSettingsSetup.withKubernetes(true).languageSettings;
const kubernetesSettings = settingsHandler.configureSchemas(
customKubernetesSchemaVersion,
['*k8s.yml'],
undefined,
initialSettings,
SchemaPriority.SchemaAssociation
);
languageService.configure(kubernetesSettings);
const content = `apiVersion: apps/v1\nkind: Deployment\nfoo: bar`;
const result = await parseSetup(content, 'invalid-k8s.yml');
expect(result.length).to.eq(1);
expect(result[0].message).to.eq('Property foo is not allowed.');
});
});
});

0 comments on commit ef7c724

Please sign in to comment.