def test_create_flexcache_size_error(self): ''' create flexcache ''' args = { 'volume': 'volume_err', 'size': '50', # 80MB minimum 'size_unit': 'mb', # 80MB minimum 'vserver': 'ansibleSVM', 'aggr_list': 'aggr1', 'origin_volume': 'fc_vol_origin', 'origin_vserver': 'ansibleSVM', } args.update(self.args) set_module_args(args) my_obj = my_module() error = 'Size "50MB" ("52428800B") is too small. Minimum size is "80MB" ("83886080B"). ' if not self.onbox: my_obj.server = MockONTAPConnection(job_error=error) with patch.object(my_module, 'flexcache_create', wraps=my_obj.flexcache_create) as mock_create: with pytest.raises(AnsibleFailJson) as exc: my_obj.apply() print('Create: ' + repr(exc.value)) msg = 'Error when creating flexcache: %s' % error assert exc.value.args[0]['msg'] == msg mock_create.assert_called_with()
def test_create_flexcache_time_out(self, mock_sleep): ''' create flexcache ''' args = { 'volume': 'volume_err', 'size': '50', # 80MB minimum 'size_unit': 'mb', # 80MB minimum 'vserver': 'ansibleSVM', 'aggr_list': 'aggr1', 'origin_volume': 'fc_vol_origin', 'origin_vserver': 'ansibleSVM', 'time_out': '2' } args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection(job_error='time_out') with patch.object(my_module, 'flexcache_create', wraps=my_obj.flexcache_create) as mock_create: with pytest.raises(AnsibleFailJson) as exc: my_obj.apply() print('Create: ' + repr(exc.value)) msg = 'Error when creating flexcache: job completion exceeded expected timer of: %s seconds' \ % args['time_out'] assert exc.value.args[0]['msg'] == msg mock_create.assert_called_with()
def test_delete_flexcache_exists_junctionpath_with_force(self): ''' delete flexcache ''' args = { 'volume': 'volume', 'vserver': 'ansibleSVM', 'junction_path': 'jpath', 'state': 'absent', 'force_offline': 'true', 'force_unmount': 'true' } self.create_flexcache(args['vserver'], args['volume'], args['junction_path']) args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection('vserver', 'flex') with patch.object(my_module, 'flexcache_delete', wraps=my_obj.flexcache_delete) as mock_delete: with pytest.raises(AnsibleExitJson) as exc: my_obj.apply() print('Delete: ' + repr(exc.value)) assert exc.value.args[0]['changed'] mock_delete.assert_called_with()
def test_create_flexcache_multiplier(self): ''' create flexcache with aggregate multiplier''' args = { 'volume': 'volume', 'size': '90', # 80MB minimum 'size_unit': 'mb', # 80MB minimum 'vserver': 'ansibleSVM', 'aggr_list': 'aggr1', 'origin_volume': 'fc_vol_origin', 'origin_vserver': 'ansibleSVM', 'aggr_list_multiplier': '2', } self.delete_flexcache(args['vserver'], args['volume']) args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection() with patch.object(my_module, 'flexcache_create', wraps=my_obj.flexcache_create) as mock_create: with pytest.raises(AnsibleExitJson) as exc: my_obj.apply() print('Create: ' + repr(exc.value)) assert exc.value.args[0]['changed'] mock_create.assert_called_with()
def test_delete_flexcache_exists_junctionpath_no_force(self): ''' delete flexcache ''' args = { 'volume': 'volume', 'vserver': 'ansibleSVM', 'junction_path': 'jpath', 'state': 'absent', 'force_offline': 'true' } self.create_flexcache(args['vserver'], args['volume'], args['junction_path']) args.update(self.args) set_module_args(args) my_obj = my_module() error = '160:Volume volume on Vserver ansibleSVM must be unmounted before being taken offline or restricted.' if not self.onbox: my_obj.server = MockONTAPConnection('vserver', 'flex', api_error=error) with patch.object(my_module, 'flexcache_delete', wraps=my_obj.flexcache_delete) as mock_delete: with pytest.raises(AnsibleFailJson) as exc: my_obj.apply() print('Delete: ' + repr(exc.value)) msg = 'Error deleting FlexCache : NetApp API failed. Reason - %s' % error assert exc.value.args[0]['msg'] == msg mock_delete.assert_called_with()
def test_delete_flexcache_not_exist(self): ''' delete flexcache ''' args = {'volume': 'volume', 'vserver': 'ansibleSVM', 'state': 'absent'} args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection() with pytest.raises(AnsibleExitJson) as exc: my_obj.apply() print('Delete: ' + repr(exc.value)) assert exc.value.args[0]['changed'] is False
def test_get_flexcache(self): ''' get flexcache info ''' args = { 'vserver': 'ansibleSVM', 'origin_volume': 'origin_volume', 'volume': 'volume' } args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection('vserver') info = my_obj.flexcache_get() print('info: ' + repr(info))
def test_missing_parameters(self): ''' fail if origin volume and origin verser are missing ''' args = {'vserver': 'vserver', 'volume': 'volume'} args.update(self.dummy_args) set_module_args(args) my_obj = my_module() my_obj.server = self.server with pytest.raises(AnsibleFailJson) as exc: # It may not be a good idea to start with apply # More atomic methods can be easier to mock # Hint: start with get methods, as they are called first my_obj.apply() msg = 'Missing parameters: origin_volume, origin_vserver' assert exc.value.args[0]['msg'] == msg
def test_missing_parameter(self): ''' fail if origin verser parameter is missing ''' args = { 'vserver': 'vserver', 'origin_volume': 'origin_volume', 'volume': 'volume' } args.update(self.dummy_args) set_module_args(args) my_obj = my_module() my_obj.server = self.server with pytest.raises(AnsibleFailJson) as exc: my_obj.apply() msg = 'Missing parameter: origin_vserver' assert exc.value.args[0]['msg'] == msg
def test_get_flexcache_double(self): ''' get flexcache info returns 2 entries! ''' args = { 'vserver': 'ansibleSVM', 'origin_volume': 'origin_volume', 'volume': 'volume' } args.update(self.dummy_args) set_module_args(args) my_obj = my_module() my_obj.server = MockONTAPConnection('vserver', 'repeats') with pytest.raises(AnsibleFailJson) as exc: my_obj.flexcache_get() msg = 'Error fetching FlexCache info: Multiple records found for %s:' % args[ 'volume'] assert exc.value.args[0]['msg'] == msg
def test_create_flexcache_autoprovision_idempotent(self): ''' create flexcache with autoprovision - already exists ''' args = { 'volume': 'volume', 'vserver': 'ansibleSVM', 'origin_volume': 'fc_vol_origin', 'origin_vserver': 'ansibleSVM', 'auto_provision_as': 'flexgroup', } args.update(self.args) set_module_args(args) my_obj = my_module() if not self.onbox: my_obj.server = MockONTAPConnection('vserver') with pytest.raises(AnsibleExitJson) as exc: my_obj.apply() print('Create: ' + repr(exc.value)) assert exc.value.args[0]['changed'] is False
def delete_flexcache(self, vserver, volume): ''' delete flexcache ''' if not self.onbox: return args = { 'volume': volume, 'vserver': vserver, 'state': 'absent', 'force_offline': 'true' } args.update(self.args) set_module_args(args) my_obj = my_module() try: my_obj.apply() except AnsibleExitJson as exc: print('Delete util: ' + repr(exc)) except AnsibleFailJson as exc: print('Delete util: ' + repr(exc))
def test_delete_flexcache_exists_no_force(self): ''' delete flexcache ''' args = {'volume': 'volume', 'vserver': 'ansibleSVM', 'state': 'absent'} args.update(self.args) set_module_args(args) my_obj = my_module() error = '13001:Volume volume in Vserver ansibleSVM must be offline to be deleted. ' \ 'Use "volume offline -vserver ansibleSVM -volume volume" command to offline ' \ 'the volume' if not self.onbox: my_obj.server = MockONTAPConnection('vserver', 'flex', api_error=error) with patch.object(my_module, 'flexcache_delete', wraps=my_obj.flexcache_delete) as mock_delete: with pytest.raises(AnsibleFailJson) as exc: my_obj.apply() print('Delete: ' + repr(exc.value)) msg = 'Error deleting FlexCache : NetApp API failed. Reason - %s' % error assert exc.value.args[0]['msg'] == msg mock_delete.assert_called_with()
def create_flexcache(self, vserver, volume, junction_path): ''' create flexcache ''' if not self.onbox: return args = { 'state': 'present', 'volume': volume, 'size': '90', # 80MB minimum 'size_unit': 'mb', # 80MB minimum 'vserver': vserver, 'aggr_list': 'aggr1', 'origin_volume': 'fc_vol_origin', 'origin_vserver': 'ansibleSVM', 'junction_path': junction_path, } args.update(self.args) set_module_args(args) my_obj = my_module() try: my_obj.apply() except AnsibleExitJson as exc: print('Create util: ' + repr(exc)) except AnsibleFailJson as exc: print('Create util: ' + repr(exc))
def test_module_fail_when_required_args_missing(self): ''' required arguments are reported as errors ''' with pytest.raises(AnsibleFailJson) as exc: set_module_args({}) my_module() print('Info: %s' % exc.value.args[0]['msg'])