def test_set_password_invalid_file(self): password.PASSWD_FILES = ['/tmp/bad_password_file'] try: password.set_password('test', 'test') assert False, 'Exception should have been raised' except Exception as e: self.assertEqual(str(e), '500: No password file found', 'Invalid message received for bad file')
def test_set_password_success(self): mock_popen = mock.Mock() mock_comm = mock.Mock() mock_comm.return_value = ('out', 'error') mock_popen.side_effect = [ mock.Mock(returncode=0, communicate=mock_comm) ] try: with mock.patch('novaagent.common.password.Popen', side_effect=mock_popen): password.set_password('test', 'test') except Exception: assert False, 'Exception should not have been raised'
def test_set_password_bad_return(self): mock_popen = mock.Mock() mock_comm = mock.Mock() mock_comm.return_value = ('out', 'error') mock_popen.side_effect = [ mock.Mock(returncode=999, communicate=mock_comm) ] try: with mock.patch('novaagent.common.password.Popen', side_effect=mock_popen): password.set_password('test', 'test') assert False, 'Exception should have been raised' except Exception as e: self.assertEqual( str(e), '500: Failed to change password for test: 999 : error', 'Invalid message received for failure on passwd cmd')
def test_set_password_change_password_success(self): self.setup_test_pw_file() original_line = ( 'testuser:$1$p9I3huSF$1acAVn1Kn.DWH1ycSknWR.:17352:0:99999:7:::') password.PASSWD_FILES = ['/tmp/passwd'] try: password.set_password('testuser', 'test') except Exception as e: assert False, ( 'Exception raised when should not have: {0}'.format(e)) files = glob.glob('/tmp/passwd*') self.assertEqual(len(files), 1, 'Invalid number of files found') with open('/tmp/passwd') as f: file_data = f.readlines() for line in file_data: if 'testuser:'******'Password was not updated and should have been')
def test_set_password_success_string(self): mock_popen = mock.Mock() mock_comm = mock.Mock() mock_comm.return_value = ('out', 'error') mock_stdin = mock.Mock() mock_stdin.side_effect = [None] mock_popen.side_effect = [ mock.Mock(returncode=0, communicate=mock_comm, stdin=mock_stdin) ] try: with mock.patch('novaagent.common.password.Popen', side_effect=mock_popen): returned = password.set_password('test', 'test') self.assertEqual(returned, None, 'Invalid return value on success') except Exception: assert False, 'Exception should not have been raised'