Esempio n. 1
0
    def test_cli_search_006():
        """Test that search keywords can be added so that they are separated
        by comma after the words like in '-t docker, container, cleanup'."""

        obj = Cli(
            ['snippy', 'search', '--sall', 'docker,', 'container,', 'cleanup'])
        assert obj.sall == ('cleanup', 'container', 'docker')
Esempio n. 2
0
    def test_config_create_013():
        """Test that multiple links can be added by separating them with
        bar."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        tags = ('cleanup', 'container', 'docker')
        links = (
            'https://askubuntu.com/questions/574163/how-to-stop-and-remove-a-docker-container',
            'https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes'
        )
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-b', brief, '-t',
                'docker, container, cleanup', '-l', '|'.join(links)
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert isinstance(Config.content_links, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_brief == brief
        assert Config.content_tags == tags
        assert Config.content_links == links
Esempio n. 3
0
    def test_config_create_006():
        """Test that tags can be added inside quotes separated by comma and
        space after comma."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        groups = ('docker', )
        tags = ('cleanup', 'container', 'docker')
        links = (
            'https://askubuntu.com/questions/574163/how-to-stop-and-remove-a-docker-container',
        )
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-b', brief, '-g', 'docker',
                '-t', 'docker, container, cleanup', '-l', links[0]
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_groups, tuple)
        assert isinstance(Config.content_tags, tuple)
        assert isinstance(Config.content_links, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_brief == brief
        assert Config.content_groups == groups
        assert Config.content_tags == tags
        assert Config.content_links == links
Esempio n. 4
0
    def init(cls, args):
        """Initialize global configuration."""

        if args is None:
            args = []

        # Set logger and development configuration.
        cls._init_logs(args)

        cls.source = Cli(args)

        # Static storage and template configurations.
        cls.storage_type = cls.source.storage_type
        cls.storage_schema = cls._test_resource('data/storage', 'database.sql')
        cls.storage_path = cls.source.storage_path
        cls.storage_file = cls._storage_file()
        cls.storage_host = cls.source.storage_host
        cls.storage_user = cls.source.storage_user
        cls.storage_password = cls.source.storage_password
        cls.storage_database = cls.source.storage_database
        cls.storage_ssl_cert = cls.source.storage_ssl_cert
        cls.storage_ssl_key = cls.source.storage_ssl_key
        cls.storage_ssl_ca_cert = cls.source.storage_ssl_ca_cert
        cls.templates = {
            'text': {
                'snippet': cls._read_resource('data/templates', 'snippet.txt'),
                'solution': cls._read_resource('data/templates',
                                               'solution.txt'),
                'reference': cls._read_resource('data/templates',
                                                'reference.txt')
            },
            'mkdn': {
                'snippet': cls._read_resource('data/templates', 'snippet.md'),
                'solution': cls._read_resource('data/templates',
                                               'solution.md'),
                'reference': cls._read_resource('data/templates',
                                                'reference.md')
            }
        }
        cls.completion = {
            'bash': cls._read_resource('data/completion',
                                       'snippy.bash-completion')
        }

        # Static server configurations.
        cls.server_readonly = cls.source.server_readonly
        cls.server_base_path_rest = cls.source.server_base_path_rest
        cls.server_minify_json = cls.source.server_minify_json
        cls.server_host = cls.source.server_host
        cls.server_ssl_cert = cls._ssl_file(cls.source.server_ssl_cert)
        cls.server_ssl_key = cls._ssl_file(cls.source.server_ssl_key)
        cls.server_ssl_ca_cert = cls._ssl_file(cls.source.server_ssl_ca_cert)

        # Server and healthchecks are started only once.
        cls.run_server = cls.source.run_server
        cls.run_healthcheck = cls.source.run_healthcheck

        # Dynamic configuration.
        cls.load(cls.source)
Esempio n. 5
0
    def test_config_search_001():
        """Test that search can be used with one keyword."""

        search_kw = ('docker', )
        Config.init(None)
        Config.load(Cli(['snippy', 'search', '--sall', 'docker']))
        assert isinstance(Config.search_all_kws, tuple)
        assert Config.search_all_kws == search_kw
Esempio n. 6
0
    def test_cli_create_010():
        """Test that tags are accepted if they contain special characters."""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker–testing, ', 'container-managemenet, ', 'cleanup_testing'])
        assert obj.data == (content,)
        assert obj.brief == ''
        assert obj.tags == (u'cleanup_testing', u'container-managemenet', u'docker–testing')
Esempio n. 7
0
    def test_cli_create_002():
        """Test that new snippet can be created without optional arguments."""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content])
        assert obj.data == (content, )
        assert obj.brief == ''
        assert obj.tags == ()
