def test_should_reconfigure_enclosure(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.update_configuration.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = PARAMS_FOR_RECONFIGURED EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=ENCLOSURE_RECONFIGURED, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW) )
def test_should_fail_when_manager_bay_not_found(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] params_power_on_not_found_bay = yaml.load(self.PARAMS_FOR_MANAGER_BAY_UID_OFF) params_power_on_not_found_bay['data']['bayNumber'] = 3 self.mock_ansible_module.params = params_power_on_not_found_bay EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.fail_json.assert_called_once_with(msg=BAY_NOT_FOUND)
def test_should_fail_when_interconnect_bay_not_found(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] params_power_on_not_found_bay = yaml.load(self.PARAMS_FOR_INTERCONNECT_BAY_POWER_STATE_E_FUSE) params_power_on_not_found_bay['data']['bayNumber'] = 3 self.mock_ansible_module.params = params_power_on_not_found_bay EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.fail_json.assert_called_once_with(msg=BAY_NOT_FOUND)
def test_should_load_config_from_environment( self, mock_ansible_module, mock_ov_client_from_env_vars, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_env_vars.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock({'config': None}) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule() mock_ov_client_from_env_vars.assert_called_once() mock_ov_client_from_json_file.not_been_called()
def test_should_do_nothing_when_enclosure_not_exist( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_REFRESH) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=ENCLOSURE_NOT_FOUND)
def test_should_not_update_when_no_changes_by_name_key(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.get_all.return_value = ALL_ENCLOSURES self.mock_ansible_module.params = PARAMS_FOR_PRESENT_NO_HOSTNAME EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=ENCLOSURE_ALREADY_EXIST, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW) )
def test_should_refresh_enclosure(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.get.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = PARAMS_FOR_REFRESH EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=ENCLOSURE_REFRESHED )
def test_replace_name_for_new_enclosure(self): self.enclosures.get_by.return_value = [] self.enclosures.get_all.return_value = [] self.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW self.enclosures.patch.return_value = [] params_ansible = deepcopy(PARAMS_FOR_PRESENT) params_ansible['data']['name'] = 'Encl1-Renamed' self.mock_ansible_module.params = params_ansible EnclosureModule().run() self.enclosures.patch.assert_called_once_with( "/a/path", "replace", "/name", "Encl1-Renamed")
def test_should_fail_when_add_raises_exception( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [] mock_ov_instance.enclosures.add.side_effect = Exception(FAKE_MSG_ERROR) mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, EnclosureModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR)
def test_should_not_set_to_off_when_it_is_already_off(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.mock_ansible_module.params = yaml.load(self.PARAMS_FOR_UID_OFF) EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=UID_ALREADY_POWERED_OFF )
def test_should_not_set_state_when_it_is_already_on_desired_state(self): enclosure_uid_on = dict(ENCLOSURE_FROM_ONEVIEW, supportDataCollectionState='PendingCollection') self.enclosures.get_by.return_value = [enclosure_uid_on] self.mock_ansible_module.params = yaml.load(self.PARAMS) EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(enclosure=enclosure_uid_on), msg=SUPPORT_DATA_COLLECTION_STATE_ALREADY_SET )
def test_should_create_new_enclosure(self): self.enclosures.get_by.return_value = [] self.enclosures.get_all.return_value = [] self.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = PARAMS_FOR_PRESENT EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=ENCLOSURE_ADDED, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW) )
def test_should_not_update_when_no_changes_by_standby_ip_key(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.get_all.return_value = ALL_ENCLOSURES params = deepcopy(PARAMS_FOR_PRESENT) params['data']['hostname'] = STANDBY_IP_ADDRESS self.mock_ansible_module.params = params EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=ENCLOSURE_ALREADY_EXIST, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW) )
def test_replace_rack_name_for_new_enclosure( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [] mock_ov_instance.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW mock_ov_instance.enclosures.patch.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_WITH_NEW_RACK_NAME) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ov_instance.enclosures.patch.assert_called_once_with( "/a/path", "replace", "/rackName", "Another-Rack-Name")
def test_should_remove_enclosure(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [ ENCLOSURE_FROM_ONEVIEW ] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=ENCLOSURE_REMOVED)
def test_replace_name_for_new_enclosure(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [] mock_ov_instance.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW mock_ov_instance.enclosures.patch.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ov_instance.enclosures.patch.assert_called_once_with( "/a/path", "replace", "/name", "OneView-Enclosure")
def test_should_not_power_on_when_state_is_already_on(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] params_power_on_do_nothing = deepcopy(self.PARAMS_FOR_BAY_POWER_ON) params_power_on_do_nothing['data']['bayNumber'] = 1 self.mock_ansible_module.params = params_power_on_do_nothing EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=APPLIANCE_BAY_ALREADY_POWERED_ON )
def test_should_perform_an_e_fuse(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = yaml.load(self.PARAMS_FOR_DEVICE_BAY_POWER_STATE_E_FUSE) EnclosureModule().run() self.enclosures.patch.assert_called_once_with( ENCLOSURE_FROM_ONEVIEW['uri'], operation='replace', path='/deviceBays/1/bayPowerState', value='E-Fuse') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=DEVICE_BAY_POWER_STATE_E_FUSED )
def test_should_reset(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = yaml.load(self.PARAMS_FOR_MANAGER_BAY_POWER_STATE_RESET) EnclosureModule().run() self.enclosures.patch.assert_called_once_with( ENCLOSURE_FROM_ONEVIEW['uri'], operation='replace', path='/managerBays/1/bayPowerState', value='Reset') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=MANAGER_BAY_POWER_STATE_RESET )
def test_should_remove_ipv4(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = yaml.load(self.PARAMS_FOR_DEVICE_BAY_IPV4_RELEASE) EnclosureModule().run() self.enclosures.patch.assert_called_once_with( ENCLOSURE_FROM_ONEVIEW['uri'], operation='remove', path='/interconnectBays/1/ipv4Setting', value='') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=INTERCONNECT_BAY_IPV4_SETTING_REMOVED )
def test_should_turn_on_uid(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = yaml.load(self.PARAMS_FOR_UID_ON) EnclosureModule().run() self.enclosures.patch.assert_called_once_with( ENCLOSURE_FROM_ONEVIEW['uri'], operation='replace', path='/uidState', value='On') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=UID_POWERED_ON )
def test_should_set_state(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = yaml.load(self.PARAMS) EnclosureModule().run() self.enclosures.patch.assert_called_once_with(ENCLOSURE_FROM_ONEVIEW['uri'], operation='replace', path='/supportDataCollectionState', value='PendingCollection') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=SUPPORT_DATA_COLLECTION_STATE_SET )
def test_should_power_on_appliance_bays(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW self.mock_ansible_module.params = self.PARAMS_FOR_BAY_POWER_ON EnclosureModule().run() self.enclosures.patch.assert_called_once_with( ENCLOSURE_FROM_ONEVIEW['uri'], operation='replace', path='/applianceBays/2/power', value='On') self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=APPLIANCE_BAY_POWERED_ON )
def test_should_fail_when_patch_raises_exception( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [ ENCLOSURE_FROM_ONEVIEW ] mock_ov_instance.enclosures.patch.side_effect = Exception( FAKE_MSG_ERROR) mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_WITH_NEW_NAME) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, EnclosureModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR)
def test_should_not_update_when_data_is_equals( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [ ENCLOSURE_FROM_ONEVIEW ] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=ENCLOSURE_ALREADY_EXIST, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW))
def test_update_when_data_has_new_rack_name(self): updated_data = ENCLOSURE_FROM_ONEVIEW.copy() updated_data['rackName'] = 'Another-Rack-Name' self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] self.enclosures.get_all.return_value = ALL_ENCLOSURES self.enclosures.patch.return_value = updated_data self.mock_ansible_module.params = PARAMS_WITH_NEW_RACK_NAME EnclosureModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=ENCLOSURE_UPDATED, ansible_facts=dict(enclosure=updated_data) )
def test_replace_rack_name_for_new_enclosure(self): updated_data = ENCLOSURE_FROM_ONEVIEW.copy() updated_data['rackName'] = 'Another-Rack-Name' self.enclosures.get_by.return_value = [] self.enclosures.get_all.return_value = [] self.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW self.enclosures.patch.return_value = [] params_ansible = deepcopy(PARAMS_FOR_PRESENT) params_ansible['data']['rackName'] = 'Another-Rack-Name' self.mock_ansible_module.params = params_ansible EnclosureModule().run() self.enclosures.patch.assert_called_once_with( "/a/path", "replace", "/rackName", "Another-Rack-Name")
def test_should_not_set_to_off_when_state_already_off(self): self.enclosures.get_by.return_value = [ENCLOSURE_FROM_ONEVIEW] params_manager_bay_uid = yaml.load(self.PARAMS_FOR_MANAGER_BAY_UID_OFF) params_manager_bay_uid['data']['bayNumber'] = '2' self.mock_ansible_module.params = params_manager_bay_uid EnclosureModule().run() self.enclosures.patch.not_been_called() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW), msg=MANAGER_BAY_UID_ALREADY_OFF )
def test_update_calibrated_max_power_for_existent_enclosure( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [ ENCLOSURE_FROM_ONEVIEW ] mock_ov_instance.enclosures.patch.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock( PARAMS_WITH_CALIBRATED_MAX_POWER) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ov_instance.enclosures.update_environmental_configuration.assert_called_once_with( "/a/path", {"calibratedMaxPower": 1750})
def test_should_create_new_enclosure(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.enclosures.get_by.return_value = [] mock_ov_instance.enclosures.add.return_value = ENCLOSURE_FROM_ONEVIEW mock_ov_instance.enclosures.patch.return_value = ENCLOSURE_FROM_ONEVIEW mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance EnclosureModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=ENCLOSURE_ADDED, ansible_facts=dict(enclosure=ENCLOSURE_FROM_ONEVIEW))