Skip to content

Commit

Permalink
Add testing of Devfile JSON Schema
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <sleshche@redhat.com>
  • Loading branch information
sleshchenko committed Feb 13, 2019
1 parent 3ccb6bc commit 7fb555c
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 35 deletions.
Expand Up @@ -144,7 +144,8 @@ private List<HasMetadata> filter(KubernetesList list, Map<String, String> select
}

/**
* Set {@link MACHINE_NAME_ATTRIBUTE} to commands which are configured in the specified tool.
* Set {@link org.eclipse.che.api.core.model.workspace.config.Command#MACHINE_NAME_ATTRIBUTE} to
* commands which are configured in the specified tool.
*
* <p>Machine name will be set only if the specified recipe objects has the only one container.
*/
Expand Down
Expand Up @@ -11,9 +11,14 @@
*/
package org.eclipse.che.api.devfile.server.validator;

import static org.testng.Assert.fail;

import java.io.IOException;
import java.util.regex.Pattern;
import org.eclipse.che.api.devfile.server.DevfileFormatException;
import org.eclipse.che.api.devfile.server.schema.DevfileSchemaProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

Expand All @@ -26,30 +31,90 @@ public void setUp() throws Exception {
schemaValidator = new DevfileSchemaValidator(new DevfileSchemaProvider());
}

