|
| 1 | +// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at |
| 3 | +// http://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +package oracle.kubernetes.operator.helm; |
| 6 | + |
| 7 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 8 | +import static org.hamcrest.Matchers.both; |
| 9 | +import static org.hamcrest.Matchers.containsString; |
| 10 | +import static org.hamcrest.Matchers.emptyString; |
| 11 | + |
| 12 | +import com.google.common.collect.ImmutableMap; |
| 13 | +import java.util.Map; |
| 14 | +import org.hamcrest.Matcher; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Ignore; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class CreateOperatorInputsValidationIT extends ChartITBase { |
| 20 | + |
| 21 | + private static final String WRONG_TYPE = "The %s property %s must be a %s instead"; |
| 22 | + |
| 23 | + private static final String[] REQUIRED_BOOLEAN_PROPERTIES = { |
| 24 | + "elkIntegrationEnabled", "createOperatorNamespace", "externalRestEnabled" |
| 25 | + }; |
| 26 | + |
| 27 | + private static final String[] REQUIRED_STRING_PROPERTIES = { |
| 28 | + "operatorNamespace", "operatorServiceAccount", "operatorImage" |
| 29 | + }; |
| 30 | + |
| 31 | + private static final String[] REQUIRED_ENUM_PROPERTIES = { |
| 32 | + "operatorImagePullPolicy", "javaLoggingLevel" |
| 33 | + }; |
| 34 | + |
| 35 | + private static final String[] LOGGING_LEVELS = { |
| 36 | + "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST" |
| 37 | + }; |
| 38 | + |
| 39 | + private final String[] PULL_POLICIES = {"Always", "IfNotPresent", "Never"}; |
| 40 | + |
| 41 | + private HelmOperatorYamlFactory factory = new HelmOperatorYamlFactory(); |
| 42 | + private Map<String, Object> overrides; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setUp() throws Exception { |
| 46 | + overrides = ((HelmOperatorValues) factory.newOperatorValues()).createMap(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenStringSpecifiedForBooleanProperties_reportError() throws Exception { |
| 51 | + for (String propertyName : REQUIRED_BOOLEAN_PROPERTIES) { |
| 52 | + setProperty(propertyName, "this is not a boolean"); |
| 53 | + } |
| 54 | + |
| 55 | + String processingError = getProcessingError(); |
| 56 | + |
| 57 | + for (String propertyName : REQUIRED_BOOLEAN_PROPERTIES) { |
| 58 | + assertThat(processingError, containsTypeError(propertyName, "bool", "string")); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void setProperty(String propertyName, Object value) { |
| 63 | + overrides.put(propertyName, value); |
| 64 | + } |
| 65 | + |
| 66 | + private String getProcessingError() throws Exception { |
| 67 | + return getChart("weblogic-operator", overrides).getError(); |
| 68 | + } |
| 69 | + |
| 70 | + private Matcher<String> containsTypeError(String name, String expectedType, String actualType) { |
| 71 | + return containsString(String.format(WRONG_TYPE, actualType, name, expectedType)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void whenRequiredBooleanPropertiesMissing_reportError() throws Exception { |
| 76 | + for (String propertyName : REQUIRED_BOOLEAN_PROPERTIES) { |
| 77 | + removeProperty(propertyName); |
| 78 | + } |
| 79 | + |
| 80 | + String processingError = getProcessingError(); |
| 81 | + |
| 82 | + for (String propertyName : REQUIRED_BOOLEAN_PROPERTIES) { |
| 83 | + assertThat(processingError, containsMissingBoolParameterError(propertyName)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void removeProperty(String propertyName) { |
| 88 | + setProperty(propertyName, null); |
| 89 | + } |
| 90 | + |
| 91 | + private Matcher<String> containsMissingBoolParameterError(String propertyName) { |
| 92 | + return containsString(String.format("The bool property %s must be specified", propertyName)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void whenRequiredStringPropertiesMissing_reportError() throws Exception { |
| 97 | + for (String propertyName : REQUIRED_STRING_PROPERTIES) { |
| 98 | + removeProperty(propertyName); |
| 99 | + } |
| 100 | + |
| 101 | + String processingError = getProcessingError(); |
| 102 | + |
| 103 | + for (String propertyName : REQUIRED_STRING_PROPERTIES) { |
| 104 | + assertThat(processingError, containsMissingStringParameterError(propertyName)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private Matcher<String> containsMissingStringParameterError(String propertyName) { |
| 109 | + return containsString(String.format("The string property %s must be specified", propertyName)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void whenEnumPropertiesMissing_reportError() throws Exception { |
| 114 | + for (String propertyName : REQUIRED_ENUM_PROPERTIES) { |
| 115 | + removeProperty(propertyName); |
| 116 | + } |
| 117 | + |
| 118 | + String processingError = getProcessingError(); |
| 119 | + |
| 120 | + for (String propertyName : REQUIRED_ENUM_PROPERTIES) { |
| 121 | + assertThat(processingError, containsMissingEnumParameterError(propertyName)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private Matcher<String> containsMissingEnumParameterError(String propertyName) { |
| 126 | + return containsString(String.format("The string property %s must be specified", propertyName)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void whenBadValuesSpecifiedForEnumProperties_reportError() throws Exception { |
| 131 | + for (String propertyName : REQUIRED_ENUM_PROPERTIES) { |
| 132 | + setProperty(propertyName, "bogus"); |
| 133 | + } |
| 134 | + |
| 135 | + assertThat( |
| 136 | + getProcessingError(), |
| 137 | + both(containsEnumParameterError("operatorImagePullPolicy", PULL_POLICIES)) |
| 138 | + .and(containsEnumParameterError("javaLoggingLevel", LOGGING_LEVELS))); |
| 139 | + } |
| 140 | + |
| 141 | + private Matcher<String> containsEnumParameterError(String propertyName, String... validValues) { |
| 142 | + return containsString( |
| 143 | + String.format( |
| 144 | + "The property %s must be one of the following values [%s]", |
| 145 | + propertyName, String.join(" ", validValues))); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + @Ignore("fails to merge null overrides") |
| 150 | + public void whenExternalRestNotEnabled_ignoreMissingRelatedParameters() throws Exception { |
| 151 | + setProperty("externalRestEnabled", false); |
| 152 | + |
| 153 | + removeProperty("externalRestHttpPort"); |
| 154 | + removeProperty("externalOperatorCert"); |
| 155 | + removeProperty("externalOperatorKey"); |
| 156 | + |
| 157 | + assertThat(getProcessingError(), emptyString()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void whenExternalRestNotEnabled_ignoreRelatedParameterErrors() throws Exception { |
| 162 | + setProperty("externalRestEnabled", false); |
| 163 | + |
| 164 | + setProperty("externalRestHttpPort", "Not a number"); |
| 165 | + setProperty("externalOperatorCert", 1234); |
| 166 | + setProperty("externalOperatorKey", true); |
| 167 | + |
| 168 | + assertThat(getProcessingError(), emptyString()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void whenExternalRestEnabled_reportRelatedParameterErrors() throws Exception { |
| 173 | + misconfigureExternalRest(); |
| 174 | + |
| 175 | + assertThat( |
| 176 | + getProcessingError(), |
| 177 | + both(containsTypeError("externalOperatorCert", "string", "float64")) |
| 178 | + .and(containsTypeError("externalOperatorKey", "string", "bool"))); |
| 179 | + } |
| 180 | + |
| 181 | + private void misconfigureExternalRest() { |
| 182 | + setProperty("externalRestEnabled", true); |
| 183 | + |
| 184 | + setProperty("externalRestHttpPort", "Not a number"); |
| 185 | + setProperty("externalOperatorCert", 1234); |
| 186 | + setProperty("externalOperatorKey", true); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + @Ignore("fails to detect non-numeric port") |
| 191 | + public void whenExternalRestEnabled_reportPortNotANumber() throws Exception { |
| 192 | + misconfigureExternalRest(); |
| 193 | + |
| 194 | + assertThat(getProcessingError(), containsTypeError("externalRestHttpPort", "int", "string")); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void whenRemoteDebugNotPortDisabled_ignoreMissingPortNumbers() throws Exception { |
| 199 | + setProperty("remoteDebugNotePortEnabled", false); |
| 200 | + |
| 201 | + removeProperty("internalDebugHttpPort"); |
| 202 | + removeProperty("externalDebugHttpPort"); |
| 203 | + |
| 204 | + assertThat(getProcessingError(), emptyString()); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + @Ignore("ignores missing port numbers") |
| 209 | + public void whenRemoteDebugNotPortDisabled_reportMissingPortNumbers() throws Exception { |
| 210 | + setProperty("remoteDebugNotePortEnabled", true); |
| 211 | + |
| 212 | + removeProperty("internalDebugHttpPort"); |
| 213 | + removeProperty("externalDebugHttpPort"); |
| 214 | + |
| 215 | + assertThat( |
| 216 | + getProcessingError(), |
| 217 | + both(containsMissingIntParameterError("internalDebugHttpPort")) |
| 218 | + .and(containsMissingIntParameterError("externalDebugHttpPort"))); |
| 219 | + } |
| 220 | + |
| 221 | + private Matcher<String> containsMissingIntParameterError(String propertyName) { |
| 222 | + return containsString(String.format("The int property %s must be specified", propertyName)); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + @Ignore("ignores port number type") |
| 227 | + public void whenRemoteDebugNotPortDisabled_reportNonNumericPortNumbers() throws Exception { |
| 228 | + setProperty("remoteDebugNotePortEnabled", true); |
| 229 | + |
| 230 | + setProperty("internalDebugHttpPort", true); |
| 231 | + setProperty("externalDebugHttpPort", "abcd"); |
| 232 | + |
| 233 | + assertThat( |
| 234 | + getProcessingError(), |
| 235 | + both(containsTypeError("internalDebugHttpPort", "int", "bool")) |
| 236 | + .and(containsTypeError("externalDebugHttpPort", "int", "string"))); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void whenDomainsNamespacesPrimitiveType_reportError() throws Exception { |
| 241 | + setProperty("domainsNamespaces", true); |
| 242 | + |
| 243 | + assertThat(getProcessingError(), containsTypeError("domainsNamespaces", "map", "bool")); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + @Ignore("Does not validate that createDomainsNamespace nested value is boolean") |
| 248 | + public void whenDomainsNamespacesCreateNotBool_reportError() throws Exception { |
| 249 | + setProperty( |
| 250 | + "domainsNamespaces", |
| 251 | + ImmutableMap.of("aaa", ImmutableMap.of("createDomainsNamespace", 123))); |
| 252 | + |
| 253 | + assertThat(getProcessingError(), containsTypeError("", "bool", "int")); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void whenDefaultNamespaceHasCreatedTrue_reportError() throws Exception { |
| 258 | + setProperty( |
| 259 | + "domainsNamespaces", |
| 260 | + ImmutableMap.of("default", ImmutableMap.of("createDomainsNamespace", true))); |
| 261 | + |
| 262 | + assertThat( |
| 263 | + getProcessingError(), |
| 264 | + containsString( |
| 265 | + "The effective createDomainsNamespace value for the 'default' domainsNamespace must be set to false.")); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void whenDefaultNamespaceHasCreatedFalse_doNotReportError() throws Exception { |
| 270 | + setProperty( |
| 271 | + "domainsNamespaces", |
| 272 | + ImmutableMap.of("default", ImmutableMap.of("createDomainsNamespace", false))); |
| 273 | + |
| 274 | + assertThat(getProcessingError(), emptyString()); |
| 275 | + } |
| 276 | +} |
0 commit comments