Exemplo n.º 1
0
    def test_post_node_success(self, m_load_neighbordb, m_repository):
        node = create_node()

        definition = create_definition()
        definition.add_action()

        cfg = {'return_value.exists.return_value': False}
        m_repository.configure_mock(**cfg)

        cfg = {'return_value.match_node.return_value': [Mock()]}
        m_load_neighbordb.configure_mock(**cfg)

        request = Request.blank('/nodes', body=node.as_json(), method='POST',
                                headers=ztp_headers())
        resp = request.get_response(ztpserver.controller.Router())

        args_list = list()
        args_list.append('nodes/%s/%s' % (node.serialnumber, DEFINITION_FN))
        args_list.append('nodes/%s/%s' % (node.serialnumber, PATTERN_FN))

        for arg in args_list:
            m_repository.return_value.add_file.assert_any_call(arg)

        location = 'http://localhost/nodes/%s' % node.serialnumber
        self.assertEqual(resp.status_code, constants.HTTP_STATUS_CREATED)
        self.assertEqual(resp.location, location)
Exemplo n.º 2
0
    def test_get_definition_wo_validation(self, m_repository):
        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', True, 'default')

        definitions_file = create_definition()
        definitions_file.add_action()

        node = create_node()
        cfg = dict()

        def m_get_file(arg):
            m_file_object = Mock()
            if arg.endswith('.node'):
                m_file_object.read.return_value = node.as_dict()
            elif arg.endswith('definition'):
                m_file_object.read.return_value = definitions_file.as_dict()
            else:
                raise ztpserver.repository.FileObjectNotFound
            return m_file_object

        cfg['return_value.get_file.side_effect'] = m_get_file
        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)
        self.assertIsInstance(json.loads(resp.body), dict)
Exemplo n.º 3
0
    def test_get_startup_config_w_validation_failure(self, m_load_pattern,
                                                     m_repository):

        m_load_pattern.return_value.match_node.return_value = list()

        node = create_node()
        cfg = dict()

        def exists(filepath):
            if filepath.endswith('startup-config'):
                return True
            return False

        cfg['return_value.exists'] = Mock(side_effect=exists)

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
                return fileobj
            elif filepath.endswith('pattern'):
                return fileobj
            raise ztpserver.repository.FileObjectNotFound

        cfg['return_value.get_file'] = Mock(side_effect=get_file)

        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_BAD_REQUEST)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_HTML)
        self.assertEqual(resp.body, str())
Exemplo n.º 4
0
    def test_get_startup_config_w_validation_failure(self, m_load_pattern,
                                                     m_repository):

        m_load_pattern.return_value.match_node.return_value = list()

        node = create_node()
        cfg = dict()

        def exists(filepath):
            if filepath.endswith('startup-config'):
                return True
            return False
        cfg['return_value.exists'] = Mock(side_effect=exists)

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
                return fileobj
            elif filepath.endswith('pattern'):
                return fileobj
            raise ztpserver.repository.FileObjectNotFound
        cfg['return_value.get_file'] = Mock(side_effect=get_file)

        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_BAD_REQUEST)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_HTML)
        self.assertEqual(resp.body, str())
Exemplo n.º 5
0
    def test_get_definition_w_validation_success(self, m_load_pattern, 
                                                 m_repository):
        node = create_node()

        definitions_file = create_definition()
        definitions_file.add_action()

        cfg = dict()

        def m_get_file(arg):
            print arg
            m_file_object = Mock()
            if arg.endswith('.node'):
                m_file_object.read.return_value = node.as_dict()
            elif arg.endswith('definition'):
                m_file_object.read.return_value = definitions_file.as_dict()
            elif arg.endswith('attributes'):
                raise ztpserver.repository.FileObjectNotFound
            return m_file_object

        cfg['return_value.get_file.side_effect'] = m_get_file

        m_repository.configure_mock(**cfg)

        m_load_pattern.return_value.match_node.return_value = Mock()

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)
        self.assertIsInstance(json.loads(resp.body), dict)
