示例#1
0
 def test_max_instances(self, plugin):
     system = System(
         name="name",
         version="1.0.0",
         instances=[Instance(name="1"),
                    Instance(name="2")],
     )
     new_system = plugin._setup_system(system, {})
     assert new_system.max_instances == 2
示例#2
0
 def test_max_instances(self, plugin, client):
     system = System(
         name="name",
         version="1.0.0",
         instances=[Instance(name="1"),
                    Instance(name="2")],
     )
     new_system = plugin._setup_system(client, "default", system, "", "",
                                       "", "", {}, None, None)
     assert new_system.max_instances == 2
示例#3
0
 def default_system(self, command1):
     return System(
         name="foo",
         version="1.0.0",
         instances=[Instance(name="foo")],
         commands=[command1],
     )
示例#4
0
    def _setup_system(self, client, inst_name, system, name, description, version, icon_name,
                      metadata, display_name, max_instances):
        if system:
            if name or description or version or icon_name or display_name or max_instances:
                raise ValidationError("Sorry, you can't specify a system as well as system "
                                      "creation helper keywords (name, description, version, "
                                      "max_instances, display_name, and icon_name)")

            if not system.instances:
                raise ValidationError("Explicit system definition requires explicit instance "
                                      "definition (use instances=[Instance(name='default')] "
                                      "for default behavior)")

            if not system.max_instances:
                system.max_instances = len(system.instances)

        else:
            name = name or os.environ.get('BG_NAME', None)
            version = version or os.environ.get('BG_VERSION', None)

            if client.__doc__ and not description:
                description = self.client.__doc__.split("\n")[0]

            system = System(name=name, description=description, version=version,
                            icon_name=icon_name, commands=client._commands,
                            max_instances=max_instances or 1,
                            instances=[Instance(name=inst_name)],
                            metadata=metadata, display_name=display_name)

        return system
示例#5
0
    def test_existing_multiple(self, tmp_path, loader, registry, plugin_1, bg_instance):
        """This is mainly to test that Instance IDs are correct

        We save a system with 2 instances:
         - instance1, 58542eb571afd47ead90beef
         - instance2, 58542eb571afd47ead90beee

        Then we load a plugin that defines instances [instance2, instance3].

        Correct behavior is:
         - instance1 removed from the database
         - instance3 created in the database
         - instance2 remains in the database, and the ID remains the same
        """

        instance1 = Instance(name="instance1", id="58542eb571afd47ead90beef")
        instance2 = Instance(name="instance2", id="58542eb571afd47ead90beee")
        create_system(
            System(name="foo", version="1.0", instances=[instance1, instance2])
        )

        plugin = tmp_path / "plugin"
        plugin.mkdir()

        write_file(
            plugin,
            textwrap.dedent(
                """
                NAME='foo'
                VERSION='1.0'
                PLUGIN_ENTRY='entry.py'
                INSTANCES=["instance2", "instance3"]
            """
            ),
        )

        plugin_runners = loader.load_plugin(plugin)
        assert len(plugin_runners) == 2

        assert db.query_unique(Instance, name="instance1") is None
        assert db.query_unique(Instance, name="instance3") is not None

        instance2_db = db.query_unique(Instance, name="instance2")
        assert instance2_db is not None
        assert instance2_db.id == instance2.id
