Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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())
Exemplo n.º 5
0
    def test_config_search_005():
        """Test that search keywords can be added so that they are separated
        by spaces before and after the words like in '-t docker container
        cleanup'."""

        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
Exemplo n.º 6
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
Exemplo n.º 7
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,)
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
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()
Exemplo n.º 13
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')
Exemplo n.º 14
0
    def test_cli_search_003():
        """Test that search keywords can be added inside quotes separated
        by comma and spaces after comma."""

        obj = Cli(['snippy', 'search', '--sall', 'docker, container, cleanup'])
        assert obj.sall == ('cleanup', 'container', 'docker')
Exemplo n.º 15
0
    def test_cli_search_001():
        """Test that search can be used with one keyword."""

        obj = Cli(['snippy', 'search', '--sall', 'docker'])
        assert obj.sall == ('docker', )
Exemplo 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')
Exemplo n.º 17
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')
Exemplo n.º 18
0
    def init(cls, args):
        """Initialize global configuration."""

        if args is None:
            args = []

        # Initialize colored CLI output for Windows.
        if Const.WINDOWS:
            from colorama import init  # pylint: disable=import-error
            init()

        # 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)
Exemplo n.º 19
0
    def test_config_create_001(mock_utcnow):
        """Create new snippet.

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

        mock_utcnow.return_value = '2018-02-17 13:23:43'

        Config.init(None)
        Config.load(Cli(['snippy', 'import']))
        assert isinstance(Config.content_category, Const.TEXT_TYPE)
        assert isinstance(Config.content_data, tuple)
        assert isinstance(Config.content_brief, Const.TEXT_TYPE)
        assert isinstance(Config.content_description, Const.TEXT_TYPE)
        assert isinstance(Config.content_groups, tuple)
        assert isinstance(Config.content_tags, tuple)
        assert isinstance(Config.content_links, tuple)
        assert isinstance(Config.content_name, Const.TEXT_TYPE)
        assert isinstance(Config.content_filename, Const.TEXT_TYPE)
        assert isinstance(Config.content_versions, tuple)
        assert isinstance(Config.content_source, Const.TEXT_TYPE)
        assert isinstance(Config.operation_digest, type(None))
        assert isinstance(Config.search_cat_kws, tuple)
        assert isinstance(Config.search_all_kws, tuple)
        assert isinstance(Config.search_tag_kws, tuple)
        assert isinstance(Config.search_grp_kws, tuple)
        assert Config.search_filter is None
        assert Config.search_limit == 99
        assert Config.search_offset == 0
        assert Config.remove_fields == ()
        assert Config.sort_fields == OrderedDict([('brief', 'ASC')])
        assert isinstance(Config.get_operation_file(), Const.TEXT_TYPE)
        assert not Config.get_resource(None)
        assert not Config.get_collection()
        assert not Config.is_operation_create
        assert not Config.is_operation_search
        assert not Config.is_operation_update
        assert not Config.is_operation_delete
        assert not Config.is_operation_export
        assert Config.is_operation_import
        assert Config.is_category_snippet
        assert not Config.is_category_solution
        assert not Config.is_category_reference
        assert not Config.is_multi_category
        assert Config.content_category == Const.SNIPPET
        assert not Config.content_data
        assert not Config.content_brief
        assert Config.content_groups == Const.DEFAULT_GROUPS
        assert not Config.content_tags
        assert not Config.content_links
        assert not Config.search_all_kws
        assert not Config.search_tag_kws
        assert not Config.search_grp_kws
        assert Config.operation_digest is None
        assert not Config.search_filter
        assert not Config.content_filename
        assert not Config.editor
        assert Config.get_operation_file() == './snippets.mkdn'
        assert Config.is_operation_file_mkdn
        assert not Config.is_operation_file_json
        assert not Config.is_operation_file_yaml
        assert not Config.is_operation_file_text
        assert Config.use_ansi