示例#1
0
    def test_switch_mode_user(self, create_new_user):
        '''
            Switch mode user and test if the session is updated correctly based on mode user
        '''
        switch_mode(user='******', password='******')
        se_1 = rest.get('serviceengine')
        session = rest.get_session()
        assert session.username == 'admin' and session.password == 'avi123'

        switch_mode(user='******')
        se_2 = rest.get('serviceengine')
        session = rest.get_session()
        assert se_1 == se_2
        assert session.username == 'test-user-1' and session.password == 'avi123'
        clear_session(all_sessions=True)
示例#2
0
def wait_until_cluster_ready(detailed_state_str="", **kwargs):
    """ Blocks until the controller cluster state is up or if a
    detailed_state_str was passed, then returns when the cluster reaches that
    state """
    # uses site_name or config
    config = AviConfig.get_instance()
    ctrl_vm = config.get_vm_of_type('controller')[0].ip
    logger.debug('controller used in wait until cluster ready: %s' % ctrl_vm)
    rsp = None
    try:
        session = get_session()
        session.password = '******'
        session.reset_session()
        status_code, rsp = get('cluster', path='runtime')
    except Exception as e:
        fail('Cluster api runtime exception %s' % str(e))
    if rsp and status_code == 200:  # REVIEW do we need this logic implicitly checking status code still?
        cluster_state = rsp.get('cluster_state', {})
        if ('CLUSTER_UP' in cluster_state.get('state', '') and
                not 'HA_NOT_READY' in cluster_state.get('state', '')):
            logger.info('Controller cluster is ready with cluster_state %s' % cluster_state)
        elif cluster_state.get('reason'):
            if (detailed_state_str and
                        detailed_state_str in cluster_state.get('reason')):
                logger.info('Controller cluster is ready with %s' % detailed_state_str)
            else:
                fail('cluster state[%s]: %s' % (ctrl_vm, cluster_state.get('state', 'unknown')))
        else:
            fail('cluster state[%s]: %s' % (ctrl_vm, cluster_state.get('state', 'unknown')))
    elif rsp is None:
        fail('Cluster api runtime exception: no response.')
    else:
        fail('Cluster api runtime returned %d' % status_code)
示例#3
0
    def test_switch_mode_session(self):
        '''
            switch mode session and test if the requests on the session go through correctly
        '''
        switch_mode(user='******', password='******')
        session = rest.get_session()
        clear_session()
        switch_mode(password='******')
        try:
            rest.get('serviceengine')
        except Exception as e:
            logger.info("authentication error expected: " + str(e))
        else:
            assert 0, "Session is not getting updated based on password"
        switch_mode(session=session)
        rest.get('serviceengine')

        # REVIEW: Once session in mode is set, no further switch_mode works
        switch_mode(user='******', password='******')
        try:
            rest.get('serviceengine')
        except Exception as e:
            logger.info("authentication error expected: " + str(e))
        else:
            assert 0, "Once switched on session, no other switch mode works"
        clear_session(all_sessions=True)
示例#4
0
    def test_controller_goes_down(self):
        '''
            Test the request on sessions if the controller goes down
            Does the request on the sessions work when the controller comes back up
        '''
        switch_mode(user='******', password='******')
        logger.info('Configuring cloud, This may take sometime..')
        setup_cloud(wait_for_cloud_ready=True)
        config = get_config()
        mode = config.get_mode()
        controller = config.get_vm_of_type('controller')[0]
        session = rest.get_session()
        data_1 = rest.get('serviceengine')

        cloud_obj = config.testbed[mode['site_name']].cloud_obj[mode['cloud']]
        controller_name = controller.name
        cloud_obj.powerOffVM(controller_name)
        try:
            rest.get('serviceengine')
        except Exception as e:
            logger.info('Expected ReadTimeout: ' + str(e))
        cloud_obj.powerOnVM(controller_name)
        wait_until_n_cluster_nodes_ready()
        data_2 = rest.get('serviceengine')
        assert data_1 == data_2
