コード例 #1
0
ファイル: test_models.py プロジェクト: petschna/linux-day
class CaptchaTestingModeMixin(TestCase):
    """Allow Captcha to pass regardless of the value provided"""
    def setUp(self):
        self.captcha_testing_mode_env = EnvironmentVarGuard()
        self.captcha_testing_mode_env.set('RECAPTCHA_TESTING', 'True')

        self.captcha_form_data = {'recaptcha_response_field': 'PASSED'}
コード例 #2
0
class TestClient(TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()

    def test_init_default_url(self):
        client = Client()

        self.assertEqual(client.api_root, 'http://localhost:8000/')

    def test_init_custom_url(self):
        client = Client(url='http://testing.com:1234')

        self.assertEqual(client.api_root, 'http://testing.com:1234')

    def test_init_no_login(self):
        client = Client()

        self.assertNotIn('Authorization', client.headers)
        self.assertIsNone(client.auth)

    def test_init_basic_auth(self):
        client = Client()

        client.login('someuser', 'withpass')

        self.assertNotIn('Authorization', client.headers)
        self.assertTrue(client.auth)

    def test_init_token(self):
        client = Client()
        PSEUDO_TOKEN = '123123'

        client.login(token=PSEUDO_TOKEN)

        self.assertTrue(client.headers['Authorization'],
                        'Token {}'.format(PSEUDO_TOKEN))
        self.assertIsNone(client.auth)

    def test_init_no_ssl(self):
        client = Client(check_certificates=False)

        self.assertFalse(client.session.verify)

    # 1.12
    def test_client_raises_error_with_false_url(self):
        with self.assertRaises(ClientError):
            Client(url='wrongurl')

    def test_client_from_env(self):
        """
        Test if the client does not provide a warning when no env file can be found

            Userwarning: 'Could not any envfile.'
            '.../lib/python3.5/site-packages/envparse.py'
        """
        self.env.set('KECHAIN_URL', 'http://localhost:8000')
        with self.env:
            with warnings.catch_warnings(record=True) as captured_warnings:
                client = Client.from_env()
                self.assertEqual(len(captured_warnings), 0)
コード例 #3
0
    def test_environment_completely_set_and_real_func_called(self):
        def real_func(cf_lib_wrapper=None):
            self.assertIsNotNone(cf_lib_wrapper)
            self.assertEqual('hello key', cf_lib_wrapper.api_key)
            self.assertEqual('hello email', cf_lib_wrapper.api_email)

        env = EnvironmentVarGuard()
        env.set('CLOUDFLARE_API_KEY', 'hello key')
        env.set('CLOUDFLARE_API_EMAIL', 'hello email')
        with env:
            bulk_dns.configured(real_func)()
コード例 #4
0
ファイル: tests.py プロジェクト: swoloszynski/kitcat
class TwilioTest(TestCase):
    def setUp(self):
        test_accout_sid = os.environ.get('TEST_TWILIO_SID')
        test_auth_token = os.environ.get('TEST_TWILIO_AUTH')
        test_to_phone = os.environ.get('TEST_TWILIO_TO_PHONE')
        twilio_test_from_number = '+15005550006'

        self.env = EnvironmentVarGuard()
        self.env.set('KITCAT_TWILIO_SID', test_accout_sid)
        self.env.set('KITACT_TWILIO_AUTH', test_auth_token)
        self.env.set('KITCAT_TWILIO_TO_PHONE', test_to_phone)
        self.env.set('KITCAT_TWILIO_FROM_PHONE', twilio_test_from_number)

    def test_send_sms(self):
        with self.env:
            account_sid = os.environ.get('KITCAT_TWILIO_SID')
            auth_token = os.environ.get('KITACT_TWILIO_AUTH')
            to_phone = os.environ.get('KITCAT_TWILIO_TO_PHONE')
            from_phone = os.environ.get('KITCAT_TWILIO_FROM_PHONE')
            twilio = Twilio(account_sid, auth_token, from_phone)
            test_message = 'Call yo momma!'
            sms = twilio.send_sms(to_phone, test_message)
            assert sms.sid is not None
            assert sms.error_code is None
            assert sms.error_message is None
コード例 #5
0
    def test_environment_email_key_not_set_error(self):
        def real_func():
            self.fail()

        env = EnvironmentVarGuard()
        env.set('CLOUDFLARE_API_KEY', 'hello key')
        env.unset('CLOUDFLARE_API_EMAIL')
        with env:
            try:
                bulk_dns.configured(real_func)()
                self.fail()
            except ValueError as e:
                self.assertTrue('CLOUDFLARE_API_EMAIL ' in e.message)
            except:
                self.fail()
コード例 #6
0
class match_patterns_test_case(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()
        value = (
            r"["
            r"(r'^/([a])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/650x650/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/650x650/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(raw)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/360x360/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(raw)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/360x360/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(cart)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/83x83/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(cart)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/83x83/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(catalog)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/180x180/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(catalog)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/180x180/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(gallery)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/22x22/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(gallery)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/22x22/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(list)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/63x63/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(list)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/63x63/raw/\2.\4'),"
            r"(r'^/([a])/([0-9a-f]+)-(related)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/47x47/sample/\2.\4'),"
            r"(r'^/([b])/([0-9a-f]+)-(related)\.([a-zA-Z0-9]+)$',"
            r"r'/fit-in/47x47/raw/\2.\4')"
            "]"
        )
        self.env.set('REWRITE_PATTERNS', value)

    def test_is_five_prime(self):
        with self.env:
            expected = (
                r"/fit-in/650x650/raw"
                r"/4fff89be6cca5cf00afe8062a54796fa.jpg"
                )
            self.assertEqual(
                match_patterns("/b/4fff89be6cca5cf00afe8062a54796fa-zoom.jpg"),
                expected
            )
コード例 #7
0
    def test_hpe_config_file_var(self):

        from hpecp.cli.base import get_config_file

        dummy_filepath = "/not/a/real/dir/not_a_real_file"

        env = EnvironmentVarGuard()
        env.set("HPECP_CONFIG_FILE", dummy_filepath)

        with env:
            sys.path.insert(0, os.path.abspath("../../"))
            from bin import cli

            # reload cli module with mock env
            reload(cli)

            self.assertEqual(dummy_filepath, get_config_file())
コード例 #8
0
    def handle(self, *args, **options):
        settings.MEDIA_ROOT = settings.TEST_MEDIA_ROOT
        connection.creation.create_test_db(verbosity=2)
        fixture_dir, _, _ = initialise_database()
        env = EnvironmentVarGuard()
        env.set('PIMP_DATABASE_NAME', settings.TEST_DATABASE_NAME)
        env.set('PIMP_MEDIA_ROOT', settings.TEST_MEDIA_ROOT)
        os.mkdir(settings.TEST_MEDIA_ROOT)
        with env:
            create_database(fixture_dir, env)
        dump_path = os.path.join(settings.BASE_DIR, 'fixtures',
                                 'test_database.fixture.json')
        with open(dump_path, 'w') as f:
            call_command('dumpdata', indent=4, stdout=f)

        connection.creation.destroy_test_db(settings.TEST_DATABASE_NAME,
                                            verbosity=2)
        settings.MEDIA_ROOT = settings.NORMAL_MEDIA_ROOT
コード例 #9
0
ファイル: tests.py プロジェクト: nbailey/quark
class SettingsTest(TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()

    def test_unset(self):
        with self.env:
            self.env.set("QUARK_ENV", "dev")
            settings = import_fresh_module("quark.settings")

            self.assertTrue(settings.DEBUG)
            self.assertEqual(settings.DATABASES, DEV_DB)

    def test_production(self):
        with self.env:
            self.env.set("QUARK_ENV", "production")
            settings = import_fresh_module("quark.settings")

            self.assertFalse(settings.DEBUG)
            self.assertEqual(settings.DATABASES, PROD_DB)

    def test_staging(self):
        with self.env:
            self.env.set("QUARK_ENV", "staging")
            settings = import_fresh_module("quark.settings")

            self.assertFalse(settings.DEBUG)
            self.assertEqual(settings.DATABASES, STAGING_DB)
コード例 #10
0
class SettingsTest(TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()

    def test_unset(self):
        with self.env:
            self.env.set('QUARK_ENV', 'dev')
            settings = import_fresh_module('quark.settings')

            self.assertTrue(settings.DEBUG)
            self.assertEqual(settings.DATABASES, DEV_DB)

    def test_production(self):
        with self.env:
            self.env.set('QUARK_ENV', 'production')
            settings = import_fresh_module('quark.settings')

            self.assertFalse(settings.DEBUG)
            self.assertEqual(settings.DATABASES, PROD_DB)

    def test_staging(self):
        with self.env:
            self.env.set('QUARK_ENV', 'staging')
            settings = import_fresh_module('quark.settings')

            self.assertFalse(settings.DEBUG)
            self.assertEqual(settings.DATABASES, STAGING_DB)
コード例 #11
0
class JWTAuthenticationTestCase(BaseTestCase):
    def setUp(self):
        BaseTestCase.create_schema(self)
        self.client = Client()
        self.env = EnvironmentVarGuard()
        self.env.set('JWT_KEY', 'secret')

    def test_redirection_to_login_for_unauthenticated_requests(self):
        with self.env:
            response = self.client.get('/')
            self.assertEqual(response.status_code, 302)
            self.assertTrue(response.url.startswith('/login'))

    def test_login_with_jwt(self):
        with self.env:
            token = jwt.encode({'username': '******'},
                               'secret',
                               algorithm='HS256')
            response = self.client.get('/' + '/?accessToken=' + token)
            self.assertEqual(response.status_code, 200)

    def tearDown(self):
        BaseTestCase.delete_schema(self)
コード例 #12
0
ファイル: test_config.py プロジェクト: bodylabs/baiji
def AWSEnvironmentVarGuard():
    env = EnvironmentVarGuard()
    env.unset('AWS_ACCESS_KEY_ID')
    env.unset('AWS_ACCESS_KEY')
    env.unset('AWS_SECRET_ACCESS_KEY')
    env.unset('AWS_SECRET')
    env.unset('AWS_DEFAULT_REGION')
    env.set('BODYLABS_CREDENTIAL_FILE', '/this_file_does_not_exist')
    env.set('AWS_CONFIG_FILE', '/this_file_does_not_exist')
    env.set('AWS_CREDENTIAL_FILE', '/this_file_does_not_exist')
    return env
コード例 #13
0
class MonitorTest(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()
        self.env.set('STATUS_ENDPOINT', 'http://localhost/status_nginx')
        self.env.set('SLEEP_DURATION', '60')
        self.env.set('MAX_IDLE_COUNT', '5')
        global count
        count = 0

    @mock.patch('requests.get')
    def test(self, mock_requests, mock_time):
        mock_requests.side_effect = get_return
        self.assertTrue(monitor.main())

    @mock.patch('requests.get')
    def test_with_reset(self, mock_requests, mock_time):
        mock_requests.side_effect = get_return_with_reset
        self.assertTrue(monitor.main())
コード例 #14
0
class TestDockerHelpers(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentVarGuard()

    def test_is_container_when_container_id(self):
        self.env.set('CONTAINER_ID', CONTAINER_ID)
        self.assertTrue(container.is_container())

    def test_not_container_when_no_container_id(self):
        self.env.unset('CONTAINER_ID')
        self.assertFalse(container.is_container())

    def test_container_id(self):
        self.env.set('CONTAINER_ID', CONTAINER_ID)
        self.assertEqual(CONTAINER_ID, container.get_container_id())

    def test_container_ip(self):
        self.env.set('CONTAINER_IP', CONTAINER_IP)
        self.assertEqual(CONTAINER_IP, container.get_container_ip())
コード例 #15
0
ファイル: tests.py プロジェクト: patogit/alterchef
class JobsTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user("ninja", "*****@*****.**", "password")
        self.network = Network.objects.create(name="quintanalibre.org.ar",
                                              user=self.user)
        self.profile = FwProfile.objects.create(network=self.network)
        self.client.login(username="******", password="******")
        self.cook_url = reverse('cook', kwargs={'slug': self.profile.slug})
        self.job_data = {"devices": ["TLMR3220"], "revision": "stable"}
        self.env = EnvironmentVarGuard()
        self.env.set('LANG', '')

    def tearDown(self):
        FwJob.set_make_commands_func(FwJob.default_make_commands)

    def test_process_some_jobs(self):
        FwJob.objects.create(profile=self.profile,
                             user=self.user,
                             job_data=self.job_data)
        FwJob.objects.create(profile=self.profile,
                             user=self.user,
                             job_data=self.job_data)

        FwJob.set_make_commands_func(lambda *x: ["sleep 0.1"])

        self.assertEqual(len(FwJob.started.all()), 0)
        self.assertEqual(len(FwJob.waiting.all()), 2)

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.waiting.all()), 1)
        self.assertEqual(len(FwJob.finished.all()), 1)

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.finished.all()), 2)

    def test_failed_job(self):
        fwjob = FwJob.objects.create(profile=self.profile,
                                     user=self.user,
                                     job_data=self.job_data)
        FwJob.set_make_commands_func(lambda *x: ["ls /inexistent"])

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.failed.all()), 1)
        with self.env:
            fwjob = FwJob.objects.get(pk=fwjob.pk)
            self.assertTrue("No such file or directory" in fwjob.build_log)

    def _test_cook(self):
        response = self.client.get(self.cook_url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(self.cook_url, {
            "other_devices": "TLMR3020",
            "openwrt_revision": "stable"
        })
        self.assertEqual(response.status_code, 302)

        self.assertEqual(len(FwJob.started.all()), 0)
        self.assertEqual(len(FwJob.waiting.all()), 1)
        FwJob.process_jobs()
        self.assertEqual(len(FwJob.started.all()), 1)
        self.assertEqual(len(FwJob.waiting.all()), 0)

    def test_make_commands(self):
        commands = FwJob.make_commands("quintanalibre.org.ar", "profile1",
                                       ["TLMR3220", "NONEatherosDefault"],
                                       "33333")
        self.assertTrue("33333 ar71xx quintanalibre.org.ar profile1 TLMR3220"
                        in commands[0])
        self.assertTrue("33333 atheros quintanalibre.org.ar profile1 Default"
                        in commands[1])

    def test_view_jobs(self):
        self.assertContains(self.client.get(reverse("view-jobs")), "List Jobs")

    def test_job_detail(self):
        fwjob = FwJob.objects.create(profile=self.profile,
                                     user=self.user,
                                     job_data=self.job_data)
        self.assertContains(
            self.client.get(reverse("fwjob-detail", kwargs={"pk": fwjob.pk})),
            "WAITING")
        fwjob.status = "FAILED"
        fwjob.build_log = "the log"
        fwjob.save()
        self.assertContains(
            self.client.get(reverse("fwjob-detail", kwargs={"pk": fwjob.pk})),
            "the log")
コード例 #16
0
ファイル: tests.py プロジェクト: PabloCastellano/alterchef
class JobsTest(TestCase):

    def setUp(self):
        self.user = User.objects.create_user("ninja", "*****@*****.**", "password")
        self.network = Network.objects.create(name="quintanalibre.org.ar", user=self.user)
        self.profile = FwProfile.objects.create(network=self.network)
        self.client.login(username="******", password="******")
        self.cook_url = reverse('cook', kwargs={'slug': self.profile.slug})
        self.job_data = {"devices": ["TLMR3220"], "revision": "stable"}
        self.env = EnvironmentVarGuard()
        self.env.set('LANG', '')

    def tearDown(self):
        FwJob.set_make_commands_func(FwJob.default_make_commands)

    def test_process_some_jobs(self):
        FwJob.objects.create(profile=self.profile, user=self.user, job_data=self.job_data)
        FwJob.objects.create(profile=self.profile, user=self.user, job_data=self.job_data)

        FwJob.set_make_commands_func(lambda *x: ["sleep 0.1"])

        self.assertEqual(len(FwJob.started.all()), 0)
        self.assertEqual(len(FwJob.waiting.all()), 2)

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.waiting.all()), 1)
        self.assertEqual(len(FwJob.finished.all()), 1)

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.finished.all()), 2)

    def test_failed_job(self):
        fwjob = FwJob.objects.create(profile=self.profile, user=self.user, job_data=self.job_data)
        FwJob.set_make_commands_func(lambda *x: ["ls /inexistent"])

        FwJob.process_jobs(sync=True)
        self.assertEqual(len(FwJob.failed.all()), 1)
        with self.env:
            fwjob = FwJob.objects.get(pk=fwjob.pk)
            self.assertTrue("No such file or directory" in fwjob.build_log)

    def _test_cook(self):
        response = self.client.get(self.cook_url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(self.cook_url, {"other_devices": "TLMR3020", "openwrt_revision": "stable"})
        self.assertEqual(response.status_code, 302)

        self.assertEqual(len(FwJob.started.all()), 0)
        self.assertEqual(len(FwJob.waiting.all()), 1)
        FwJob.process_jobs()
        self.assertEqual(len(FwJob.started.all()), 1)
        self.assertEqual(len(FwJob.waiting.all()), 0)

    def test_make_commands(self):
        commands = FwJob.make_commands("quintanalibre.org.ar", "profile1", ["TLMR3220", "NONEatherosDefault"], "33333")
        self.assertTrue("33333 ar71xx quintanalibre.org.ar profile1 TLMR3220" in commands[0])
        self.assertTrue("33333 atheros quintanalibre.org.ar profile1 Default" in commands[1])

    def test_view_jobs(self):
        self.assertContains(self.client.get(reverse("view-jobs")), "List Jobs")

    def test_job_detail(self):
        fwjob = FwJob.objects.create(profile=self.profile, user=self.user, job_data=self.job_data)
        self.assertContains(self.client.get(reverse("fwjob-detail", kwargs={"pk": fwjob.pk})), "WAITING")
        fwjob.status = "FAILED"
        fwjob.build_log = "the log"
        fwjob.save()
        self.assertContains(self.client.get(reverse("fwjob-detail", kwargs={"pk": fwjob.pk})), "the log")
コード例 #17
0
class TestPaverBokChoyCmd(unittest.TestCase):
    """
    Paver Bok Choy Command test cases
    """

    def _expected_command(self, name, store=None, verify_xss=True):
        """
        Returns the command that is expected to be run for the given test spec
        and store.
        """

        shard_str = "/shard_" + self.shard if self.shard else ""

        expected_statement = [
            "DEFAULT_STORE={}".format(store),
            "SCREENSHOT_DIR='{}/test_root/log{}'".format(REPO_DIR, shard_str),
            "BOK_CHOY_HAR_DIR='{}/test_root/log{}/hars'".format(REPO_DIR, shard_str),
            "BOKCHOY_A11Y_CUSTOM_RULES_FILE='{}/{}'".format(
                REPO_DIR, "node_modules/edx-custom-a11y-rules/lib/custom_a11y_rules.js"
            ),
            "SELENIUM_DRIVER_LOG_DIR='{}/test_root/log{}'".format(REPO_DIR, shard_str),
            "VERIFY_XSS='{}'".format(verify_xss),
            "nosetests",
            "{}/common/test/acceptance/{}".format(REPO_DIR, name),
            "--xunit-file={}/reports/bok_choy{}/xunit.xml".format(REPO_DIR, shard_str),
            "--verbosity=2",
        ]
        return expected_statement

    def setUp(self):
        super(TestPaverBokChoyCmd, self).setUp()
        self.shard = os.environ.get("SHARD")
        self.env_var_override = EnvironmentVarGuard()

    def test_default(self):
        suite = BokChoyTestSuite("")
        name = "tests"
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_suite_spec(self):
        spec = "test_foo.py"
        suite = BokChoyTestSuite("", test_spec=spec)
        name = "tests/{}".format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_class_spec(self):
        spec = "test_foo.py:FooTest"
        suite = BokChoyTestSuite("", test_spec=spec)
        name = "tests/{}".format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_testcase_spec(self):
        spec = "test_foo.py:FooTest.test_bar"
        suite = BokChoyTestSuite("", test_spec=spec)
        name = "tests/{}".format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_spec_with_draft_default_store(self):
        spec = "test_foo.py"
        suite = BokChoyTestSuite("", test_spec=spec, default_store="draft")
        name = "tests/{}".format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name, store="draft"))

    def test_invalid_default_store(self):
        # the cmd will dumbly compose whatever we pass in for the default_store
        suite = BokChoyTestSuite("", default_store="invalid")
        name = "tests"
        self.assertEqual(suite.cmd, self._expected_command(name=name, store="invalid"))

    def test_serversonly(self):
        suite = BokChoyTestSuite("", serversonly=True)
        self.assertEqual(suite.cmd, None)

    def test_verify_xss(self):
        suite = BokChoyTestSuite("", verify_xss=True)
        name = "tests"
        self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=True))

    def test_verify_xss_env_var(self):
        self.env_var_override.set("VERIFY_XSS", "False")
        with self.env_var_override:
            suite = BokChoyTestSuite("")
            name = "tests"
            self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=False))

    def test_test_dir(self):
        test_dir = "foo"
        suite = BokChoyTestSuite("", test_dir=test_dir)
        self.assertEqual(suite.cmd, self._expected_command(name=test_dir))

    def test_verbosity_settings_1_process(self):
        """
        Using 1 process means paver should ask for the traditional xunit plugin for plugin results
        """
        expected_verbosity_command = [
            "--xunit-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR, shard_str="/shard_" + self.shard if self.shard else ""
            ),
            "--verbosity=2",
        ]
        suite = BokChoyTestSuite("", num_processes=1)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)

    def test_verbosity_settings_2_processes(self):
        """
        Using multiple processes means specific xunit, coloring, and process-related settings should
        be used.
        """
        process_count = 2
        expected_verbosity_command = [
            "--xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR, shard_str="/shard_" + self.shard if self.shard else ""
            ),
            "--processes={}".format(process_count),
            "--no-color",
            "--process-timeout=1200",
        ]
        suite = BokChoyTestSuite("", num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)

    def test_verbosity_settings_3_processes(self):
        """
        With the above test, validate that num_processes can be set to various values
        """
        process_count = 3
        expected_verbosity_command = [
            "--xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR, shard_str="/shard_" + self.shard if self.shard else ""
            ),
            "--processes={}".format(process_count),
            "--no-color",
            "--process-timeout=1200",
        ]
        suite = BokChoyTestSuite("", num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)

    def test_invalid_verbosity_and_processes(self):
        """
        If an invalid combination of verbosity and number of processors is passed in, a
        BuildFailure should be raised
        """
        suite = BokChoyTestSuite("", num_processes=2, verbosity=3)
        with self.assertRaises(BuildFailure):
            # pylint: disable=pointless-statement
            suite.verbosity_processes_command