示例#6
0
    def _initialize(self):
        self.logger.debug("Initializing plugin %s", self.unique_name)

        # TODO: We should use self.bm_client.upsert_system once it is supported
        existing_system = self.bm_client.find_unique_system(
            name=self.system.name, version=self.system.version)

        if existing_system:
            if existing_system.has_different_commands(self.system.commands):
                new_commands = self.system.commands
            else:
                new_commands = None

            if not existing_system.has_instance(self.instance_name):
                if len(existing_system.instances) < existing_system.max_instances:
                    existing_system.instances.append(Instance(name=self.instance_name))
                    self.bm_client.create_system(existing_system)
                else:
                    raise PluginValidationError('Unable to create plugin with instance name "%s": '
                                                'System "%s[%s]" has an instance limit of %d and '
                                                'existing instances %s' %
                                                (self.instance_name, existing_system.name,
                                                 existing_system.version,
                                                 existing_system.max_instances,
                                                 ', '.join(existing_system.instance_names)))

            # We always update in case the metadata has changed.
            self.system = self.bm_client.update_system(existing_system.id,
                                                       new_commands=new_commands,
                                                       metadata=self.system.metadata,
                                                       description=self.system.description,
                                                       display_name=self.system.display_name,
                                                       icon_name=self.system.icon_name)
        else:
            self.system = self.bm_client.create_system(self.system)

        # Sanity check to make sure an instance with this name was registered
        if self.system.has_instance(self.instance_name):
            instance_id = self.system.get_instance(self.instance_name).id
        else:
            raise PluginValidationError('Unable to find registered instance with name "%s"' %
                                        self.instance_name)

        self.instance = self.bm_client.initialize_instance(instance_id)

        self.queue_connection_params = self.instance.queue_info.get('connection', None)
        if self.queue_connection_params and self.queue_connection_params.get('ssl', None):
            self.queue_connection_params['ssl'].update({
                'ca_cert': self.ca_cert,
                'ca_verify': self.ca_verify,
                'client_cert': self.client_cert,
            })

        self.logger.debug("Plugin %s is initialized", self.unique_name)
示例#7
0
    def _setup_system(self, system, plugin_kwargs):
        helper_keywords = {
            "name",
            "version",
            "description",
            "icon_name",
            "display_name",
            "max_instances",
            "metadata",
            "namespace",
            "template",
        }

        if system:
            if helper_keywords.intersection(plugin_kwargs.keys()):
                raise ValidationError(
                    "Sorry, you can't provide a complete system definition as well as "
                    "system creation helper kwargs %s" % helper_keywords
                )

            if not system.instances:
                raise ValidationError(
                    "Explicit system definition requires explicit instance "
                    "definition (use instances=[Instance(name='default')] for "
                    "default behavior)"
                )

            if not system.max_instances:
                system.max_instances = len(system.instances)

        else:
            # Commands are not defined here - they're set in the client property setter
            system = System(
                name=self._config.name,
                version=self._config.version,
                description=self._config.description,
                namespace=self._config.namespace,
                metadata=json.loads(self._config.metadata),
                instances=[Instance(name=self._config.instance_name)],
                max_instances=self._config.max_instances,
                icon_name=self._config.icon_name,
                display_name=self._config.display_name,
                template=self._config.template,
            )

        return system
示例#8
0
    def setUp(self):
        self.app = brew_view.app.test_client()

        self.default_instance = Instance(name='default', status='RUNNING')
        self.default_command = Command(id='54ac18f778c4b57e963f3c18',
                                       name='command',
                                       description='foo')
        self.default_system = System(id='54ac18f778c4b57e963f3c18',
                                     name='default_system',
                                     version='1.0.0',
                                     instances=[self.default_instance],
                                     commands=[self.default_command])

        self.client_mock = Mock(name='client_mock')
        self.fake_context = MagicMock(
            __enter__=Mock(return_value=self.client_mock),
            __exit__=Mock(return_value=False))
示例#9
0
    def test_existing(self, loader, registry, plugin_1):
        system_id = "58542eb571afd47ead90face"
        instance_id = "58542eb571afd47ead90beef"
        create_system(
            System(
                id=system_id,
                name="foo",
                version="1.0",
                instances=[Instance(id=instance_id)],
            )
        )

        plugin_runners = loader.load_plugin(plugin_1)

        assert len(plugin_runners) == 1
        assert str(plugin_runners[0].system.id) == system_id
        assert str(plugin_runners[0].instance.id) == instance_id
        assert plugin_runners[0].name == "foo[default]-1.0"
        assert plugin_runners[0].entry_point == "entry.py"
示例#10
0
    def setUp(self):
        self.app = brew_view.app.test_client()

        self.default_instance = Instance(name="default", status="RUNNING")
        self.default_command = Command(
            id="54ac18f778c4b57e963f3c18", name="command", description="foo"
        )
        self.default_system = System(
            id="54ac18f778c4b57e963f3c18",
            name="default_system",
            version="1.0.0",
            instances=[self.default_instance],
            commands=[self.default_command],
        )

        self.client_mock = Mock(name="client_mock")
        self.fake_context = MagicMock(
            __enter__=Mock(return_value=self.client_mock),
            __exit__=Mock(return_value=False),
        )