示例#5
0
    def test_get_session(self, create_new_user):
        '''
            Test get_session
                - Test if it returns the already created session if it exists
                - If there is no already created session, it should create a new session and return
        '''
        switch_mode(user='******', password='******')
        se_1 = rest.get('serviceengine')
        session_test_user = rest.get_session()

        switch_mode(user='******', password='******')
        rest.get('serviceengine')
        session = rest.get_session()
        assert session.username == 'admin' and session.password == 'avi123'

        switch_mode(user='******')
        se_2 = rest.get('serviceengine')
        session = rest.get_session()
        assert session is session_test_user and se_1 == se_2
        clear_session(all_sessions=True)
示例#6
0
def get_and_delete_all_configs(skip_cloud=False, check_status_code=False, tenant_list=[], fix_url=True, **kwargs):
    move_all_se_to_group('Default-Group')

    session = get_session()
    config = AviConfig.get_instance()
    defaults = get('default-values').json()
    logger.info(defaults)
    tenant_resp = get('tenant').json()
    if not tenant_list:
        tenants = []
        tenants = [str(entry['name']) for entry in tenant_resp.get('results', [])]
    else:
        tenants = tenant_list

    for _tenant in tenants:
        switch_mode(tenant=_tenant)
        for obj_type in reversed(obj_order):
            if (((obj_type == 'cloud' or obj_type == 'tenant') and skip_cloud) or
                    (obj_type in ['sslcertificaterequest', 'staticroute'])):
                continue
            status_code, data = get(obj_type, check_status_code=check_status_code)
            if status_code > 400:
                continue
            for d in data['results']:
                if obj_type == 'cloud' and d['name'] == 'Default-Cloud':
                    if d['vtype'] != 'CLOUD_NONE':
                        logger.info('Update Default-Cloud from %s to no-access' % d['vtype'])
                        if d.get('vcenter_configuration'):
                            d.pop('vcenter_configuration')
                        elif d.get('openstack_configuration'):
                            d.pop('openstack_configuration')
                        elif d.get('aws_configuration'):
                            d.pop('aws_configuration')
                        elif d.get('cloudstack_configuration'):
                            d.pop('cloudstack_configuration')
                        elif d.get('vca_configuration'):
                            d.pop('vca_configuration')
                        elif d.get('apic_configuration'):
                            d.pop('apic_configuration')
                        d['vtype'] = 'CLOUD_NONE'
                        put('cloud', name=d['name'], data=json.dumps(d))  # review can we use uuid=d['uuid']?
                if obj_type in defaults.get('default', []) and \
                                d['uuid'] in defaults['default'][obj_type]:
                    continue
                logger.info('Deleting: %s:%s' % (obj_type, d['name']))
                if obj_type in ['sslcertificaterequest', 'sslkeyandcertificate_import']:
                    delete('sslkeyandcertificate', name=d['name'], check_status_code=False)
                else:
                    delete(obj_type, name=d['name'], check_status_code=False)
示例#7
0
    def test_clear_session_basic(self):
        '''
            Basic usage of clear_session
        '''
        switch_mode(user='******', password='******')
        rest.get('serviceengine')
        config = get_config()
        session = rest.get_session()
        switch_mode(session=session)
        assert config.sessions and config.session

        clear_session()
        config = get_config()
        context_key = config.get_context_key()
        assert config.session is None and context_key not in config.sessions
示例#8
0
    def test_switch_mode_password(self):
        '''
            switch mode password and test if the session is updated correctly based on mode password
        '''
        switch_mode(user='******', password='******')
        rest.get('serviceengine')
        session = rest.get_session()
        assert session.username == 'admin' and session.password == 'avi123'

        # REVIEW: switch_mode on password does nothing as of now
        switch_mode(password='******')
        try:
            rest.get('serviceengine')
        except Exception as e:
            logger.info("authentication error expected: " + str(e))
        else:
            assert 0, "Session is not getting updated based on password"
        clear_session()
