Skip to content

Commit 1d51846

Browse files
authored
Filtering on validate (#852)
* Add filtering to model validation and persist post filtering changes * Add unit tests for filtering on validateModel and persistence post-filtering * Apply variable substitution if variable map file defined * Invoke filter after validation of merged model
1 parent d916ec3 commit 1d51846

File tree

6 files changed

+153
-8
lines changed

6 files changed

+153
-8
lines changed

core/src/main/python/validate.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2021, Oracle Corporation and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
The WLS Deploy tooling entry point for the validateModel tool.
@@ -22,6 +22,7 @@
2222
from wlsdeploy.aliases.wlst_modes import WlstModes
2323
from wlsdeploy.exception import exception_helper
2424
from wlsdeploy.logging.platform_logger import PlatformLogger
25+
from wlsdeploy.tool.util import filter_helper
2526
from wlsdeploy.tool.util import model_context_helper
2627
from wlsdeploy.tool.validate.validator import Validator
2728
from wlsdeploy.util import cla_helper
@@ -117,6 +118,18 @@ def __perform_model_file_validation(model_file_name, model_context):
117118

118119
model_validator.validate_in_standalone_mode(model_dictionary, variable_map,
119120
model_context.get_archive_file_name())
121+
122+
# substitute variables before filtering
123+
variables.substitute(model_dictionary, variable_map, model_context)
124+
# apply filters to merged model
125+
if filter_helper.apply_filters(model_dictionary, "validate"):
126+
# persist model after filtering
127+
cla_helper.persist_model(model_context, model_dictionary)
128+
129+
# validate model changes after filtering
130+
model_validator.validate_in_standalone_mode(model_dictionary, variable_map,
131+
model_context.get_archive_file_name())
132+
120133
except (TranslateException, VariableException), te:
121134
__logger.severe('WLSDPLY-20009', _program_name, model_file_name, te.getLocalizedMessage(),
122135
error=te, class_name=_class_name, method_name=_method_name)

core/src/main/python/wlsdeploy/util/cla_helper.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2019, 2021, Oracle Corporation and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
Utility CLS methods shared by multiple tools.
@@ -243,14 +243,12 @@ def load_model(program_name, model_context, aliases, filter_type, wlst_mode):
243243
clean_up_temp_files()
244244
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
245245

246+
filter_helper.apply_filters(model_dictionary, filter_type)
247+
246248
persist_model(model_context, model_dictionary)
247249

248250
validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode)
249251

250-
if filter_helper.apply_filters(model_dictionary, filter_type):
251-
# if any filters were applied, re-validate the model
252-
validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode)
253-
254252
return model_dictionary
255253

256254

core/src/test/python/validation_test.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2021, Oracle Corporation and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import unittest
@@ -18,7 +18,10 @@
1818
from wlsdeploy.aliases.wlst_modes import WlstModes
1919
from wlsdeploy.aliases import alias_constants
2020

21+
from validate import __perform_model_file_validation
22+
2123
import oracle.weblogic.deploy.util.TranslateException as TranslateException
24+
import oracle.weblogic.deploy.validate.ValidateException as ValidateException
2225

2326
from wlsdeploy.tool.create import wlsroles_helper
2427

@@ -27,6 +30,8 @@ class ValidationTestCase(unittest.TestCase):
2730
_program_name = 'validation_test'
2831
_class_name = 'ValidationTestCase'
2932
_resources_dir = '../../test-classes'
33+
# Model persistence file
34+
_wlsdeply_store_model = os.path.abspath(os.getcwd()) + '/' + _resources_dir + '/validate-mii-model.json'
3035
# _variable_file = _resources_dir + "/test_sub_variable_file.properties"
3136
# _model_file = _resources_dir + '/test_empty.json'
3237
# _variable_file = _resources_dir + "/test_invalid_variable_file.properties"
@@ -41,10 +46,21 @@ def setUp(self):
4146
self._summary_handler = SummaryHandler()
4247
self._logger.logger.addHandler(self._summary_handler)
4348

49+
# Define custom configuration path for WDT
50+
os.environ['WDT_CUSTOM_CONFIG'] = self._resources_dir
51+
# Indicate that WDT should persist model file
52+
os.environ['__WLSDEPLOY_STORE_MODEL__'] = self._wlsdeply_store_model
53+
4454
def tearDown(self):
4555
# remove summary handler for next test suite
4656
self._logger.logger.removeHandler(self._summary_handler)
4757

58+
# Clean up temporary WDT custom configuration environment variables
59+
# and model persistence files
60+
del os.environ['WDT_CUSTOM_CONFIG']
61+
del os.environ['__WLSDEPLOY_STORE_MODEL__']
62+
self.deleteFile(self._wlsdeply_store_model)
63+
4864
def testModelValidation(self):
4965
_method_name = 'testModelValidation'
5066

@@ -161,6 +177,44 @@ def testWLSRolesValidation(self):
161177
self.assertEqual(handler.getMessageCount(Level.SEVERE), 0)
162178
self.assertEqual(handler.getMessageCount(Level.WARNING), 3)
163179