コード例 #18
0
class ConfigurationTest(unittest.TestCase):
    def setUp(self):
        def getLogger(name):
            self.mock_logger = mock.Mock()
            return self.mock_logger

        sys.modules['logging'].getLogger = getLogger

        def get(url, headers):
            get_return = mock.Mock()
            get_return.ok = True
            get_return.json = mock.Mock()
            get_return.json.return_value = {'data': {'status': 1}}
            return get_return

        sys.modules['requests'].get = get

        self.env = EnvironmentVarGuard()
        self.env.set('CACHET_TOKEN', 'token2')

        self.configuration = Configuration('config.yml')
        sys.modules['requests'].Timeout = Timeout
        sys.modules['requests'].ConnectionError = ConnectionError
        sys.modules['requests'].HTTPError = HTTPError

    def test_init(self):
        self.assertEqual(len(self.configuration.data), 3, 'Configuration data size is incorrect')
        self.assertEquals(len(self.configuration.expectations), 3, 'Number of expectations read from file is incorrect')
        self.assertDictEqual(self.configuration.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
        self.assertEquals(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
                          'Cachet API URL was set incorrectly')

    def test_evaluate(self):
        def total_seconds():
            return 0.1

        def request(method, url, timeout=None):
            response = mock.Mock()
            response.status_code = 200
            response.elapsed = mock.Mock()
            response.elapsed.total_seconds = total_seconds
            response.text = '<body>'
            return response

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
                          'Component status set incorrectly')

    def test_evaluate_with_failure(self):
        def total_seconds():
            return 0.1

        def request(method, url, timeout=None):
            response = mock.Mock()
            # We are expecting a 200 response, so this will fail the expectation.
            response.status_code = 400
            response.elapsed = mock.Mock()
            response.elapsed.total_seconds = total_seconds
            response.text = '<body>'
            return response

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
                          'Component status set incorrectly')

    def test_evaluate_with_timeout(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise Timeout()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES,
                          'Component status set incorrectly')
        self.mock_logger.warning.assert_called_with('Request timed out')

    def test_evaluate_with_connection_error(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise ConnectionError()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
                          'Component status set incorrectly')
        self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')

    def test_evaluate_with_http_error(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise HTTPError()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
                          'Component status set incorrectly')
        self.mock_logger.exception.assert_called_with('Unexpected HTTP response')

    def test_push_status(self):
        def put(url, params=None, headers=None):
            self.assertEquals(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
            self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
            self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')

            response = mock.Mock()
            response.status_code = 200
            return response

        sys.modules['requests'].put = put
        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
                          'Incorrect component update parameters')
        self.configuration.push_status()

    def test_push_status_with_failure(self):
        def put(url, params=None, headers=None):
            self.assertEquals(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
            self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
            self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')

            response = mock.Mock()
            response.status_code = 400
            return response

        sys.modules['requests'].put = put
        self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
                          'Incorrect component update parameters')
        self.configuration.push_status()
