From e0437a4237c99a16955d5ec56ff5fe9996c76b57 Mon Sep 17 00:00:00 2001 From: Remigiusz Janeczek Date: Tue, 16 Mar 2021 16:00:26 +0100 Subject: Add helm validator sources Issue-ID: SDC-3185 Signed-off-by: Remigiusz Janeczek Change-Id: I32dea3b4294a90c4dfc75864fb4200f044e7a0b6 --- .../HelmValidatorApplicationTests.java | 33 +++ .../api/SupportedVersionsControllerTest.java | 67 +++++ .../api/ValidationControllerTest.java | 195 +++++++++++++ .../errorhandling/ValidationErrorHandlerTest.java | 125 ++++++++ .../helm/validation/BashExecutorTest.java | 43 +++ .../helm/validation/FileManagerTest.java | 100 +++++++ .../helm/validation/ValidationServiceTest.java | 314 +++++++++++++++++++++ .../helm/versions/ApiVersionsReaderTest.java | 128 +++++++++ .../versions/ChartBasedVersionProviderTest.java | 73 +++++ .../versions/SupportedVersionsProviderTest.java | 72 +++++ .../helm/versions/SystemEnvVersionsReaderTest.java | 69 +++++ 11 files changed, 1219 insertions(+) create mode 100644 src/test/java/org/onap/sdc/helmvalidator/HelmValidatorApplicationTests.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/api/SupportedVersionsControllerTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/api/ValidationControllerTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/errorhandling/ValidationErrorHandlerTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/validation/BashExecutorTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/validation/FileManagerTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/validation/ValidationServiceTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReaderTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProviderTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProviderTest.java create mode 100644 src/test/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReaderTest.java (limited to 'src/test') diff --git a/src/test/java/org/onap/sdc/helmvalidator/HelmValidatorApplicationTests.java b/src/test/java/org/onap/sdc/helmvalidator/HelmValidatorApplicationTests.java new file mode 100644 index 0000000..5b81936 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/HelmValidatorApplicationTests.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class HelmValidatorApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/api/SupportedVersionsControllerTest.java b/src/test/java/org/onap/sdc/helmvalidator/api/SupportedVersionsControllerTest.java new file mode 100644 index 0000000..ef93a90 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/api/SupportedVersionsControllerTest.java @@ -0,0 +1,67 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.api; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.helmvalidator.helm.versions.SupportedVersionsProvider; +import org.springframework.http.ResponseEntity; + +@ExtendWith(MockitoExtension.class) +class SupportedVersionsControllerTest { + + private static final String SAMPLE_VERSION1 = "3.4.1"; + private static final String SAMPLE_VERSION2 = "3.3.4"; + private static final String SAMPLE_VERSION3 = "2.17.0"; + private static final String VERSIONS = "versions"; + private static final int EXPECTED_SIZE = 3; + + private SupportedVersionsController supportedVersionsController; + + @Mock + private SupportedVersionsProvider versionsProvider; + + @BeforeEach + void setUp() { + supportedVersionsController = new SupportedVersionsController(versionsProvider); + } + + @Test + void shouldReturnVersions() { + when(versionsProvider.getVersions()).thenReturn(List.of(SAMPLE_VERSION1, SAMPLE_VERSION2, SAMPLE_VERSION3)); + + ResponseEntity>> supportedVersionsResponse = supportedVersionsController + .supportedVersions(); + List supportedVersions = supportedVersionsResponse.getBody().get(VERSIONS); + + assertThat(supportedVersions).isNotNull(); + assertThat(supportedVersions).hasSize(EXPECTED_SIZE); + assertThat(supportedVersions).contains(SAMPLE_VERSION1, SAMPLE_VERSION2, SAMPLE_VERSION3); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/api/ValidationControllerTest.java b/src/test/java/org/onap/sdc/helmvalidator/api/ValidationControllerTest.java new file mode 100644 index 0000000..03f976a --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/api/ValidationControllerTest.java @@ -0,0 +1,195 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.api; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; + +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.helmvalidator.helm.validation.ValidationService; +import org.onap.sdc.helmvalidator.helm.validation.exception.BashExecutionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.NotSupportedVersionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.SaveFileException; +import org.onap.sdc.helmvalidator.helm.validation.model.LintValidationResult; +import org.onap.sdc.helmvalidator.helm.validation.model.TemplateValidationResult; +import org.onap.sdc.helmvalidator.helm.validation.model.ValidationResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.web.multipart.MultipartFile; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) +@AutoConfigureMockMvc +@ExtendWith(SpringExtension.class) +@ExtendWith(MockitoExtension.class) +class ValidationControllerTest { + + private static final String SAMPLE_VERSION = "3.3.4"; + private static final String VERSION_PARAM = "versionDesired"; + private static final String IS_LINTED_PARAM = "isLinted"; + private static final String IS_STRICT_LINTED_PARAM = "isStrictLinted"; + private static final String VALID = "valid"; + private static final String DEPLOYABLE = "deployable"; + private static final String RENDER_ERRORS = "renderErrors"; + private static final String LINT_WARNING = "lintWarning"; + private static final String LINT_ERROR = "lintError"; + private static final String SAMPLE_ORIGINAL_FILENAME = "sampleChart.tar.gz"; + private static final String FILE_KEY = "file"; + private static final String VALIDATION_ENDPOINT = "/validate"; + private static final String VERSION_USED = "versionUsed"; + + private ValidationController validationController; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private ValidationService validationService; + + @Mock + private MultipartFile multipartFile; + + @BeforeEach + void setUp() { + validationController = new ValidationController(validationService); + } + + @Test + void shouldReturnValidResponseForMockedFile() { + TemplateValidationResult templateValidationResult = new TemplateValidationResult(true, new ArrayList<>()); + LintValidationResult lintValidationResult = new LintValidationResult(true, new ArrayList<>(), + new ArrayList<>()); + + when(validationService.process(SAMPLE_VERSION, multipartFile, true, true)) + .thenReturn(new ValidationResult(templateValidationResult, lintValidationResult, SAMPLE_VERSION)); + + ResponseEntity result = validationController + .validate(SAMPLE_VERSION, multipartFile, true, true); + + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(result.getBody().isDeployable()).isTrue(); + assertThat(result.getBody().isValid()).isTrue(); + assertThat(result.getBody().getRenderErrors()).isEmpty(); + assertThat(result.getBody().getLintError()).isEmpty(); + assertThat(result.getBody().getLintWarning()).isEmpty(); + assertThat(result.getBody().getVersionUsed()).isEqualTo(SAMPLE_VERSION); + + } + + @Test + void shouldThrowExceptionWhenCannotSaveFile() { + when(validationService.process(SAMPLE_VERSION, multipartFile, true, true)).thenThrow(SaveFileException.class); + + assertThatExceptionOfType(SaveFileException.class) + .isThrownBy(() -> validationController.validate(SAMPLE_VERSION, multipartFile, true, true)); + } + + @Test + void shouldThrowExceptionIfErrorOccursDuringBashExecution() { + when(validationService.process(SAMPLE_VERSION, multipartFile, true, true)) + .thenThrow(BashExecutionException.class); + + assertThatExceptionOfType(BashExecutionException.class) + .isThrownBy(() -> validationController.validate(SAMPLE_VERSION, multipartFile, true, true)); + } + + @Test + void shouldThrowExceptionWhenProvidedVersionIsNotSupported() { + when(validationService.process(SAMPLE_VERSION, multipartFile, true, true)).thenThrow( + NotSupportedVersionException.class); + + assertThatExceptionOfType(NotSupportedVersionException.class) + .isThrownBy(() -> validationController.validate(SAMPLE_VERSION, multipartFile, true, true)); + } + + @Test + void shouldContainDeployAndValidParametersInResponseBodyIfLintedIsTrue() throws Exception { + + TemplateValidationResult templateValidationResult = new TemplateValidationResult(true, new ArrayList<>()); + LintValidationResult lintValidationResult = new LintValidationResult(true, new ArrayList<>(), + new ArrayList<>()); + MockMultipartFile file = new MockMultipartFile(FILE_KEY, SAMPLE_ORIGINAL_FILENAME, + MediaType.MULTIPART_FORM_DATA_VALUE, "test".getBytes()); + + when(validationService.process(SAMPLE_VERSION, file, true, true)) + .thenReturn(new ValidationResult(templateValidationResult, lintValidationResult, SAMPLE_VERSION)); + + MvcResult mvcResult = mockMvc.perform( + multipart(VALIDATION_ENDPOINT) + .file(file) + .param(VERSION_PARAM, SAMPLE_VERSION) + .param(IS_LINTED_PARAM, "true") + .param(IS_STRICT_LINTED_PARAM, "true")) + .andReturn(); + String contentAsString = mvcResult.getResponse().getContentAsString(); + + assertThat(contentAsString).contains(VALID); + assertThat(contentAsString).contains(DEPLOYABLE); + assertThat(contentAsString).contains(RENDER_ERRORS); + assertThat(contentAsString).contains(LINT_WARNING); + assertThat(contentAsString).contains(LINT_ERROR); + assertThat(contentAsString).contains(VERSION_USED); + assertThat(mvcResult.getResponse().getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void shouldNotContainValidParametersInResponseBodyIfLintedIsFalse() throws Exception { + + TemplateValidationResult templateValidationResult = new TemplateValidationResult(true, new ArrayList<>()); + MockMultipartFile file = new MockMultipartFile(FILE_KEY, SAMPLE_ORIGINAL_FILENAME, + MediaType.MULTIPART_FORM_DATA_VALUE, "test".getBytes()); + + when(validationService.process(SAMPLE_VERSION, file, false, false)) + .thenReturn(new ValidationResult(templateValidationResult, SAMPLE_VERSION)); + + MvcResult mvcResult = mockMvc.perform( + multipart(VALIDATION_ENDPOINT) + .file(file) + .param(VERSION_PARAM, SAMPLE_VERSION) + .param(IS_LINTED_PARAM, "false") + .param(IS_STRICT_LINTED_PARAM, "false")) + .andReturn(); + String contentAsString = mvcResult.getResponse().getContentAsString(); + + assertThat(contentAsString).doesNotContain(VALID); + assertThat(contentAsString).contains(DEPLOYABLE); + assertThat(contentAsString).contains(RENDER_ERRORS); + assertThat(contentAsString).doesNotContain(LINT_WARNING); + assertThat(contentAsString).doesNotContain(LINT_ERROR); + assertThat(contentAsString).contains(VERSION_USED); + assertThat(mvcResult.getResponse().getStatus()).isEqualTo(HttpStatus.OK.value()); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/errorhandling/ValidationErrorHandlerTest.java b/src/test/java/org/onap/sdc/helmvalidator/errorhandling/ValidationErrorHandlerTest.java new file mode 100644 index 0000000..5fbad30 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/errorhandling/ValidationErrorHandlerTest.java @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.errorhandling; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.helmvalidator.helm.validation.exception.BashExecutionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.NotSupportedVersionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.SaveFileException; +import org.onap.sdc.helmvalidator.helm.versions.exception.ApiVersionNotFoundException; +import org.onap.sdc.helmvalidator.helm.versions.exception.NotSupportedApiVersionException; +import org.onap.sdc.helmvalidator.helm.versions.exception.ReadFileException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@ExtendWith(MockitoExtension.class) +class ValidationErrorHandlerTest { + + private ValidationErrorHandler errorHandler; + + @Mock + private Throwable cause; + + @BeforeEach + void setUp() { + errorHandler = new ValidationErrorHandler(); + } + + @Test + void shouldReturnResponseEntityWithMessageWhenCannotSaveFile() { + String expectedMessage = "Cannot save file test-chart.tar.gz"; + SaveFileException saveFileException = new SaveFileException(expectedMessage, cause); + + ResponseEntity responseEntity = errorHandler.handle(saveFileException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + } + + @Test + void shouldReturnResponseEntityWithMessageWhenProvidedVersionIsNotSupported() { + String version = "3.3.3"; + String expectedMessage = "Version: " + version + " is not supported"; + + NotSupportedVersionException notSupportedVersionException = new NotSupportedVersionException(version); + + ResponseEntity responseEntity = errorHandler.handle(notSupportedVersionException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + } + + @Test + void shouldReturnResponseEntityWithMessageWhenErrorOccursDuringBashExecution() { + String expectedMessage = "Error in bash executions"; + BashExecutionException bashExecutionException = new BashExecutionException(expectedMessage, cause); + + ResponseEntity responseEntity = errorHandler.handle(bashExecutionException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + + } + + @Test + void shouldReturnResponseEntityWithMessageWhenHelmApiVersionNotFoundInChart() { + String expectedMessage = "Cannot find apiVersion value in a main chart"; + ApiVersionNotFoundException apiVersionNotFoundException = new ApiVersionNotFoundException(); + + ResponseEntity responseEntity = errorHandler.handle(apiVersionNotFoundException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + + } + + @Test + void shouldReturnResponseEntityWithMessageWhenHelmApiVersionIsNotSupported() { + String expectedMessage = "Cannot obtain Helm version from API version"; + NotSupportedApiVersionException notSupportedApiVersionException = new NotSupportedApiVersionException( + expectedMessage); + + ResponseEntity responseEntity = errorHandler.handle(notSupportedApiVersionException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + + } + + @Test + void shouldReturnResponseEntityWithMessageWhenCannotReadChartFile() { + String expectedMessage = "Cannot read tar file from path"; + ReadFileException readFileException = new ReadFileException(expectedMessage, cause); + + ResponseEntity responseEntity = errorHandler.handle(readFileException); + + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(responseEntity.getBody().getMessage()).isEqualTo(expectedMessage); + + } + +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/validation/BashExecutorTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/BashExecutorTest.java new file mode 100644 index 0000000..5f76b83 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/BashExecutorTest.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.validation; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.onap.sdc.helmvalidator.helm.validation.exception.BashExecutionException; +import org.onap.sdc.helmvalidator.helm.validation.model.BashOutput; + +class BashExecutorTest { + + + @Test + @Disabled("Disabled due to Operating System dependency - allowed on Linux OS") + void testExecution() throws BashExecutionException { + + BashExecutor executor = new BashExecutor(); + + BashOutput output = executor.execute("ls -al"); + + assertThat(output.getOutputLines()).isNotEmpty(); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/validation/FileManagerTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/FileManagerTest.java new file mode 100644 index 0000000..2d05cd9 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/FileManagerTest.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.validation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Comparator; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.web.multipart.MultipartFile; + +@ExtendWith(MockitoExtension.class) +class FileManagerTest { + + private static final String TEST_RESOURCES_TMP = "src/test/resources/tmp"; + private static final File TEST_RESOURCES_DIR = new File(TEST_RESOURCES_TMP); + private static final ByteArrayInputStream TEST_INPUT_STREAM = new ByteArrayInputStream("test".getBytes()); + private static final String SAMPLE_FILE_NAME = "sample_file"; + + private FileManager fileManager; + + @Mock + private MultipartFile multipartFile; + + @BeforeAll + static void createTmpDir() { + TEST_RESOURCES_DIR.mkdirs(); + } + + @AfterAll + static void cleanTmpDir() throws IOException { + Files.walk(Paths.get(TEST_RESOURCES_TMP)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .filter(File::isFile) + .forEach(File::delete); + TEST_RESOURCES_DIR.delete(); + } + + @BeforeEach + void setUp() { + fileManager = new FileManager(TEST_RESOURCES_TMP); + } + + @Test + void saveMultipartFileAndReturnFilePath() throws IOException { + mockMultipartFile(); + + String filePath = fileManager.saveFile(multipartFile); + + assertThat(filePath).isNotBlank(); + assertThat(Files.exists(Paths.get(filePath))).isTrue(); + } + + + @Test + void removeFileByPath() throws IOException { + mockMultipartFile(); + + String filePath = fileManager.saveFile(multipartFile); + fileManager.removeFile(filePath); + + assertThat(Files.exists(Paths.get(filePath))).isFalse(); + } + + private void mockMultipartFile() throws IOException { + when(multipartFile.getOriginalFilename()).thenReturn(SAMPLE_FILE_NAME); + when(multipartFile.getInputStream()).thenReturn(TEST_INPUT_STREAM); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/validation/ValidationServiceTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/ValidationServiceTest.java new file mode 100644 index 0000000..c3c4093 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/validation/ValidationServiceTest.java @@ -0,0 +1,314 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.validation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.helmvalidator.helm.validation.exception.BashExecutionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.NotSupportedVersionException; +import org.onap.sdc.helmvalidator.helm.validation.exception.SaveFileException; +import org.onap.sdc.helmvalidator.helm.validation.model.BashOutput; +import org.onap.sdc.helmvalidator.helm.validation.model.ValidationResult; +import org.onap.sdc.helmvalidator.helm.versions.ChartBasedVersionProvider; +import org.onap.sdc.helmvalidator.helm.versions.SupportedVersionsProvider; +import org.springframework.web.multipart.MultipartFile; + +@ExtendWith(MockitoExtension.class) +class ValidationServiceTest { + + private static final String HELM_ERROR_ON_TEMPLATE = + "Error: parse error at (sample/templates/fail.yaml:1): function \"deliberateSyntaxError\" not defined"; + private static final String HELM_ERROR_ON_LINT = + "[ERROR] templates/: parse error at (sample/templates/fail.yaml:1): function \"deliberateSyntaxError\" " + + "not defined\n"; + private static final String HELM_WARNING_ON_LINT = + "[WARNING] templates/: directory not found\n"; + private static final String HELM_WITHOUT_STANDARD_ERROR_ON_LINT = + "unable to check Chart.yaml file in chart: stat /charts/1610528407457_apiv2.tar.gz/Chart.yaml: not a directory"; + private static final String HELM_LINT_ERROR_SUMMARY_MESSAGE = "Error: 0 chart(s) linted, 1 chart(s) failed"; + private static final String HELM_EMPTY_OUTPUT = ""; + + private static final boolean LINTED = true; + private static final boolean NOT_LINTED = false; + private static final boolean STRICT_LINTED = true; + private static final boolean NOT_STRICT_LINTED = false; + private static final int SUCCESS_HELM_EXIT_CODE = 0; + private static final int UNSUCCESSFUL_HELM_EXIT_CODE = 1; + + private static final String SAMPLE_VERSION = "3.3.3"; + private static final List SAMPLE_VERSIONS = List.of("3.3.4", "3.3.3", "2.3.1", "2.1.4", "2.3.4"); + private static final String NOT_SUPPORTED_VERSION = "not supported version"; + + private static final String SAMPLE_PATH = "samplePath"; + private static final String HELM_TEMPLATE = "helm-v3.3.3 template samplePath"; + private static final String HELM_LINT = "helm-v3.3.3 lint samplePath"; + private static final String HELM_LINT_STRICT = "helm-v3.3.3 lint samplePath --strict"; + private static final int EXPECTED_ONE = 1; + + private ValidationService validationService; + + @Mock + private FileManager fileManager; + + @Mock + private SupportedVersionsProvider versionsProvider; + + @Mock + private ChartBasedVersionProvider chartBasedProvider; + + @Mock + private MultipartFile multipartFile; + + @Mock + private BashExecutor bashExecutor; + + @BeforeEach + void setUp() { + when(fileManager.saveFile(multipartFile)).thenReturn(SAMPLE_PATH); + lenient().when(versionsProvider.getVersions()).thenReturn(SAMPLE_VERSIONS); + this.validationService = new ValidationService(fileManager, bashExecutor, versionsProvider, chartBasedProvider); + } + + @Test + void shouldThrowExceptionWhenCannotSaveFile() { + when(fileManager.saveFile(multipartFile)).thenThrow(SaveFileException.class); + + assertThatExceptionOfType(SaveFileException.class) + .isThrownBy( + () -> validationService.process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED)); + } + + @Test + void shouldThrowExceptionWhenVersionsIsNotSupported() { + + assertThatExceptionOfType(NotSupportedVersionException.class) + .isThrownBy( + () -> validationService.process(NOT_SUPPORTED_VERSION, multipartFile, LINTED, STRICT_LINTED)); + } + + @Test + void shouldBeValidAndDeployableForNoErrorsAndNoWarning() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isTrue(); + assertThat(validationResult.isValid()).isTrue(); + assertThat(validationResult.getRenderErrors()).isEmpty(); + assertThat(validationResult.getLintError()).isEmpty(); + assertThat(validationResult.getLintWarning()).isEmpty(); + } + + @ParameterizedTest + @CsvSource({"v2", "v3"}) + void shouldBeDeployableForLatestHelmVersion(String desiredVersion) { + final String majorVersion = desiredVersion.substring(1); + final String helmVersion = getLatestSampleHelmVersion(majorVersion); + final String helmTemplate = String.format("helm-v%s template samplePath", helmVersion); + when(versionsProvider.getLatestVersion(Mockito.anyString())).thenReturn(helmVersion); + mockBashCommand(helmTemplate, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(desiredVersion, multipartFile, NOT_LINTED, NOT_STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isTrue(); + assertThat(validationResult.getRenderErrors()).isEmpty(); + verify(versionsProvider).getLatestVersion(majorVersion); + } + + @Test + void shouldBeValidAndDeployableForVersionTakenFromChart() { + when(chartBasedProvider.getVersion(Mockito.anyString())).thenReturn(SAMPLE_VERSION); + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(null, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isTrue(); + assertThat(validationResult.isValid()).isTrue(); + assertThat(validationResult.getRenderErrors()).isEmpty(); + assertThat(validationResult.getLintError()).isEmpty(); + assertThat(validationResult.getLintWarning()).isEmpty(); + verify(chartBasedProvider).getVersion(SAMPLE_PATH); + } + + @Test + void shouldBeUndeployableForRenderErrorsWithMessages() { + mockBashCommand(HELM_TEMPLATE, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_ERROR_ON_TEMPLATE); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, NOT_LINTED, NOT_STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isFalse(); + assertThat(validationResult.getRenderErrors()).isNotEmpty(); + } + + @Test + void shouldBeUndeployableAndValidWhenRenderFailAndLintSuccessfulWithMessages() + throws BashExecutionException, SaveFileException { + mockBashCommand(HELM_TEMPLATE, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_ERROR_ON_TEMPLATE); + mockBashCommand(HELM_LINT_STRICT, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isFalse(); + assertThat(validationResult.isValid()).isTrue(); + assertThat(validationResult.getRenderErrors()).isNotEmpty(); + assertThat(validationResult.getLintError()).isEmpty(); + assertThat(validationResult.getLintWarning()).isEmpty(); + } + + @Test + void shouldBeDeployableAndInvalidForErrorOnLintWithMessages() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_ERROR_ON_LINT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, NOT_STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isTrue(); + assertThat(validationResult.isValid()).isFalse(); + assertThat(validationResult.getRenderErrors()).isEmpty(); + assertThat(validationResult.getLintError()).isNotEmpty(); + assertThat(validationResult.getLintWarning()).isEmpty(); + } + + @Test + void shouldBeDeployableAndInvalidForWarningOnLintAndStrictLintWithMessages() + throws BashExecutionException, SaveFileException { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_WARNING_ON_LINT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isTrue(); + assertThat(validationResult.isValid()).isFalse(); + assertThat(validationResult.getRenderErrors()).isEmpty(); + assertThat(validationResult.getLintError()).isEmpty(); + assertThat(validationResult.getLintWarning()).isNotEmpty(); + } + + @Test + void shouldBeUndeployableAndInvalidForErrorOnTemplateAndErrorOnLintWithMessages() + throws BashExecutionException, SaveFileException { + mockBashCommand(HELM_TEMPLATE, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_ERROR_ON_TEMPLATE); + mockBashCommand(HELM_LINT_STRICT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_WARNING_ON_LINT, HELM_ERROR_ON_LINT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isDeployable()).isFalse(); + assertThat(validationResult.isValid()).isFalse(); + assertThat(validationResult.getRenderErrors()).isNotEmpty(); + assertThat(validationResult.getLintError()).isNotEmpty(); + assertThat(validationResult.getLintWarning()).isNotEmpty(); + } + + @Test + void shouldBeInvalidForWarningOnStrictLintWithMessages() throws BashExecutionException, SaveFileException { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_WARNING_ON_LINT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + + assertThat(validationResult.isValid()).isFalse(); + assertThat(validationResult.getLintError()).isEmpty(); + assertThat(validationResult.getLintWarning()).isNotEmpty(); + } + + @Test + void shouldAddUsedVersionToValidationResultOnLinted() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + assertThat(validationResult.getVersionUsed()).isEqualTo(SAMPLE_VERSION); + } + + @Test + void shouldAddUsedVersionToValidationResultOnNotLinted() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, NOT_LINTED, NOT_STRICT_LINTED); + assertThat(validationResult.getVersionUsed()).isEqualTo(SAMPLE_VERSION); + } + + @Test + void shouldAddBashOutputToResultWhenHelmReturnNonStandardError() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_WITHOUT_STANDARD_ERROR_ON_LINT); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + assertThat(validationResult.getLintError()).isNotEmpty(); + } + + @Test + void shouldNotAddSummaryMessageToLintErrors() { + mockBashCommand(HELM_TEMPLATE, SUCCESS_HELM_EXIT_CODE, HELM_EMPTY_OUTPUT); + mockBashCommand(HELM_LINT_STRICT, UNSUCCESSFUL_HELM_EXIT_CODE, HELM_ERROR_ON_LINT, + HELM_LINT_ERROR_SUMMARY_MESSAGE); + + ValidationResult validationResult = validationService + .process(SAMPLE_VERSION, multipartFile, LINTED, STRICT_LINTED); + assertThat(validationResult.getLintError()).isNotEmpty(); + assertThat(validationResult.getLintError()).hasSize(EXPECTED_ONE); + } + + private String getLatestSampleHelmVersion(String majorVersion) { + return SAMPLE_VERSIONS.stream() + .filter(version -> version.startsWith(majorVersion)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Not supported version")); + } + + private void mockBashCommand(String command, int exitValue, String... consoleOutput) { + when(bashExecutor.execute(command)) + .thenReturn(new BashOutput(exitValue, mockBashConsoleLog(consoleOutput))); + } + + private List mockBashConsoleLog(String... args) { + return Arrays.stream(args).collect(Collectors.toList()); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReaderTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReaderTest.java new file mode 100644 index 0000000..7343e59 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReaderTest.java @@ -0,0 +1,128 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.versions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.onap.sdc.helmvalidator.helm.versions.exception.ApiVersionNotFoundException; +import org.onap.sdc.helmvalidator.helm.versions.exception.ReadFileException; + +class ApiVersionsReaderTest { + + private static final String TEST_RESOURCES_TMP = "src/test/resources/tmp"; + private static final Path TEST_CHART_PATH = Path.of(TEST_RESOURCES_TMP).resolve(Path.of("Chart.yaml")); + + private static final Path TEST_TAR_PATH = Path.of(TEST_RESOURCES_TMP, "test.tar"); + + private ApiVersionsReader apiVersionsReader; + + @BeforeEach + void setUp() { + apiVersionsReader = new ApiVersionsReader(); + } + + @ParameterizedTest + @ValueSource(strings = {"v1", "v2"}) + void shouldCorrectlyReadApiVersionFromTar(String apiVersion) throws IOException { + prepareTestTar(apiVersion); + + String helmVersion = apiVersionsReader.readVersion(TEST_TAR_PATH.toString()); + + assertThat(helmVersion).isEqualTo(apiVersion); + } + + @Test + void shouldThrowExceptionWhenApiVersionIsNotProvided() throws IOException { + prepareTestTar(null); + + Exception exception = assertThrows(ApiVersionNotFoundException.class, + () -> apiVersionsReader.readVersion(TEST_TAR_PATH.toString())); + + assertThat(exception).hasMessageContaining("Cannot find apiVersion value in a main chart"); + } + + @Test + void shouldThrowExceptionForNotExistingPath() { + String notExistingChartPath = "not/exiting/chart/path"; + + Exception exception = assertThrows(ReadFileException.class, + () -> apiVersionsReader.readVersion(notExistingChartPath)); + + assertThat(exception).hasMessageContaining("Cannot read tar from path: " + notExistingChartPath); + } + + @AfterEach + void cleanTmpDir() throws IOException { + Files.walk(Paths.get(TEST_RESOURCES_TMP)) + .map(Path::toFile) + .filter(File::isFile) + .forEach(File::delete); + } + + private void prepareTestTar(String apiVersion) throws IOException { + createTestChart(apiVersion); + createTestTar(); + } + + private void createTestTar() throws IOException { + TarArchiveOutputStream tarOutput = null; + try { + tarOutput = new TarArchiveOutputStream( + new GzipCompressorOutputStream(new FileOutputStream(String.valueOf(TEST_TAR_PATH)))); + + final String tarChartPath = "test/Chart.yaml"; + TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(TEST_CHART_PATH.toFile(), tarChartPath); + tarOutput.putArchiveEntry(tarArchiveEntry); + + Files.copy(TEST_CHART_PATH, tarOutput); + } finally { + if (tarOutput != null) { + tarOutput.closeArchiveEntry(); + tarOutput.close(); + } + } + } + + private void createTestChart(String apiVersion) throws IOException { + String apiVersionLine = apiVersion != null ? "apiVersion: " + apiVersion : ""; + + Files.createDirectories(TEST_CHART_PATH.getParent()); + Files.createFile(TEST_CHART_PATH); + Files.write(TEST_CHART_PATH, List.of("appVersion: 1.0", apiVersionLine, "name: test-chart")); + } + +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProviderTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProviderTest.java new file mode 100644 index 0000000..64daf2d --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProviderTest.java @@ -0,0 +1,73 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.versions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.helmvalidator.helm.versions.exception.NotSupportedApiVersionException; + +@ExtendWith(MockitoExtension.class) +class ChartBasedVersionProviderTest { + + private final String testChartPath = "test/path"; + @Mock + private SupportedVersionsProvider versionsProvider; + @Mock + private ApiVersionsReader apiVersionsReader; + private ChartBasedVersionProvider chartBasedVersionProvider; + + @BeforeEach + void setUp() { + chartBasedVersionProvider = new ChartBasedVersionProvider(versionsProvider, apiVersionsReader); + } + + @ParameterizedTest + @CsvSource({"v1,2.18", "v2,3.11"}) + void shouldGetLatestHelmVersionBasedOnApiVersion(String apiVersion, String expectedHelmVersion) { + when(apiVersionsReader.readVersion(testChartPath)).thenReturn(apiVersion); + when(versionsProvider.getLatestVersion(Mockito.anyString())).thenReturn(expectedHelmVersion); + + String helmVersion = chartBasedVersionProvider.getVersion(testChartPath); + + assertThat(helmVersion).isEqualTo(expectedHelmVersion); + } + + @Test + void shouldThrowExceptionWhenApiVersionIsNotSupported() { + when(apiVersionsReader.readVersion(testChartPath)).thenReturn("v3"); + + Exception exception = assertThrows(NotSupportedApiVersionException.class, + () -> chartBasedVersionProvider.getVersion(testChartPath)); + + assertThat(exception).hasMessageContaining("Cannot obtain Helm version from API version: v3"); + } + +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProviderTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProviderTest.java new file mode 100644 index 0000000..c28d339 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProviderTest.java @@ -0,0 +1,72 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.versions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SupportedVersionsProviderTest { + + private static final List UNSORTED_VERSIONS = List.of("1.0.0", "2.2.1", "2.0.0", "3.0.0"); + private static final List SORTED_VERSIONS = List.of("3.0.0", "2.2.1", "2.0.0", "1.0.0"); + private static final List SUPPORTED_VERSIONS = List.of("4.2.0", "3.11.3", "3.1.0", "3.0.4", "2.18.2", + "2.1.5", "1.5.6"); + + private SupportedVersionsProvider versionsProvider; + + @Mock + private SystemEnvVersionsReader versionsReader; + + @BeforeEach + void setUp() { + versionsProvider = new SupportedVersionsProvider(versionsReader); + } + + @Test + void shouldReturnSortedVersionsInRevertOrder() { + + when(versionsReader.readVersions()).thenReturn(UNSORTED_VERSIONS); + + List versions = versionsProvider.getVersions(); + + assertThat(versions).isEqualTo(SORTED_VERSIONS); + } + + @ParameterizedTest + @CsvSource({"2,2.18.2", "3,3.11.3"}) + void shouldGetLatestHelmVersionBasedOnDesiredMajorVersion(String desiredMajorVersion, String expectedHelmVersion) { + when(versionsProvider.getVersions()).thenReturn(SUPPORTED_VERSIONS); + + String helmVersion = versionsProvider.getLatestVersion(desiredMajorVersion); + + assertThat(helmVersion).isEqualTo(expectedHelmVersion); + } +} diff --git a/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReaderTest.java b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReaderTest.java new file mode 100644 index 0000000..7748355 --- /dev/null +++ b/src/test/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReaderTest.java @@ -0,0 +1,69 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.helmvalidator.helm.versions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SystemEnvVersionsReaderTest { + + private static final String SINGLE_VERSION_VALUE = "3.2.1"; + private static final String MULTIPLE_VERSIONS_VALUE = "3.2.1,2.0.0,1.0.0"; + private static final int EXPECTED_SIZE_ONE = 1; + private static final int EXPECTED_SIZE_THREE = 3; + + @Spy + private SystemEnvVersionsReader versionsReader; + + @Test + void canReadSingleValue() { + when(versionsReader.getSupportedVersionsFromEnv()).thenReturn(SINGLE_VERSION_VALUE); + + List versions = versionsReader.readVersions(); + + assertThat(versions).hasSize(EXPECTED_SIZE_ONE); + } + + @Test + void canReadMultipleValues() { + when(versionsReader.getSupportedVersionsFromEnv()).thenReturn(MULTIPLE_VERSIONS_VALUE); + + List versions = versionsReader.readVersions(); + + assertThat(versions).hasSize(EXPECTED_SIZE_THREE); + } + + @Test + void returnEmptyListWhenVariableIsNotPresent() { + when(versionsReader.getSupportedVersionsFromEnv()).thenReturn(""); + + List versions = versionsReader.readVersions(); + + assertThat(versions).isEmpty(); + } +} -- cgit 1.2.3-korg