Add Voluptuous to validate the action parameters
We want a simplest way to validate the input parameters of an Action through a schema. APIImpact DocImpact Partially implements: blueprint watcher-add-actions-via-conf Change-Id: I139775f467fe7778c7354b0cfacf796fc27ffcb2
This commit is contained in:
committed by
Vincent Françoise
parent
33ee575936
commit
e3198d25a5
@@ -0,0 +1,54 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
# 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.
|
||||
#
|
||||
import voluptuous
|
||||
|
||||
from watcher.applier.actions import base as baction
|
||||
from watcher.applier.actions import change_nova_service_state
|
||||
from watcher.decision_engine.model import hypervisor_state as hstate
|
||||
from watcher.tests import base
|
||||
|
||||
|
||||
class TestChangeNovaServiceState(base.TestCase):
|
||||
def setUp(self):
|
||||
super(TestChangeNovaServiceState, self).setUp()
|
||||
self.a = change_nova_service_state.ChangeNovaServiceState()
|
||||
|
||||
def test_parameters_down(self):
|
||||
self.a.input_parameters = {
|
||||
baction.BaseAction.RESOURCE_ID: "compute-1",
|
||||
self.a.STATE: hstate.HypervisorState.OFFLINE.value}
|
||||
self.assertEqual(True, self.a.validate_parameters())
|
||||
|
||||
def test_parameters_up(self):
|
||||
self.a.input_parameters = {
|
||||
baction.BaseAction.RESOURCE_ID: "compute-1",
|
||||
self.a.STATE: hstate.HypervisorState.ONLINE.value}
|
||||
self.assertEqual(True, self.a.validate_parameters())
|
||||
|
||||
def test_parameters_exception_wrong_state(self):
|
||||
self.a.input_parameters = {
|
||||
baction.BaseAction.RESOURCE_ID: "compute-1",
|
||||
self.a.STATE: 'error'}
|
||||
self.assertRaises(voluptuous.Invalid, self.a.validate_parameters)
|
||||
|
||||
def test_parameters_resource_id_empty(self):
|
||||
self.a.input_parameters = {
|
||||
self.a.STATE: None}
|
||||
self.assertRaises(voluptuous.Invalid, self.a.validate_parameters)
|
||||
|
||||
def test_parameters_applies_add_extra(self):
|
||||
self.a.input_parameters = {"extra": "failed"}
|
||||
self.assertRaises(voluptuous.Invalid, self.a.validate_parameters)
|
||||
78
watcher/tests/applier/actions/test_migration.py
Normal file
78
watcher/tests/applier/actions/test_migration.py
Normal file
@@ -0,0 +1,78 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
# 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.
|
||||
#
|
||||
import voluptuous
|
||||
|
||||
from watcher.applier.actions import base as baction
|
||||
from watcher.applier.actions import migration
|
||||
from watcher.tests import base
|
||||
|
||||
|
||||
class TestMigration(base.TestCase):
|
||||
def setUp(self):
|
||||
super(TestMigration, self).setUp()
|
||||
self.mig = migration.Migrate()
|
||||
|
||||
def test_parameters(self):
|
||||
params = {baction.BaseAction.RESOURCE_ID:
|
||||
"45a37aeb-95ab-4ddb-a305-7d9f62c2f5ba",
|
||||
self.mig.MIGRATION_TYPE: 'live',
|
||||
self.mig.DST_HYPERVISOR: 'compute-2',
|
||||
self.mig.SRC_HYPERVISOR: 'compute3'}
|
||||
self.mig.input_parameters = params
|
||||
self.assertEqual(True, self.mig.validate_parameters())
|
||||
|
||||
def test_parameters_exception_resource_id(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID: "EFEF",
|
||||
'migration_type': 'live',
|
||||
'src_hypervisor': 'compute-2',
|
||||
'dst_hypervisor': 'compute3'}
|
||||
self.mig.input_parameters = parameters
|
||||
self.assertRaises(voluptuous.Invalid, self.mig.validate_parameters)
|
||||
|
||||
def test_parameters_exception_migration_type(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID:
|
||||
"45a37aeb-95ab-4ddb-a305-7d9f62c2f5ba",
|
||||
'migration_type': 'cold',
|
||||
'src_hypervisor': 'compute-2',
|
||||
'dst_hypervisor': 'compute3'}
|
||||
self.mig.input_parameters = parameters
|
||||
self.assertRaises(voluptuous.Invalid, self.mig.validate_parameters)
|
||||
|
||||
def test_parameters_exception_src_hypervisor(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID:
|
||||
"45a37aeb-95ab-4ddb-a305-7d9f62c2f5ba",
|
||||
'migration_type': 'cold',
|
||||
'src_hypervisor': None,
|
||||
'dst_hypervisor': 'compute3'}
|
||||
self.mig.input_parameters = parameters
|
||||
self.assertRaises(voluptuous.Invalid, self.mig.validate_parameters)
|
||||
|
||||
def test_parameters_exception_dst_hypervisor(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID:
|
||||
"45a37aeb-95ab-4ddb-a305-7d9f62c2f5ba",
|
||||
'migration_type': 'cold',
|
||||
'src_hypervisor': 'compute-1',
|
||||
'dst_hypervisor': None}
|
||||
self.mig.input_parameters = parameters
|
||||
self.assertRaises(voluptuous.Invalid, self.mig.validate_parameters)
|
||||
|
||||
def test_parameters_exception_empty_fields(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID: None,
|
||||
'migration_type': None,
|
||||
'src_hypervisor': None,
|
||||
'dst_hypervisor': None}
|
||||
self.mig.input_parameters = parameters
|
||||
self.assertRaises(voluptuous.Invalid, self.mig.validate_parameters)
|
||||
42
watcher/tests/applier/actions/test_sleep.py
Normal file
42
watcher/tests/applier/actions/test_sleep.py
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
# 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.
|
||||
#
|
||||
import voluptuous
|
||||
|
||||
from watcher.applier.actions import sleep
|
||||
from watcher.tests import base
|
||||
|
||||
|
||||
class TestSleep(base.TestCase):
|
||||
def setUp(self):
|
||||
super(TestSleep, self).setUp()
|
||||
self.s = sleep.Sleep()
|
||||
|
||||
def test_parameters_duration(self):
|
||||
self.s.input_parameters = {self.s.DURATION: 1.0}
|
||||
self.assertEqual(True, self.s.validate_parameters())
|
||||
|
||||
def test_parameters_duration_empty(self):
|
||||
self.s.input_parameters = {self.s.DURATION: None}
|
||||
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
|
||||
|
||||
def test_parameters_wrong_parameter(self):
|
||||
self.s.input_parameters = {self.s.DURATION: "ef"}
|
||||
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
|
||||
|
||||
def test_parameters_add_field(self):
|
||||
self.s.input_parameters = {self.s.DURATION: 1.0, "not_required": "nop"}
|
||||
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
|
||||
Reference in New Issue
Block a user