def test_constructor_params_moks(self):
        """test constructor passing an instance of osclients
        (a mock is used)"""
        mock = MagicMock()

        trustfactory = TrustFactory(mock)
        self.assertTrue(mock.get_keystoneclientv3.called)
        self.assertEquals(trustfactory.keystone, mock.get_keystoneclientv3())
def generate_trust_ids(users_to_delete):
    """
    From a list of users to delete, generate a file with a trustid for each
    user. The user is acting as the trustor, delegating in a trustee, which
    will impersonate it to delete its resources.

    :param users_to_delete: a list of trustors.
    :return: this function does not return anything. It creates a file.
    """
    global logger

    osclients = OpenStackClients()
    users_trusted_ids = open('users_trusted_ids.txt', 'w')
    check_users = CheckUsers()

    # Use an alternative URL that allow direct access to the keystone admin
    # endpoint, because the registered one uses an internal IP address.

    osclients.override_endpoint('identity', osclients.region, 'admin',
                                KEYSTONE_ENDPOINT)

    trust_factory = TrustFactory(osclients)
    lines = users_to_delete.readlines()
    total = len(lines)
    count = 0
    if 'TRUSTEE_USER' in env:
        trustee = env['TRUSTEE_USER']
    else:
        trustee = TRUSTEE

    for user in lines:
        user = user.strip()
        if user == '':
            continue
        try:
            count += 1
            (username,
             trust_id) = trust_factory.create_trust_admin(user, trustee)
            users_trusted_ids.write(username + ',' + trust_id + '\n')
            msg = 'Generated trustid for user {0} ({1}/{2})'
            logger.info(msg.format(user, count, total))
        except Exception, e:
            msg = 'Failed getting trust-id from trustor {0}. Reason: {1}'
            logger.error(msg.format(user, str(e)))
 def setUp(self):
     """create object and init object.keystone with a mock"""
     self.trustfactory = TrustFactory(MagicMock())
     self.trustfactory.keystone = MagicMock()
 def test_constructor_params_validity(self, mock):
     """test constructor passing trustid_validity"""
     trustfactory = TrustFactory(trustid_validity=0)
     self.assertEquals(trustfactory.trustid_validity, 0)
     self.assertCommonCalls(mock, trustfactory)
 def test_constructor_no_params_with_environ(self, mock):
     """test constructo without params but with KEYSTONE_ADMIN_ENDPOINT"""
     os.environ['KEYSTONE_ADMIN_ENDPOINT'] = 'foo'
     trustfactory = TrustFactory()
     self.assertTrue(mock.return_value.override_endpoint.called)
     self.assertCommonCalls(mock, trustfactory)
 def test_constructor_no_params(self, mock):
     """test call to constructor without params nor environment"""
     trustfactory = TrustFactory()
     self.assertFalse(mock.return_value.override_endpoint.called)
     self.assertEquals(trustfactory.trustid_validity, TRUSTID_VALIDITY)
     self.assertCommonCalls(mock, trustfactory)