@Test public void compareImageAndInstancesMandatoryPackageVersionBaseOk() throws JsonProcessingException { String packageName = "package"; Map<String, String> packageVersions = Collections.singletonMap(packageName, "1"); when(statedImage.getImage()).thenReturn(image); when(image.isPrewarmed()).thenReturn(false); when(image.getPackageVersions()).thenReturn(packageVersions); InstanceMetadataUpdater.Package aPackage = new InstanceMetadataUpdater.Package(); aPackage.setName(packageName); aPackage.setPrewarmed(false); InstanceMetadataUpdater.Package prewarmedPackage = new InstanceMetadataUpdater.Package(); prewarmedPackage.setName(packageName); prewarmedPackage.setPrewarmed(true); when(instanceMetadataUpdater.getPackages()).thenReturn(Lists.newArrayList(aPackage, prewarmedPackage)); when(instanceMetadataUpdater.isPackagesVersionEqual(anyString(), anyString())).thenReturn(true); InstanceMetaData instanceMetaData = new InstanceMetaData(); instanceMetaData.setImage(new Json(new com.sequenceiq.cloudbreak.cloud.model.Image( "image", Collections.emptyMap(), "os", "ostype", "catalogn", "catalogu", "id", packageVersions))); Set<InstanceMetaData> instanceMetaDataSet = Collections.singleton(instanceMetaData); CheckResult result = underTest.compareImageAndInstancesMandatoryPackageVersion(statedImage, instanceMetaDataSet); assertEquals(EventStatus.OK, result.getStatus()); }
public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) { Map<String, String> packageVersions = newImage.getImage().getPackageVersions(); List<String> packagesToCompare; packagesToCompare = newImage.getImage().isPrewarmed() ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList()) : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList()); List<String> missingPackageVersion = new ArrayList<>(); List<String> differentPackageVersion = new ArrayList<>(); Map<String, String> instancePackageVersions; try { instancePackageVersions = instanceMetaDataSet.stream() .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing")) .getImage().get(Image.class).getPackageVersions(); } catch (IOException e) { LOGGER.debug("Could not get image", e); return CheckResult.failed("Could not get image"); } for (String packageToCompare : packagesToCompare) { if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) { String packageVersionInImage = packageVersions.get(packageToCompare); if (StringUtils.isBlank(packageVersionInImage)) { LOGGER.debug("Missing package in image: " + packageToCompare); missingPackageVersion.add(packageToCompare); } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) { LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]", packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare))); differentPackageVersion.add(packageToCompare); } } } if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) { String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(), Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ","))); return CheckResult.failed(message); } return CheckResult.ok(); }
PackageVersionChecker { public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) { Map<String, String> packageVersions = newImage.getImage().getPackageVersions(); List<String> packagesToCompare; packagesToCompare = newImage.getImage().isPrewarmed() ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList()) : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList()); List<String> missingPackageVersion = new ArrayList<>(); List<String> differentPackageVersion = new ArrayList<>(); Map<String, String> instancePackageVersions; try { instancePackageVersions = instanceMetaDataSet.stream() .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing")) .getImage().get(Image.class).getPackageVersions(); } catch (IOException e) { LOGGER.debug("Could not get image", e); return CheckResult.failed("Could not get image"); } for (String packageToCompare : packagesToCompare) { if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) { String packageVersionInImage = packageVersions.get(packageToCompare); if (StringUtils.isBlank(packageVersionInImage)) { LOGGER.debug("Missing package in image: " + packageToCompare); missingPackageVersion.add(packageToCompare); } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) { LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]", packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare))); differentPackageVersion.add(packageToCompare); } } } if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) { String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(), Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ","))); return CheckResult.failed(message); } return CheckResult.ok(); } }
PackageVersionChecker { public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) { Map<String, String> packageVersions = newImage.getImage().getPackageVersions(); List<String> packagesToCompare; packagesToCompare = newImage.getImage().isPrewarmed() ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList()) : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList()); List<String> missingPackageVersion = new ArrayList<>(); List<String> differentPackageVersion = new ArrayList<>(); Map<String, String> instancePackageVersions; try { instancePackageVersions = instanceMetaDataSet.stream() .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing")) .getImage().get(Image.class).getPackageVersions(); } catch (IOException e) { LOGGER.debug("Could not get image", e); return CheckResult.failed("Could not get image"); } for (String packageToCompare : packagesToCompare) { if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) { String packageVersionInImage = packageVersions.get(packageToCompare); if (StringUtils.isBlank(packageVersionInImage)) { LOGGER.debug("Missing package in image: " + packageToCompare); missingPackageVersion.add(packageToCompare); } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) { LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]", packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare))); differentPackageVersion.add(packageToCompare); } } } if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) { String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(), Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ","))); return CheckResult.failed(message); } return CheckResult.ok(); }  }
PackageVersionChecker { public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) { Map<String, String> packageVersions = newImage.getImage().getPackageVersions(); List<String> packagesToCompare; packagesToCompare = newImage.getImage().isPrewarmed() ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList()) : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList()); List<String> missingPackageVersion = new ArrayList<>(); List<String> differentPackageVersion = new ArrayList<>(); Map<String, String> instancePackageVersions; try { instancePackageVersions = instanceMetaDataSet.stream() .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing")) .getImage().get(Image.class).getPackageVersions(); } catch (IOException e) { LOGGER.debug("Could not get image", e); return CheckResult.failed("Could not get image"); } for (String packageToCompare : packagesToCompare) { if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) { String packageVersionInImage = packageVersions.get(packageToCompare); if (StringUtils.isBlank(packageVersionInImage)) { LOGGER.debug("Missing package in image: " + packageToCompare); missingPackageVersion.add(packageToCompare); } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) { LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]", packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare))); differentPackageVersion.add(packageToCompare); } } } if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) { String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(), Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ","))); return CheckResult.failed(message); } return CheckResult.ok(); }  CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet); CheckResult checkInstancesHaveAllMandatoryPackageVersion(Set<InstanceMetaData> instanceMetaDataSet); CheckResult checkInstancesHaveMultiplePackageVersions(Set<InstanceMetaData> instanceMetaDataSet); }
PackageVersionChecker { public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) { Map<String, String> packageVersions = newImage.getImage().getPackageVersions(); List<String> packagesToCompare; packagesToCompare = newImage.getImage().isPrewarmed() ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList()) : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList()); List<String> missingPackageVersion = new ArrayList<>(); List<String> differentPackageVersion = new ArrayList<>(); Map<String, String> instancePackageVersions; try { instancePackageVersions = instanceMetaDataSet.stream() .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing")) .getImage().get(Image.class).getPackageVersions(); } catch (IOException e) { LOGGER.debug("Could not get image", e); return CheckResult.failed("Could not get image"); } for (String packageToCompare : packagesToCompare) { if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) { String packageVersionInImage = packageVersions.get(packageToCompare); if (StringUtils.isBlank(packageVersionInImage)) { LOGGER.debug("Missing package in image: " + packageToCompare); missingPackageVersion.add(packageToCompare); } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) { LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]", packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare))); differentPackageVersion.add(packageToCompare); } } } if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) { String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(), Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ","))); return CheckResult.failed(message); } return CheckResult.ok(); }  CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet); CheckResult checkInstancesHaveAllMandatoryPackageVersion(Set<InstanceMetaData> instanceMetaDataSet); CheckResult checkInstancesHaveMultiplePackageVersions(Set<InstanceMetaData> instanceMetaDataSet); static final String SALT_BOOTSTRAP; }