Exemplo n.º 6
0
    def test_get_definition_w_validation_success(self, m_load_pattern,
                                                 m_repository):
        node = create_node()

        definitions_file = create_definition()
        definitions_file.add_action()

        cfg = dict()

        def m_get_file(arg):
            print arg
            m_file_object = Mock()
            if arg.endswith('.node'):
                m_file_object.read.return_value = node.as_dict()
            elif arg.endswith('definition'):
                m_file_object.read.return_value = definitions_file.as_dict()
            elif arg.endswith('attributes'):
                raise ztpserver.repository.FileObjectNotFound
            return m_file_object

        cfg['return_value.get_file.side_effect'] = m_get_file

        m_repository.configure_mock(**cfg)

        m_load_pattern.return_value.match_node.return_value = Mock()

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)
        self.assertIsInstance(json.loads(resp.body), dict)
    def test_get_startup_config_wo_validation(self):
        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', True, 'default')

        node = create_node()

        filestore = Mock()

        def exists(filepath):
            if filepath.endswith('startup-config'):
                return True
            return False
        filestore.return_value.exists = Mock(side_effect=exists)

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
            else:
                type(fileobj).contents = mock.PropertyMock(side_effect=\
                    ztpserver.repository.FileObjectNotFound)
            return fileobj

        filestore.return_value.get_file = Mock(side_effect=get_file)

        ztpserver.controller.create_file_store = filestore

        url = '/nodes/%s' % node.systemmac
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content_type, 'application/json')
        self.assertIsInstance(json.loads(resp.body), dict)
Exemplo n.º 8
0
    def test_post_node_success(self, m_load_neighbordb, m_repository):
        node = create_node()

        definition = create_definition()
        definition.add_action()

        cfg = {'return_value.exists.return_value': False}
        m_repository.configure_mock(**cfg)

        cfg = {'return_value.match_node.return_value': [Mock()]}
        m_load_neighbordb.configure_mock(**cfg)

        request = Request.blank('/nodes',
                                body=node.as_json(),
                                method='POST',
                                headers=ztp_headers())
        resp = request.get_response(ztpserver.controller.Router())

        args_list = list()
        args_list.append('nodes/%s/%s' % (node.serialnumber, DEFINITION_FN))
        args_list.append('nodes/%s/%s' % (node.serialnumber, PATTERN_FN))

        for arg in args_list:
            m_repository.return_value.add_file.assert_any_call(arg)

        location = 'http://localhost/nodes/%s' % node.serialnumber
        self.assertEqual(resp.status_code, constants.HTTP_STATUS_CREATED)
        self.assertEqual(resp.location, location)
Exemplo n.º 9
0
    def test_get_startup_config_wo_validation(self, m_load_pattern,
                                              m_repository, m_create_node):

        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', True, 'default')

        definition = create_definition()
        definition.add_action()

        node = create_node()
        cfg = dict()

        def m_get_file(arg):
            fileobj = Mock()
            if arg.endswith('.node'):
                fileobj.return_value.read.return_value = node.as_dict()
            elif arg.endswith('startup-config'):
                fileobj.return_value.read.return_value = random_string()
            else:
                raise ztpserver.repository.FileObjectNotFound
            return fileobj
        cfg['return_value.get_file'] = Mock(side_effect=m_get_file)

        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)
        self.assertIsInstance(json.loads(resp.body), dict)
    def test_get_definition_w_attributes_no_substitution(self):
        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', False, 'default')

        node = create_node()

        g_attr_foo = random_string()
        attributes_file = create_attributes()
        attributes_file.add_attribute('variables', {'foo': g_attr_foo})

        l_attr_url = random_string()
        definitions_file = create_definition()
        definitions_file.add_attribute('foo', random_string())
        definitions_file.add_action(name='dummy action',
                                    attributes=dict(url=l_attr_url))

        filestore = Mock()

        def exists(filepath):
            if filepath.endswith('startup-config'):
                return False
            return True
        filestore.return_value.exists = Mock(side_effect=exists)

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
            elif filepath.endswith('definition'):
                fileobj.contents = definitions_file.as_yaml()
            elif filepath.endswith('attributes'):
                fileobj.contents = attributes_file.as_yaml()
            return fileobj
        filestore.return_value.get_file = Mock(side_effect=get_file)

        ztpserver.controller.create_file_store = filestore

        ztpserver.neighbordb.load_pattern = Mock()
        cfg = {'return_value.match_node.return_value': True}
        ztpserver.neighbordb.load_pattern.configure_mock(**cfg)

        url = '/nodes/%s' % node.systemmac
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content_type, 'application/json')

        attrs = resp.json['actions'][0]['attributes']
        self.assertFalse('variables' in attrs)
        self.assertFalse('foo' in attrs)
        self.assertEqual(attrs['url'], l_attr_url)
