<commit_before>import logging
from argparse import Namespace
from mock import Mock, patch, call
import StringIO

from assess_resources import (
    assess_resources,
    large_assess,
    parse_args,
    push_resource,
    main,
    verify_status,
)
from tests import (
    parse_error,
    TestCase,
)
from utility import JujuAssertionError


class TestParseArgs(TestCase):

    def test_common_args(self):
        args = parse_args(
            ["an-env",
             "/bin/juju",
             "/tmp/logs",
             "an-env-mod"])
        self.assertEqual("an-env", args.env)
        self.assertEqual("/bin/juju", args.juju_bin)
        self.assertEqual("/tmp/logs", args.logs)
        self.assertEqual("an-env-mod", args.temp_env_name)
        self.assertEqual(False, args.debug)

    def test_help(self):
        fake_stdout = StringIO.StringIO()
        with parse_error(self) as fake_stderr:
            with patch("sys.stdout", fake_stdout):
                parse_args(["--help"])
        self.assertEqual("", fake_stderr.getvalue())
        self.assertNotIn("TODO", fake_stdout.getvalue())


class TestMain(TestCase):

    def test_main(self):
        argv = [
            "an-env",
            "/bin/juju",
            "/tmp/logs",
            "an-env-mod",
            "--verbose",
            ]
        client = Mock(spec=["is_jes_enabled"])
        with patch("assess_resources.configure_logging",
                   autospec=True) as mock_cl:
            with patch("assess_resources.BootstrapManager.booted_context",
                       autospec=True) as mock_bc:
                    with patch("deploy_stack.client_from_config",
                               return_value=client) as mock_c:
                        with patch("assess_resources.assess_resources",
                                   autospec=True) as mock_assess:
                            main(argv)
        mock_cl.assert_called_once_with(logging.DEBUG)
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False)
        self.assertEqual(mock_bc.call_count, 1)
        mock_assess.assert_called_once_with(client, make_args())


