From 372c2470e613128f1e33cfc581ea2c5b1b732d2b Mon Sep 17 00:00:00 2001 From: Jan Malkiewicz Date: Tue, 9 Feb 2021 08:39:23 +0100 Subject: Add validation of manifest for helm packages. For ONAP native zip packages added validation of manifest file. HELM package is only valid if: * isBase flag is set to 'true' for exactly one helm entry * isBase flag is present for all helm entries Zip package is considered to be a helm package if it contains HELM entries but does not contain any HEAT entries (however it may contains entries of other types). Refactored method OnboardingPackageProcessor.processPackage(): * simplified logic * enhanced exception handling Issue-ID: SDC-3185 Signed-off-by: Jan Malkiewicz Change-Id: Ica3b1f1504ce4fc3a671c4b8fa8de2bf0236bd77 --- .../impl/onboarding/ManifestAnalyzerTest.java | 152 +++++++++++++++++++++ .../onboarding/OnboardingPackageProcessorTest.java | 40 ++++-- .../OnboardingPackageProcessorUnitTest.java | 143 +++++++++++++++++++ .../validation/CnfPackageValidatorTest.java | 137 +++++++++++++++++++ 4 files changed, 464 insertions(+), 8 deletions(-) create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/ManifestAnalyzerTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorUnitTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/validation/CnfPackageValidatorTest.java (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/ManifestAnalyzerTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/ManifestAnalyzerTest.java new file mode 100644 index 0000000000..51b34b6bb4 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/ManifestAnalyzerTest.java @@ -0,0 +1,152 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nokia + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.FileData.Type; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; + + +public class ManifestAnalyzerTest { + + @Test + public void shouldAnalyzeManifestWithOnlyHelmEntries() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(helmOnly())); + + assertThat(analyzer.hasHelmEntries(), is(true)); + assertThat(analyzer.hasHeatEntries(), is(false)); + assertThat(analyzer.getHelmEntries().size(), is(3)); + assertThatContainsOnlyHelm(analyzer.getHelmEntries()); + } + + @Test + public void shouldAnalyzeManifestWithoutHelmEntries() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(withoutHelm())); + + assertThat(analyzer.hasHelmEntries(), is(false)); + assertThat(analyzer.hasHeatEntries(), is(true)); + assertThat(analyzer.getHelmEntries().size(), is(0)); + } + + @Test + public void shouldAnalyzeManifestWitoutyHelmAndHeatEntries() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(withoutHelmAndHeat())); + + assertThat(analyzer.hasHelmEntries(), is(false)); + assertThat(analyzer.hasHeatEntries(), is(false)); + assertThat(analyzer.getHelmEntries().size(), is(0)); + } + + @Test + public void shouldAnalyzeManifestWithHelmAndHeatEntries() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(helmAndHeat())); + + assertThat(analyzer.hasHelmEntries(), is(true)); + assertThat(analyzer.hasHeatEntries(), is(true)); + assertThat(analyzer.getHelmEntries().size(), is(2)); + assertThatContainsOnlyHelm(analyzer.getHelmEntries()); + } + + @Test + public void shouldAnalyzeManifestWithMultipleTypeEntries() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(helmAndHeatAndOther())); + + assertThat(analyzer.hasHelmEntries(), is(true)); + assertThat(analyzer.hasHeatEntries(), is(true)); + assertThat(analyzer.getHelmEntries().size(), is(2)); + assertThatContainsOnlyHelm(analyzer.getHelmEntries()); + } + + private void assertThatContainsOnlyHelm(List entries) { + entries.forEach(fileData -> assertThat(fileData.getType(), is(Type.HELM))); + } + + private ManifestContent manifest(List entries) { + ManifestContent manifest = new ManifestContent(); + manifest.setData(entries); + return manifest; + } + + private List withoutHelm() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HEAT, true)); + entries.add(createFileData(Type.CHEF, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + private List withoutHelmAndHeat() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.PUPPET, true)); + entries.add(createFileData(Type.CHEF, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + private List helmOnly() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HELM, true)); + entries.add(createFileData(Type.HELM, false)); + entries.add(createFileData(Type.HELM, false)); + + return entries; + } + + private List helmAndHeat() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HELM, true)); + entries.add(createFileData(Type.HELM, false)); + entries.add(createFileData(Type.HEAT, false)); + + return entries; + } + + private List helmAndHeatAndOther() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HELM, true)); + entries.add(createFileData(Type.HELM, false)); + entries.add(createFileData(Type.HEAT, false)); + entries.add(createFileData(Type.PUPPET, false)); + entries.add(createFileData(Type.CHEF, false)); + + return entries; + } + + private FileData createFileData(Type type, Boolean base) { + FileData f = new FileData(); + f.setType(type); + f.setBase(base); + return f; + } + +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorTest.java index a62aea761a..d6327866f4 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorTest.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2021 Nokia * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import static org.openecomp.sdc.common.errors.Messages.COULD_NOT_READ_MANIFEST_FILE; +import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_MISSING; +import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET; import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR; import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION; @@ -49,6 +53,7 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo; @RunWith(Parameterized.class) public class OnboardingPackageProcessorTest { + private static final String BASE_DIR = "/vspmanager.csar/"; private final String packageName; private final byte[] packageBytes; @@ -56,8 +61,8 @@ public class OnboardingPackageProcessorTest { private final OnboardingTypesEnum expectedPackageType; public OnboardingPackageProcessorTest(final String packageName, final byte[] packageBytes, - final Set expectedErrorSet, - final OnboardingTypesEnum expectedPackageType) { + final Set expectedErrorSet, + final OnboardingTypesEnum expectedPackageType) { this.packageName = packageName; this.packageBytes = packageBytes; this.expectedErrorSet = expectedErrorSet; @@ -87,23 +92,42 @@ public class OnboardingPackageProcessorTest { {"successfulUpload.csar", getFileBytes("successfulUpload.csar"), Collections.emptySet(), OnboardingTypesEnum.CSAR}, - {"fakeNonSignedZipPackage.zip", getFileBytes("signing/fakeNonSignedZipPackage.zip"), Collections.emptySet(), + {"helm-package-valid.zip", getFileBytes("helm-package-valid.zip"), Collections.emptySet(), + OnboardingTypesEnum.ZIP}, + + {"helm-package-invalid.zip", getFileBytes("helm-package-invalid-missing-flag-isbase.zip"), ImmutableSet.of( + new ErrorMessage(ErrorLevel.ERROR, + MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET.getErrorMessage()), + new ErrorMessage(ErrorLevel.ERROR, + MANIFEST_VALIDATION_HELM_IS_BASE_MISSING.formatMessage(3) + ) + ), + OnboardingTypesEnum.ZIP}, + + {"fakeNonSignedZipPackage.zip", getFileBytes("signing/fakeNonSignedZipPackage.zip"), ImmutableSet.of( + new ErrorMessage(ErrorLevel.ERROR, + COULD_NOT_READ_MANIFEST_FILE.formatMessage("MANIFEST.json", "fakeNonSignedZipPackage.zip") + )), OnboardingTypesEnum.ZIP} }); } @Test public void processPackage() { - final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName, packageBytes); + final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName, + packageBytes); assertThat("Should contains errors", onboardingPackageProcessor.hasErrors(), is(!expectedErrorSet.isEmpty())); - assertThat("Should have the same number of errors", onboardingPackageProcessor.getErrorMessageSet().size(), equalTo(expectedErrorSet.size())); + assertThat("Should have the same number of errors", onboardingPackageProcessor.getErrorMessages().size(), + equalTo(expectedErrorSet.size())); if (expectedErrorSet.size() > 0) { - assertThat("Should have the expected errors", onboardingPackageProcessor.getErrorMessageSet(), containsInAnyOrder(expectedErrorSet.toArray())); + assertThat("Should have the expected errors", onboardingPackageProcessor.getErrorMessages(), + containsInAnyOrder(expectedErrorSet.toArray())); return; } final OnboardPackageInfo onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null); assertThat("Should build onboardPackageInfo", onboardPackageInfo, is(notNullValue())); - assertThat("Should have the expected package type", onboardPackageInfo.getPackageType(), is(equalTo(expectedPackageType))); + assertThat("Should have the expected package type", onboardPackageInfo.getPackageType(), + is(equalTo(expectedPackageType))); } private static byte[] getFileBytes(final String filePath) { @@ -117,4 +141,4 @@ public class OnboardingPackageProcessorTest { return null; } -} \ No newline at end of file +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorUnitTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorUnitTest.java new file mode 100644 index 0000000000..a97a0f639a --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorUnitTest.java @@ -0,0 +1,143 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nokia + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.Test; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.FileData.Type; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + + +public class OnboardingPackageProcessorUnitTest { + + private OnboardingPackageProcessor processor = new OnboardingPackageProcessor("unitTestPackage", null); + + @Test + public void shouldValidateZipPackage_helmWithoutHeat() { + assertThat(processor.validateZipPackage(manifest(withHelmWithoutHeat())).size(), is(0)); + } + + @Test + public void shouldValidateZipPackage_withHelmAndHeat() { + assertThat(processor.validateZipPackage(manifest(withHelmAndHeat())).size(), is(0)); + } + + @Test + public void shouldValidateZipPackage_withHelmWithoutHeat() { + assertThat(processor.validateZipPackage(manifest(withoutHelmWithoutHeat())).size(), is(0)); + } + + @Test + public void shouldValidateZipPackage_helmInvalid() { + assertThat(processor.validateZipPackage(manifest(withHelmInvalid())).size(), is(1)); + } + + @Test + public void shouldValidateHelmPackage() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(withHelmWithoutHeat())); + + assertThat(processor.shouldValidateHelmPackage(analyzer), is(true)); + } + + @Test + public void shouldNotValidateHelmPackage_emptyInput() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(empty())); + + assertThat(processor.shouldValidateHelmPackage(analyzer), is(false)); + } + + @Test + public void shouldNotValidateHelmPackage_containsHeatModule() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(withHelmAndHeat())); + + assertThat(processor.shouldValidateHelmPackage(analyzer), is(false)); + } + + @Test + public void shouldNotValidateHelmPackage_noHelmModule() { + ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest(withoutHelmWithoutHeat())); + + assertThat(processor.shouldValidateHelmPackage(analyzer), is(false)); + } + + private ManifestContent manifest(List entries) { + ManifestContent manifest = new ManifestContent(); + manifest.setData(entries); + return manifest; + } + + private List empty() { + return Collections.emptyList(); + } + + private List withHelmAndHeat() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HEAT, true)); + entries.add(createFileData(Type.HELM, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + private List withHelmWithoutHeat() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HELM, true)); + entries.add(createFileData(Type.CHEF, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + private List withHelmInvalid() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.HELM, false)); + entries.add(createFileData(Type.CHEF, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + private List withoutHelmWithoutHeat() { + List entries = new ArrayList<>(); + + entries.add(createFileData(Type.CHEF, false)); + entries.add(createFileData(Type.PM_DICTIONARY, false)); + + return entries; + } + + + private FileData createFileData(Type type, Boolean base) { + FileData f = new FileData(); + f.setType(type); + f.setBase(base); + return f; + } + +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/validation/CnfPackageValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/validation/CnfPackageValidatorTest.java new file mode 100644 index 0000000000..e969c3d389 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/validation/CnfPackageValidatorTest.java @@ -0,0 +1,137 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nokia + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation; + +import static org.hamcrest.Matchers.emptyIterable; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.Test; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; + +public class CnfPackageValidatorTest { + + private CnfPackageValidator validator = new CnfPackageValidator(); + + @Test + public void shouldBeValidForNullInput() { + List messages = validator.validateHelmPackage(null); + + assertThat(messages, is(emptyIterable())); + } + + @Test + public void shouldBeValidForEmptyInput() { + List messages = validator.validateHelmPackage(Collections.emptyList()); + + assertThat(messages, is(emptyIterable())); + } + + @Test + public void shouldBeValid() { + List messages = validator.validateHelmPackage(createValidInput()); + + assertThat(messages, is(emptyIterable())); + } + + @Test + public void shouldBeInvalidNoneIsMarkedAsBase() { + List messages = validator.validateHelmPackage(noneIsMarkedAsBase()); + + assertThat(messages.size(), is(1)); + assertThat(messages.get(0), is("None of charts is marked as 'isBase'.")); + } + + @Test + public void shouldBeInvalidMultipleAreMarkedAsBase() { + List messages = validator.validateHelmPackage(multipleAreMarkedAsBase()); + + assertThat(messages.size(), is(1)); + assertThat(messages.get(0), is("More than one chart is marked as 'isBase'.")); + } + + @Test + public void shouldBeInvalidIsBaseMissing() { + List messages = validator.validateHelmPackage(isBaseMissing()); + + assertThat(messages.size(), is(1)); + assertThat(messages.get(0), is("Definition of 'isBase' is missing in 2 charts.")); + } + + @Test + public void shouldBeInvalidDueMultipleReasons() { + List messages = validator.validateHelmPackage(invalidMultipleReasons()); + + assertThat(messages.size(), is(2)); + assertThat(messages.get(0), is("Definition of 'isBase' is missing in 1 charts.")); + assertThat(messages.get(1), is("None of charts is marked as 'isBase'.")); + } + + private List createValidInput() { + List files = new ArrayList<>(); + files.add(createFileData(true)); + files.add(createFileData(false)); + files.add(createFileData(false)); + return files; + } + + private List noneIsMarkedAsBase() { + List files = new ArrayList<>(); + files.add(createFileData(false)); + files.add(createFileData(false)); + files.add(createFileData(false)); + return files; + } + + private List multipleAreMarkedAsBase() { + List files = new ArrayList<>(); + files.add(createFileData(true)); + files.add(createFileData(true)); + files.add(createFileData(false)); + return files; + } + + private List isBaseMissing() { + List files = new ArrayList<>(); + files.add(createFileData(true)); + files.add(createFileData(null)); + files.add(createFileData(null)); + files.add(createFileData(false)); + return files; + } + + private List invalidMultipleReasons() { + List files = new ArrayList<>(); + files.add(createFileData(false)); + files.add(createFileData(null)); + files.add(createFileData(false)); + files.add(createFileData(false)); + return files; + } + + private FileData createFileData(Boolean base) { + FileData f = new FileData(); + f.setBase(base); + return f; + } +} -- cgit 1.2.3-korg