Exemplo n.º 11
0
    def test_get_attributes_missing(self, m_repository):
        node = create_node()

        m_repository.return_value.exists.return_value = False
        m_repository.return_value.get_file = \
            Mock(side_effect=ztpserver.repository.FileObjectNotFound)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.get_attributes(dict(),
                                                  resource=node.serialnumber)

        self.assertEqual(state, 'do_substitution')
        self.assertIsInstance(resp, dict)
        self.assertEqual(resp['attributes'], dict())
Exemplo n.º 12
0
    def test_get_attributes_missing(self, m_repository):
        node = create_node()

        m_repository.return_value.exists.return_value = False
        m_repository.return_value.get_file = \
            Mock(side_effect=ztpserver.repository.FileObjectNotFound)


        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.get_attributes(dict(),
                                                  resource=node.serialnumber)

        self.assertEqual(state, 'do_substitution')
        self.assertIsInstance(resp, dict)
        self.assertEqual(resp['attributes'], dict())
    def test_create_node_neighbors_remote_interface_missing(self):
        nodeattrs = create_node()

        remote_device = random_string()

        neighbors = {'Ethernet1': {'remote_device': remote_device}}
        nodeattrs.add_neighbors(neighbors)

        kwargs = nodeattrs.as_dict()
        try:
            node = None
            node = Node(**kwargs)
        except NodeError:
            pass
        finally:
            self.assertIsNone(node)
    def test_node_exists_sysmac_folder_exists(self):
        filestore = Mock()

        node = create_node()
        def exists(filepath):
            if filepath.endswith(node.systemmac):
                return True
            return False
        filestore.return_value.exists = Mock(side_effect=exists)
        ztpserver.controller.create_file_store = filestore

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(), node=node)

        self.assertEqual(state, 'post_config')
        self.assertTrue('status' not in resp)
    def test_node_exists_startup_config_exists(self):
        filestore = Mock()
        node = create_node()

        def exists(filepath):
            if filepath.endswith(ztpserver.controller.STARTUP_CONFIG_FN):
                return True
            return False
        filestore.return_value.exists = Mock(side_effect=exists)
        ztpserver.controller.create_file_store = filestore

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(), node=node)

        self.assertEqual(state, 'dump_node')
        self.assertEqual(resp['status'], 409)
Exemplo n.º 16
0
    def test_create_node_neighbors_remote_interface_missing(self):
        nodeattrs = create_node()

        remote_device = random_string()

        neighbors = {'Ethernet1': {'remote_device': remote_device}}
        nodeattrs.add_neighbors(neighbors)

        kwargs = nodeattrs.as_dict()
        try:
            node = None
            node = Node(**kwargs)
        except NodeError:
            pass
        finally:
            self.assertIsNone(node)
    def test_add_neighbors_success(self):
        nodeattrs = create_node()

        remote_device = random_string()
        remote_interface = random_string()
        neighbors = {'Ethernet1': [{'device': remote_device, 
                                    'port': remote_interface}]}

        kwargs = nodeattrs.as_dict()
        node = Node(**kwargs)
        node.add_neighbors(neighbors)

        self.assertIsNotNone(node.neighbors('Ethernet1'))
        self.assertEqual(node.neighbors('Ethernet1')[0].device, 
                         remote_device)
        self.assertEqual(node.neighbors('Ethernet1')[0].interface, 
                         remote_interface)
