You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2019/11/13 23:14:13 UTC

[allura] 04/05: [#8340] tests for scripts/commands

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8340
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 0e9a8189cca4a1bf4b0154cd3e37b9d851d0a943
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Wed Nov 13 16:34:46 2019 -0500

    [#8340] tests for scripts/commands
---
 Allura/allura/tests/decorators.py             | 17 +++++-
 Allura/allura/tests/scripts/test_reindexes.py | 74 +++++++++++++++++++++++++++
 Allura/allura/tests/test_commands.py          | 36 ++++++++++++-
 ForgeGit/forgegit/tests/test_tasks.py         | 17 ++----
 4 files changed, 128 insertions(+), 16 deletions(-)

diff --git a/Allura/allura/tests/decorators.py b/Allura/allura/tests/decorators.py
index 7c42206..726a402 100644
--- a/Allura/allura/tests/decorators.py
+++ b/Allura/allura/tests/decorators.py
@@ -14,7 +14,7 @@
 #       KIND, either express or implied.  See the License for the
 #       specific language governing permissions and limitations
 #       under the License.
-
+import logging
 import sys
 import re
 from functools import wraps
@@ -198,3 +198,18 @@ def out_audits(*messages, **kwargs):
     for message in messages:
         assert not M.AuditLog.query.find(dict(
             message=re.compile(preamble + message))).count(), 'Found unexpected: "%s"' % message
+
+
+# not a decorator but use it with LogCapture() decorator
+def assert_logmsg_and_no_warnings_or_errors(logs, msg):
+    """
+    :param testfixtures.logcapture.LogCapture logs: LogCapture() instance
+    :param str msg: Message to look for
+    """
+    found_msg = False
+    for r in logs.records:
+        if msg in r.getMessage():
+            found_msg = True
+        if r.levelno > logging.INFO:
+            raise AssertionError('unexpected log {} {}'.format(r.levelname, r.getMessage()))
+    assert found_msg, 'Did not find {} in logs: {}'.format(msg, '\n'.join([r.getMessage() for r in logs.records]))
diff --git a/Allura/allura/tests/scripts/test_reindexes.py b/Allura/allura/tests/scripts/test_reindexes.py
new file mode 100644
index 0000000..d6dbeb0
--- /dev/null
+++ b/Allura/allura/tests/scripts/test_reindexes.py
@@ -0,0 +1,74 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+from nose.tools import assert_in, assert_equal
+from testfixtures import LogCapture
+
+from allura.scripts.reindex_projects import ReindexProjects
+from allura.scripts.reindex_users import ReindexUsers
+from allura.tests.decorators import assert_logmsg_and_no_warnings_or_errors
+from alluratest.controller import setup_basic_test
+from allura import model as M
+
+
+class TestReindexProjects(object):
+
+    def setUp(self):
+        setup_basic_test()
+
+    def run_script(self, options):
+        cls = ReindexProjects
+        opts = cls.parser().parse_args(options)
+        cls.execute(opts)
+
+    def test(self):
+        with LogCapture() as logs:
+            self.run_script(['-n', '/p/', '-p', 'test'])
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex project test')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex done')
+
+    def test_with_tasks(self):
+        with LogCapture() as logs:
+            self.run_script(['-n', '/p/', '-p', 'test', '--tasks'])
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex project test')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex queued')
+        assert_equal(M.MonQTask.query.find({'task_name': 'allura.tasks.index_tasks.add_projects'}).count(), 1)
+
+
+class TestReindexUsers(object):
+
+    def setUp(self):
+        setup_basic_test()
+
+    def run_script(self, options):
+        cls = ReindexUsers
+        opts = cls.parser().parse_args(options)
+        cls.execute(opts)
+
+    def test(self):
+        with LogCapture() as logs:
+            self.run_script([])
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex user root')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex user test-user-1')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex done')
+
+    def test_with_tasks(self):
+        with LogCapture() as logs:
+            self.run_script(['--tasks'])
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex user root')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex user test-user-1')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Reindex queued')
+        assert_equal(M.MonQTask.query.find({'task_name': 'allura.tasks.index_tasks.add_users'}).count(), 1)
diff --git a/Allura/allura/tests/test_commands.py b/Allura/allura/tests/test_commands.py
index ee97f7e..19cd19b 100644
--- a/Allura/allura/tests/test_commands.py
+++ b/Allura/allura/tests/test_commands.py
@@ -16,6 +16,8 @@
 #       under the License.
 
 from nose.tools import assert_raises, assert_in
+from testfixtures import OutputCapture
+
 from datadiff.tools import assert_equal
 
 from ming.base import Object
@@ -26,7 +28,7 @@ import pkg_resources
 
 from alluratest.controller import setup_basic_test, setup_global_objects
 from allura.command import base, script, set_neighborhood_features, \
-    create_neighborhood, show_models, taskd_cleanup
+    create_neighborhood, show_models, taskd_cleanup, taskd
 from allura import model as M
 from allura.lib.exceptions import InvalidNBFeatureValueError
 from allura.tests import decorators as td
@@ -262,6 +264,18 @@ class TestEnsureIndexCommand(object):
         ])
 
 