Esempio n. 8
0
    def test_cli_create_005():
        """Test that tags can be added inside quotes separated by comma and
        without spaces."""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker,container,cleanup'])
        assert obj.data == (content,)
        assert obj.brief == ''
        assert obj.tags == ('cleanup', 'container', 'docker')
Esempio n. 9
0
    def test_cli_create_007():
        """Test that tags can be added so that they are separated by spaces
        before and after the words."""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker container cleanup'])
        assert obj.data == (content,)
        assert obj.brief == ''
        assert obj.tags == ('cleanup', 'container', 'docker')
Esempio n. 10
0
    def test_cli_create_009():
        """Test that tags can be added so that they are separated by comma
        after the words like in '-t docker, container, cleanup'."""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker,', 'container,', 'cleanup'])
        assert obj.data == (content,)
        assert obj.brief == ''
        assert obj.tags == ('cleanup', 'container', 'docker')
Esempio n. 11
0
    def test_cli_create_004():
        """Test that new snippet can be created with a single tag."""

        content = 'docker rm $(docker ps -a -q)'
        tags = ['docker']
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker'])
        assert obj.data == (content, )
        assert obj.brief == ''
        assert obj.tags == tuple(tags, )
Esempio n. 12
0
    def test_config_search_003():
        """Test that search keywords can be added inside quotes separated by
        comma and spaces after comma."""

        search_kw = ('cleanup', 'container', 'docker')
        Config.init(None)
        Config.load(
            Cli(['snippy', 'search', '--sall', 'docker, container, cleanup']))
        assert isinstance(Config.search_all_kws, tuple)
        assert Config.search_all_kws == search_kw
Esempio n. 13
0
    def test_config_search_004():
        """Test that search keywords can be added so that they are separated
        by spaces before and after the words."""

        search_kw = ('cleanup', 'container', 'docker')
        Config.init(None)
        Config.load(
            Cli(['snippy', 'search', '--sall', 'docker, container, cleanup']))
        assert isinstance(Config.search_all_kws, tuple)
        assert Config.search_all_kws == search_kw