Exemplo n.º 18
0
    def test_get_definition_w_attributes_no_substitution(
            self, m_load_pattern, m_repository):

        node = create_node()

        g_attr_foo = random_string()
        attributes_file = create_attributes()
        attributes_file.add_attribute('variables', {'foo': g_attr_foo})

        l_attr_url = random_string()
        definitions_file = create_definition()
        definitions_file.add_attribute('foo', random_string())
        definitions_file.add_action(name='dummy action',
                                    attributes=dict(url=l_attr_url))

        cfg = dict()

        def m_get_file(arg):
            m_file_object = Mock()
            if arg.endswith('.node'):
                m_file_object.read.return_value = node.as_dict()
            elif arg.endswith('definition'):
                m_file_object.read.return_value = definitions_file.as_dict()
            elif arg.endswith('attributes'):
                m_file_object.read.return_value = attributes_file.as_dict()
            elif arg.endswith('startup-config'):
                raise ztpserver.repository.FileObjectNotFound
            return m_file_object

        cfg['return_value.get_file'] = Mock(side_effect=m_get_file)

        cfg['return_value.match_node.return_value'] = True
        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')

        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)

        attrs = resp.json['actions'][0]['attributes']
        self.assertFalse('variables' in attrs)
        self.assertFalse('foo' in attrs)
        self.assertEqual(attrs['url'], l_attr_url)
Exemplo n.º 19
0
    def test_node_exists_startup_config_exists(self, m_repository):
        node = create_node()
        cfg = dict()

        def m_exists(arg):
            if arg.endswith(ztpserver.controller.STARTUP_CONFIG_FN):
                return True
            return False
        cfg['return_value.exists.side_effect'] = m_exists

        m_repository.configure_mock(**cfg)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(), node=node,
                                               node_id=node.serialnumber)

        self.assertEqual(state, 'dump_node')
        self.assertEqual(resp['status'], constants.HTTP_STATUS_CONFLICT)
Exemplo n.º 20
0
    def test_get_definition_w_attributes_no_substitution(self, m_load_pattern,
                                                         m_repository):

        node = create_node()

        g_attr_foo = random_string()
        attributes_file = create_attributes()
        attributes_file.add_attribute('variables', {'foo': g_attr_foo})

        l_attr_url = random_string()
        definitions_file = create_definition()
        definitions_file.add_attribute('foo', random_string())
        definitions_file.add_action(name='dummy action',
                                    attributes=dict(url=l_attr_url))

        cfg = dict()
        def m_get_file(arg):
            m_file_object = Mock()
            if arg.endswith('.node'):
                m_file_object.read.return_value = node.as_dict()
            elif arg.endswith('definition'):
                m_file_object.read.return_value = definitions_file.as_dict()
            elif arg.endswith('attributes'):
                m_file_object.read.return_value = attributes_file.as_dict()
            elif arg.endswith('startup-config'):
                raise ztpserver.repository.FileObjectNotFound
            return m_file_object
        cfg['return_value.get_file'] = Mock(side_effect=m_get_file)

        cfg['return_value.match_node.return_value'] = True
        m_repository.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')

        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)

        attrs = resp.json['actions'][0]['attributes']
        self.assertFalse('variables' in attrs)
        self.assertFalse('foo' in attrs)
        self.assertEqual(attrs['url'], l_attr_url)
Exemplo n.º 21
0
    def test_node_exists_sysmac_folder_exists(self, m_repository):
        node = create_node()
        cfg = dict()

        def m_exists(arg):
            if arg.endswith(node.serialnumber):
                return True
            return False
        cfg['return_value.exists.side_effect'] = m_exists

        m_repository.configure_mock(**cfg)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(), node=node,
                                               node_id=node.serialnumber)

        self.assertEqual(state, None)
        self.assertEqual(resp['status'], 
                         constants.HTTP_STATUS_BAD_REQUEST)