コード例 #19
0
class TestPaverBokChoyCmd(unittest.TestCase):
    """
    Paver Bok Choy Command test cases
    """

    def _expected_command(self, name, store=None, verify_xss=True):
        """
        Returns the command that is expected to be run for the given test spec
        and store.
        """

        shard_str = '/shard_' + self.shard if self.shard else ''

        expected_statement = [
            "DEFAULT_STORE={}".format(store),
            "SCREENSHOT_DIR='{}/test_root/log{}'".format(REPO_DIR, shard_str),
            "BOK_CHOY_HAR_DIR='{}/test_root/log{}/hars'".format(REPO_DIR, shard_str),
            "BOKCHOY_A11Y_CUSTOM_RULES_FILE='{}/{}'".format(
                REPO_DIR,
                'node_modules/edx-custom-a11y-rules/lib/custom_a11y_rules.js'
            ),
            "SELENIUM_DRIVER_LOG_DIR='{}/test_root/log{}'".format(REPO_DIR, shard_str),
            "VERIFY_XSS='{}'".format(verify_xss),
            "python",
            "-Wd",
            "-m",
            "pytest",
            "{}/common/test/acceptance/{}".format(REPO_DIR, name),
            "--junitxml={}/reports/bok_choy{}/xunit.xml".format(REPO_DIR, shard_str),
            "--verbose",
        ]
        return expected_statement

    def setUp(self):
        super(TestPaverBokChoyCmd, self).setUp()
        self.shard = os.environ.get('SHARD')
        self.env_var_override = EnvironmentVarGuard()

    def test_default(self):
        suite = BokChoyTestSuite('')
        name = 'tests'
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_suite_spec(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_class_spec(self):
        spec = 'test_foo.py:FooTest'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_testcase_spec(self):
        spec = 'test_foo.py:FooTest.test_bar'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_spec_with_draft_default_store(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec, default_store='draft')
        name = 'tests/{}'.format(spec)
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=name, store='draft')
        )

    def test_invalid_default_store(self):
        # the cmd will dumbly compose whatever we pass in for the default_store
        suite = BokChoyTestSuite('', default_store='invalid')
        name = 'tests'
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=name, store='invalid')
        )

    def test_serversonly(self):
        suite = BokChoyTestSuite('', serversonly=True)
        self.assertEqual(suite.cmd, None)

    def test_verify_xss(self):
        suite = BokChoyTestSuite('', verify_xss=True)
        name = 'tests'
        self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=True))

    def test_verify_xss_env_var(self):
        self.env_var_override.set('VERIFY_XSS', 'False')
        with self.env_var_override:
            suite = BokChoyTestSuite('')
            name = 'tests'
            self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=False))

    def test_test_dir(self):
        test_dir = 'foo'
        suite = BokChoyTestSuite('', test_dir=test_dir)
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=test_dir)
        )

    def test_verbosity_settings_1_process(self):
        """
        Using 1 process means paver should ask for the traditional xunit plugin for plugin results
        """
        expected_verbosity_command = [
            "--junitxml={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else ''
            ),
            "--verbose",
        ]
        suite = BokChoyTestSuite('', num_processes=1)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)

    def test_verbosity_settings_2_processes(self):
        """
        Using multiple processes means specific xunit, coloring, and process-related settings should
        be used.
        """
        process_count = 2
        expected_verbosity_command = [
            "--junitxml={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
            ),
            u"-n {}".format(process_count),
            "--color=no",
            "--verbose",
        ]
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)

    def test_verbosity_settings_3_processes(self):
        """
        With the above test, validate that num_processes can be set to various values
        """
        process_count = 3
        expected_verbosity_command = [
            "--junitxml={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
            ),
            u"-n {}".format(process_count),
            "--color=no",
            "--verbose",
        ]
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command, expected_verbosity_command)
コード例 #20
0
class TestPaverBokChoyCmd(unittest.TestCase):
    """
    Paver Bok Choy Command test cases
    """

    def _expected_command(self, name, store=None, verify_xss=True):
        """
        Returns the command that is expected to be run for the given test spec
        and store.
        """

        expected_statement = (
            "DEFAULT_STORE={default_store} "
            "SCREENSHOT_DIR='{repo_dir}/test_root/log{shard_str}' "
            "BOK_CHOY_HAR_DIR='{repo_dir}/test_root/log{shard_str}/hars' "
            "BOKCHOY_A11Y_CUSTOM_RULES_FILE='{repo_dir}/{a11y_custom_file}' "
            "SELENIUM_DRIVER_LOG_DIR='{repo_dir}/test_root/log{shard_str}' "
            "VERIFY_XSS='{verify_xss}' "
            "nosetests {repo_dir}/common/test/acceptance/{exp_text} "
            "--with-xunit "
            "--xunit-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml "
            "--verbosity=2 "
        ).format(
            default_store=store,
            repo_dir=REPO_DIR,
            shard_str='/shard_' + self.shard if self.shard else '',
            exp_text=name,
            a11y_custom_file='node_modules/edx-custom-a11y-rules/lib/custom_a11y_rules.js',
            verify_xss=verify_xss
        )
        return expected_statement

    def setUp(self):
        super(TestPaverBokChoyCmd, self).setUp()
        self.shard = os.environ.get('SHARD')
        self.env_var_override = EnvironmentVarGuard()

    def test_default(self):
        suite = BokChoyTestSuite('')
        name = 'tests'
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_suite_spec(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_class_spec(self):
        spec = 'test_foo.py:FooTest'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_testcase_spec(self):
        spec = 'test_foo.py:FooTest.test_bar'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_spec_with_draft_default_store(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec, default_store='draft')
        name = 'tests/{}'.format(spec)
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=name, store='draft')
        )

    def test_invalid_default_store(self):
        # the cmd will dumbly compose whatever we pass in for the default_store
        suite = BokChoyTestSuite('', default_store='invalid')
        name = 'tests'
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=name, store='invalid')
        )

    def test_serversonly(self):
        suite = BokChoyTestSuite('', serversonly=True)
        self.assertEqual(suite.cmd, "")

    def test_verify_xss(self):
        suite = BokChoyTestSuite('', verify_xss=True)
        name = 'tests'
        self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=True))

    def test_verify_xss_env_var(self):
        self.env_var_override.set('VERIFY_XSS', 'False')
        with self.env_var_override:
            suite = BokChoyTestSuite('')
            name = 'tests'
            self.assertEqual(suite.cmd, self._expected_command(name=name, verify_xss=False))

    def test_test_dir(self):
        test_dir = 'foo'
        suite = BokChoyTestSuite('', test_dir=test_dir)
        self.assertEqual(
            suite.cmd,
            self._expected_command(name=test_dir)
        )

    def test_verbosity_settings_1_process(self):
        """
        Using 1 process means paver should ask for the traditional xunit plugin for plugin results
        """
        expected_verbosity_string = (
            "--with-xunit --xunit-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml --verbosity=2".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else ''
            )
        )
        suite = BokChoyTestSuite('', num_processes=1)
        self.assertEqual(BokChoyTestSuite.verbosity_processes_string(suite), expected_verbosity_string)

    def test_verbosity_settings_2_processes(self):
        """
        Using multiple processes means specific xunit, coloring, and process-related settings should
        be used.
        """
        process_count = 2
        expected_verbosity_string = (
            "--with-xunitmp --xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml"
            " --processes={procs} --no-color --process-timeout=1200".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
                procs=process_count
            )
        )
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(BokChoyTestSuite.verbosity_processes_string(suite), expected_verbosity_string)

    def test_verbosity_settings_3_processes(self):
        """
        With the above test, validate that num_processes can be set to various values
        """
        process_count = 3
        expected_verbosity_string = (
            "--with-xunitmp --xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml"
            " --processes={procs} --no-color --process-timeout=1200".format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
                procs=process_count
            )
        )
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(BokChoyTestSuite.verbosity_processes_string(suite), expected_verbosity_string)

    def test_invalid_verbosity_and_processes(self):
        """
        If an invalid combination of verbosity and number of processors is passed in, a
        BuildFailure should be raised
        """
        suite = BokChoyTestSuite('', num_processes=2, verbosity=3)
        with self.assertRaises(BuildFailure):
            BokChoyTestSuite.verbosity_processes_string(suite)
