def test_invalid_put_method(self):
        status_code, response_data = execute_request(
            self.url_system,
            "PUT",
            json.dumps(DATA),
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, ("Wrong status code %s " %
                                                    status_code)
        info('### PASSED in Execute rest command "PUT" as expected for: ' +
             'url: ' + self.url_system + '  ###\n')
        info('### Success in INVALID Rest PUT for system ###\n')

        status_code, response_data = execute_request(
            self.url_system,
            "PUT",
            json.dumps(PUT_DATA),
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, ("Wrong status code %s " %
                                                    status_code)

        info('### PASSED in Execute rest command "PUT" as expected for: ' +
             'url: ' + self.url_interfaces + '  ###\n')

        info('### Success in INVALID Rest PUT for Interfaces id ###\n')
    def test_single_column_retrieval(self):
        # Test Setup
        test_title = "- Single column retrieval in GET request"
        info(TEST_START % test_title)
        keys = ["name"]
        appended_keys = self.get_keys_to_retrieve(keys)
        new_path = self.path + "keys=" + appended_keys
        # 1 - Query Resource
        response, response_data = execute_request(
            self.path, "GET", None, self.switch_ip, True,
            xtra_header=self.cookie_header)

        status_code = response.status
        assert status_code == httplib.OK, "Wrong status code %s " % status_code
        complete_data_rows = get_json(response_data)

        response, response_data = execute_request(
            new_path, "GET", None, self.switch_ip, True,
            xtra_header=self.cookie_header)

        status_code = response.status
        assert status_code == httplib.OK, "Wrong status code %s " % status_code
        data_rows = get_json(response_data)
        info("### Data successfully retrieved. Status code 200 OK ###\n")
        # Test
        # 2 - Validate that data exist in rows by keys retrieval
        result = \
            self.validate_retrieved_data_by_keys(complete_data_rows,
                                                 data_rows, keys)
        assert result is True, "Wrong data length: %s <--> %s\n" \
            % (len(complete_data_rows), len(data_rows))
        info("### Retrieved data validated ###\n")
        # Test Teardown
        info(TEST_END % test_title)
    def test_recursive_get_depth_first_level(self):
        specific_interface_path = self.PATH + "/50-1"
        depth_interface_path = self.PATH + "?depth=1;name=50-1"
        status_code, expected_data = execute_request(
            specific_interface_path,
            "GET",
            None,
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        status_code, response_data = execute_request(
            depth_interface_path,
            "GET",
            None,
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        json_expected_data = self.get_json(expected_data)
        json_data = self.get_json(response_data)[0]

        info("\n########## Test to Validate recursive GET Interface 50-1 "
             "depth=1 request ##########\n")

        assert status_code == httplib.OK, "Wrong status code %s " % status_code
        info("### Status code is OK ###\n")

        assert self.validate_keys_complete_object(json_data)
        info("### Validated first level of depth ###\n")

        json_expected_data = json_expected_data["status"]
        json_data = json_data["status"]

        assert self.validate_keys_inner_object(json_data, json_expected_data)
        info("########## End Test to Validate recursive GET Interface 50-1 "
             "depth=1 request ##########\n")
    def logs_with_offset_limit_negative_test_cases(self):
        info("\n########## Test to Validate logs with negative pagination" +
             " parameters ##########\n")

        self.LOGS_PATH = (self.PATH + "?offset=1&limit=-1")
        status_code, response_data = execute_request(
            self.LOGS_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, "Wrong status code %s " \
            % status_code
        info("### Status code for negative limit is okay ###\n")

        self.LOGS_PATH = (self.PATH + "?offset=-1&limit=-1")
        status_code, response_data = execute_request(
            self.LOGS_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, "Wrong status code %s " \
            % status_code
        info("### Status code for negative offset and limit is OK ###\n")

        self.LOGS_PATH = (self.PATH + "?offset=-1&limit=1")
        status_code, response_data = execute_request(
            self.LOGS_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, "Wrong status code %s " \
            % status_code
        info("### Status code for negative offset is okay ###\n")

        info("\n########## End Test to Validate logs with negative" +
             " pagination parameters ##########\n")
    def test_ecmp_enable(self):

        # enable ecmp
        ECMP_PATCH[0]["value"]["enabled"] = \
            ECMP_PATCH[0]["value"].pop(ECMP_PATCH[0]["value"].keys()[0])
        ECMP_PATCH[0]["value"]["enabled"] = TRUE

        status_code, response_data = execute_request(
            self.PATH,
            "PATCH",
            json.dumps(ECMP_PATCH),
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.NO_CONTENT, "Error patching a ecmp " \
            "enable Status code: %s Response data: %s " % (status_code,
                                                           response_data)
        info("### Enable ECMP Patched. Status code is 204 NO CONTENT  ###\n")

        # Verify data
        status_code, response_data = execute_request(
            self.PATH,
            "GET",
            None,
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Failed to query ecmp config"
        json_data = get_json(response_data)
        assert json_data["configuration"]["ecmp_config"]["enabled"] == TRUE,\
            "ECMP enable failed"
        info("### ECMP enable validated ###\n")
Esempio n. 6
0
    def verify_post_bgp_neighbors(self):

        info("\n#####################################################\n")
        info("#               Testing POST for BGP_Neighbors      #")
        info("\n#####################################################\n")

        status_code, response_data = execute_request(
            self.path_bgp, "POST", json.dumps(_DATA),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)
        info('\nPOST BGP router with the asn: ' +
             str(_DATA['configuration']['asn']) + ' passed successfully')

        status_code, response_data = execute_request(
            self.path_bgp_neighbors, "POST",
            json.dumps(_DATA_BGP_NEIGHBORS), self.SWITCH_IP, False,
            xtra_header=self.cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)

        info('\nPOST BGP neighbors with the ip: ' + str(_DATA_BGP_NEIGHBORS
             ['configuration']['ip_or_group_name']) + ' passed successfully\n')

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        d = get_json(response_data)
        assert d['configuration'] == _DATA_BGP_NEIGHBORS_COPY['configuration']
        info('Post test passed successfully')
    def modify_port_with_depth(self):
        info("\n########## Test to Validate Modify Port with depth "
             "##########\n")

        # 1 - Query port
        status_code, response_data = execute_request(
            self.PORT_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Port %s doesn't exists" % \
            self.PORT_PATH

        pre_put_get_data = {}
        try:
            pre_put_get_data = json.loads(response_data)
        except:
            assert False, "Malformed JSON"
        info("### Query Port %s  ###\n" % response_data)

        # 2 - Modify data
        put_data = pre_put_get_data["configuration"]
        put_data["ip4_address"] = "192.168.1.2"

        status_code, response_data = execute_request(
            self.PORT_PATH + "?depth=1", "PUT",
            json.dumps({'configuration':put_data}), self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, \
            "Unexpected status code. Received: %s Response data: %s " % \
            (status_code, response_data)
        info("### Port Not Modified. Status code is 400 BAD REQUEST  ###\n")

        info("\n########## End Test to Validate Modify Port ##########\n")
    def successful_login(self):
        '''
        This verifies Login is successful when using correct data
        '''
        test_title = "successful Login"
        info(TEST_START % test_title)

        data = {'username': DEFAULT_USER, 'password': DEFAULT_PASSWORD}

        # Attempt Login
        info("Attempting login with correct data...")
        response, response_data = execute_request(LOGIN_URI, "POST",
                                                  urllib.urlencode(data),
                                                  self.SWITCH_IP, True,
                                                  HEADERS)
        assert response.status == httplib.OK, ("Login POST not successful, " +
                                               "code: %s " % response.status)
        info(" All good.\n")

        # Get cookie header
        cookie_header = {'Cookie': response.getheader('set-cookie')}

        time.sleep(2)

        # Verify Login was successful
        info("Verifying Login was successful...")
        status_code, response_data = execute_request(LOGIN_URI, "GET", None,
                                                     self.SWITCH_IP, False,
                                                     cookie_header)
        assert status_code == httplib.OK, ("Login GET not successful, " +
                                           "code: %s " % status_code)
        info(" All good.\n")

        info(TEST_END % test_title)
Esempio n. 9
0
    def dc_test_custom_validator_valid_put(self):
        info("### Testing valid DC PUT request ###\n")

        status_code, response_data = execute_request(
            self.dc_put_url, "PUT", json.dumps(DC_PUT_DATA),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)

        assert status_code == httplib.OK, ("Wrong status code %s " %
                                           status_code)
        info("### Successfully executed PUT for url=%s ###\n" %
             self.dc_put_url)

        status_code, response_data = execute_request(
            self.dc_put_url, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, ("Wrong status code %s " %
                                                status_code)
        d = get_json(response_data)
        assert d['Interface']['49'] == DC_PUT_DATA['Interface']['49'], \
            'Failed in checking the GET METHOD JSON response validation for \
            Interface 49'
        assert d['Port']['p1'] == DC_PUT_DATA['Port']['p1'], \
            'Failed in checking the GET METHOD JSON response validation for \
            Port p1'

        info("### Received successful HTTP status code ###\n")
    def verify_post_bgp_neighbors(self):

        info("\n#####################################################\n")
        info("#               Testing POST for BGP_Neighbors      #")
        info("\n#####################################################\n")

        status_code, response_data = execute_request(
            self.path_bgp, "POST", json.dumps(_DATA),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)
        info('\nPOST BGP router with the asn: ' +
             str(_DATA['configuration']['asn']) + ' passed successfully')

        status_code, response_data = execute_request(
            self.path_bgp_neighbors, "POST",
            json.dumps(_DATA_BGP_NEIGHBORS), self.SWITCH_IP, False,
            xtra_header=self.cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)

        info('\nPOST BGP neighbors with the ip: ' + str(_DATA_BGP_NEIGHBORS
             ['configuration']['ip_or_group_name']) + ' passed successfully\n')

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        d = get_json(response_data)
        assert d['configuration'] == _DATA_BGP_NEIGHBORS_COPY['configuration']
        info('Post test passed successfully')
    def test_ecmp_reshash_disable(self):
        # disable ecmp resilient hash
        ECMP_PATCH[0]["value"]["resilient_hash_enabled"] = \
            ECMP_PATCH[0]["value"].pop(ECMP_PATCH[0]["value"].keys()[0])
        ECMP_PATCH[0]["value"]["resilient_hash_enabled"] = FALSE

        status_code, response_data = execute_request(
            self.PATH,
            "PATCH",
            json.dumps(ECMP_PATCH),
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.NO_CONTENT, \
            "Error patching ecmp resilient hash disable Status code: \
            %s Response data: %s "                                   % (status_code, response_data)
        info("### Disable Resilient Hash ECMP Patched. Status code is 204 "
             "NO CONTENT  ###\n")

        # Verify data
        status_code, response_data = execute_request(
            self.PATH,
            "GET",
            None,
            self.SWITCH_IP,
            False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Failed to query ecmp config"
        json_data = get_json(response_data)
        assert json_data[
            "configuration"]["ecmp_config"]["resilient_hash_enabled"]\
            == FALSE, "ECMP resilient hash disable failed"
        info("### ECMP resilient hash disable validated ###\n")
Esempio n. 12
0
    def verify_startup_config(self):
        info('''"\n########## Verify startup config writes and reads the
             config to startup config db ##########\n"''')
        src_path = os.path.dirname(os.path.realpath(__file__))
        src_file = os.path.join(src_path, 'json.data')

        path = '/rest/v1/system' + '/full-configuration?type=startup'
        with open(src_file) as data_file:
            _data = json.loads(data_file.read())

        status_code, response_data = execute_request(
            path,
            "PUT",
            json.dumps(_data),
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK

        status_code, response_data = execute_request(
            path, "GET", None, self.SWITCH_IP, xtra_header=self.cookie_header)
        content = json.loads(response_data)

        assert status_code == httplib.OK

        assert ordered(content) == ordered(_data)

        info("\n### Startup config write & read were successful ###\n")
Esempio n. 13
0
    def change_password_successful_change(self):
        '''
        Assuming the user is logged in, attempt to change
        their password with all correct data
        '''
        test_title = "attempt password change with correct data"
        info(TEST_START % test_title)

        # Initialize test data
        newpassword = '******'
        data = {
            'configuration': {
                'password': DEFAULT_PASSWORD,
                'new_password': newpassword
            }
        }

        # Attempt to change password
        status_code, response_data = \
            execute_request(ACCOUNT_URI, "PUT", json.dumps(data),
                            self.SWITCH_IP, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        # Attempt a login with the new password
        self.cookie_header = login(self.SWITCH_IP, password=newpassword)

        # Change password back to default
        data['configuration']['password'] = newpassword
        data['configuration']['new_password'] = DEFAULT_PASSWORD
        status_code, response_data = \
            execute_request(ACCOUNT_URI, "PUT", json.dumps(data),
                            self.SWITCH_IP, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        info(TEST_END % test_title)
Esempio n. 14
0
    def test_interfaces_id(self):
        status_code, response_data = execute_request(
            self.url, "PUT", json.dumps(DATA),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)

        assert status_code == httplib.OK, ("Wrong status code %s " %
                                           status_code)
        info('### Success in executing the rest command "PUT" for url: ' +
             self.url + ' ###\n')
        info('### Success in Rest PUT Interfaces id ###\n')

        status_code, response_data = execute_request(
            self.url, "GET", None, self.SWITCH_IP, False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, ("Wrong status code %s " %
                                           status_code)
        d = get_json(response_data)
        assert d['configuration'] == DATA['configuration']

        info('### Success in executing the rest command "GET" for url: ' +
             self.url + ' ###\n')

        info('### Success in Rest GET Interfacesid ###\n')

        assert d['configuration']['name'] == 'bridge_normal', 'Failed in checking the \
            GET METHOD JSON response validation for Interface name'
        info('### Success in Rest GET system for Interface name ###\n')

        status_code, response_data = execute_request(
            self.url, "DELETE", json.dumps(DATA), self.SWITCH_IP, False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.NO_CONTENT, ("Wrong status code %s " %
                                                   status_code)
        info('### Success in executing the rest command "DELETE" for url: ' +
             self.url + ' ###\n')

        info('### Success in executing the rest command \
        "DELETE for url=/rest/v1/system/interfaces/bridge_normal" ###\n')

        info('### Success in Rest DELETE Interfacesid ###\n')

        status_code, response_data = execute_request(
            self.url, "GET", None, self.SWITCH_IP, False,
            xtra_header=self.cookie_header)

        assert status_code == httplib.NOT_FOUND, ("Wrong status code %s " %
                                                  status_code)

        info('### Success in executing the rest command" "GET" as not \
             expected for url: ' + self.url + ' ###\n')
        info('### Success in Rest Checking http code 404 for GET method \
             #once DELETED Interfacesid ###\n')
    def delete_fake_vlan_if_exists(self, vlan_name):
        info("\n### Deleting VLAN %s  ###\n" % vlan_name)
        path = self.vlan_path + "/" + vlan_name
        status_code, response_data = execute_request(
            path, "GET", None, self.switch_ip, xtra_header=self.cookie_header)

        if status_code == httplib.OK:
            status_code, response_data = execute_request(
                path, "DELETE", None, self.switch_ip,
                xtra_header=self.cookie_header)

            assert status_code == httplib.NO_CONTENT, "VLAN deleted" % path
Esempio n. 16
0
    def logs_with_since_negative_test_cases(self):
        info("\n########## Test to Validate negative test cases for logs" +
             " with since timestamp ##########\n")

        since_test = "0000-00-00%2000:00:00"
        self.LOGS_PATH = self.PATH + "?since=%s&offset=%s&limit=%s" \
            % (since_test, OFFSET_TEST, LIMIT_TEST)
        status_code, response_data = execute_request(
            self.LOGS_PATH,
            "GET",
            None,
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, "Wrong status code %s " \
            % status_code
        info("### Status code for since 0000-00-00 00:00:00 "
             "case is okay ###\n")

        since_test = "2050-01-01%2001:00:00"
        self.LOGS_PATH = self.PATH + "?since=%s&offset=%s&limit=%s" \
            % (since_test, OFFSET_TEST, LIMIT_TEST)
        status_code, response_data = execute_request(
            self.LOGS_PATH,
            "GET",
            None,
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Wrong status code %s "\
            % status_code
        info("### Status code for since 2050-01-01 01:00:00 case is OK ###\n")

        assert "Empty logs" in response_data, "Response data is empty"
        info("### Response data returned for empty logs returned fine  ###\n")

        since_test = "-1%20hour%20ago"
        self.LOGS_PATH = self.PATH + "?since=%s&offset=%s&limit=%s" \
            % (since_test, OFFSET_TEST, LIMIT_TEST)
        status_code, response_data = execute_request(
            self.LOGS_PATH,
            "GET",
            None,
            self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.BAD_REQUEST, "Wrong status code %s "\
            % status_code
        info("### Status code for since parameter with negative "
             "value is okay ###\n")

        info("\n########## End Test to Validate negative test case for logs" +
             " with since timestamp ##########\n")
def create_test_port(request):
    switch_ip = request.cls.test_var.SWITCH_IP
    port_path = request.cls.test_var.PORT_PATH
    default_port_path = request.cls.test_var.DEFAULT_PORT_PATH
    test_port_path = request.cls.test_var.TEST_PORT_PATH
    test_port_name = test_port_path.split("/")[-1]

    # Login with default authorized user
    cookie_header = login(switch_ip)

    # Get default port data
    status_code, response_data = \
        execute_request(default_port_path, "GET",
                        None, switch_ip, xtra_header=cookie_header)

    assert status_code == httplib.OK, "Could not get default port data, " + \
        "status code: %s, data: %s" % (status_code, response_data)

    data = get_json(response_data)

    assert 'configuration' in data, "Corrupt default port data"

    data['configuration']['name'] = test_port_name
    data = {
        'configuration': data['configuration'],
        "referenced_by": [{
            "uri": "/rest/v1/system/bridges/bridge_normal"
        }]
    }

    # Save the port data
    request.cls.test_var.port_data = data

    # Create the test port
    status_code, response_data = \
        execute_request(port_path, "POST", json.dumps(data),
                        switch_ip, xtra_header=cookie_header)

    assert status_code == httplib.CREATED, "Unable to create test port, " + \
        "Status code: %s Response data: %s " % (status_code, response_data)

    def fin():
        status_code, response_data = \
            execute_request(test_port_path, "DELETE", None,
                            switch_ip, xtra_header=cookie_header)

        assert status_code in [httplib.NO_CONTENT, httplib.NOT_FOUND], \
            "Unable to delete test port. Status code: %s Response data: %s" % \
            (status_code, response_data)

    request.addfinalizer(fin)
    def test(self):
        data = """
               {
                   "configuration": {
                       "name": "fake_vlan_1",
                       "id": 2,
                       "description": "test vlan",
                       "admin": ["up"],
                       "other_config": {},
                       "external_ids": {}
                   }
               }
               """

        info("\n########## Executing POST to /system/bridges ##########\n")
        info("Testing Path: %s\n" % self.vlan_path)

        response_status, response_data = execute_request(
            self.vlan_path,
            "POST",
            data,
            self.switch_ip,
            xtra_header=self.cookie_header)

        assert response_status == httplib.CREATED, \
            "Response status received: %s\n" % response_status
        info("Response status received: \"%s\"\n" % response_status)

        assert response_data is "", \
            "Response data received: %s\n" % response_data
        info("Response data received: %s\n" % response_data)

        # Create duplicated
        response_status, response_data = execute_request(
            self.vlan_path,
            "POST",
            data,
            self.switch_ip,
            xtra_header=self.cookie_header)

        assert response_status == httplib.BAD_REQUEST, \
            "Response status received: %s\n" % response_status
        info("Response status received: \"%s\"\n" % response_status)

        assert response_data is not "", \
            "Response data received: %s\n" % response_data
        info("Response data received: %s\n" % response_data)

        info("########## Executing POST to test duplicated VLAN DONE "
             "##########\n")
Esempio n. 19
0
    def post_setup(self, cookie_header=None):
        if cookie_header is None:
            cookie_header = login(self.SWITCH_IP)
        status_code, response_data = execute_request(
            self.path_bgp, "POST", json.dumps(_DATA),
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)

        status_code, response_data = execute_request(
            self.path_bgp_neighbors, "POST",
            json.dumps(_DATA_BGP_NEIGHBORS), self.SWITCH_IP, False,
            xtra_header=cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)
    def post_setup(self, cookie_header=None):
        if cookie_header is None:
            cookie_header = login(self.SWITCH_IP)
        status_code, response_data = execute_request(
            self.path_bgp, "POST", json.dumps(_DATA),
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)

        status_code, response_data = execute_request(
            self.path_bgp_neighbors, "POST",
            json.dumps(_DATA_BGP_NEIGHBORS), self.SWITCH_IP, False,
            xtra_header=cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)
    def test_post_vlan_etag_match(self):
        info(TEST_START % "POST VLAN with matching Etag")
        # 1 - Query Resource
        cond_path = self.vlan_path + self.config_selector
        etag, pre_put_get_data = self.get_etag_and_data(cond_path)

        # Fill Vlan data
        fake_vlan_name = "VLAN3"
        vlan_id = 3
        data = FAKE_VLAN_DATA % {"name": fake_vlan_name, "id": vlan_id}

        # Try to create the resource using a valid etag
        headers = {"If-Match": etag}
        headers.update(self.cookie_header)
        status_code, response_data = execute_request(
            cond_path, "POST", data, self.switch_ip, False, headers)

        assert status_code == httplib.CREATED, "Error creating a VLAN using "\
            "if-match option. Status code: %s Response data: %s "\
            % (status_code, response_data)
        info("### VLAN Created. Status code 201 CREATED  ###\n")

        # Delete created VLAN
        self.delete_fake_vlan_if_exists(fake_vlan_name)

        info(TEST_END % "POST VLAN with matching Etag")
    def test(self):
        data = {}
        data["name"] = deepcopy(base_vlan_data)
        data["id"] = deepcopy(base_vlan_data)

        data["name"]["configuration"].pop("name")
        data["id"]["configuration"].pop("id")

        info("\n########## Executing POST test with missing fields "
             "##########\n")
        info("Testing Path: %s\n" % self.vlan_path)

        for field, value in data.iteritems():
            info("Testing missing field \"%s\" with value: %s\n" %
                 (field, value))

            response_status, response_data = execute_request(
                self.vlan_path,
                "POST",
                json.dumps(value),
                self.switch_ip,
                xtra_header=self.cookie_header)

            assert response_status == httplib.BAD_REQUEST, \
                "Response status received: %s\n" % response_status
            info("Response status received: \"%s\"\n" % response_status)

            assert response_data is not "", \
                "Response data received: %s\n" % response_data
            info("Response data received: %s\n" % response_data)

        info("########## Executing POST test with missing fields DONE "
             "##########\n")
    def test_call_system_options(self):
        info("\n########## Executing OPTIONS request on %s ##########\n" %
             self.PATH)

        # # Execute OPTIONS

        response, json_string = execute_request(self.PATH,
                                                "OPTIONS",
                                                None,
                                                self.SWITCH_IP,
                                                True,
                                                xtra_header=self.cookie_header)

        assert response.status == httplib.OK, "OPTIONS request failed: {0} \
            {1}".format(response.status, response.reason)

        # # Check expected options are correct

        # TODO change these to propper expected values after correct OPTIONS
        # is implemented
        expected_allow = ["DELETE", "GET", "OPTIONS", "POST", "PUT", "PATCH"]
        response_allow = response.getheader("allow").split(", ")

        assert expected_allow == response_allow, "OPTIONS: unexpected 'allow'\
            options"

        info("\n########## Finished executing OPTIONS request on %s \
            ##########\n" % self.PATH)
    def logs_with_offset_limit(self):
        info("\n########## Test to Validate logs with pagination parameters \
              ##########\n")

        self.LOGS_PATH = (self.PATH + "?offset=%s&limit=%s") % \
                         (OFFSET_TEST, LIMIT_TEST)
        status_code, response_data = execute_request(
            self.LOGS_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Wrong status code %s " % status_code
        info("### Status code is OK ###\n")

        assert response_data is not None, "Response data is empty"
        info("### Response data returned for logs are not empty ###\n")

        json_data = get_json(response_data)
        info("### JSON data is in good shape ###\n")

        assert len(json_data) <= LIMIT_TEST, "Pagination for logs failed %s " \
            % len(json_data)
        info("### Pagination for logs works fine ###\n")

        info("\n########## End Test to Validate logs with pagination" +
             " paramteres ##########\n")
Esempio n. 25
0
    def delete_teardown(self, cookie_header=None):
        if cookie_header is None:
            cookie_header = login(self.SWITCH_IP)
        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert (status_code == httplib.NO_CONTENT or
                status_code == httplib.NOT_FOUND), ("Wrong status code %s " %
                                                    status_code)

        status_code, response_data = execute_request(
            self.path_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert (status_code == httplib.NO_CONTENT or
                status_code == httplib.NOT_FOUND), ("Wrong status code %s " %
                                                    status_code)
    def test_call_system_get(self):
        global response_global
        info("\n########## Executing GET request on %s ##########\n" %
             self.PATH)

        # # Execute GET

        response, json_string = execute_request(self.PATH,
                                                "GET",
                                                None,
                                                self.SWITCH_IP,
                                                True,
                                                xtra_header=self.cookie_header)

        assert response.status == httplib.OK, "GET request failed: {0} \
            {1}".format(response.status, response.reason)

        get_data = {}

        try:
            # A malformed json should throw an exception here
            get_data = json.loads(json_string)
        except:
            assert False, "GET: Malformed JSON in response body"

        # # Check data was received

        assert get_data, "GET: Empty response"
        assert type(get_data) is dict, "GET: Malformed response"
        assert len(get_data) > 0, "GET: No data in response"

        info("\n########## Finished executing GET request on %s ##########\n" %
             self.PATH)
    def test_delete_vlan_etag_match(self):
        info(TEST_START % "DELETE VLAN with matching Etag")
        # 1- Create Fake VLAN
        fake_vlan_name = "VLAN3"
        vlan_id = 3
        create_fake_vlan(Test_IfMatchVlan.test_var.vlan_path,
                         Test_IfMatchVlan.test_var.switch_ip,
                         fake_vlan_name, vlan_id)

        # 2- Query Resource
        cond_path = self.vlan_path + "/" + fake_vlan_name\
            + self.config_selector
        etag, pre_put_get_data = self.get_etag_and_data(cond_path)

        # 3- Delete the vlan using the matching etag
        headers = {"If-Match": etag}
        headers.update(self.cookie_header)
        status_code, response_data = execute_request(
            cond_path, "DELETE", None, self.switch_ip, False, headers)

        assert status_code == httplib.NO_CONTENT, "Error deleting VLAN using "\
            "valid etag Status code: %s Response data: %s " % \
            (status_code, response_data)
        info("### VLAN deleted. Status code NOT CONTENT 204  ###\n")

        info(TEST_END % "DELETE VLAN with matching Etag")
    def test_post_vlan_etag_not_match(self):
        info(TEST_START % "POST VLAN with not matching Etag")
        # 1 - Query Resource
        cond_path = self.vlan_path + self.config_selector
        etag, pre_put_get_data = self.get_etag_and_data(cond_path)
        # Set wrong etag
        if etag:
            etag = etag[::-1]
        else:
            etag = '"abcdef"'

        # Fill Vlan data
        fake_vlan_name = "VLAN3"
        vlan_id = 3
        data = FAKE_VLAN_DATA % {"name": fake_vlan_name, "id": vlan_id}

        # Try to create the resource using a invalid etag
        headers = {"If-Match": etag}
        headers.update(self.cookie_header)
        status_code, response_data = execute_request(
            cond_path, "POST", data, self.switch_ip, False, headers)

        assert status_code == httplib.PRECONDITION_FAILED, "Error creating "\
            "using if-match using invalid etag. Status code: %s "\
            "Response data: %s " % (status_code, response_data)
        info("### VLAN No Created. Status code 412 Precondition Failed  ###\n")

        info(TEST_END % "POST VLAN with not matching Etag")
    def test(self):
        path = "%s/%s" % (self.vlan_path, self.vlan_name)

        expected_configuration_data = {}
        expected_configuration_data["name"] = "%s" % self.vlan_name
        expected_configuration_data["id"] = 2
        expected_configuration_data["description"] = "test_vlan"
        expected_configuration_data["admin"] = "up"
        #expected_configuration_data["other_config"] = {}
        #expected_configuration_data["external_ids"] = {}

        info("\n########## Executing GET to /system/bridges/{id}/vlans/ "
             "{id} ##########\n")
        info("Testing path: %s\n" % path)

        response_status, response_data = execute_request(
            path, "GET", None, self.switch_ip, xtra_header=self.cookie_header)

        expected_response = json.loads(response_data)

        assert response_status == httplib.OK, \
            "Response status received: %s\n" % response_status
        info("Response status received %s" % response_status)

        assert compare_dict(expected_response["configuration"],
                            expected_configuration_data), \
            "Response data received: %s\n" % response_data

        info("########## Executing GET to /system/bridges/{id}/vlans/{id} "
             "DONE ##########\n")
    def delete_teardown(self, cookie_header=None):
        if cookie_header is None:
            cookie_header = login(self.SWITCH_IP)
        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert (status_code == httplib.NO_CONTENT or
                status_code == httplib.NOT_FOUND), ("Wrong status code %s " %
                                                    status_code)

        status_code, response_data = execute_request(
            self.path_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=cookie_header)
        assert (status_code == httplib.NO_CONTENT or
                status_code == httplib.NOT_FOUND), ("Wrong status code %s " %
                                                    status_code)
Esempio n. 31
0
    def update_bridge_ports(self, switch, port, action):
        # action will add or remove this port from the existing ports
        patch = [copy.deepcopy(PATCH_PRT)]
        new_data = copy.deepcopy(PATCH_PRT)
        # Get existing bridge ports list
        ports = self.get_bridge_ports(switch)
        # info("### Bridge Ports ".join(ports))
        entry = "/rest/v1/system/ports/" + port
        if action == ADD:
            if entry not in ports:
                ports.append("/rest/v1/system/ports/" + port)
            new_data["value"] = ports
        else:
            if entry in ports:
                ports.remove("/rest/v1/system/ports/" + port)
            new_data["value"] = ports

        patch.append(new_data)
        status_code, response_data = execute_request(
            self.PATH_BRIDGE_NORMAL, "PATCH",
            json.dumps(patch),
            switch, False, xtra_header=self.cookie_header)

        assert status_code == httplib.NO_CONTENT, "Error update  Ports" \
            + " code: %s Response data: %s " % (status_code, response_data)
        info("### Bridge Port " + switch + " Status 204 NO CONTENT  ###\n")
    def unauthorized_user_login_attempt(self):
        '''
        This verifies that you can't login with
        a user that has no REST login permissions.
        Current login permissions include
        READ_SWITCH_CONFIG and WRITE_SWITCH_CONFIG.
        Currently, the only users that do not have
        either of these permissions are any user from
        the ops_admin group
        '''
        test_title = "login attempt by unauthorized user"
        info(TEST_START % test_title)

        data = {'username': '******', 'password': '******'}

        # Attempt Login
        info("Attempting login with an unauthorized user...")
        status_code, response_data = execute_request(LOGIN_URI, "POST",
                                                     urllib.urlencode(data),
                                                     self.SWITCH_IP, False,
                                                     HEADERS)
        assert status_code == httplib.UNAUTHORIZED, "Wrong status code " + \
            "when attempting login by an unauthorized user: %s " % status_code
        info(" All good.\n")

        info(TEST_END % test_title)
    def logs_with_offset_None(self):
        info("\n########## Test to Validate logs with no offset for" +
             " pagination ##########\n")

        offset_test = None

        self.LOGS_PATH = self.PATH + "?limit=%s" % LIMIT_TEST
        status_code, response_data = execute_request(
            self.LOGS_PATH, "GET", None, self.SWITCH_IP,
            xtra_header=self.cookie_header)

        assert status_code == httplib.OK, "Wrong status code %s " % status_code
        info("### Status code is OK ###\n")

        assert response_data is not None, "Response data is empty"
        info("### Response data returned is not empty ###\n")

        json_data = get_json(response_data)
        info("### JSON data is in good shape ###\n")

        assert len(json_data) <= LIMIT_TEST, "Pagination for logs failed %s " \
            % len(json_data)
        info("### Pagination for logs works fine ###\n")

        info("\n########## End Test to Validate logs with no offset for" +
             " pagination ##########\n")
    def verify_delete_bgp_neighbors(self):

        info("\n#####################################################\n")
        info("#               Testing DELETE for BGP_Neighbors    #")
        info("\n#####################################################\n")

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.NO_CONTENT, ("Wrong status code %s " %
                                                   status_code)

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.NOT_FOUND, ("Wrong status code %s " %
                                                  status_code)

        info('DELETE passed successfully')
        info("\n")
    def verify_delete_bgp_routers(self):

        info("\n#####################################################\n")
        info("#         Testing DELETE for BGP_Routers            #")
        info("\n#####################################################\n")

        # DELETE the bgp_router
        status_code, response_data = execute_request(
            self.path_id, "DELETE", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.NO_CONTENT, ("Wrong status code %s " %
                                                   status_code)

        # GET after deleting the bgp_router
        status_code, response_data = execute_request(
            self.path_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.NOT_FOUND, ("Wrong status code %s " %
                                                  status_code)
        info('Successful\n')
    def verify_post_bgp_routers(self):

        info("\n#####################################################\n")
        info("#         Testing POST for BGP_Routers              #")
        info("\n#####################################################\n")

        status_code, response_data = execute_request(
            self.path_bgp, "POST", json.dumps(_DATA),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.CREATED, ("Wrong status code %s " %
                                                status_code)

        status_code, response_data = execute_request(
            self.path_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, ("Wrong status code %s " %
                                           status_code)

        d = get_json(response_data)
        assert d['configuration'] == _DATA_COPY['configuration']
        info('Successful')
    def verify_put_bgp_routers(self):

        info("\n#####################################################\n")
        info("#         Testing PUT for BGP_Routers               #")
        info("\n#####################################################\n")

        _DATA_COPY['configuration']['networks'] = ["10.10.1.0/24"]
        status_code, response_data = execute_request(
            self.path_id, "PUT", json.dumps(_DATA_COPY),
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, ("Wrong status code %s " %
                                           status_code)

        status_code, response_data = execute_request(
            self.path_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        status_code == httplib.OK, ("Wrong status code %s " %
                                    status_code)

        content = response_data
        d = get_json(content)
        assert d['configuration'] == _DATA_COPY['configuration']
        info('Successful')
    def verify_put_bgp_neighbors(self):

        info("\n#####################################################\n")
        info("#               Testing PUT for BGP_Neighbors       #")
        info("\n#####################################################\n")

        _DATA_BGP_NEIGHBORS_COPY['configuration']['description'] = \
            'BGP_Neighbors'

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "PUT",
            json.dumps(_DATA_BGP_NEIGHBORS_COPY), self.SWITCH_IP, False,
            xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        status_code, response_data = execute_request(
            self.path_bgp_neighbors_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        assert status_code == httplib.OK, "Wrong status code %s " % status_code

        d = get_json(response_data)
        d['configuration'].pop('capability', None)
        assert d['configuration'] == _DATA_BGP_NEIGHBORS_COPY['configuration']
        info('PUT passed successfully')
    def verify_get_bgp_routers(self):
        info("\n#####################################################\n")
        info("#         Testing GET for BGP_Routers               #")
        info("\n#####################################################\n")

        status_code, response_data = execute_request(
            self.path_id, "GET", None,
            self.SWITCH_IP, False, xtra_header=self.cookie_header)
        status_code == httplib.OK, ("Wrong status code %s " %
                                    status_code)

        d = get_json(response_data)
        assert d['configuration'] == _DATA_COPY['configuration']

        info('GET for BGP router with asn: ' +
             str(_DATA['configuration']['asn']) + ' passed successfully')