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 19, 2023
1 parent 908a638 commit 88fc144
Showing 1 changed file with 64 additions and 19 deletions.
83 changes: 64 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 @@ -1152,6 +1157,65 @@ obj:
})
.then(done, done);
});

it('single custom kubernetes schema version 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.');
});

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 @@ -1904,24 +1968,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 88fc144

Please sign in to comment.