New na_ontap_volume option (#52587)

* Revert "changes to clusteR"

This reverts commit 33ee1b71e4bc8435fb315762a871f8c4cb6c5f80.

* Revert "Revert "changes to clusteR""

This reverts commit f1104a37b42886aebb4d2b2ab27c91c96d97858a.

* Update to volume

* fix documentation

* Fix doc
This commit is contained in:
Chris Archibald
2019-03-22 07:12:18 -07:00
committed by John R Barker
parent 36a790dcde
commit 1e6aa9533c
2 changed files with 999 additions and 97 deletions

View File

@@ -52,18 +52,30 @@ def fail_json(*args, **kwargs): # pylint: disable=unused-argument
class MockONTAPConnection(object):
''' mock server connection to ONTAP host '''
def __init__(self, kind=None, data=None):
def __init__(self, kind=None, data=None, job_error=None):
''' save arguments '''
self.kind = kind
self.params = data
self.xml_in = None
self.xml_out = None
self.job_error = job_error
def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
''' mock invoke_successfully returning xml data '''
self.xml_in = xml
if self.kind == 'volume':
xml = self.build_volume_info(self.params)
elif self.kind == 'job_info':
xml = self.build_job_info(self.job_error)
elif self.kind == 'error_modify':
xml = self.build_modify_error()
elif self.kind == 'failure_modify_async':
xml = self.build_failure_modify_async()
elif self.kind == 'success_modify_async':
xml = self.build_success_modify_async()
elif self.kind == 'zapi_error':
error = netapp_utils.zapi.NaApiError('test', 'error')
raise error
self.xml_out = xml
return xml
@@ -77,11 +89,62 @@ class MockONTAPConnection(object):
'volume-attributes': {
'volume-id-attributes': {
'containing-aggregate-name': vol_details['aggregate'],
'junction-path': vol_details['junction_path']
'junction-path': vol_details['junction_path'],
'style-extended': 'flexvol'
},
'volume-language-attributes': {
'language-code': 'en'
},
'volume-export-attributes': {
'policy': 'default'
},
'volume-performance-attributes': {
'is-atime-update-enabled': 'true'
},
'volume-state-attributes': {
'state': "online"
},
'volume-space-attributes': {
'space-guarantee': 'none',
'size': vol_details['size'],
'percentage-snapshot-reserve': vol_details['percent_snapshot_space']
},
'volume-snapshot-attributes': {
'snapshot-policy': vol_details['snapshot_policy']
},
'volume-security-attributes': {
'volume-security-unix-attributes': {
'permissions': vol_details['unix_permissions']
}
}
}
}
}
xml.translate_struct(attributes)
return xml
@staticmethod
def build_flex_group_info(vol_details):
''' build xml data for flexGroup volume-attributes '''
xml = netapp_utils.zapi.NaElement('xml')
attributes = {
'num-records': 1,
'attributes-list': {
'volume-attributes': {
'volume-id-attributes': {
'aggr-list': vol_details['aggregate'],
'junction-path': vol_details['junction_path'],
'style-extended': 'flexgroup'
},
'volume-language-attributes': {
'language-code': 'en'
},
'volume-export-attributes': {
'policy': 'default'
},
'volume-performance-attributes': {
'is-atime-update-enabled': 'true'
},
'volume-state-attributes': {
'state': "online"
},
@@ -103,6 +166,65 @@ class MockONTAPConnection(object):
xml.translate_struct(attributes)
return xml
@staticmethod
def build_job_info(error):
''' build xml data for a job '''
xml = netapp_utils.zapi.NaElement('xml')
attributes = netapp_utils.zapi.NaElement('attributes')
if error is None:
state = 'success'
elif error == 'time_out':
state = 'running'
elif error == 'failure':
state = 'failure'
else:
state = 'other'
attributes.add_node_with_children('job-info', **{
'job-state': state,
'job-progress': 'dummy',
'job-completion': error,
})
xml.add_child_elem(attributes)
xml.add_new_child('result-status', 'in_progress')
xml.add_new_child('result-jobid', '1234')
return xml
@staticmethod
def build_modify_error():
''' build xml data for modify error '''
xml = netapp_utils.zapi.NaElement('xml')
attributes = netapp_utils.zapi.NaElement('failure-list')
info_list_obj = netapp_utils.zapi.NaElement('volume-modify-iter-info')
info_list_obj.add_new_child('error-message', 'modify error message')
attributes.add_child_elem(info_list_obj)
xml.add_child_elem(attributes)
return xml
@staticmethod
def build_success_modify_async():
''' build xml data for success modify async '''
xml = netapp_utils.zapi.NaElement('xml')
attributes = netapp_utils.zapi.NaElement('success-list')
info_list_obj = netapp_utils.zapi.NaElement('volume-modify-iter-async-info')
info_list_obj.add_new_child('status', 'in_progress')
info_list_obj.add_new_child('jobid', '1234')
attributes.add_child_elem(info_list_obj)
xml.add_child_elem(attributes)
return xml
@staticmethod
def build_failure_modify_async():
''' build xml data for failure modify async '''
xml = netapp_utils.zapi.NaElement('xml')
attributes = netapp_utils.zapi.NaElement('failure-list')
info_list_obj = netapp_utils.zapi.NaElement('volume-modify-iter-async-info')
info_list_obj.add_new_child('status', 'failed')
info_list_obj.add_new_child('jobid', '1234')
info_list_obj.add_new_child('error-message', 'modify error message')
attributes.add_child_elem(info_list_obj)
xml.add_child_elem(attributes)
return xml
class TestMyModule(unittest.TestCase):
''' a group of related Unit Tests '''
@@ -118,40 +240,71 @@ class TestMyModule(unittest.TestCase):
'aggregate': 'test_aggr',
'junction_path': '/test',
'vserver': 'test_vserver',
'size': 20,
'size': 20971520,
'unix_permissions': '755',
'snapshot_policy': 'default'
'snapshot_policy': 'default',
'percent_snapshot_space': 60,
'language': 'en'
}
def mock_args(self):
return {
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'aggregate_name': self.mock_vol['aggregate'],
'space_guarantee': 'none',
'policy': 'default',
'is_online': True,
def mock_args(self, tag=None):
args = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'space_guarantee': 'none',
'policy': 'default',
'language': self.mock_vol['language'],
'is_online': True,
'unix_permissions': '---rwxr-xr-x',
'snapshot_policy': 'default'
'snapshot_policy': 'default',
'size': 20,
'size_unit': 'mb',
'junction_path': '/test',
'percent_snapshot_space': 60,
'type': 'type'
}
if tag is None:
args['aggregate_name'] = self.mock_vol['aggregate']
return args
def get_volume_mock_object(self, kind=None):
elif tag == 'flexGroup_manual':
args['aggr_list'] = 'aggr_0,aggr_1'
args['aggr_list_multiplier'] = 2
return args
elif tag == 'flexGroup_auto':
args['auto_provision_as'] = 'flexgroup'
return args
def get_volume_mock_object(self, kind=None, job_error=None):
"""
Helper method to return an na_ontap_volume object
:param kind: passes this param to MockONTAPConnection()
:param kind: passes this param to MockONTAPConnection().
:param job_error: error message when getting job status.
:return: na_ontap_volume object
"""
vol_obj = vol_module()
vol_obj.ems_log_event = Mock(return_value=None)
vol_obj.cluster = Mock()
vol_obj.cluster.invoke_successfully = Mock()
vol_obj.volume_style = None
if kind is None:
vol_obj.server = MockONTAPConnection()
else:
elif kind == 'volume':
vol_obj.server = MockONTAPConnection(kind='volume', data=self.mock_vol)
elif kind == 'job_info':
vol_obj.server = MockONTAPConnection(kind='job_info', data=self.mock_vol, job_error=job_error)
elif kind == 'error_modify':
vol_obj.server = MockONTAPConnection(kind='error_modify', data=self.mock_vol)
elif kind == 'failure_modify_async':
vol_obj.server = MockONTAPConnection(kind='failure_modify_async', data=self.mock_vol)
elif kind == 'success_modify_async':
vol_obj.server = MockONTAPConnection(kind='success_modify_async', data=self.mock_vol)
elif kind == 'zapi_error':
vol_obj.server = MockONTAPConnection(kind='zapi_error', data=self.mock_vol)
return vol_obj
def test_module_fail_when_required_args_missing(self):
@@ -236,6 +389,14 @@ class TestMyModule(unittest.TestCase):
self.get_volume_mock_object('volume').apply()
assert not exc.value.args[0]['changed']
def test_modify_error(self):
''' Test modify idempotency '''
data = self.mock_args()
set_module_args(data)
with pytest.raises(AnsibleFailJson) as exc:
self.get_volume_mock_object('error_modify').volume_modify_attributes()
assert exc.value.args[0]['msg'] == 'Error modifying volume test_vol: modify error message'
def test_mount_volume(self):
''' Test mount volume '''
data = self.mock_args()
@@ -263,15 +424,6 @@ class TestMyModule(unittest.TestCase):
self.get_volume_mock_object('volume').apply()
assert exc.value.args[0]['changed']
def test_successful_modify_state(self):
''' Test successful modify state '''
data = self.mock_args()
data['is_online'] = False
set_module_args(data)
with pytest.raises(AnsibleExitJson) as exc:
self.get_volume_mock_object('volume').apply()
assert exc.value.args[0]['changed']
def test_successful_modify_unix_permissions(self):
''' Test successful modify unix_permissions '''
data = self.mock_args()
@@ -290,6 +442,15 @@ class TestMyModule(unittest.TestCase):
self.get_volume_mock_object('volume').apply()
assert exc.value.args[0]['changed']
def test_successful_modify_percent_snapshot_space(self):
''' Test successful modify snapshot_policy '''
data = self.mock_args()
data['percent_snapshot_space'] = '90'
set_module_args(data)
with pytest.raises(AnsibleExitJson) as exc:
self.get_volume_mock_object('volume').apply()
assert exc.value.args[0]['changed']
def test_successful_move(self):
''' Test successful modify aggregate '''
data = self.mock_args()
@@ -299,6 +460,52 @@ class TestMyModule(unittest.TestCase):
self.get_volume_mock_object('volume').apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_rename(self, get_volume):
''' Test successful rename volume '''
data = self.mock_args()
data['from_name'] = self.mock_vol['name']
data['name'] = 'new_name'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
}
get_volume.side_effect = [
None,
current
]
with pytest.raises(AnsibleExitJson) as exc:
self.get_volume_mock_object().apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_rename_async(self, get_volume):
''' Test successful rename volume '''
data = self.mock_args()
data['from_name'] = self.mock_vol['name']
data['name'] = 'new_name'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'is_infinite': True
}
get_volume.side_effect = [
None,
current
]
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.change_volume_state')
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.volume_mount')
def test_modify_helper(self, mount_volume, change_state):
@@ -436,3 +643,268 @@ class TestMyModule(unittest.TestCase):
]
obj = self.get_volume_mock_object()
assert not obj.compare_chmod_value(current)
def test_successful_create_flex_group_manually(self):
''' Test successful create flexGroup manually '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 20
set_module_args(data)
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
def test_successful_create_flex_group_auto_provision(self):
''' Test successful create flexGroup auto provision '''
data = self.mock_args('flexGroup_auto')
data['time_out'] = 20
set_module_args(data)
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_delete_flex_group(self, get_volume):
''' Test successful delete felxGroup '''
data = self.mock_args('flexGroup_manual')
data['state'] = 'absent'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'unix_permissions': '755'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_resize_flex_group(self, get_volume):
''' Test successful reszie flexGroup '''
data = self.mock_args('flexGroup_manual')
data['size'] = 400
data['size_unit'] = 'mb'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'size': 20971520,
'unix_permissions': '755'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.check_job_status')
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_modify_unix_permissions_flex_group(self, get_volume, check_job_status):
''' Test successful modify unix permissions flexGroup '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 20
data['unix_permissions'] = '---rw-r-xr-x'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'unix_permissions': '777'
}
get_volume.side_effect = [
current
]
check_job_status.side_effect = [
None
]
obj = self.get_volume_mock_object('success_modify_async')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_modify_unix_permissions_flex_group_0_time_out(self, get_volume):
''' Test successful modify unix permissions flexGroup '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 0
data['unix_permissions'] = '---rw-r-xr-x'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'unix_permissions': '777'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('success_modify_async')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.check_job_status')
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_error_modify_unix_permissions_flex_group(self, get_volume, check_job_status):
''' Test error modify unix permissions flexGroup '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 20
data['unix_permissions'] = '---rw-r-xr-x'
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'unix_permissions': '777'
}
get_volume.side_effect = [
current
]
check_job_status.side_effect = ['error']
obj = self.get_volume_mock_object('success_modify_async')
with pytest.raises(AnsibleFailJson) as exc:
obj.apply()
assert exc.value.args[0]['msg'] == 'Error when modify volume: error'
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_failure_modify_unix_permissions_flex_group(self, get_volume):
''' Test failure modify unix permissions flexGroup '''
data = self.mock_args('flexGroup_manual')
data['unix_permissions'] = '---rw-r-xr-x'
data['time_out'] = 20
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexvol',
'unix_permissions': '777'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('failure_modify_async')
with pytest.raises(AnsibleFailJson) as exc:
obj.apply()
assert exc.value.args[0]['msg'] == 'Error modifying volume test_vol: modify error message'
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_offline_state_flex_group(self, get_volume):
''' Test successful offline flexGroup state '''
data = self.mock_args('flexGroup_manual')
data['is_online'] = False
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'is_online': True,
'junction_path': 'anything',
'unix_permissions': '755'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
@patch('ansible.modules.storage.netapp.na_ontap_volume.NetAppOntapVolume.get_volume')
def test_successful_online_state_flex_group(self, get_volume):
''' Test successful online flexGroup state '''
data = self.mock_args('flexGroup_manual')
set_module_args(data)
current = {
'hostname': 'test',
'username': 'test_user',
'password': 'test_pass!',
'name': self.mock_vol['name'],
'vserver': self.mock_vol['vserver'],
'style_extended': 'flexgroup',
'is_online': False,
'junction_path': 'anything',
'unix_permissions': '755'
}
get_volume.side_effect = [
current
]
obj = self.get_volume_mock_object('job_info')
with pytest.raises(AnsibleExitJson) as exc:
obj.apply()
assert exc.value.args[0]['changed']
def test_check_job_status_error(self):
''' Test check job status error '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 0
set_module_args(data)
obj = self.get_volume_mock_object('job_info', job_error='failure')
result = obj.check_job_status('123')
assert result == 'failure'
def test_check_job_status_time_out_is_0(self):
''' Test check job status time out is 0'''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 0
set_module_args(data)
obj = self.get_volume_mock_object('job_info', job_error='time_out')
result = obj.check_job_status('123')
assert result == 'job completion exceeded expected timer of: 0 seconds'
def test_check_job_status_unexpected(self):
''' Test check job status unexpected state '''
data = self.mock_args('flexGroup_manual')
data['time_out'] = 20
set_module_args(data)
obj = self.get_volume_mock_object('job_info', job_error='other')
with pytest.raises(AnsibleFailJson) as exc:
obj.check_job_status('123')
assert exc.value.args[0]['failed']
def test_error_assign_efficiency_policy(self):
data = self.mock_args()
data['efficiency_policy'] = 'test_policy'
set_module_args(data)
obj = self.get_volume_mock_object('zapi_error')
with pytest.raises(AnsibleFailJson) as exc:
obj.assign_efficiency_policy()
assert exc.value.args[0]['msg'] == 'Error enable efficiency on volume test_vol: NetApp API failed. Reason - test:error'
def test_error_assign_efficiency_policy_async(self):
data = self.mock_args()
data['efficiency_policy'] = 'test_policy'
set_module_args(data)
obj = self.get_volume_mock_object('zapi_error')
with pytest.raises(AnsibleFailJson) as exc:
obj.assign_efficiency_policy_async()
assert exc.value.args[0]['msg'] == 'Error enable efficiency on volume test_vol: NetApp API failed. Reason - test:error'