Exemplo n.º 22
0
    def test_node_exists_startup_config_exists(self, m_repository):
        node = create_node()
        cfg = dict()

        def m_exists(arg):
            if arg.endswith(ztpserver.controller.STARTUP_CONFIG_FN):
                return True
            return False

        cfg['return_value.exists.side_effect'] = m_exists

        m_repository.configure_mock(**cfg)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(),
                                               node=node,
                                               node_id=node.serialnumber)

        self.assertEqual(state, 'dump_node')
        self.assertEqual(resp['status'], constants.HTTP_STATUS_CONFLICT)
    def test_do_validation_success(self):
        node = create_node()

        filestore = Mock()
        ztpserver.controller.create_file_store = filestore

        ztpserver.neighbordb.load_pattern = Mock()
        cfg = {'return_value.match_node.return_value': True}
        ztpserver.neighbordb.load_pattern.configure_mock(**cfg)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.do_validation(dict(),
                                                 resource=node.systemmac,
                                                 node=Mock())

        filepath = '%s/%s' % (node.systemmac, ztpserver.controller.PATTERN_FN)
        filestore.return_value.get_file.assert_called_with(filepath)
        self.assertEqual(state, 'get_startup_config')
        self.assertIsInstance(resp, dict)
        self.assertEqual(resp, dict())
Exemplo n.º 24
0
    def test_add_neighbors_success(self):
        nodeattrs = create_node()

        remote_device = random_string()
        remote_interface = random_string()
        neighbors = {
            'Ethernet1': [{
                'device': remote_device,
                'port': remote_interface
            }]
        }

        kwargs = nodeattrs.as_dict()
        node = Node(**kwargs)
        node.add_neighbors(neighbors)

        self.assertIsNotNone(node.neighbors('Ethernet1'))
        self.assertEqual(node.neighbors('Ethernet1')[0].device, remote_device)
        self.assertEqual(
            node.neighbors('Ethernet1')[0].interface, remote_interface)
Exemplo n.º 25
0
    def test_node_exists_sysmac_folder_exists(self, m_repository):
        node = create_node()
        cfg = dict()

        def m_exists(arg):
            if arg.endswith(node.serialnumber):
                return True
            return False

        cfg['return_value.exists.side_effect'] = m_exists

        m_repository.configure_mock(**cfg)

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.node_exists(dict(),
                                               node=node,
                                               node_id=node.serialnumber)

        self.assertEqual(state, None)
        self.assertEqual(resp['status'], constants.HTTP_STATUS_BAD_REQUEST)
    def test_get_definition_success(self):

        node = create_node()

        definition = create_definition()

        filestore = Mock()
        filestore.return_value.get_file.return_value = \
            Mock(contents=definition.as_yaml())
        ztpserver.controller.create_file_store = filestore

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.get_definition(dict(),
                                                  resource=node.systemmac)

        filepath = '%s/%s' % (node.systemmac,
                              ztpserver.controller.DEFINITION_FN)
        filestore.return_value.get_file.assert_called_with(filepath)
        self.assertEqual(state, 'do_validation')
        self.assertIsInstance(resp, dict)
        self.assertEqual(resp['definition'], definition.as_dict())
    def test_post_node_success(self):
        node = create_node()

        definition_file = create_definition()
        definition_file.add_action()

        filestore = Mock()
        filestore.return_value.exists.return_value = False
        filestore.return_value.get_file.return_value.contents = \
            definition_file.as_yaml()

        ztpserver.controller.create_file_store = filestore

        ztpserver.neighbordb.topology.match_node = Mock(return_value=[Mock()])

        request = Request.blank('/nodes', body=node.as_json(), method='POST',
                                headers=ztp_headers())
        resp = request.get_response(ztpserver.controller.Router())

        location = 'http://localhost/nodes/%s' % node.systemmac
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(resp.location, location)
    def test_get_startup_config_w_validation_failure(self):
        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', False, 'default')

        node = create_node()

        filestore = Mock()

        def exists(filepath):
            if filepath.endswith('startup-config'):
                return True
            return False
        filestore.return_value.exists = Mock(side_effect=exists)

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
            else:
                type(fileobj).contents = mock.PropertyMock(side_effect=\
                    ztpserver.repository.FileObjectNotFound)
            return fileobj
        filestore.return_value.get_file = Mock(side_effect=get_file)

        ztpserver.controller.create_file_store = filestore

        ztpserver.neighbordb.load_pattern = Mock()
        cfg = {'return_value.match_node.return_value': False}
        ztpserver.neighbordb.load_pattern.configure_mock(**cfg)

        url = '/nodes/%s' % node.systemmac
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content_type, 'text/html')
        self.assertEqual(resp.body, str())
    def test_get_definition_w_validation_success(self):
        ztpserver.config.runtime.set_value(\
            'disable_topology_validation', False, 'default')

        node = create_node()

        definitions_file = create_definition()
        definitions_file.add_action()

        filestore = Mock()
        ztpserver.controller.create_file_store = filestore

        def get_file(filepath):
            fileobj = Mock()
            if filepath.endswith('node'):
                fileobj.contents = node.as_json()
            elif filepath.endswith('definition'):
                fileobj.contents = definitions_file.as_yaml()
            elif filepath.endswith('attributes'):
                type(fileobj).contents = mock.PropertyMock(side_effect=\
                    ztpserver.repository.FileObjectNotFound)
            return fileobj

        filestore.return_value.get_file = Mock(side_effect=get_file)

        ztpserver.neighbordb.load_pattern = Mock()
        cfg = {'return_value.match_node.return_value': True}
        ztpserver.neighbordb.load_pattern.configure_mock(**cfg)

        url = '/nodes/%s' % node.systemmac
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content_type, 'application/json')
        self.assertIsInstance(json.loads(resp.body), dict)
