DockerImageConfig { @Nonnull public static List<DockerImageConfig> processParams(@Nonnull DockerImageConfigParser imageParser, @Nonnull Map<String, String> properties, @Nonnull Collection<CloudImageParameters> imagesParameters) { DockerCloudUtils.requireNonNull(imageParser, "Image parser cannot be null."); DockerCloudUtils.requireNonNull(properties, "Properties map cannot be null."); List<InvalidProperty> invalidProperties = new ArrayList<>(); String imagesJSon = properties.get(DockerCloudUtils.IMAGES_PARAM); Set<String> profileNames = new HashSet<>(); List<DockerImageConfig> images = Collections.emptyList(); if (imagesJSon != null && !imagesJSon.isEmpty()) { try { Node imagesNode = Node.parse(imagesJSon); images = new ArrayList<>(imagesNode.getArrayValues().size()); for (Node imageNode : imagesNode.getArrayValues()) { DockerImageConfig imageConfig = imageParser.fromJSon(imageNode, imagesParameters); boolean duplicateProfileName = !profileNames.add(imageConfig.getProfileName()); if (duplicateProfileName) { invalidProperties.add(new InvalidProperty(DockerCloudUtils.IMAGES_PARAM, "Duplicate profile name: " + imageConfig.getProfileName())); } else { images.add(imageConfig); } } } catch (Exception e) { LOG.error("Failed to parse image configuration.", e); invalidProperties.add(new InvalidProperty(DockerCloudUtils.IMAGES_PARAM, "Cannot parse image data.")); } } if (images.isEmpty()){ invalidProperties.add(new InvalidProperty(DockerCloudUtils.IMAGES_PARAM, "No image provided.")); } if (!invalidProperties.isEmpty()) { throw new DockerCloudClientConfigException(invalidProperties); } assert images != null; return images; } DockerImageConfig(@Nonnull String profileName, @Nonnull Node agentHolderSpec, boolean pullOnCreate,
                             boolean rmOnExit, boolean useOfficialTCAgentImage,
                             @Nonnull DockerRegistryCredentials registryCredentials, int maxInstanceCount,
                             @Nullable Integer agentPoolId); @Nonnull String getProfileName(); @Nonnull Node getAgentHolderSpec(); boolean isPullOnCreate(); boolean isUseOfficialTCAgentImage(); int getMaxInstanceCount(); @Nonnull Optional<Integer> getAgentPoolId(); @Nonnull DockerRegistryCredentials getRegistryCredentials(); @Nonnull static List<DockerImageConfig> processParams(@Nonnull DockerImageConfigParser imageParser,
            @Nonnull Map<String, String> properties,
            @Nonnull Collection<CloudImageParameters> imagesParameters);  }
@Test public void fromValidConfigMap() { Map<String, String> params = new HashMap<>(); CloudImageParameters imageParameters = new CloudImageParameters(); imageParameters.setParameter(CloudImageParameters.SOURCE_ID_FIELD, "TestProfile"); imageParameters.setParameter(CloudImageParameters.AGENT_POOL_ID_FIELD, "42"); Collection<CloudImageParameters> cloudImageParameters = Collections.singleton(imageParameters); params.put(CloudImageParameters.SOURCE_IMAGES_JSON, CloudImageParameters.collectionToJson(cloudImageParameters)); DockerImageConfig imageConfig = DockerImageConfigBuilder. newBuilder("TestProfile", Node.EMPTY_OBJECT). build(); TestDockerImageConfigParser imageParser = new TestDockerImageConfigParser(); imageParser.addConfig(imageConfig); params.put(DockerCloudUtils.IMAGES_PARAM, imageParser.getImagesParams().toString()); List<DockerImageConfig> images = DockerImageConfig.processParams(imageParser, params, cloudImageParameters); assertThat(images).containsExactly(imageConfig); List<Collection<CloudImageParameters>> imagesParametersList = imageParser.getImagesParametersList(); assertThat(imagesParametersList).hasSize(1); assertThat(imagesParametersList.get(0)).isSameAs(cloudImageParameters); }
@Test public void duplicateProfileName() { Map<String, String> params = new HashMap<>(); DockerImageConfig imageConfig1 = DockerImageConfigBuilder. newBuilder("TestProfile", Node.EMPTY_OBJECT). build(); DockerImageConfig imageConfig2 = DockerImageConfigBuilder. newBuilder("TestProfile2", Node.EMPTY_OBJECT). build(); TestDockerImageConfigParser parser = new TestDockerImageConfigParser(); parser.addConfig(imageConfig1).addConfig(imageConfig2); params.put(DockerCloudUtils.IMAGES_PARAM, parser.getImagesParams().toString()); DockerImageConfig.processParams(parser, params, Collections.emptySet()); imageConfig2 = DockerImageConfigBuilder. newBuilder("TestProfile", Node.EMPTY_OBJECT). build(); parser = new TestDockerImageConfigParser(); parser.addConfig(imageConfig1).addConfig(imageConfig2); params.put(DockerCloudUtils.IMAGES_PARAM, parser.getImagesParams().toString()); assertInvalidProperty(parser, params, DockerCloudUtils.IMAGES_PARAM); }