Skip to content

Commit

Permalink
Add compose plugin in pdc client
Browse files Browse the repository at this point in the history
JIRA: PDC-1022
  • Loading branch information
simozhan committed Oct 9, 2015
1 parent 2a5b8a6 commit 6dd0a49
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 0 deletions.
120 changes: 120 additions & 0 deletions pdc_client/plugins/compose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
import json

from pdc_client import get_paged
from pdc_client.plugin_helpers import PDCClientPlugin, add_parser_arguments, extract_arguments


class ComposePlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('compose-list', help='list all composes')
subcmd.add_argument('--deleted', action='store_true',
help='show deleted composes')
subcmd.set_defaults(func=self.list_composes)

subcmd = self.add_command('compose-info', help='display details of a compose')
subcmd.add_argument('compose_id', metavar='COMPOSE_ID')
subcmd.set_defaults(func=self.compose_info)

subcmd = self.add_admin_command('compose-update',
help='partial update an existing compose.',
description='only some compose fields can be modified by this call.\
these are acceptance_testing, linked_releases and rtt_tested_architectures.')
subcmd.add_argument('compose_id', metavar='COMPOSE_ID')
self.add_compose_arguments(subcmd)
subcmd.set_defaults(func=self.compose_update)
self.compose_update = subcmd

def add_compose_arguments(self, parser):
add_parser_arguments(parser, {
'acceptance_testing': {'choices': ['passed', 'failed', 'untested']},
'linked_releases': {'nargs': '*', 'metavar': 'RELEASE_ID'},
'rtt_tested_architectures': {'nargs': '*', 'default': [], 'help': 'input format VARIANT:ARCHES:TESTING_STATUS'}})

def list_composes(self, args):
filters = {}
if not args.deleted:
filters['deleted'] = False
else:
filters['deleted'] = args.deleted

composes = get_paged(self.client.composes._, **filters)
if args.json:
print json.dumps(list(composes))
return

for compose in composes:
print compose['compose_id']

def compose_info(self, args, compose_id=None):
compose_id = compose_id or args.compose_id
compose = self.client.composes[compose_id]._()
if args.json:
print json.dumps(compose)
return

fmt = '{:25} {}'
print fmt.format('Compose ID', compose['compose_id'])
print fmt.format('Compose Label', compose['compose_label'] or '')
print fmt.format('Compose Date', compose['compose_date'])
print fmt.format('Compose Respin', compose['compose_respin'])
print fmt.format('Compose Type', compose['compose_type'])
print fmt. format('Acceptance Testing', compose['acceptance_testing'])
print fmt.format('Deleted', compose['deleted'])
print fmt.format('Release', compose['release'])
print fmt.format('Rpm Mapping Template', compose['rpm_mapping_template'])

if compose['linked_releases']:
print '\nLinked Releases'
for release in compose['linked_releases']:
print fmt.format('Release', release)

if compose['sigkeys']:
print '\nSigkeys'
for sigkey in compose['sigkeys']:
print fmt.format('Sigkey', sigkey)

if compose['rtt_tested_architectures']:
print '\nRtt Tested Architectures'
fmt = '{:25} {:15} {}'
print fmt.format('Variant', 'Arches', 'Testing Status')
for key, value in compose['rtt_tested_architectures'].iteritems():
for subkey, subvalue in value.iteritems():
print fmt.format(key, subkey, subvalue)

def compose_update(self, args):
data = self.get_compose_data(args)

if data:
self.logger.debug('Updating compose {} with data {}'.format(args.compose_id, data))
self.client.composes[args.compose_id]._ += data
else:
self.logger.info('No change required, not making a request')

self.compose_info(args)

def get_compose_data(self, args):
data = extract_arguments(args)
rtts = data.get('rtt_tested_architectures', None)
rtts = rtts if type(rtts) is list else [rtts]
dic = {}
for rtt in rtts:
parts = rtt.split(':')
if not len(parts) == 3:
self.compose_update.error('Please input rtt-tested-architectures in format VARIANT:ARCHES:TESTING_STATUS.\n')

variant = parts[0]
arches = parts[1]
status = parts[2]
dic.update({variant: {arches: status}})

data['rtt_tested_architectures'] = dic

return data

PLUGIN_CLASSES = [ComposePlugin]
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions pdc_client/tests/compose/data/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compose_id": "awesome-product-20130203.7",
"compose_date": "2013-02-03",
"compose_type": "nightly",
"compose_respin": 0,
"release": "aifv-5802",
"compose_label": "RC-3.0",
"deleted": false,
"rpm_mapping_template": "https://pdc.example.com/rest_api/v1/composes/awesome-product-20130203.7/rpm-mapping/{{package}}/",
"sigkeys": [
"fd431d51"
],
"acceptance_testing": "untested",
"linked_releases": [
"sap-7.0-awesome-product"
],
"rtt_tested_architectures": {
"Workstation": {
"x86_64": "untested"
}
}
}
19 changes: 19 additions & 0 deletions pdc_client/tests/compose/data/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Compose ID awesome-product-20130203.7
Compose Label RC-3.0
Compose Date 2013-02-03
Compose Respin 0
Compose Type nightly
Acceptance Testing untested
Deleted False
Release aifv-5802
Rpm Mapping Template https://pdc.example.com/rest_api/v1/composes/awesome-product-20130203.7/rpm-mapping/{{package}}/