@Test
public void shouldValidateCorrectYamlBySchema() throws Exception {
String devFileYamlContent =
Files.readFile(getClass().getClassLoader().getResourceAsStream("devfile.yaml"));
// when
schemaValidator.validateBySchema(devFileYamlContent, false);
@Test(dataProvider = "validDevfiles")
public void shouldDoNotThrowExceptionOnValidationValidDevfile(String resourceFilePath)
throws Exception {
schemaValidator.validateBySchema(getResource(resourceFilePath), false);
}

@DataProvider
public Object[][] validDevfiles() {
return new Object[][] {
{"editor_plugin_tool/devfile_editor_plugins.yaml"},
{"kubernetes_openshift_tool/devfile_openshift_tool.yaml"}
};
}

@Test(dataProvider = "invalidDevfiles")
public void shouldThrowExceptionOnValidationNonValidDevfile(
String resourceFilePath, String expectedMessageRegexp) throws Exception {
try {
schemaValidator.validateBySchema(getResource(resourceFilePath), false);
} catch (DevfileFormatException e) {
if (!Pattern.matches(expectedMessageRegexp, e.getMessage())) {
fail("DevfileFormatException with unexpected message is thrown: " + e.getMessage());
}
return;
}
fail("DevfileFormatException expected to be thrown but is was not");
}

@Test
public void shouldValidateCorrectYamlWithoutCommandsBySchema() throws Exception {
String devFileYamlContent =
Files.readFile(getClass().getClassLoader().getResourceAsStream("devfile_no_commands.yaml"));
// when
schemaValidator.validateBySchema(devFileYamlContent, false);
@DataProvider
public Object[][] invalidDevfiles() {
return new Object[][] {
// Devfile model testing
{
"devfile/devfile_missing_name.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"name\"\\]\\)\\]$"
},
{
"devfile/devfile_missing_spec_version.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"specVersion\"\\]\\)\\]$"
},
{
"devfile/devfile_with_undeclared_field.yaml",
"Devfile schema validation failed. Errors: \\[object instance has properties which are not allowed by the schema: \\[\"unknown\"\\]\\]$"
},
// Tool model testing
{
"tool/devfile_missing_tool_name.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"name\"\\]\\)\\]$"
},
{
"tool/devfile_missing_tool_type.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"type\"\\]\\)\\]$"
},
{
"tool/devfile_tool_with_undeclared_field.yaml",
"Devfile schema validation failed. Errors: \\[object instance has properties which are not allowed by the schema: \\[\"unknown\"\\]\\]$"
},
// Command model testing
{
"command/devfile_missing_command_name.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"name\"\\]\\)\\]$"
},
{
"command/devfile_missing_command_actions.yaml",
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"actions\"\\]\\)\\]$"
},
{
"command/devfile_multiple_commands_actions.yaml",
"Devfile schema validation failed. Errors: \\[array is too long: must have at most 1 elements but instance has 2 elements\\]$"
},
// cheEditor/chePlugin tool model testing
{
"editor_plugin_tool/devfile_editor_tool_with_missing_id.yaml",
"Devfile schema validation failed\\. Errors: \\[instance failed to match exactly one schema \\(matched 0 out of 2\\)\\]"
},
// kubernetes/openshift tool model testing
{
"kubernetes_openshift_tool/devfile_openshift_tool_with_missing_local.yaml",
"Devfile schema validation failed\\. Errors: \\[instance failed to match exactly one schema \\(matched 0 out of 2\\)\\]"
},
};
}

@Test(
expectedExceptions = DevfileFormatException.class,
expectedExceptionsMessageRegExp =
"Devfile schema validation failed. Errors: \\[object has missing required properties \\(\\[\"name\"\\]\\)\\]$")
public void shouldValidateIncorrectYamlBySchema() throws Exception {
String devFileYamlContent =
Files.readFile(getClass().getClassLoader().getResourceAsStream("devfile_bad.yaml"));
// when
schemaValidator.validateBySchema(devFileYamlContent, false);
private String getResource(String name) throws IOException {
return Files.readFile(getClass().getClassLoader().getResourceAsStream("schema_test/" + name));
}
}
@@ -0,0 +1,21 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: theia
type: cheEditor
id: eclipse/theia:0.0.3
commands:
- name: build
@@ -0,0 +1,25 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: theia
type: cheEditor
id: eclipse/theia:0.0.3
commands:
- actions:
- type: exec
tool: mvn-stack
command: mvn package
workdir: /projects/spring-petclinic
@@ -0,0 +1,34 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: mysql
type: openshift
local: petclinic.yaml
selector:
app.kubernetes.io/name: mysql
app.kubernetes.io/component: database
app.kubernetes.io/part-of: petclinic
commands:
- name: build
actions:
- type: exec
tool: mysql
command: mvn clean
workdir: /projects/spring-petclinic
- type: exec
tool: mysql
command: mvn package
workdir: /projects/spring-petclinic
@@ -0,0 +1,17 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
tools:
- name: maven
id: eclipse/maven-jdk8:1.0.0
@@ -0,0 +1,17 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
name: petclinic-dev-environment
tools:
- name: maven
id: eclipse/maven-jdk8:1.0.0
Expand Up @@ -13,18 +13,8 @@
---
specVersion: 0.0.1
name: petclinic-dev-environment
projects:
- name: petclinic
source:
type: git
location: 'git@github.com:spring-projects/spring-petclinic.git'
unknown: blah-blah
tools:
- name: mvn-stack
- name: maven
type: chePlugin
id: eclipse/maven-jdk8:1.0.0
- name: theia-ide
type: cheEditor
id: eclipse/theia:0.0.3
- name: jdt.ls
type: chePlugin
id: eclipse/theia-jdtls:0.0.3
Expand Up @@ -19,8 +19,15 @@ projects:
type: git
location: 'git@github.com:spring-projects/spring-petclinic.git'
tools:
- type: chePlugin
- name: theia-ide
type: cheEditor
id: eclipse/theia:0.0.3
- name: mvn-stack
type: chePlugin
id: eclipse/maven-jdk8:1.0.0
- name: jdt.ls
type: chePlugin
id: eclipse/theia-jdtls:0.0.3
commands:
- name: build
actions:
Expand Down
@@ -0,0 +1,18 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: theia-ide
type: cheEditor
@@ -0,0 +1,30 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: mysql
type: openshift
local: petclinic.yaml
selector:
app.kubernetes.io/name: mysql
app.kubernetes.io/component: database
app.kubernetes.io/part-of: petclinic
commands:
- name: build
actions:
- type: exec
tool: mysql
command: mvn clean
workdir: /projects/spring-petclinic
@@ -0,0 +1,18 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- name: mysql
type: openshift
@@ -0,0 +1,18 @@
#
# Copyright (c) 2012-2018 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

---
specVersion: 0.0.1
name: petclinic-dev-environment
tools:
- type: chePlugin
id: eclipse/maven-jdk8:1.0.0

0 comments on commit 7fb555c

Please sign in to comment.