def test__prepare_job():
    # Test for `zpmlib.zpm._prepare_job`.

    # Contents of `boot/system.map`, which is expected to be in the
    # `myapp.zapp` archive.
    myapp_json = [
        {'exec': {'args': 'myapp.py', 'path': 'file://python2.7:python'},
         'devices': [{'name': 'python2.7'}, {'name': 'stdout'}],
         'name': 'myapp'}
    ]
    zapp = {'meta': {'name': 'myapp'}}
    zapp_swift_url = ('swift://AUTH_469a9cd20b5a4fc5be9438f66bb5ee04/'
                      'test_container/hello.zapp')

    # Expected result
    exp_job_json = copy.deepcopy(myapp_json)
    exp_job_json[0]['devices'].append(
        {'name': 'image', 'path': zapp_swift_url}
    )

    tempdir = tempfile.mkdtemp()
    try:
        tempzapp = os.path.join(tempdir, 'myapp.zapp')
        tf = tarfile.open(tempzapp, 'w:gz')

        # prepare a sample job description
        system_map = os.path.join(tempdir, 'system.map')
        with open(system_map, 'w') as fp:
            json.dump(myapp_json, fp)

        tf.add(system_map, arcname='boot/system.map')
        tf.close()

        tf = tarfile.open(tempzapp, 'r:gz')
        job = zpm._prepare_job(tf, zapp, zapp_swift_url)
        tf.close()
        assert exp_job_json == job
    finally:
        shutil.rmtree(tempdir)