コード例 #21
0
class TestPaverBokChoyCmd(unittest.TestCase):
    """
    Paver Bok Choy Command test cases
    """
    def _expected_command(self, name, store=None, verify_xss=True):
        """
        Returns the command that is expected to be run for the given test spec
        and store.
        """

        shard_str = '/shard_' + self.shard if self.shard else ''

        expected_statement = [
            "DEFAULT_STORE={}".format(store),
            "SCREENSHOT_DIR='{}/test_root/log{}'".format(REPO_DIR, shard_str),
            "BOK_CHOY_HAR_DIR='{}/test_root/log{}/hars'".format(
                REPO_DIR, shard_str),
            "BOKCHOY_A11Y_CUSTOM_RULES_FILE='{}/{}'".format(
                REPO_DIR,
                'node_modules/edx-custom-a11y-rules/lib/custom_a11y_rules.js'),
            "SELENIUM_DRIVER_LOG_DIR='{}/test_root/log{}'".format(
                REPO_DIR, shard_str),
            "VERIFY_XSS='{}'".format(verify_xss),
            "nosetests",
            "{}/common/test/acceptance/{}".format(REPO_DIR, name),
            "--xunit-file={}/reports/bok_choy{}/xunit.xml".format(
                REPO_DIR, shard_str),
            "--verbosity=2",
        ]
        return expected_statement

    def setUp(self):
        super(TestPaverBokChoyCmd, self).setUp()
        self.shard = os.environ.get('SHARD')
        self.env_var_override = EnvironmentVarGuard()

    def test_default(self):
        suite = BokChoyTestSuite('')
        name = 'tests'
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_suite_spec(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_class_spec(self):
        spec = 'test_foo.py:FooTest'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_testcase_spec(self):
        spec = 'test_foo.py:FooTest.test_bar'
        suite = BokChoyTestSuite('', test_spec=spec)
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd, self._expected_command(name=name))

    def test_spec_with_draft_default_store(self):
        spec = 'test_foo.py'
        suite = BokChoyTestSuite('', test_spec=spec, default_store='draft')
        name = 'tests/{}'.format(spec)
        self.assertEqual(suite.cmd,
                         self._expected_command(name=name, store='draft'))

    def test_invalid_default_store(self):
        # the cmd will dumbly compose whatever we pass in for the default_store
        suite = BokChoyTestSuite('', default_store='invalid')
        name = 'tests'
        self.assertEqual(suite.cmd,
                         self._expected_command(name=name, store='invalid'))

    def test_serversonly(self):
        suite = BokChoyTestSuite('', serversonly=True)
        self.assertEqual(suite.cmd, None)

    def test_verify_xss(self):
        suite = BokChoyTestSuite('', verify_xss=True)
        name = 'tests'
        self.assertEqual(suite.cmd,
                         self._expected_command(name=name, verify_xss=True))

    def test_verify_xss_env_var(self):
        self.env_var_override.set('VERIFY_XSS', 'False')
        with self.env_var_override:
            suite = BokChoyTestSuite('')
            name = 'tests'
            self.assertEqual(
                suite.cmd, self._expected_command(name=name, verify_xss=False))

    def test_test_dir(self):
        test_dir = 'foo'
        suite = BokChoyTestSuite('', test_dir=test_dir)
        self.assertEqual(suite.cmd, self._expected_command(name=test_dir))

    def test_verbosity_settings_1_process(self):
        """
        Using 1 process means paver should ask for the traditional xunit plugin for plugin results
        """
        expected_verbosity_command = [
            "--xunit-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".
            format(repo_dir=REPO_DIR,
                   shard_str='/shard_' + self.shard if self.shard else ''),
            "--verbosity=2",
        ]
        suite = BokChoyTestSuite('', num_processes=1)
        self.assertEqual(suite.verbosity_processes_command,
                         expected_verbosity_command)

    def test_verbosity_settings_2_processes(self):
        """
        Using multiple processes means specific xunit, coloring, and process-related settings should
        be used.
        """
        process_count = 2
        expected_verbosity_command = [
            "--xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".
            format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
            ),
            "--processes={}".format(process_count),
            "--no-color",
            "--process-timeout=1200",
        ]
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command,
                         expected_verbosity_command)

    def test_verbosity_settings_3_processes(self):
        """
        With the above test, validate that num_processes can be set to various values
        """
        process_count = 3
        expected_verbosity_command = [
            "--xunitmp-file={repo_dir}/reports/bok_choy{shard_str}/xunit.xml".
            format(
                repo_dir=REPO_DIR,
                shard_str='/shard_' + self.shard if self.shard else '',
            ),
            "--processes={}".format(process_count),
            "--no-color",
            "--process-timeout=1200",
        ]
        suite = BokChoyTestSuite('', num_processes=process_count)
        self.assertEqual(suite.verbosity_processes_command,
                         expected_verbosity_command)

    def test_invalid_verbosity_and_processes(self):
        """
        If an invalid combination of verbosity and number of processors is passed in, a
        BuildFailure should be raised
        """
        suite = BokChoyTestSuite('', num_processes=2, verbosity=3)
        with self.assertRaises(BuildFailure):
            # pylint: disable=pointless-statement
            suite.verbosity_processes_command