示例#9
0
def _upload_multipart_file_form(file_path, file_uri):
    if not os.path.exists(file_path):
        error('File not found: ' + file_path)

    #port = get_controller_port()i
    session = get_session()
    #port = session.port
    port = 443
    path = 'https://%s:%s/api/fileservice/uploads' % (session.controller_ip,
                                                      port)
    file_name = os.path.basename(file_path)

    with open(file_path, "rb") as f:
        f_data = {
            "file": (file_name, f, "application/octet-stream"),
            "uri": file_uri
        }
        m = MultipartEncoder(f_data)
        r = session.post(path, data=m)

        if r.status_code > 300:
            error('Fail to upload: ' + r.content)
示例#10
0
    def test_clear_session_making_get_request(self):
        '''
            Test if get() requests work as expected after clearing sessions
        '''
        switch_mode(user='******', password='******')
        se_1 = rest.get('serviceengine')
        session = rest.get_session()

        switch_mode(password='******')
        # This should pass because it uses already created session
        se_2 = rest.get('serviceengine')
        assert se_1 == se_2
        clear_session()
        try:
            # This should fail during authentication(uses password Temp123)
            rest.get('serviceengine')
        except Exception as e:
            logger.info('Authentication failure expected: ' + str(e))
        else:
            assert 0, 'Sessions are not cleared, Still using password avi123'

        switch_mode(password='******')
        clear_session()
示例#11
0
    def test_clear_session_making_get_request(self):
        '''
            test_clear_session_making_get_requests
                - Make a get request after clear_session and verify that the request uses mode user and password
        '''
        switch_mode(user='******', password='******')
        rest.get('serviceengine')
        session = rest.get_session()

        switch_mode(password='******')
        # This should pass because it uses already created session
        rest.get('serviceengine')
        clear_session()
        try:
            # This should fail during authentication(uses password Temp123)
            rest.get('serviceengine')
        except Exception as e:
            logger.info('Authentication failure expected: ' + str(e))
        else:
            assert 0, 'Sessions are not cleared, Still using password avi123'

        switch_mode(password='******')
        clear_session()
示例#12
0
    def process_openstack_data(self, tenant_data, user_data, operation):
        """
        Implements openstack-specific handling for data to be created/deleted in horizon
        """
        tenants = [t['name'] for t in tenant_data]
        users = [u['name'] for u in user_data]
        roles = self.extract_roles_from_users(user_data)
        logger.debug('extracted openstack specific configs: tenants=%s, users=%s, roles=%s' %(tenants, users, roles))

        if operation == 'create':
            for tenant in tenants:
                # REVIEW should these ignore errors if already existing?
                self.create_keystone_tenant(tenant)
            for user in user_data:
                self.create_keystone_user(user['username'], user['password']) # FIXME tenant?
            for role in roles:
                self.create_keystone_role(role)
            self.setup_openstack_rolemapping(**{r:r for r in roles})

            # add keystone role mapping
            for user in user_data:
                username = user['name']
                for access in user['access']:
                    role_ref = access['role_ref']
                    role_name = role_ref.split('=')[1]
                    tenant_name = None
                    tenant_ref = access.get('tenant_ref', None)
                    if tenant_ref:
                        tenant_name = tenant_ref.split('=')[1]
                    elif access.get('all_tenants'):
                        tenant_name = '*'
                    self.add_keystone_role(username, tenant_name, role_name)

            # REVIEW: create? get? switch_mode(some other user?)
            get_session().reset_session()
        elif operation == 'delete':
            for tenant in tenants:
                try:
                    self.delete_keystone_tenant(tenant)
                except Exception as e:
                    # REVIEW should these take flags similar to ignore_deleted?
                    if '404' in str(e):
                        pass
                    else:
                        raise e
            for user in user_data:
                try:
                    self.delete_keystone_user(user['username'])
                except Exception as e:
                    if '404' in str(e):
                        pass
                    else:
                        raise e
            for role in roles:
                try:
                    self.delete_keystone_role(role)
                except Exception as e:
                    if '404' in str(e):
                        pass
                    else:
                        raise e
            # We don't sync deletes, instead call this API explicitly
            get('openstack-cleanup')
        else:
            fail('Unsupported operation %s' % operation)