Exemplo n.º 30
0
    def test_get_startup_config_w_validation_success(self, m_load_pattern,
                                                     m_repository,
                                                     m_create_node):

        definition = create_definition()
        definition.add_action()

        node = create_node()
        cfg = dict()

        def m_get_file(arg):
            fileobj = Mock()
            if arg.endswith('.node'):
                fileobj.read.return_value = node.as_dict()
            elif arg.endswith('startup-config'):
                fileobj.read.return_value = random_string()
            elif arg.endswith('pattern'):
                fileobj.read.return_value = random_string()
            else:
                raise ztpserver.repository.FileObjectNotFound
            return fileobj

        cfg['return_value.get_file'] = Mock(side_effect=m_get_file)

        m_repository.configure_mock(**cfg)

        cfg = {'return_value.match_node.return_value': True}
        m_load_pattern.configure_mock(**cfg)

        url = '/nodes/%s' % node.serialnumber
        request = Request.blank(url, method='GET')
        resp = request.get_response(ztpserver.controller.Router())

        self.assertEqual(resp.status_code, constants.HTTP_STATUS_OK)
        self.assertEqual(resp.content_type, constants.CONTENT_TYPE_JSON)
        self.assertIsInstance(json.loads(resp.body), dict)
Exemplo n.º 31
0
 def test_create_node_success(self):
     node = create_node()
     systemmac = node.systemmac
     kwargs = node.as_dict()
     node = Node(**kwargs)
     self.assertEqual(node.systemmac, systemmac)
Exemplo n.º 32
0
 def test_serialize_success(self):
     nodeattrs = create_node()
     kwargs = nodeattrs.as_dict()
     node = Node(**kwargs)
     result = node.serialize()
     self.assertEqual(result, nodeattrs.as_dict())
 def test_create_node_success(self):
     node = create_node()
     systemmac = node.systemmac
     kwargs = node.as_dict()
     node = Node(**kwargs)
     self.assertEqual(node.systemmac, systemmac)
 def test_serialize_success(self):
     nodeattrs = create_node()
     kwargs = nodeattrs.as_dict()
     node = Node(**kwargs)
     result = node.serialize()
     self.assertEqual(result, nodeattrs.as_dict())