示例#11
0
    def setUp(self):
        self.safe_copy = os.environ.copy()

        consumer_patcher = patch('brewtils.plugin.RequestConsumer')
        self.addCleanup(consumer_patcher.stop)
        self.consumer_patch = consumer_patcher.start()

        self.instance = Instance(id='id', name='default', queue_type='rabbitmq',
                                 queue_info={'url': 'url', 'admin': {'name': 'admin_queue'},
                                             'request': {'name': 'request_queue'}})
        self.system = System(name='test_system', version='1.0.0', instances=[self.instance],
                             metadata={'foo': 'bar'})
        self.client = MagicMock(name='client', spec='command', _commands=['command'])
        self.plugin = PluginBase(self.client, bg_host='localhost', system=self.system,
                                 metadata={'foo': 'bar'})
        self.plugin.instance = self.instance

        self.bm_client_mock = Mock(create_system=Mock(return_value=self.system),
                                   initialize_instance=Mock(return_value=self.instance))
        self.plugin.bm_client = self.bm_client_mock

        self.parser_mock = Mock()
        self.plugin.parser = self.parser_mock
示例#12
0
 def __repr__(self):
     return BrewtilsInstance.__repr__(self)
示例#13
0
    def _initialize_system(self):
        """Let Beergarden know about System-level info

        This will attempt to find a system with a name and version matching this plugin.
        If one is found this will attempt to update it (with commands, metadata, etc.
        from this plugin).

        If a System is not found this will attempt to create one.

        Returns:
            Definition of a Beergarden System this plugin belongs to.

        Raises:
            PluginValidationError: Unable to find or create a System for this Plugin
        """
        # Make sure that the system is actually valid before trying anything
        self._validate_system()

        # Do any necessary template resolution
        self._system.template = resolve_template(self._system.template)

        existing_system = self._ez_client.find_unique_system(
            name=self._system.name,
            version=self._system.version,
            namespace=self._system.namespace,
        )

        if not existing_system:
            try:
                # If this succeeds can just finish here
                return self._ez_client.create_system(self._system)
            except ConflictError:
                # If multiple instances are starting up at once and this is a new system
                # the create can return a conflict. In that case just try the get again
                existing_system = self._ez_client.find_unique_system(
                    name=self._system.name,
                    version=self._system.version,
                    namespace=self._system.namespace,
                )

        # If we STILL can't find a system something is really wrong
        if not existing_system:
            raise PluginValidationError(
                "Unable to find or create system {0}".format(self._system)
            )

        # We always update with these fields
        update_kwargs = {
            "new_commands": self._system.commands,
            "metadata": self._system.metadata,
            "description": self._system.description,
            "display_name": self._system.display_name,
            "icon_name": self._system.icon_name,
            "template": self._system.template,
        }

        # And if this particular instance doesn't exist we want to add it
        if not existing_system.has_instance(self._config.instance_name):
            update_kwargs["add_instance"] = Instance(name=self._config.instance_name)

        return self._ez_client.update_system(existing_system.id, **update_kwargs)
示例#14
0
 def test_repr(self):
     instance = Instance(name='name', status='RUNNING')
     assert 'name' in repr(instance)
     assert 'RUNNING' in repr(instance)
示例#15
0
 def test_setup_system_no_max_instances(self):
     system = System(name='test_system', version='1.0.0', instances=[Instance(name='1'),
                                                                     Instance(name='2')])
     new_system = self.plugin._setup_system(self.client, 'default', system, '', '', '', '', {},
                                            None, None)
     self.assertEqual(2, new_system.max_instances)
示例#16
0
def bg_instance(instance_dict, ts_dt):
    """An instance as a model."""
    dict_copy = copy.deepcopy(instance_dict)
    dict_copy['status_info']['heartbeat'] = ts_dt
    return Instance(**dict_copy)
示例#17
0
 def default_system(self, command1):
     return System(name='foo',
                   version='1.0.0',
                   instances=[Instance(name='foo')],
                   commands=[command1])
示例#18
0
 def test_repr(self):
     instance = Instance(name="name", status="RUNNING")
     assert "name" in repr(instance)
     assert "RUNNING" in repr(instance)
示例#19
0
 def test_str(self):
     assert "name" == str(Instance(name="name"))
示例#20
0
 def test_str(self):
     assert 'name' == str(Instance(name='name'))