Linked Releases
Release sap-7.0-awesome-product

Sigkeys
Sigkey fd431d51

Rtt Tested Architectures
Variant Arches Testing Status
Workstation x86_64 untested
23 changes: 23 additions & 0 deletions pdc_client/tests/compose/data/list_multi_page.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
awesome-product-20150924.0
awesome-product-20150924.1
awesome-product-20150924.2
awesome-product-20150924.3
awesome-product-20150924.4
awesome-product-20150924.5
awesome-product-20150924.6
awesome-product-20150924.7
awesome-product-20150924.8
awesome-product-20150924.9
awesome-product-20150924.10
awesome-product-20150924.11
awesome-product-20150924.12
awesome-product-20150924.13
awesome-product-20150924.14
awesome-product-20150924.15
awesome-product-20150924.16
awesome-product-20150924.17
awesome-product-20150924.18
awesome-product-20150924.19
awesome-product-20150924.20
awesome-product-20150924.21
awesome-product-20150924.22
98 changes: 98 additions & 0 deletions pdc_client/tests/compose/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
from pdc_client.test_helpers import CLITestCase
from pdc_client.runner import Runner


class ComposeTestCase(CLITestCase):
def setUp(self):
self.runner = Runner()
self.runner.setup()
self.compose_detail = {
"compose_id": "awesome-product-20130203.7",
"compose_date": "2013-02-03",
"compose_type": "nightly",
"compose_respin": 0,
"release": "aifv-5802",
"compose_label": "RC-3.0",
"deleted": False,
"rpm_mapping_template": "https://pdc.example.com/rest_api/v1/composes/awesome-product-20130203.7/rpm-mapping/{{package}}/",
"sigkeys": [
"fd431d51"
],
"acceptance_testing": "untested",
"linked_releases": [
"sap-7.0-awesome-product"
],
"rtt_tested_architectures": {
"Workstation": {
"x86_64": "untested"
}
}
}

def test_list_multi_page(self, api):
api.add_endpoint('composes', 'GET', [
{'compose_id': 'awesome-product-20150924.{}'.format(x)}
for x in range(23)
])
with self.expect_output('list_multi_page.txt'):
self.runner.run(['compose-list'])
self.assertEqual(api.calls['composes'],
[('GET', {'page': 1, 'deleted': False}),
('GET', {'page': 2, 'deleted': False})])

def test_list_deleted(self, api):
api.add_endpoint('composes', 'GET', [])
with self.expect_output('empty.txt'):
self.runner.run(['compose-list', '--deleted'])
self.assertEqual(api.calls['composes'],
[('GET', {'page': 1, 'deleted': True})])

def test_info(self, api):
api.add_endpoint('composes/awesome-product-20130203.7', 'GET', self.compose_detail)
with self.expect_output('info.txt'):
self.runner.run(['compose-info', 'awesome-product-20130203.7'])
self.assertDictEqual(api.calls,
{'composes/awesome-product-20130203.7': [('GET', {})]})

def test_info_json(self, api):
api.add_endpoint('composes/awesome-product-20130203.7', 'GET', self.compose_detail)
with self.expect_output('info.json', parse_json=True):
self.runner.run(['--json', 'compose-info', 'awesome-product-20130203.7'])
self.assertDictEqual(api.calls,
{'composes/awesome-product-20130203.7': [('GET', {})]})

def test_update(self, api):
api.add_endpoint('composes/awesome-product-20130203.7', 'GET', self.compose_detail)
with self.expect_output('info.txt'):
self.runner.run(['compose-update', 'awesome-product-20130203.7',
'--acceptance-testing', 'passed',
'--linked-releases', 'sap-7.0-awesome-product',
'--rtt-tested-architectures', 'Workstation:x86_64:passed'
])
self.assertDictEqual(api.calls, {'composes/awesome-product-20130203.7':
[('PATCH', {'acceptance_testing': 'passed',
'linked_releases': ['sap-7.0-awesome-product'],
'rtt_tested_architectures': {'Workstation': {'x86_64': 'passed'}}}),
('GET', {})]})

def test_update_wrong_input1(self, api):
with self.expect_failure():
self.runner.run(['compose-update', 'awesome-product-20130203.7',
'--acceptance-testing', 'passed',
'--linked-releases', 'sap-7.0-awesome-product',
'--rtt-tested-architectures', 'Workstation:x86_64:passed:wronginput'
])

def test_update_wrong_input2(self, api):
with self.expect_failure():
self.runner.run(['compose-update', 'awesome-product-20130203.7',
'--acceptance-testing', 'wronginput',
'--linked-releases', 'sap-7.0-awesome-product',
'--rtt-tested-architectures', 'Workstation:x86_64:passed'
])

0 comments on commit 6dd0a49

Please sign in to comment.