awx/awxkit/test/cli/test_format.py
Rigel Di Scala 579604d2c6 Allow YAML as a CLI import format
This changset allows the import of YAML formatted resources. The CLI
user can indicate which format to use with the `-f, --format` option.
The CLI help text has been amended to reflect the new feature.

The AWX CLI `export` subcommand offers the option of formatting the output
as YAML or JSON, so it makes sense that the `import` subcommand reflects
this.

A simple test is also provided. In order to ease the task of testing
commands that import resources by reading the stdin, the CLI has been
extended to allow specifying an alternative file descriptor for stdin,
similarly to stdout and stderr.
2020-08-10 23:43:53 +02:00

73 lines
1.7 KiB
Python

import io
import json
import yaml
from awxkit.api.pages import Page
from awxkit.api.pages.users import Users, User
from awxkit.cli import CLI
from awxkit.cli.format import format_response
from awxkit.cli.resource import Import
def test_json_empty_list():
page = Page.from_json({
'results': []
})
formatted = format_response(page)
assert json.loads(formatted) == {'results': []}
def test_yaml_empty_list():
page = Page.from_json({
'results': []
})
formatted = format_response(page, fmt='yaml')
assert yaml.safe_load(formatted) == {'results': []}
def test_json_list():
users = {
'results': [
{'username': 'betty'},
{'username': 'tom'},
{'username': 'anne'},
]
}
page = Users.from_json(users)
formatted = format_response(page)
assert json.loads(formatted) == users
def test_yaml_list():
users = {
'results': [
{'username': 'betty'},
{'username': 'tom'},
{'username': 'anne'},
]
}
page = Users.from_json(users)
formatted = format_response(page, fmt='yaml')
assert yaml.safe_load(formatted) == users
def test_yaml_import():
class MockedV2:
def import_assets(self, data):
self._parsed_data = data
def _dummy_authenticate():
pass
yaml_fd = io.StringIO(
"""
workflow_job_templates:
- name: Workflow1
"""
)
cli = CLI(stdin=yaml_fd)
cli.parse_args(['--conf.format', 'yaml'])
cli.v2 = MockedV2()
cli.authenticate = _dummy_authenticate
Import().handle(cli, None)
assert cli.v2._parsed_data['workflow_job_templates'][0]['name']