Exemple #1
0
def test_progress_percentage(cleanup_tempfiles):
    class ProgressCallbackInvoker(BaseSubscriber):
        """A back-compat wrapper to invoke a provided callback via a subscriber

        :param callback: A callable that takes a single positional argument for
            how many bytes were transferred.
        """
        def __init__(self, callback):
            self._callback = callback

        def on_progress(self, bytes_transferred, **kwargs):
            self._callback(bytes_transferred)

    # create dummy file
    tf = NamedTemporaryFile(delete=False, suffix='.tgz')
    cleanup_tempfiles.append(tf.name)
    open(tf.name, 'w').write('some content here...')
    out = StringIO()
    # instantiate ProgressReporter
    callback = ProgressPercentage(tf.name, out=out)
    subscriber = ProgressCallbackInvoker(callback)
    # 1 byte -> 5%
    time.sleep(0.001)
    subscriber.on_progress(bytes_transferred=1)
    assert_regexp_matches(out.getvalue().strip(),
                          '.*\.tgz  1 / 20\.0  \(5\.00%\)')
    # 11 (1+10) byte -> 55%
    subscriber.on_progress(bytes_transferred=10)
    assert_regexp_matches(out.getvalue().strip(),
                          '.*\.tgz  11 / 20\.0  \(55\.00%\)')
Exemple #2
0
def test_compile_template(cleanup_tempfiles):
    swagger_template_file = create_tempfile(
        textwrap.dedent("""\
        ---
          swagger: "2.0"
          info:
            title: {{apiName}}
            description: {{apiDescription}}
            version: "0.0.1"
          basePath: "/{{apiBasePath}}"
          host: "{{apiHostname}}"
    """))
    cleanup_tempfiles.append(swagger_template_file)

    template_params = {
        'apiName': 'apiName',
        'apiDescription': 'apiDescription',
        'apiBasePath': 'apiBasePath',
        'apiHostname': 'apiHostname'
    }

    expected = textwrap.dedent("""\
        ---
          swagger: "2.0"
          info:
            title: apiName
            description: apiDescription
            version: "0.0.1"
          basePath: "/apiBasePath"
          host: "apiHostname"
    """)

    assert_equal(_compile_template(swagger_template_file, template_params),
                 expected)
Exemple #3
0
def test_load_cloudformation_template(cleanup_tempfiles):
    tf = NamedTemporaryFile(delete=False, suffix='py')
    open(tf.name, 'w').write('def plus(a, b):\n    return a+b')
    cleanup_tempfiles.append(tf.name)

    module, success = load_cloudformation_template(tf.name)
    assert_equal(success, True)
    assert_equal(module.plus(1, 2), 3)