コード例 #22
0
class TestEnvironmentProjectKeychain(TestBaseProjectKeychain):
    keychain_class = EnvironmentProjectKeychain

    def setUp(self):
        super(TestEnvironmentProjectKeychain, self).setUp()
        self.env = EnvironmentVarGuard()
        self._clean_env(self.env)
        self.env.set(
            '{}test'.format(self.keychain_class.org_var_prefix),
            json.dumps(self.org_config.config)
        )
        self.env.set(
            self.keychain_class.app_var,
            json.dumps(self.connected_app_config.config)
        )
        self.env.set(
            '{}github'.format(self.keychain_class.service_var_prefix),
            json.dumps(self.services['github'].config)
        )
        self.env.set(
            '{}mrbelvedere'.format(self.keychain_class.service_var_prefix),
            json.dumps(self.services['mrbelvedere'].config)
        )
        self.env.set(
            '{}apextestsdb'.format(self.keychain_class.service_var_prefix),
            json.dumps(self.services['apextestsdb'].config)
        )

    def _clean_env(self, env):
        for key, value in env.items():
            if key.startswith(self.keychain_class.org_var_prefix):
                del env[key]
        for key, value in env.items():
            if key.startswith(self.keychain_class.service_var_prefix):
                del env[key]
        if self.keychain_class.app_var in env:
            del env[self.keychain_class.app_var]

    def test_get_org(self):
        keychain = self.keychain_class(self.project_config, self.key)
        self.assertEquals(keychain.orgs.keys(), ['test'])
        self.assertEquals(keychain.get_org('test').config, self.org_config.config)

    def _test_list_orgs(self):
        with self.env:
            keychain = self.keychain_class(self.project_config, self.key)
            self.assertEquals(keychain.list_orgs(), ['test'])

    def test_list_orgs_empty(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            env.set(
                self.keychain_class.app_var,
                json.dumps(self.connected_app_config.config)
            )
            self._test_list_orgs_empty()

    def test_get_org_not_found(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            env.set(
                self.keychain_class.app_var,
                json.dumps(self.connected_app_config.config)
            )
            self._test_get_org_not_found()

    def test_get_default_org(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            org_config = self.org_config.config.copy()
            org_config['default'] = True
            self.env.set(
                '{}test'.format(self.keychain_class.org_var_prefix),
                json.dumps(org_config)
            )
            env.set(
                self.keychain_class.app_var,
                json.dumps(self.connected_app_config.config)
            )
            self._test_get_default_org()
コード例 #23
0
class TestLightsailAutoSnapshots(unittest.TestCase):
    def test_snapshot_instances(self):
        """
        Tests instance snapshotting by stubbing one instance in the response to
        the get_instances call and verifying the create_instance_snapshot API
        endpoint is called with the expected parameters and verifying the
        output.
        """
        mock_time = MagicMock()
        mock_time.return_value = 1485044158
        logger = StringIO()
        client = boto3.client('lightsail')
        stubber = Stubber(client)
        stubber.add_response('get_instances',
                             {'instances': [{
                                 'name': 'LinuxBox1'
                             }]})
        stubber.add_response(
            'create_instance_snapshot', {}, {
                'instanceName': 'LinuxBox1',
                'instanceSnapshotName': 'LinuxBox1-system-1485044158000-auto'
            })
        stubber.activate()

        index._snapshot_instances(client, mock_time, logger)

        stubber.assert_no_pending_responses()
        self.assertEqual(
            'Created Snapshot name="LinuxBox1-system-1485044158000-auto"',
            logger.getvalue().strip())

    def test_prune_snapshots(self):
        """
        Tests snapshot pruning by providing three snapshots - one manual and
        two automatic of which one is has an elapsed retention period. The
        test asserts that only the eligible snapshot is pruned via stubbing
        the API call to delete_instance_snapshot and verifying the output.
        """
        mock_dt = MagicMock()
        mock_dt.now.return_value = datetime(2016, 12, 2)
        logger = StringIO()
        client = boto3.client('lightsail')
        stubber = Stubber(client)
        stubber.add_response(
            'get_instance_snapshots', {
                'instanceSnapshots': [{
                    'name': 'snapshot-manual',
                    'createdAt': datetime(2016, 12, 1)
                }, {
                    'name': 'new-snapshot-auto',
                    'createdAt': datetime(2016, 12, 1)
                }, {
                    'name': 'old-snapshot-auto',
                    'createdAt': datetime(2016, 10, 15)
                }]
            })
        stubber.add_response('delete_instance_snapshot', {},
                             {'instanceSnapshotName': 'old-snapshot-auto'})
        stubber.activate()

        index._prune_snapshots(client, timedelta(days=5), mock_dt, logger)

        stubber.assert_no_pending_responses()
        self.assertEqual('Deleted Snapshot name="old-snapshot-auto"',
                         logger.getvalue().strip())

    def test_pagination(self):
        """
        Tests pagination by returning three pages of results and asserts each
        request is made with the expected pageToken.
        """
        client = boto3.client('lightsail')
        stubber = Stubber(client)
        stubber.add_response(
            'get_instance_snapshots', {
                'instanceSnapshots': [
                    {
                        'name': 'snapshot1',
                        'createdAt': datetime(2016, 12, 1)
                    },
                ],
                'nextPageToken':
                'token1'
            })
        stubber.add_response(
            'get_instance_snapshots', {
                'instanceSnapshots': [
                    {
                        'name': 'snapshot2',
                        'createdAt': datetime(2016, 12, 2)
                    },
                ],
                'nextPageToken':
                'token2'
            }, {'pageToken': 'token1'})
        stubber.add_response(
            'get_instance_snapshots', {
                'instanceSnapshots': [
                    {
                        'name': 'snapshot2',
                        'createdAt': datetime(2016, 12, 3)
                    },
                ]
            }, {'pageToken': 'token2'})
        stubber.activate()

        index._prune_snapshots(client, timedelta(days=5))

        stubber.assert_no_pending_responses()

    @patch('index._prune_snapshots')
    @patch('index._snapshot_instances')
    def test_handler(self, snapshot_instances_mock, prune_snapshots_mock):
        """
        Tests that the handler honors the value of RENTENTION_DAYS and calls
        the snapshot and prune functions with the expected values.
        """
        lightsail = Mock()
        self.env = EnvironmentVarGuard()
        self.env.set('RETENTION_DAYS', '90')

        with patch('boto3.client', return_value=lightsail) as client_factory:
            with self.env:
                index.handler(Mock(), Mock())

        client_factory.assert_called_with('lightsail')
        snapshot_instances_mock.assert_called_with(lightsail)
        prune_snapshots_mock.assert_called_with(lightsail, timedelta(days=90))
コード例 #24
0
class ConfigurationTest(unittest.TestCase):
    def setUp(self):
        def getLogger(name):
            self.mock_logger = mock.Mock()
            return self.mock_logger

        sys.modules['logging'].getLogger = getLogger

        def get(url, headers):
            get_return = mock.Mock()
            get_return.ok = True
            get_return.json = mock.Mock()
            get_return.json.return_value = {'data': {'status': 1}}
            return get_return

        sys.modules['requests'].get = get

        self.env = EnvironmentVarGuard()
        self.env.set('CACHET_TOKEN', 'token2')

        self.configuration = Configuration('config.yml')
        sys.modules['requests'].Timeout = Timeout
        sys.modules['requests'].ConnectionError = ConnectionError
        sys.modules['requests'].HTTPError = HTTPError

    def test_init(self):
        self.assertEqual(len(self.configuration.data), 3,
                         'Configuration data size is incorrect')
        self.assertEquals(
            len(self.configuration.expectations), 3,
            'Number of expectations read from file is incorrect')
        self.assertDictEqual(self.configuration.headers,
                             {'X-Cachet-Token': 'token2'},
                             'Header was not set correctly')
        self.assertEquals(self.configuration.api_url,
                          'https://demo.cachethq.io/api/v1',
                          'Cachet API URL was set incorrectly')

    def test_evaluate(self):
        def total_seconds():
            return 0.1

        def request(method, url, timeout=None):
            response = mock.Mock()
            response.status_code = 200
            response.elapsed = mock.Mock()
            response.elapsed.total_seconds = total_seconds
            response.text = '<body>'
            return response

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
            'Component status set incorrectly')

    def test_evaluate_with_failure(self):
        def total_seconds():
            return 0.1

        def request(method, url, timeout=None):
            response = mock.Mock()
            # We are expecting a 200 response, so this will fail the expectation.
            response.status_code = 400
            response.elapsed = mock.Mock()
            response.elapsed.total_seconds = total_seconds
            response.text = '<body>'
            return response

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
            'Component status set incorrectly')

    def test_evaluate_with_timeout(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger',
                              'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise Timeout()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES,
            'Component status set incorrectly')
        self.mock_logger.warning.assert_called_with('Request timed out')

    def test_evaluate_with_connection_error(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger',
                              'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise ConnectionError()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
            'Component status set incorrectly')
        self.mock_logger.warning.assert_called_with(
            'The URL is unreachable: GET http://localhost:8080/swagger')

    def test_evaluate_with_http_error(self):
        def request(method, url, timeout=None):
            self.assertEquals(method, 'GET', 'Incorrect HTTP method')
            self.assertEquals(url, 'http://localhost:8080/swagger',
                              'Monitored URL is incorrect')
            self.assertEquals(timeout, 0.010)

            raise HTTPError()

        sys.modules['requests'].request = request
        self.configuration.evaluate()

        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
            'Component status set incorrectly')
        self.mock_logger.exception.assert_called_with(
            'Unexpected HTTP response')

    def test_push_status(self):
        def put(url, params=None, headers=None):
            self.assertEquals(url,
                              'https://demo.cachethq.io/api/v1/components/1',
                              'Incorrect cachet API URL')
            self.assertDictEqual(params, {
                'id': 1,
                'status': 1
            }, 'Incorrect component update parameters')
            self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'},
                                 'Incorrect component update parameters')

            response = mock.Mock()
            response.status_code = 200
            return response

        sys.modules['requests'].put = put
        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
            'Incorrect component update parameters')
        self.configuration.push_status()

    def test_push_status_with_failure(self):
        def put(url, params=None, headers=None):
            self.assertEquals(url,
                              'https://demo.cachethq.io/api/v1/components/1',
                              'Incorrect cachet API URL')
            self.assertDictEqual(params, {
                'id': 1,
                'status': 1
            }, 'Incorrect component update parameters')
            self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'},
                                 'Incorrect component update parameters')

            response = mock.Mock()
            response.status_code = 400
            return response

        sys.modules['requests'].put = put
        self.assertEquals(
            self.configuration.status,
            cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
            'Incorrect component update parameters')
        self.configuration.push_status()
