def testDownloadAuthUrl(self, mock_urlopen, mock_request, mock_tempfile): auth_url = 'https://storage.googleapis.com/fake/url' mock_tempfile.return_value = mock_tempfile mock_tempfile.name = self.dest self.retriever.token = 'bar' mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open): self.assertEqual( self.retriever._DownloadAuthUrl(auth_url, self.dest_dir), self.dest) mock_tempfile.assert_called_once_with(dir=self.dest_dir, delete=False) mock_tempfile.close.assert_called_once_with() self.mock_logger.info.assert_called_once_with(mock.ANY, auth_url, self.dest) mock_request.assert_called_with(auth_url) mocked_request = mock_request() mocked_request.add_unredirected_header.assert_called_with( 'Authorization', 'bar') mock_urlopen.assert_called_with(mocked_request) urlopen_read = mock_urlopen().read(return_value='foo') self.mock_logger.warning.assert_not_called() mock_open.assert_called_once_with(self.dest, 'w') handle = mock_open() handle.write.assert_called_once_with(urlopen_read)
def testDisableNetworkManager(self, mock_exists): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(mock_open, 'open') mocks.attach_mock(self.mock_logger, 'logger') mocks.attach_mock(self.mock_setup._ModifyInterface, 'modify') mock_exists.side_effect = [True, False] with mock.patch('%s.open' % builtin, mock_open, create=False): utils.Utils._DisableNetworkManager(self.mock_setup, ['eth0', 'eth1'], self.mock_logger) expected_calls = [ mock.call.exists('/etc/sysconfig/network-scripts/ifcfg-eth0'), mock.call.modify(mock.ANY, 'DEVICE', 'eth0', replace=False), mock.call.modify(mock.ANY, 'NM_CONTROLLED', 'no', replace=True), mock.call.exists('/etc/sysconfig/network-scripts/ifcfg-eth1'), mock.call.open('/etc/sysconfig/network-scripts/ifcfg-eth1', 'w'), mock.call.open().__enter__(), mock.call.open().write(mock.ANY), mock.call.open().__exit__(None, None, None), mock.call.logger.info(mock.ANY, 'eth1'), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testDownloadAuthUrl(self, mock_urlopen, mock_request, mock_tempfile): auth_url = 'https://storage.googleapis.com/fake/url' mock_tempfile.return_value = mock_tempfile mock_tempfile.name = self.dest self.retriever.token = 'bar' mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open): self.assertEqual( self.retriever._DownloadAuthUrl(auth_url, self.dest_dir), self.dest) mock_tempfile.assert_called_once_with(dir=self.dest_dir, delete=False) mock_tempfile.close.assert_called_once_with() self.mock_logger.info.assert_called_once_with( mock.ANY, auth_url, self.dest) mock_request.assert_called_with(auth_url) mocked_request = mock_request() mocked_request.add_unredirected_header.assert_called_with( 'Authorization', 'bar') mock_urlopen.assert_called_with(mocked_request) urlopen_read = mock_urlopen().read(return_value=b'foo').decode() self.mock_logger.warning.assert_not_called() mock_open.assert_called_once_with(self.dest, 'w') handle = mock_open() handle.write.assert_called_once_with(urlopen_read)
def testWriteConfigNoHeader(self, mock_lock): self.mock_config_manager = config_manager.ConfigManager( config_file='/tmp/file.cfg') mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open, create=False): self.mock_config_manager.WriteConfig() mock_open().write.assert_not_called() mock_lock.LockFile.assert_called_once_with('/var/lock/google_file.lock')
def testGetConfiguredUsers(self, mock_exists): mock_open = mock.mock_open() mock_exists.return_value = True with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().readlines.return_value = ['a\n', 'b\n', 'c\n', '\n'] self.assertEqual( accounts_utils.AccountsUtils.GetConfiguredUsers( self.mock_utils), ['a', 'b', 'c', ''])
def testIsEnabledError(self): mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.side_effect = [OSError('OSError')] self.assertEqual(self.mock_utils.IsEnabled('a'), False) self.mock_logger.warning.assert_called_once_with( mock.ANY, 'a', 'OSError'),
def testGetConfiguredUsers(self, mock_exists): mock_open = mock.mock_open() mock_exists.return_value = True with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().readlines.return_value = ['a\n', 'b\n', 'c\n', '\n'] self.assertEqual( accounts_utils.AccountsUtils.GetConfiguredUsers(self.mock_utils), ['a', 'b', 'c', ''])
def testUpdateAuthorizedKeys(self, mock_islink, mock_exists, mock_tempfile, mock_copy, mock_permissions): mock_open = mock.mock_open() user = '******' ssh_keys = ['Google key 1', 'Google key 2'] temp_dest = '/tmp/dest' pw_uid = 1 pw_gid = 2 pw_dir = '/home' ssh_dir = '/home/.ssh' authorized_keys_file = '/home/.ssh/authorized_keys' pw_entry = accounts_utils.pwd.struct_passwd( ('', '', pw_uid, pw_gid, '', pw_dir, '')) self.mock_utils._GetUser.return_value = pw_entry mock_islink.return_value = False mock_exists.return_value = True mock_tempfile.return_value = mock_tempfile mock_tempfile.__enter__.return_value.name = temp_dest self.mock_logger.name = 'test' with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().readlines.return_value = [ 'User key a\n', 'User key b\n', '\n', self.mock_utils.google_comment + '\n', 'Google key a\n', self.mock_utils.google_comment + '\n', 'Google key b\n', 'User key c\n', ] accounts_utils.AccountsUtils._UpdateAuthorizedKeys( self.mock_utils, user, ssh_keys) expected_calls = [ mock.call(mode='w', prefix='test-', delete=True), mock.call.__enter__(), mock.call.__enter__().write('User key a\n'), mock.call.__enter__().write('User key b\n'), mock.call.__enter__().write('\n'), mock.call.__enter__().write('User key c\n'), mock.call.__enter__().write(self.mock_utils.google_comment + '\n'), mock.call.__enter__().write('Google key 1\n'), mock.call.__enter__().write(self.mock_utils.google_comment + '\n'), mock.call.__enter__().write('Google key 2\n'), mock.call.__enter__().flush(), mock.call.__exit__(None, None, None), ] self.assertEqual(mock_tempfile.mock_calls, expected_calls) mock_copy.assert_called_once_with(temp_dest, authorized_keys_file) expected_calls = [ mock.call(ssh_dir, mode=0o700, uid=pw_uid, gid=pw_gid, mkdir=True), mock.call(authorized_keys_file, mode=0o600, uid=pw_uid, gid=pw_gid), ] self.assertEqual(mock_permissions.mock_calls, expected_calls) self.mock_logger.warning.assert_not_called()
def testUpdateAuthorizedKeys( self, mock_islink, mock_exists, mock_tempfile, mock_copy, mock_permissions): mock_open = mock.mock_open() user = '******' ssh_keys = ['Google key 1', 'Google key 2'] temp_dest = '/tmp/dest' pw_uid = 1 pw_gid = 2 pw_dir = '/home' ssh_dir = '/home/.ssh' authorized_keys_file = '/home/.ssh/authorized_keys' pw_entry = accounts_utils.pwd.struct_passwd( ('', '', pw_uid, pw_gid, '', pw_dir, '')) self.mock_utils._GetUser.return_value = pw_entry mock_islink.return_value = False mock_exists.return_value = True mock_tempfile.return_value = mock_tempfile mock_tempfile.__enter__.return_value.name = temp_dest self.mock_logger.name = 'test' with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().readlines.return_value = [ 'User key a\n', 'User key b\n', '\n', self.mock_utils.google_comment + '\n', 'Google key a\n', self.mock_utils.google_comment + '\n', 'Google key b\n', 'User key c\n', ] accounts_utils.AccountsUtils._UpdateAuthorizedKeys( self.mock_utils, user, ssh_keys) expected_calls = [ mock.call(mode='w', prefix='test-', delete=True), mock.call.__enter__(), mock.call.__enter__().write('User key a\n'), mock.call.__enter__().write('User key b\n'), mock.call.__enter__().write('\n'), mock.call.__enter__().write('User key c\n'), mock.call.__enter__().write(self.mock_utils.google_comment + '\n'), mock.call.__enter__().write('Google key 1\n'), mock.call.__enter__().write(self.mock_utils.google_comment + '\n'), mock.call.__enter__().write('Google key 2\n'), mock.call.__enter__().flush(), mock.call.__exit__(None, None, None), ] self.assertEqual(mock_tempfile.mock_calls, expected_calls) mock_copy.assert_called_once_with(temp_dest, authorized_keys_file) expected_calls = [ mock.call(ssh_dir, mode=0o700, uid=pw_uid, gid=pw_gid, mkdir=True), mock.call(authorized_keys_file, mode=0o600, uid=pw_uid, gid=pw_gid), ] self.assertEqual(mock_permissions.mock_calls, expected_calls) self.mock_logger.warning.assert_not_called()
def testWriteConfig(self): mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open, create=False): self.mock_config_manager.WriteConfig() expected_calls = [ mock.call('# %s' % self.config_header), mock.call('\n\n'), ] self.assertEqual(mock_open().write.mock_calls, expected_calls)
def testIsEnabled(self): mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.side_effect = ['up', 'down', 'up\n', '', 'Garbage'] self.assertEqual(self.mock_utils.IsEnabled('a'), True) self.assertEqual(self.mock_utils.IsEnabled('a'), False) self.assertEqual(self.mock_utils.IsEnabled('a'), True) self.assertEqual(self.mock_utils.IsEnabled('a'), False) self.assertEqual(self.mock_utils.IsEnabled('a'), False)
def testWriteConfigLocked(self, mock_lock): ioerror = IOError('Test Error') mock_lock.LockFile.side_effect = ioerror mock_open = mock.mock_open() with mock.patch('%s.open' % builtin, mock_open, create=False): with self.assertRaises(IOError) as error: self.mock_config_manager.WriteConfig() self.assertEqual(error.exception, ioerror) mock_open().write.assert_not_called() mock_lock.LockFile.assert_called_once_with('/var/lock/google_test.lock')
def testCreateInterfaceMapSysfsError(self, mock_listdir): mock_open = mock.mock_open() mock_listdir.return_value = ['a', 'b', 'c'] with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.side_effect = [ '1', OSError('OSError'), IOError('IOError')] self.assertEqual(self.mock_utils._CreateInterfaceMap(), {'1': 'a'}) expected_calls = [ mock.call.warning(mock.ANY, 'b', 'OSError'), mock.call.warning(mock.ANY, 'c', 'IOError'), ] self.assertEqual(self.mock_logger.mock_calls, expected_calls)
def testCreateInterfaceMapError(self, mock_listdir): mock_open = mock.mock_open() mock_listdir.return_value = ['a', 'b', 'c'] with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.side_effect = [ '1', OSError('OSError'), IOError('IOError')] self.assertEqual(self.mock_utils._CreateInterfaceMap(), {'1': 'a'}) expected_calls = [ mock.call.warning(mock.ANY, 'b', 'OSError'), mock.call.warning(mock.ANY, 'c', 'IOError'), ] self.assertEqual(self.mock_logger.mock_calls, expected_calls)
def testCreateInterfaceMap(self, mock_listdir): mock_open = mock.mock_open() interface_map = { '1': 'a', '2': 'b', '3': 'c', } mock_listdir.return_value = interface_map.values() with mock.patch('%s.open' % builtin, mock_open, create=False): addresses = interface_map.keys() addresses = ['%s\n' % address for address in addresses] mock_open().read.side_effect = interface_map.keys() self.assertEqual(self.mock_utils._CreateInterfaceMap(), interface_map)
def testCreateInterfaceMapSysfs(self, mock_listdir): mock_open = mock.mock_open() interface_map = { '1': 'a', '2': 'b', '3': 'c', } mock_listdir.return_value = interface_map.values() with mock.patch('%s.open' % builtin, mock_open, create=False): addresses = interface_map.keys() addresses = ['%s\n' % address for address in addresses] mock_open().read.side_effect = interface_map.keys() self.assertEqual(self.mock_utils._CreateInterfaceMap(), interface_map)
def testCreateSudoersGroupWriteError(self, mock_exists): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = True mock_exists.return_value = False mock_open.side_effect = IOError() accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) expected_calls = [ mock.call.group(self.sudoers_group), mock.call.exists(self.sudoers_file), mock.call.logger.error(mock.ANY, self.sudoers_file, mock.ANY), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testCreateSudoersGroupSkip(self, mock_call, mock_permissions): mock_open = mock.mock_open(read_data="%%%s ALL=(ALL:ALL) NOPASSWD:ALL" % self.sudoers_group) mock_open.return_value.__iter__ = lambda self: iter(self.readline, '') mocks = mock.Mock() mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = True with mock.patch('%s.open' % builtin, mock_open, create=False): accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) mock_open().write.assert_not_called() expected_calls = [ mock.call.group(self.sudoers_group), mock.call.permissions(self.sudoers_file, mode=0o440, uid=0, gid=0), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testWriteIfcfg(self): mocks = mock.Mock() mock_open = mock.mock_open() mocks.attach_mock(mock_open, 'open') with mock.patch('%s.open' % builtin, mock_open, create=False): utils.Utils._WriteIfcfg( self.mock_setup, ['eth1', 'eth2'], self.mock_logger) expected_calls = [ mock.call.open('/etc/sysconfig/network/ifcfg-eth1', 'w'), mock.call.open().__enter__(), mock.call.open().write(mock.ANY), mock.call.open().__exit__(None, None, None), mock.call.open('/etc/sysconfig/network/ifcfg-eth2', 'w'), mock.call.open().__enter__(), mock.call.open().write(mock.ANY), mock.call.open().__exit__(None, None, None), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testGenerateSshKey(self, mock_tempfile, mock_call, mock_move, mock_permissions): mocks = mock.Mock() mocks.attach_mock(mock_tempfile, 'tempfile') mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_move, 'move') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_logger, 'logger') key_type = 'key-type' key_dest = '/key/dest' temp_dest = '/tmp/dest' mock_tempfile.return_value = mock_tempfile mock_tempfile.__enter__.return_value.name = temp_dest mock_open = mock.mock_open() key_file_contents = 'ssh-rsa asdfasdf' expected_key_data = ('ssh-rsa', 'asdfasdf') with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.return_value = key_file_contents key_data = instance_setup.InstanceSetup._GenerateSshKey( self.mock_setup, key_type, key_dest) expected_calls = [ mock.call.tempfile(prefix=key_type, delete=True), mock.call.tempfile.__enter__(), mock.call.tempfile.__exit__(None, None, None), mock.call.logger.info(mock.ANY, key_dest), mock.call.call([ 'ssh-keygen', '-t', key_type, '-f', temp_dest, '-N', '', '-q' ]), mock.call.move(temp_dest, key_dest), mock.call.move('%s.pub' % temp_dest, '%s.pub' % key_dest), mock.call.permissions(key_dest, mode=0o600), mock.call.permissions('%s.pub' % key_dest, mode=0o644), ] self.assertEqual(mocks.mock_calls, expected_calls) self.assertEqual(key_data, expected_key_data) mock_open().read.return_value = '' key_data = instance_setup.InstanceSetup._GenerateSshKey( self.mock_setup, key_type, key_dest) self.assertEqual(key_data, None)
def testCreateSudoersGroupSkip(self, mock_call, mock_permissions): mock_open = mock.mock_open( read_data="%%%s ALL=(ALL:ALL) NOPASSWD:ALL" % self.sudoers_group) mock_open.return_value.__iter__ = lambda self: iter(self.readline, '') mocks = mock.Mock() mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = True with mock.patch('%s.open' % builtin, mock_open, create=False): accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) mock_open().write.assert_not_called() expected_calls = [ mock.call.group(self.sudoers_group), mock.call.permissions(self.sudoers_file, mode=0o440, uid=0, gid=0), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testCreateSudoersGroup(self, mock_call, mock_permissions): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = False command = ['groupadd', self.sudoers_group] with mock.patch('%s.open' % builtin, mock_open, create=False): accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) mock_open().write.assert_called_once_with(mock.ANY) expected_calls = [ mock.call.group(self.sudoers_group), mock.call.call(command), mock.call.permissions(self.sudoers_file, mode=0o440, uid=0, gid=0), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testCreateSudoersGroupError(self, mock_call, mock_permissions): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = False mock_call.side_effect = subprocess.CalledProcessError(1, 'Test') command = ['groupadd', self.sudoers_group] with mock.patch('%s.open' % builtin, mock_open, create=False): accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) expected_calls = [ mock.call.group(self.sudoers_group), mock.call.call(command), mock.call.logger.warning(mock.ANY, mock.ANY), mock.call.permissions(self.sudoers_file, mode=0o440, uid=0, gid=0), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testGenerateSshKey( self, mock_tempfile, mock_call, mock_move, mock_permissions): mocks = mock.Mock() mocks.attach_mock(mock_tempfile, 'tempfile') mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_move, 'move') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_logger, 'logger') key_type = 'key-type' key_dest = '/key/dest' temp_dest = '/tmp/dest' mock_tempfile.return_value = mock_tempfile mock_tempfile.__enter__.return_value.name = temp_dest mock_open = mock.mock_open() key_file_contents = 'ssh-rsa asdfasdf' expected_key_data = ('ssh-rsa', 'asdfasdf') with mock.patch('%s.open' % builtin, mock_open, create=False): mock_open().read.return_value = key_file_contents key_data = instance_setup.InstanceSetup._GenerateSshKey( self.mock_setup, key_type, key_dest) expected_calls = [ mock.call.tempfile(prefix=key_type, delete=True), mock.call.tempfile.__enter__(), mock.call.tempfile.__exit__(None, None, None), mock.call.logger.info(mock.ANY, key_dest), mock.call.call( ['ssh-keygen', '-t', key_type, '-f', temp_dest, '-N', '', '-q']), mock.call.move(temp_dest, key_dest), mock.call.move('%s.pub' % temp_dest, '%s.pub' % key_dest), mock.call.permissions(key_dest, mode=0o600), mock.call.permissions('%s.pub' % key_dest, mode=0o644), ] self.assertEqual(mocks.mock_calls, expected_calls) self.assertEqual(key_data, expected_key_data) mock_open().read.return_value = '' key_data = instance_setup.InstanceSetup._GenerateSshKey( self.mock_setup, key_type, key_dest) self.assertEqual(key_data, None)
def testCreateSudoersGroupSkip(self, mock_exists, mock_call, mock_permissions): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(mock_call, 'call') mocks.attach_mock(mock_permissions, 'permissions') mocks.attach_mock(self.mock_utils._GetGroup, 'group') mocks.attach_mock(self.mock_logger, 'logger') self.mock_utils._GetGroup.return_value = True mock_exists.return_value = True with mock.patch('%s.open' % builtin, mock_open, create=False): accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils) mock_open().write.assert_not_called() expected_calls = [ mock.call.group(self.sudoers_group), mock.call.exists(self.sudoers_file), mock.call.permissions(self.sudoers_file, mode=0o440, uid=0, gid=0), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testDisableNetworkManager(self, mock_exists): mock_open = mock.mock_open() mocks = mock.Mock() mocks.attach_mock(mock_exists, 'exists') mocks.attach_mock(mock_open, 'open') mocks.attach_mock(self.mock_logger, 'logger') mocks.attach_mock(self.mock_setup._ModifyInterface, 'modify') mock_exists.side_effect = [True, False] with mock.patch('%s.open' % builtin, mock_open, create=False): network_setup.NetworkSetup._DisableNetworkManager( self.mock_setup, ['eth0', 'eth1']) expected_calls = [ mock.call.exists('/etc/sysconfig/network-scripts/ifcfg-eth0'), mock.call.modify(mock.ANY, 'DEVICE', 'eth0', replace=False), mock.call.modify(mock.ANY, 'NM_CONTROLLED', 'no', replace=True), mock.call.exists('/etc/sysconfig/network-scripts/ifcfg-eth1'), mock.call.open('/etc/sysconfig/network-scripts/ifcfg-eth1', 'w'), mock.call.open().__enter__(), mock.call.open().write(mock.ANY), mock.call.open().__exit__(None, None, None), mock.call.logger.info(mock.ANY, 'eth1'), ] self.assertEqual(mocks.mock_calls, expected_calls)