class TestAssessResources(TestCase):

    def test_verify_status(self):
        verify_status(
            make_resource_list(), 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_serviceid(self):
        verify_status(make_resource_list('serviceid'), 'dummy-resource/foo',
                      'foo', '1234', 27)

    def test_verify_status_exception(self):
        status = make_resource_list()
        status['resources'][0]['expected']['origin'] = 'charmstore'
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Unexpected resource list values'):
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_unit_exception(self):
        status = make_resource_list()
        status['resources'][0]['unit']['origin'] = 'charmstore'
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Unexpected unit resource list values'):
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_no_resoruce_id_exception(self):
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Resource id not found.'):
            verify_status(make_resource_list(), 'NO_ID', 'foo', '1234', 27)

    def test_push_resource(self):
        mock_client = Mock(
            spec=["deploy", "wait_for_started", "list_resources",
                  "wait_for_resource", "show_status"])
        mock_client.version = '2.0.0'
        mock_client.list_resources.return_value = make_resource_list()
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800)
        mock_client.deploy.assert_called_once_with(
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)
        mock_client.show_status.assert_called_once_with()

    def test_push_resource_attach(self):
        mock_client = Mock(
            spec=["attach", "wait_for_started", "list_resources",
                  "wait_for_resource", "show_status"])
        mock_client.version = '2.0.0'
        mock_client.list_resources.return_value = make_resource_list()
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800, deploy=False)
        mock_client.attach.assert_called_once_with(
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)

    def test_assess_resources(self):
        fake_file = FakeFile()
        args = make_args()
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
            with patch('assess_resources.NamedTemporaryFile',
                       autospec=True) as mock_ntf:
                mock_ntf.return_value.__enter__.return_value = fake_file
                assess_resources(None, args)
        calls = [
            call(
                None, 'foo',
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
            call(
                None, 'bar',
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
                deploy=False),
            call(
                None, 'bar',
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
                deploy=False, resource_file='baz.txt'),
            call(
                None, 'bar',
                '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e74'
                '7c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800, 1800,
                deploy=False, resource_file='/tmp/fake'),
        ]
        self.assertEqual(mock_p.mock_calls, calls)

    def test_assess_resources_large_test(self):
        fake_file = FakeFile()
        args = make_args()
        args.large_test_enabled = True
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
            with patch('assess_resources.fill_dummy_file',
                       autospec=True) as mock_fdf:
                with patch('assess_resources.NamedTemporaryFile',
                           autospec=True) as mock_ntf:
                    mock_ntf.return_value.__enter__.return_value = fake_file
                    with patch('assess_resources.large_assess',
                               autospec=True) as mock_lt:
                        assess_resources(None, args)
        calls = [
            call(
                None, 'foo',
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
            call(
                None, 'bar',
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
                deploy=False),
            call(
                None, 'bar',
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
                deploy=False, resource_file='baz.txt'),
            call(None, 'bar',
                 '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e7'
                 '47c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800,
                 1800, deploy=False, resource_file='/tmp/fake')]
        self.assertEqual(mock_p.mock_calls, calls)
        mock_fdf.assert_called_once_with('/tmp/fake', 1024 * 1024)
        mock_lt.assert_called_once_with(None, 1800, 1800)

    def test_large_tests(self):
        fake_file = FakeFile()
        with patch("assess_resources.push_resource", autospec=True) as mock_pr:
            with patch('assess_resources.NamedTemporaryFile',
                       autospec=True) as mock_ntf:
                mock_ntf.return_value.__enter__.return_value = fake_file<commit_msg>Expect mocked call uses kwarg size to match real call<commit_after>import logging
from argparse import Namespace
from mock import Mock, patch, call
import StringIO

from assess_resources import (
    assess_resources,
    large_assess,
    parse_args,
    push_resource,
    main,
    verify_status,
)
from tests import (
    parse_error,
    TestCase,
)
from utility import JujuAssertionError


class TestParseArgs(TestCase):

    def test_common_args(self):
        args = parse_args(
            ["an-env",
             "/bin/juju",
             "/tmp/logs",
             "an-env-mod"])
        self.assertEqual("an-env", args.env)
        self.assertEqual("/bin/juju", args.juju_bin)
        self.assertEqual("/tmp/logs", args.logs)
        self.assertEqual("an-env-mod", args.temp_env_name)
        self.assertEqual(False, args.debug)

    def test_help(self):
        fake_stdout = StringIO.StringIO()
        with parse_error(self) as fake_stderr:
            with patch("sys.stdout", fake_stdout):
                parse_args(["--help"])
        self.assertEqual("", fake_stderr.getvalue())
        self.assertNotIn("TODO", fake_stdout.getvalue())


class TestMain(TestCase):

    def test_main(self):
        argv = [
            "an-env",
            "/bin/juju",
            "/tmp/logs",
            "an-env-mod",
            "--verbose",
            ]
        client = Mock(spec=["is_jes_enabled"])
        with patch("assess_resources.configure_logging",
                   autospec=True) as mock_cl:
            with patch("assess_resources.BootstrapManager.booted_context",
                       autospec=True) as mock_bc:
                    with patch("deploy_stack.client_from_config",
                               return_value=client) as mock_c:
                        with patch("assess_resources.assess_resources",
                                   autospec=True) as mock_assess:
                            main(argv)
        mock_cl.assert_called_once_with(logging.DEBUG)
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False)
        self.assertEqual(mock_bc.call_count, 1)
        mock_assess.assert_called_once_with(client, make_args())


class TestAssessResources(TestCase):

    def test_verify_status(self):
        verify_status(
            make_resource_list(), 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_serviceid(self):
        verify_status(make_resource_list('serviceid'), 'dummy-resource/foo',
                      'foo', '1234', 27)

    def test_verify_status_exception(self):
        status = make_resource_list()
        status['resources'][0]['expected']['origin'] = 'charmstore'
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Unexpected resource list values'):
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_unit_exception(self):
        status = make_resource_list()
        status['resources'][0]['unit']['origin'] = 'charmstore'
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Unexpected unit resource list values'):
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)

    def test_verify_status_no_resoruce_id_exception(self):
        with self.assertRaisesRegexp(
                JujuAssertionError, 'Resource id not found.'):
            verify_status(make_resource_list(), 'NO_ID', 'foo', '1234', 27)

    def test_push_resource(self):
        mock_client = Mock(
            spec=["deploy", "wait_for_started", "list_resources",
                  "wait_for_resource", "show_status"])
        mock_client.version = '2.0.0'
        mock_client.list_resources.return_value = make_resource_list()
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800)
        mock_client.deploy.assert_called_once_with(
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)
        mock_client.show_status.assert_called_once_with()

    def test_push_resource_attach(self):
        mock_client = Mock(
            spec=["attach", "wait_for_started", "list_resources",
                  "wait_for_resource", "show_status"])
        mock_client.version = '2.0.0'
        mock_client.list_resources.return_value = make_resource_list()
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800, deploy=False)
        mock_client.attach.assert_called_once_with(
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)

    def test_assess_resources(self):
        fake_file = FakeFile()
        args = make_args()
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
            with patch('assess_resources.NamedTemporaryFile',
                       autospec=True) as mock_ntf:
                mock_ntf.return_value.__enter__.return_value = fake_file
                assess_resources(None, args)
        calls = [
            call(
                None, 'foo',
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
            call(
                None, 'bar',
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
                deploy=False),
            call(
                None, 'bar',
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
                deploy=False, resource_file='baz.txt'),
            call(
                None, 'bar',
                '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e74'
                '7c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800, 1800,
                deploy=False, resource_file='/tmp/fake'),
        ]
        self.assertEqual(mock_p.mock_calls, calls)

    def test_assess_resources_large_test(self):
        fake_file = FakeFile()
        args = make_args()
        args.large_test_enabled = True
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
            with patch('assess_resources.fill_dummy_file',
                       autospec=True) as mock_fdf:
                with patch('assess_resources.NamedTemporaryFile',
                           autospec=True) as mock_ntf:
                    mock_ntf.return_value.__enter__.return_value = fake_file
                    with patch('assess_resources.large_assess',
                               autospec=True) as mock_lt:
                        assess_resources(None, args)
        calls = [
            call(
                None, 'foo',
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
            call(
                None, 'bar',
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
                deploy=False),
            call(
                None, 'bar',
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
                deploy=False, resource_file='baz.txt'),
            call(None, 'bar',
                 '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e7'
                 '47c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800,
                 1800, deploy=False, resource_file='/tmp/fake')]
        self.assertEqual(mock_p.mock_calls, calls)
        mock_fdf.assert_called_once_with('/tmp/fake', size=1024 * 1024)
        mock_lt.assert_called_once_with(None, 1800, 1800)

    def test_large_tests(self):
        fake_file = FakeFile()
        with patch("assess_resources.push_resource", autospec=True) as mock_pr:
            with patch('assess_resources.NamedTemporaryFile',
                       autospec=True) as mock_ntf:
                mock_ntf.return_value.__enter__.return_value = fake_file
{'fraction_non_alphanumeric': 0.09878725089889695, 'fraction_numerical': 0.08300322993479188, 'mean_word_length': 2.2938578883982337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465953', 'n_tokens_mistral': 6055, 'n_tokens_neox': 5085, 'n_words': 768}