+class TestTaskCommand(object):
+
+    def test_commit(self):
+        exit_code = taskd.TaskCommand('task').run([test_config, 'commit'])
+        assert_equal(M.MonQTask.query.find({'task_name': 'allura.tasks.index_tasks.commit'}).count(), 1)
+        assert_equal(exit_code, 0)
+
+    def test_list(self):
+        exit_code = taskd.TaskCommand('task').run([test_config, 'list'])
+        assert_equal(exit_code, 0)
+
+
 class TestTaskdCleanupCommand(object):
 
     def setUp(self):
@@ -376,7 +390,19 @@ def test_status_log_retries():
     assert cmd._taskd_status.mock_calls == expected_calls
 
 
-class TestBackgroundCommand(object):
+class TestShowModels(object):
+
+    def test_show_models(self):
+        cmd = show_models.ShowModelsCommand('models')
+        with OutputCapture() as output:
+            cmd.run([test_config])
+        assert_in('''allura.model.notification.SiteNotification
+         - <FieldProperty content>
+         - <FieldProperty page_regex>
+        ''', output.captured)
+
+
+class TestReindexAsTask(object):
 
     cmd = 'allura.command.show_models.ReindexCommand'
     task_name = 'allura.command.base.run_command'
@@ -418,6 +444,7 @@ class TestReindexCommand(object):
         assert not g.solr.delete.called, 'solr.delete() must not be called'
 
     @patch('pysolr.Solr')
+    @td.with_wiki  # so there's some artifacts to reindex
     def test_solr_hosts_1(self, Solr):
         cmd = show_models.ReindexCommand('reindex')
         cmd.options, args = cmd.parser.parse_args([
@@ -494,3 +521,8 @@ class TestReindexCommand(object):
         cmd.options = Mock(ming_config=None)
         with td.raises(pymongo.errors.InvalidDocument):
             cmd._post_add_artifacts(range(5))
+
+    @td.with_wiki  # so there's some artifacts to reindex
+    def test_ming_config(self):
+        cmd = show_models.ReindexCommand('reindex')
+        cmd.run([test_config, '-p', 'test', '--tasks', '--ming-config', 'test.ini'])
\ No newline at end of file
diff --git a/ForgeGit/forgegit/tests/test_tasks.py b/ForgeGit/forgegit/tests/test_tasks.py
index 812a8b4..4563457 100644
--- a/ForgeGit/forgegit/tests/test_tasks.py
+++ b/ForgeGit/forgegit/tests/test_tasks.py
@@ -14,7 +14,6 @@
 #       KIND, either express or implied.  See the License for the
 #       specific language governing permissions and limitations
 #       under the License.
-import logging
 import unittest
 import mock
 from testfixtures import LogCapture
@@ -27,6 +26,7 @@ from allura.scripts.refreshrepo import RefreshRepo
 from allura.scripts.refresh_last_commits import RefreshLastCommits
 from allura.lib import helpers as h
 from allura.tasks import repo_tasks
+from allura.tests.decorators import assert_logmsg_and_no_warnings_or_errors
 from allura import model as M
 from forgegit.tests import with_git
 from forgegit.tests.functional.test_controllers import _TestCase as GitRealDataBaseTestCase
@@ -73,28 +73,19 @@ class TestCoreAlluraTasks(GitRealDataBaseTestCase):
         super(TestCoreAlluraTasks, self).setUp()
         self.setup_with_tools()
 
-    def _assert_logmsg_and_no_warnings_or_errors(self, logs, msg):
-        found_msg = False
-        for r in logs.records:
-            if msg in r.getMessage():
-                found_msg = True
-            if r.levelno > logging.INFO:
-                raise AssertionError('unexpected log {} {}'.format(r.levelname, r.getMessage()))
-        assert found_msg, 'Did not find {} in logs: {}'.format(msg, '\n'.join([str(r) for r in logs.records]))
-
     def test_refreshrepo(self):
         opts = RefreshRepo.parser().parse_args(
             ['--nbhd', '/p/', '--project', 'test', '--clean', '--all', '--repo-types', 'git'])
         with LogCapture() as logs:
             RefreshRepo.execute(opts)
-        self._assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing ALL commits in ')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing ALL commits in ')
 
         # run again with some different params
         opts = RefreshRepo.parser().parse_args(
             ['--nbhd', '/p/', '--project', 'test', '--clean-after', '2010-01-01T00:00:00'])
         with LogCapture() as logs:
             RefreshRepo.execute(opts)
-        self._assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing NEW commits in ')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing NEW commits in ')
 
     def test_refresh_last_commits(self):
         repo = c.app.repo
@@ -105,7 +96,7 @@ class TestCoreAlluraTasks(GitRealDataBaseTestCase):
         with LogCapture() as logs:
             RefreshLastCommits.execute(opts)
 
-        self._assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing all last commits ')
+        assert_logmsg_and_no_warnings_or_errors(logs, 'Refreshing all last commits ')
 
         # mostly just making sure nothing errored, but here's at least one thing we can assert:
         assert repo.status == 'ready'