def test_do_substitution_with_attributes(self):

        ztpserver.controller.create_file_store = Mock()

        defattrs = dict(foo='$foo')

        definition = create_definition()
        definition.add_attribute('foo', 'bar')
        definition.add_action(name='dummy action', attributes=defattrs)

        g_attr_foo = random_string()
        attributes = create_attributes()
        attributes.add_attribute('foo', g_attr_foo)

        response = dict(definition=definition.as_dict(),
                        attributes=attributes.as_dict())

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.do_substitution(response)

        self.assertEqual(state, 'do_resources')
        self.assertIsInstance(resp, dict)

        foo = resp['definition']['actions'][0]['attributes']['foo']
        self.assertEqual(foo, g_attr_foo)
    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)
Пример #3
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)
Пример #4
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)
Пример #5
0
    def test_do_substitution_with_attributes(self):
        defattrs = dict(foo='$foo')

        definition = create_definition()
        definition.add_attribute('foo', 'bar')
        definition.add_action(name='dummy action', attributes=defattrs)

        g_attr_foo = random_string()
        attributes = create_attributes()
        attributes.add_attribute('foo', g_attr_foo)

        response = dict(definition=definition.as_dict(),
                        attributes=attributes.as_dict())

        controller = ztpserver.controller.NodesController()
        (resp, state) = controller.do_substitution(response,
                                                   resource=random_string())

        self.assertEqual(state, 'do_resources')
        self.assertIsInstance(resp, dict)

        foo = resp['definition']['actions'][0]['attributes']['foo']
        self.assertEqual(foo, g_attr_foo)