コード例 #25
0
ファイル: test_keychain.py プロジェクト: cdcarter/CumulusCI
class TestEnvironmentProjectKeychain(TestBaseProjectKeychain):
    keychain_class = EnvironmentProjectKeychain

    def setUp(self):
        super(TestEnvironmentProjectKeychain, self).setUp()
        self.env = EnvironmentVarGuard()
        self._clean_env(self.env)
        self.env.set("{}test".format(self.keychain_class.org_var_prefix), json.dumps(self.org_config.config))
        self.env.set(self.keychain_class.app_var, json.dumps(self.connected_app_config.config))
        self.env.set(
            "{}github".format(self.keychain_class.service_var_prefix), json.dumps(self.services["github"].config)
        )
        self.env.set(
            "{}mrbelvedere".format(self.keychain_class.service_var_prefix),
            json.dumps(self.services["mrbelvedere"].config),
        )
        self.env.set(
            "{}apextestsdb".format(self.keychain_class.service_var_prefix),
            json.dumps(self.services["apextestsdb"].config),
        )

    def _clean_env(self, env):
        for key, value in env.items():
            if key.startswith(self.keychain_class.org_var_prefix):
                del env[key]
        for key, value in env.items():
            if key.startswith(self.keychain_class.service_var_prefix):
                del env[key]
        if self.keychain_class.app_var in env:
            del env[self.keychain_class.app_var]

    def test_get_org(self):
        keychain = self.keychain_class(self.project_config, self.key)
        self.assertEquals(keychain.orgs.keys(), ["test"])
        self.assertEquals(keychain.get_org("test").config, self.org_config.config)

    def _test_list_orgs(self):
        with self.env:
            keychain = self.keychain_class(self.project_config, self.key)
            self.assertEquals(keychain.list_orgs(), ["test"])

    def test_list_orgs_empty(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            env.set(self.keychain_class.app_var, json.dumps(self.connected_app_config.config))
            self._test_list_orgs_empty()

    def test_get_org_not_found(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            env.set(self.keychain_class.app_var, json.dumps(self.connected_app_config.config))
            self._test_get_org_not_found()

    def test_get_default_org(self):
        with EnvironmentVarGuard() as env:
            self._clean_env(env)
            org_config = self.org_config.config.copy()
            org_config["default"] = True
            self.env.set("{}test".format(self.keychain_class.org_var_prefix), json.dumps(org_config))
            env.set(self.keychain_class.app_var, json.dumps(self.connected_app_config.config))
            self._test_get_default_org()