Esempio n. 14
0
    def test_cli_create_003():
        """Test that new snippet can be created with brief description but
        no tags."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        obj = Cli(['snippy', 'create', '-c', content, '-b', brief])
        assert obj.data == (content, )
        assert obj.brief == brief
        assert obj.tags == ()
Esempio n. 15
0
    def test_cli_create_011():
        """Test that tags are accepted if the tags are elements in a list.
        This might not be realistic case since user might not be able to
        reproduce this?"""

        content = 'docker rm $(docker ps -a -q)'
        obj = Cli(['snippy', 'create', '-c', content, '-t', 'docker', 'container', 'cleanup'])
        assert obj.data == (content,)
        assert obj.brief == ''
        assert obj.tags == ('cleanup', 'container', 'docker')
Esempio n. 16
0
    def test_cli_search_007():
        """Test that search keywords are accepted if they contain special
        characters."""

        obj = Cli([
            'snippy', 'search', '--sall', 'docker–testing, ',
            'container-managemenet, ', 'cleanup_testing'
        ])
        assert obj.sall == (u'cleanup_testing', u'container-managemenet',
                            u'docker–testing')
Esempio n. 17
0
    def test_config_create_002():
        """Test that new snippet can be created without optional arguments."""

        content = 'docker rm $(docker ps -a -q)'
        Config.init(None)
        Config.load(Cli(['snippy', 'create', '-c', content]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert not Config.content_brief
        assert not Config.content_tags
Esempio n. 18
0
    def test_config_create_004():
        """Test that new snippet can be created with a single tag."""

        content = 'docker rm $(docker ps -a -q)'
        tags = ('docker', )
        Config.init(None)
        Config.load(Cli(['snippy', 'create', '-c', content, '-t', 'docker']))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert not Config.content_brief
        assert Config.content_tags == tags
Esempio n. 19
0
    def test_cli_create_001():
        """Create new snippet.

        Test default values when only mandatory arguments are used.
        """

        cli = Cli(['snippy', 'create'])
        assert cli.brief == ''
        assert cli.category == Const.SNIPPET
        assert cli.data == ()
        assert not cli.debug
        assert not cli.defaults
        assert cli.description == ''
        assert cli.digest is None
        assert cli.editor
        assert not cli.no_editor
        assert not cli.failure
        assert cli.filename == ''
        assert cli.groups == ('default', )
        assert cli.links == ()
        assert not cli.log_json
        assert cli.log_msg_max == Cli.DEFAULT_LOG_MSG_MAX
        assert cli.merge
        assert cli.name == ''
        assert not cli.no_ansi
        assert cli.operation == 'create'
        assert not cli.profiler
        assert not cli.quiet
        assert cli.remove_fields == ()
        assert cli.sall == ()
        assert cli.scat == ('snippet', )
        assert cli.search_filter is None
        assert cli.search_limit == Cli.LIMIT_DEFAULT_CLI
        assert cli.search_offset == 0
        assert cli.server_base_path_rest == '/api/snippy/rest/'
        assert cli.server_host == ''
        assert not cli.server_minify_json
        assert cli.server_ssl_ca_cert is None
        assert cli.server_ssl_cert is None
        assert cli.server_ssl_key is None
        assert cli.sgrp == ()
        assert cli.sort_fields == OrderedDict([('brief', 'ASC')])
        assert cli.source == ''
        assert cli.stag == ()
        assert cli.storage_path == ''
        assert cli.tags == ()
        assert not cli.template
        assert cli.uuid is None
        assert cli.version is None  # Tool version
        assert cli.versions == ()  # Content versions
        assert not cli.very_verbose
Esempio n. 20
0
    def test_config_create_003():
        """Test that new snippet can be created with brief description but
        no tags."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        Config.init(None)
        Config.load(Cli(['snippy', 'create', '-c', content, '-b', brief]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_brief == brief
        assert not Config.content_tags
Esempio n. 21
0
    def test_cli_create_012():
        """Test that multiple links can be added by separating them with
        space."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        tags = 'docker, container, cleanup'
        links = 'https://askubuntu.com/questions/574163/how-to-stop-and-remove-a-docker-container \
                 https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes'
        obj = Cli(['snippy', 'create', '-c', content, '-b', brief, '-t', tags, '-l', links])
        assert obj.data == (content,)
        assert obj.brief == brief
        assert obj.tags == ('cleanup', 'container', 'docker')
        assert obj.links == tuple(links.split())
Esempio n. 22
0
    def test_cli_create_006():
        """Test that tags can be added inside quotes separated by comma and
        spaces after comma."""

        content = 'docker rm $(docker ps -a -q)'
        brief = 'Remove all docker containers'
        groups = 'docker'
        tags = 'docker, container, cleanup'
        links = 'https://askubuntu.com/questions/574163/how-to-stop-and-remove-a-docker-container'
        obj = Cli(['snippy', 'create', '-c', content, '-b', brief, '-g', groups, '-t', tags, '-l', links])
        assert obj.data == (content,)
        assert obj.brief == brief
        assert obj.groups == (groups,)
        assert obj.tags == ('cleanup', 'container', 'docker')
        assert obj.links == (links,)
Esempio n. 23
0
    def test_config_search_007():
        """Test that search keywords are accepted if they contain special
        characters."""

        search_kw = (u'cleanup_testing', u'container-managemenet',
                     u'docker–testing')
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'search', '--sall', 'docker–testing, ',
                'container-managemenet, ', 'cleanup_testing'
            ]))
        assert isinstance(Config.search_all_kws, tuple)
        assert Config.search_all_kws == search_kw
        assert len(Config.search_all_kws) == 3
Esempio n. 24
0
    def test_config_create_009():
        """Test that tags can be added so that they are separated by comma
        after the words like in '-t docker, container, cleanup'."""

        content = 'docker rm $(docker ps -a -q)'
        tags = ('cleanup', 'container', 'docker')
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-t', 'docker,',
                'container,', 'cleanup'
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_tags == tags
Esempio n. 25
0
    def test_config_create_005():
        """Test that tags can be added inside quotes separated by comma and
        without spaces."""

        content = 'docker rm $(docker ps -a -q)'
        tags = ('cleanup', 'container', 'docker')
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-t',
                'docker,container,cleanup'
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert not Config.content_brief
        assert Config.content_tags == tags
Esempio n. 26
0
    def test_config_create_010():
        """Test that tags are accepted if they contain special characters."""

        content = 'docker rm $(docker ps -a -q)'
        tags = (u'cleanup_testing', u'container-managemenet',
                u'docker–testing')
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-t', 'docker–testing, ',
                'container-managemenet, ', 'cleanup_testing'
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_tags == tags
        assert len(Config.content_tags) == 3
Esempio n. 27
0
    def test_config_create_011():
        """Test that tags are accepted if the tags are elements in a list.
        This might not be realistic case since user might not be able to
        reproduce this?"""

        content = 'docker rm $(docker ps -a -q)'
        tags = ('cleanup', 'container', 'docker')
        Config.init(None)
        Config.load(
            Cli([
                'snippy', 'create', '-c', content, '-t', 'docker', 'container',
                'cleanup'
            ]))
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_tags, tuple)
        assert Config.content_data == tuple([content])
        assert Config.content_tags == tags
Esempio n. 28
0
    def run(self, args=None):
        """Run service.

        Args:
            list: Command line arguments.
        """

        if args:
            Config.load(Cli(args))

        if Config.failure:
            Cause.print_failure()

            return Cause.reset()

        if Config.run_server:
            self._run_server()
        elif Config.run_healthcheck:
            self._run_healthcheck()
        else:
            self._run_cli()

        return Cause.reset()
Esempio n. 29
0
    def _init_logs(cls, args):
        """Init logger and development configuration.

        Parse log configuration manually from sys.argv in order to initialize
        logger as early as possible. The same parameters are read eventually
        by the Cli class parser.

        Args:
            args (list): Command line arguments.
        """

        cls.debug_logs = Cli.read_arg('--debug', False, args)
        cls.log_json = Cli.read_arg('--log-json', False, args)
        cls.log_msg_max = Cli.read_arg('--log-msg-max',
                                       Logger.DEFAULT_LOG_MSG_MAX, args)
        cls.profiler = Cli.read_arg('--profile', False, args)
        cls.quiet = Cli.read_arg('-q', False, args)
        cls.very_verbose = Cli.read_arg('-vv', False, args)

        # Profile code.
        Profiler.enable(cls.profiler)

        cls._update_logger()
Esempio n. 30
0
    def test_cli_search_004():
        """Test that search keywords can be added so that they are separated
        by spaces before and after the words."""

        obj = Cli(['snippy', 'search', '--sall', 'docker container cleanup'])
        assert obj.sall == ('cleanup', 'container', 'docker')