180+
def testFilterInvokedOnModelValidation(self):
181+
"""
182+
Verify filter was run and changes are persisted to model file
183+
"""
184+
185+
# Setup model context arguments
186+
_model_file = self._resources_dir + '/simple-model.yaml'
187+
_archive_file = self._resources_dir + "/SingleAppDomain.zip"
188+
_method_name = 'testFilterInvokedOnModelValidation'
189+
190+
mw_home = os.environ['MW_HOME']
191+
192+
args_map = {
193+
'-oracle_home': mw_home,
194+
'-model_file': _model_file,
195+
'-archive_file': _archive_file
196+
}
197+
198+
model_context = ModelContext('validate', args_map)
199+
200+
try:
201+
# Invoke model validation
202+
__perform_model_file_validation(_model_file, model_context)
203+
204+
# read persisted model file and convert to python dictionary
205+
model_dictionary = FileToPython(self._wlsdeply_store_model, True)._parse_json()
206+
except ValidateException, ve:
207+
self._logger.severe('WLSDPLY-20000', self._program_name, ve.getLocalizedMessage(), error=ve,
208+
class_name=self._class_name, method_name=_method_name)
209+
210+
# assert the validate filter made modications and was persisted
211+
self.assertEquals('gumby1234', model_dictionary['domainInfo']['AdminPassword'], "Expected validate filter to have changed AdminPassword to 'gumby1234'")
212+
213+
def deleteFile(self, path):
214+
try:
215+
os.remove(path)
216+
except OSError:
217+
pass
164218

165219
if __name__ == '__main__':
166220
unittest.main()

core/src/test/python/wlsdeploy/util/cla_helper_test.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
"""
2-
Copyright (c) 2019, 2020, Oracle and/or its affiliates.
2+
Copyright (c) 2019, 2021, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
44
"""
5+
import os
56
import unittest
67

8+
from wlsdeploy.aliases.aliases import Aliases
9+
from wlsdeploy.aliases.wlst_modes import WlstModes
10+
from wlsdeploy.exception.expection_types import ExceptionType
11+
from wlsdeploy.util.model_context import ModelContext
712
from wlsdeploy.util import cla_helper
813

914

1015
class ClaHelperTest(unittest.TestCase):
1116

17+
_resources_dir = '../../test-classes'
18+
# Model persistence file
19+
_wlsdeply_store_model = os.path.abspath(os.getcwd()) + '/' + _resources_dir + '/validate-mii-model.json'
1220
properties_file = '../../test-classes/test.properties'
21+
22+
def setUp(self):
23+
# Define custom configuration path for WDT
24+
os.environ['WDT_CUSTOM_CONFIG'] = self._resources_dir
25+
# Indicate that WDT should persist model file
26+
os.environ['__WLSDEPLOY_STORE_MODEL__'] = self._wlsdeply_store_model
27+
28+
def tearDown(self):
29+
# Clean up temporary WDT custom configuration environment variables
30+
# and model persistence files
31+
del os.environ['WDT_CUSTOM_CONFIG']
32+
del os.environ['__WLSDEPLOY_STORE_MODEL__']
33+
deleteFile(self._wlsdeply_store_model)
34+
1335
# merging should combine elements with the same name (m1), add any elements only in the second model (m3),
1436
# and leave existing elements (m2) in place.
1537
def testMergeModels(self):
@@ -136,6 +158,33 @@ def testMergeNameToDeletedName(self):
136158
server = self._check_single_server(dictionary, 'm1')
137159
self.assertEquals(2, len(server), "server should have two attributes")
138160

161+
def testPersistModelAfterFilter(self):
162+
"""
163+
Verify filter was run and changes are persisted to model file
164+
"""
165+
# Setup model context arguments
166+
_model_file = self._resources_dir + '/simple-model.yaml'
167+
_archive_file = self._resources_dir + "/SingleAppDomain.zip"
168+
_method_name = 'testPersistModelAfterFilter'
169+
170+
mw_home = os.environ['MW_HOME']
171+
172+
args_map = {
173+
'-oracle_home': mw_home,
174+
'-model_file': _model_file,
175+
'-archive_file': _archive_file
176+
}
177+
178+
model_context = ModelContext('validate', args_map)
179+
180+
aliases = Aliases(model_context, wlst_mode=WlstModes.OFFLINE, exception_type=ExceptionType.DEPLOY)
181+
182+
# Load model and invoke filter
183+
model_dictionary = cla_helper.load_model('validateModel', model_context, aliases, "validate", WlstModes.OFFLINE)
184+
185+
# assert the validate filter made modications and was persisted
186+
self.assertEquals('gumby1234', model_dictionary['domainInfo']['AdminPassword'], "Expected validate filter to have changed AdminPassword to 'gumby1234'")
187+
139188
# check that a single server exists in the result, and its attributes were merged correctly
140189
def _check_merged_server(self, dictionary, key):
141190
server = self._check_single_server(dictionary, key)
@@ -190,3 +239,9 @@ def _build_variable_map():
190239
"server1a": "m1",
191240
"server1b": "m1"
192241
}
242+
243+
def deleteFile(path):
244+
try:
245+
os.remove(path)
246+
except OSError:
247+
pass
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"create": [
3+
],
4+
"deploy": [
5+
],
6+
"discover": [
7+
],
8+
"update": [
9+
],
10+
"validate": [
11+
{ "name": "validate_filter", "path": "../../test-classes/wdt_validate_filter.py" }
12+
]
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
#
4+
# ------------
5+
# Description:
6+
# ------------
7+
# This is a WDT filter for handling WLS configuration overrides.
8+
#
9+
10+
def filter_model(model):
11+
if 'domainInfo' in model:
12+
model['domainInfo']['AdminPassword'] = 'gumby1234'

0 commit comments

Comments
 (0)