Implemented plugin creation and building in fpb

* `fpb --create plugin_name` creates a plugin structure
* `fpb --build plugin_path` builds repository, and
  packs plugin as tar.gz
* add run_tests.sh which runs pep8 checks and unit
  tests for fpb

Change-Id: Id4d108b7427869a5dc0200158d3e2c93c59a33ca
Implements: blueprint cinder-neutron-plugins-in-fuel
This commit is contained in:
Evgeniy L 2014-10-14 14:08:59 +04:00
parent d95ac95b0a
commit d9b923ef86
26 changed files with 1350 additions and 40 deletions

View File

@ -14,13 +14,14 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import logging
import yaml
from os.path import join as join_path
from fuel_plugin_builder import utils
from fuel_plugin_builder import errors
from fuel_plugin_builder.actions import BaseAction
from fuel_plugin_builder import errors
from fuel_plugin_builder import utils
logger = logging.getLogger(__name__)
@ -31,23 +32,65 @@ class BuildPlugin(BaseAction):
def __init__(self, plugin_path):
self.plugin_path = plugin_path
self.pre_build_hook_path = os.path.join(plugin_path, 'pre_build_hook')
self.centos_repo_path = os.path.join(plugin_path, 'repos/centos')
self.ubuntu_repo_path = os.path.join(plugin_path, 'repos/ubuntu')
self.pre_build_hook_path = join_path(plugin_path, 'pre_build_hook')
self.meta = yaml.load(open(join_path(plugin_path, 'metadata.yaml')))
self.build_dir = join_path(plugin_path, '.build')
def run(self):
self.build_repos()
self.make_package()
def make_package(self):
full_name = '{0}-{1}'.format(self.meta['name'],
self.meta['version'])
tar_name = '{0}.fp'.format(full_name)
tar_path = join_path(
self.plugin_path,
tar_name)
if utils.exists(tar_path):
utils.remove(tar_path)
utils.make_tar_gz(self.build_dir, tar_path, full_name)
def build_repos(self):
utils.remove(self.build_dir)
utils.create_dir(self.build_dir)
if utils.which(self.pre_build_hook_path):
utils.exec_cmd(self.pre_build_hook_path)
utils.exec_cmd(
'createrepo -o {0} {1}'.format(
os.path.join(self.centos_repo_path, 'x86_64'),
os.path.join(self.centos_repo_path, 'x86_64', 'Packages')))
utils.exec_cmd(
'dpkg-scanpackages {0} | gzip -c9 > {1}'.format(
self.ubuntu_repo_path,
os.path.join(self.ubuntu_repo_path, 'Packages.gz')))
utils.copy_files_in_dir(
join_path(self.plugin_path, '*'),
self.build_dir)
releases_paths = {}
for release in self.meta['releases']:
releases_paths.setdefault(release['os'], [])
releases_paths[release['os']].append(
join_path(self.build_dir, release['repository_path']))
self.build_ubuntu_repos(releases_paths.get('ubuntu', []))
self.build_centos_repos(releases_paths.get('centos', []))
@classmethod
def build_ubuntu_repos(cls, releases_paths):
for repo_path in releases_paths:
utils.exec_cmd(
'dpkg-scanpackages {0} | gzip -c9 > {1}'.format(
repo_path,
join_path(repo_path, 'Packages.gz')))
@classmethod
def build_centos_repos(cls, releases_paths):
for repo_path in releases_paths:
repo_packages = join_path(repo_path, 'Packages')
utils.create_dir(repo_packages)
utils.move_files_in_dir(
join_path(repo_path, '*.rpm'),
repo_packages)
utils.exec_cmd('createrepo -o {0} {1}'.format(
repo_path, repo_packages))
def check(self):
not_found = filter(lambda r: not utils.which(r), self.requires)
@ -56,4 +99,4 @@ class BuildPlugin(BaseAction):
raise errors.FuelCannotFindCommandError(
'Cannot find commands "{0}", '
'install required commands and try again'.format(
','.join(not_found)))
', '.join(not_found)))

View File

@ -15,20 +15,102 @@
# under the License.
import logging
import os
from fuel_plugin_builder.actions import BaseAction
from fuel_plugin_builder import errors
from fuel_plugin_builder import utils
logger = logging.getLogger(__name__)
class CreatePlugin(BaseAction):
def __init__(self, plugin_name):
self.plugin_name = plugin_name
plugin_structure = [
{'path': 'metadata.yaml',
'action': 'render',
'from': 'metadata.yaml'},
{'path': 'environment_config.yaml',
'action': 'render',
'from': 'environment_config.yaml'},
{'path': 'LICENSE',
'action': 'copy',
'from': 'LICENSE'},
{'path': 'deployment_scripts',
'action': 'mkdir'},
{'path': 'deployment_scripts/deploy.sh',
'action': 'render',
'from': 'deploy.sh'},
{'path': 'README.md',
'action': 'render',
'from': 'README.md'},
{'path': 'repositories',
'action': 'mkdir'},
{'path': 'repositories/centos',
'action': 'mkdir'},
{'path': 'repositories/ubuntu',
'action': 'mkdir'},
{'path': 'tasks.yaml',
'action': 'copy',
'from': 'tasks.yaml'},
{'path': '.gitignore',
'action': 'copy',
'from': '.gitignore'},
{'path': 'pre_build_hook',
'action': 'copy',
'from': 'pre_build_hook'},
{'path': 'install',
'action': 'render',
'from': 'install'},
{'path': 'register_plugin.py',
'action': 'copy',
'from': 'register_plugin.py'}]
template_dir = os.path.join(os.path.dirname(__file__), '..', 'templates')
def __init__(self, plugin_path):
self.plugin_name = utils.basename(plugin_path)
self.plugin_path = plugin_path
self.render_ctx = {
'plugin_name': self.plugin_name,
'plugin_version': '0.1.0'}
def check(self):
pass
if utils.exists(self.plugin_path):
raise errors.PluginDirectoryExistsError(
'Plugins directory {0} already exists, '
'choose anothe name'.format(self.plugin_path))
def run(self):
pass
utils.create_dir(self.plugin_path)
for conf in self.plugin_structure:
if conf['action'] == 'render':
utils.render_to_file(
os.path.join(self.template_dir, conf['from']),
os.path.join(self.plugin_path, conf['path']),
self.render_ctx)
utils.copy_file_permissions(
os.path.join(self.template_dir, conf['from']),
os.path.join(self.plugin_path, conf['path']))
elif conf['action'] == 'mkdir':
utils.create_dir(os.path.join(
self.plugin_path,
conf['path']))
elif conf['action'] == 'copy':
utils.copy(
os.path.join(self.template_dir, conf['from']),
os.path.join(self.plugin_path, conf['path']))

View File

@ -14,16 +14,17 @@
# License for the specific language governing permissions and limitations
# under the License.
import sys
import argparse
import logging
import sys
from fuel_plugin_builder import actions
from fuel_plugin_builder import messages
from fuel_plugin_builder import errors
from fuel_plugin_builder import messages
from fuel_plugin_builder.logger import configure_logger
logger = configure_logger()
logger = logging.getLogger(__name__)
def handle_exception(exc):
@ -51,6 +52,9 @@ def parse_args():
group.add_argument(
'--build', help='build a plugin',
nargs=1, metavar='path_to_directory')
parser.add_argument(
'--debug', help='enable debug mode',
action="store_true")
return parser.parse_args()
@ -78,6 +82,8 @@ def main():
"""Entry point
"""
try:
perform_action(parse_args())
args = parse_args()
configure_logger(debug=args.debug)
perform_action(args)
except Exception as exc:
handle_exception(exc)

View File

@ -17,9 +17,13 @@ class FuelPluginException(Exception):
pass
class FuelCannotFindCommandError(Exception):
class FuelCannotFindCommandError(FuelPluginException):
pass
class ExecutedErrorNonZeroExitCode(Exception):
class ExecutedErrorNonZeroExitCode(FuelPluginException):
pass
class PluginDirectoryExistsError(FuelPluginException):
pass

View File

@ -15,12 +15,15 @@
# under the License.
import logging
import sys
def configure_logger():
def configure_logger(debug=False):
logger = logging.getLogger('fuel_plugin_builder')
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
if debug:
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s %(levelname)s %(process)d (%(module)s) %(message)s',
"%Y-%m-%d %H:%M:%S")

View File

@ -0,0 +1,3 @@
.tox
.build
*.pyc

View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1,4 @@
${plugin_name}
============
Plugin description

View File

@ -0,0 +1,4 @@
#!/bin/bash
# It's a script which deploys your plugin
echo ${plugin_name} > /tmp/${plugin_name}

View File

@ -0,0 +1,7 @@
attributes:
${plugin_name}_text:
value: 'Set default value'
label: 'Text field'
description: 'Description for text field'
weight: 25
type: "text"

View File

@ -0,0 +1,11 @@
#!/bin/bash
plugin_name=${plugin_name}-${plugin_version}
repo_path=/var/www/nailgun
plugin_path=$repo_path/plugins/$plugin_name
rm -rf $plugin_path
mkdir -p $plugin_path
cp -rf * $plugin_path/
python register_plugin.py ${plugin_name}

View File

@ -0,0 +1,31 @@
# Plugin name
name: ${plugin_name}
# Plugin version
version: ${plugin_version}
# Description
description: Enable to use plugin X for Neutron
# Required fuel version
fuel_version: 6.0
# The plugin is compatible with releases in the list
releases:
- os: ubuntu
version: 2014.2-6.0
mode: ['ha', 'multinode']
deployment_scripts_path: deployment_scripts/
repository_path: repositories/ubuntu
- os: centos
version: 2014.2-6.0
mode: ['ha', 'multinode']
deployment_scripts_path: deployment_scripts/
repository_path: repositories/centos
# Plugin types are required to determine what this plugins
# extends and how to install them
types:
- nailgun
- repository
- deployment_scripts
# Version of plugin package
package_version: '1'

View File

@ -0,0 +1,5 @@
#!/bin/bash
# Add here any the actions which are required before plugin build
# like packages building, packages downloading from mirrors and so on.
# The script should return 0 if there were no errors.

View File

@ -0,0 +1,105 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import requests
import yaml
# TODO(eli): it's a development version, and requires a
# lot of refactoring:
# * ask user for credentials
# * use fuel clien config to retrieve information
# about keystone/nailgun end-points
# * validation if current fuel version is compatibele with
# plugin's one
#
# Also there is discussion about moving this code in fuel-client
keystone = '0.0.0.0:5000'
master = '0.0.0.0:8000'
class KeystoneClient(object):
"""Simple keystone authentification client
:param str username: is user name
:param str password: is user password
:param str auth_url: authentification url
:param str tenant_name: tenant name
"""
def __init__(self, username=None, password=None,
auth_url=None, tenant_name=None):
self.auth_url = auth_url
self.tenant_name = tenant_name
self.username = username
self.password = password
@property
def request(self):
"""Creates authentification session if required
:returns: :class:`requests.Session` object
"""
session = requests.Session()
token = self.get_token()
if token:
session.headers.update({'X-Auth-Token': token})
return session
def get_token(self):
try:
resp = requests.post(
self.auth_url,
headers={'content-type': 'application/json'},
data=json.dumps({
'auth': {
'tenantName': self.tenant_name,
'passwordCredentials': {
'username': self.username,
'password': self.password}}})).json()
return (isinstance(resp, dict) and
resp.get('access', {}).get('token', {}).get('id'))
except (ValueError, requests.exceptions.RequestException) as exc:
print('Cannot authenticate in keystone: {0}'.format(exc))
return None
keystone_client = KeystoneClient(
username='admin',
password='admin',
auth_url='http://{0}/v2.0/tokens'.format(keystone),
tenant_name='admin')
def register_plugin(plugin_name):
with open('metadata.yaml') as m:
data = yaml.load(m.read())
print(data)
resp = keystone_client.request.post(
'http://{0}/api/plugins'.format(master),
json.dumps(data))
print(resp)
if __name__ == '__main__':
import sys
plugin_name = sys.argv[1]
register_plugin(plugin_name)

View File

@ -0,0 +1,19 @@
# This tasks will be applied on controller nodes,
# here you can also specify several roles, for example
# ['cinder', 'compute'] will be applied only on
# cinder and compute nodes
- role: ['controller']
stage: post_deployment
type: shell
priority: 10
parameters:
cmd: ./deploy.sh
timeout: 42
# Task is applied for all roles
- role: '*'
stage: pre_deployment
type: shell
priority: 10
parameters:
cmd: echo all > /tmp/plugin.all
timeout: 42

View File

@ -0,0 +1,13 @@
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

View File

@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
try:
from unittest.case import TestCase
except ImportError:
# Required for python 2.6
from unittest2.case import TestCase
import mock
from StringIO import StringIO
class FakeFile(StringIO):
"""It's a fake file which returns StringIO
when file opens with 'with' statement.
NOTE(eli): We cannot use mock_open from mock library
here, because it hangs when we use 'with' statement,
and when we want to read file by chunks.
"""
def __enter__(self):
return self
def __exit__(self, *args):
pass
class BaseTestCase(TestCase):
"""Base class for test cases
"""
def method_was_not_called(self, method):
"""Checks that mocked method was not called
"""
self.assertEqual(method.call_count, 0)
def called_times(self, method, count):
"""Checks that mocked method was called `count` times
"""
self.assertEqual(method.call_count, count)
def mock_open(self, text, filename='some.yaml'):
"""Mocks builtin open function.
Usage example:
with mock.patch(
'__builtin__.open',
self.mock_open('file content')
):
# call some methods that are used open() to read some
# stuff internally
"""
fileobj = FakeFile(text)
setattr(fileobj, 'name', filename)
return mock.MagicMock(return_value=fileobj)

View File

@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from fuel_plugin_builder.actions import BuildPlugin
from fuel_plugin_builder import errors
from fuel_plugin_builder.tests.base import BaseTestCase
class TestBuild(BaseTestCase):
def setUp(self):
self.plugins_name = 'fuel_plugin'
self.plugin_path = '/tmp/{0}'.format(self.plugins_name)
with mock.patch(
'__builtin__.open',
self.mock_open("line 1\n line2\n line3")):
self.builder = BuildPlugin(self.plugin_path)
self.releases = [
{'os': 'ubuntu',
'deployment_scripts_path': 'deployment_scripts_path',
'repository_path': 'repository_path'}]
self.builder.meta = {
'name': 'awesome_plugin',
'version': '2.0.1',
'releases': self.releases}
@mock.patch('fuel_plugin_builder.actions.build.BuildPlugin.build_repos')
@mock.patch('fuel_plugin_builder.actions.build.BuildPlugin.make_package')
def test_run(self, builde_repo_mock, make_package_mock):
self.builder.run()
builde_repo_mock.assert_called_once_with()
make_package_mock.assert_called_once_with()
@mock.patch('fuel_plugin_builder.actions.build.utils')
def test_make_package(self, utils_mock):
utils_mock.exists.return_value = True
self.builder.make_package()
tar_path = '/tmp/fuel_plugin/awesome_plugin-2.0.1.fp'
utils_mock.exists.assert_called_once_with(tar_path)
utils_mock.remove.assert_called_once_with(tar_path)
utils_mock.make_tar_gz.assert_called_once_with(
self.builder.build_dir,
tar_path,
'awesome_plugin-2.0.1')
@mock.patch(
'fuel_plugin_builder.actions.build.BuildPlugin.build_ubuntu_repos')
@mock.patch(
'fuel_plugin_builder.actions.build.BuildPlugin.build_centos_repos')
@mock.patch('fuel_plugin_builder.actions.build.utils')
def test_build_repos(self,
utils_mock,
build_centos_mock,
build_ubuntu_mock):
utils_mock.which.return_value = True
self.builder.build_repos()
utils_mock.remove.assert_called_once_with(self.builder.build_dir)
utils_mock.create_dir.assert_called_once_with(self.builder.build_dir)
utils_mock.which.assert_called_once_with(
self.builder.pre_build_hook_path)
utils_mock.exec_cmd.assert_called_once_with(
self.builder.pre_build_hook_path)
utils_mock.copy_files_in_dir.assert_called_once_with(
'/tmp/fuel_plugin/*',
self.builder.build_dir)
build_centos_mock.assert_called_once_with([])
build_ubuntu_mock.assert_called_once_with([
'/tmp/fuel_plugin/.build/repository_path'])
@mock.patch('fuel_plugin_builder.actions.build.utils')
def test_build_ubuntu_repos(self, utils_mock):
path = '/repo/path'
self.builder.build_ubuntu_repos([path])
utils_mock.exec_cmd.assert_called_once_with(
'dpkg-scanpackages /repo/path | gzip -c9 > /repo/path/Packages.gz')
@mock.patch('fuel_plugin_builder.actions.build.utils')
def test_build_centos_repos(self, utils_mock):
path = '/repo/path'
self.builder.build_centos_repos([path])
utils_mock.create_dir.assert_called_once_with(
'/repo/path/Packages')
utils_mock.move_files_in_dir.assert_called_once_with(
'/repo/path/*.rpm', '/repo/path/Packages')
utils_mock.exec_cmd.assert_called_once_with(
'createrepo -o /repo/path /repo/path/Packages')
@mock.patch('fuel_plugin_builder.actions.build.utils.which',
return_value=True)
def test_check(self, _):
self.builder.check()
@mock.patch('fuel_plugin_builder.actions.build.utils.which',
return_value=False)
def test_check_raises_error(self, _):
self.assertRaisesRegexp(
errors.FuelCannotFindCommandError,
'Cannot find commands "rpm, createrepo, dpkg-scanpackages", '
'install required commands and try again',
self.builder.check)

View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from fuel_plugin_builder.cli import perform_action
from fuel_plugin_builder.tests.base import BaseTestCase
class TestCli(BaseTestCase):
@mock.patch('fuel_plugin_builder.cli.actions')
def test_perform_action_create(self, actions_mock):
args = mock.MagicMock()
args.create = ['plugin_path']
creatre_obj = mock.MagicMock()
actions_mock.CreatePlugin.return_value = creatre_obj
perform_action(args)
actions_mock.CreatePlugin.assert_called_once_with('plugin_path')
creatre_obj.check.assert_called_once_with()
creatre_obj.run.assert_called_once_with()
@mock.patch('fuel_plugin_builder.cli.actions')
def test_perform_action_build(self, actions_mock):
args = mock.MagicMock()
args.create = None
args.build = ['plugin_path']
build_obj = mock.MagicMock()
actions_mock.BuildPlugin.return_value = build_obj
perform_action(args)
actions_mock.BuildPlugin.assert_called_once_with('plugin_path')
build_obj.check.assert_called_once_with()
build_obj.run.assert_called_once_with()

View File

@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from fuel_plugin_builder.actions import CreatePlugin
from fuel_plugin_builder import errors
from fuel_plugin_builder.tests.base import BaseTestCase
class TestCreate(BaseTestCase):
def setUp(self):
self.plugins_name = 'fuel_plugin'
self.plugin_path = '/tmp/{0}'.format(self.plugins_name)
self.template_dir = '/temp_dir'
self.creator = CreatePlugin(self.plugin_path)
self.creator.template_dir = self.template_dir
self.creator.plugin_structure = [
{'action': 'render', 'path': 'file1', 'from': 'temp_file1'},
{'action': 'copy', 'path': 'file2', 'from': 'temp_file2'},
{'action': 'mkdir', 'path': 'tmp'}]
@mock.patch('fuel_plugin_builder.actions.create.utils.exists',
return_value=False)
def test_check(self, exists_mock):
self.creator.check()
exists_mock.assert_called_once_with(self.plugin_path)
@mock.patch('fuel_plugin_builder.actions.create.utils.exists',
return_value=True)
def test_check_raises_errors(self, exists_mock):
self.assertRaisesRegexp(
errors.PluginDirectoryExistsError,
'Plugins directory {0} already exists, '
'choose anothe name'.format(self.plugin_path),
self.creator.check)
exists_mock.assert_called_once_with(self.plugin_path)
@mock.patch('fuel_plugin_builder.actions.create.utils')
def test_run(self, utils_mock):
self.creator.run()
utils_mock.render_to_file.assert_called_once_with(
'/temp_dir/temp_file1',
'/tmp/fuel_plugin/file1',
self.creator.render_ctx)
utils_mock.copy_file_permissions.assert_called_once_with(
'/temp_dir/temp_file1',
'/tmp/fuel_plugin/file1')
self.assertEqual(
utils_mock.create_dir.call_args_list,
[mock.call('/tmp/fuel_plugin'),
mock.call('/tmp/fuel_plugin/tmp')])
utils_mock.copy.assert_called_once_with(
'/temp_dir/temp_file2',
'/tmp/fuel_plugin/file2')

View File

@ -0,0 +1,207 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import subprocess
import mock
from mock import patch
from fuel_plugin_builder import errors
from fuel_plugin_builder.tests.base import BaseTestCase
from fuel_plugin_builder import utils
class TestUtils(BaseTestCase):
@mock.patch('fuel_plugin_builder.utils.os.path.isfile', return_value=True)
@mock.patch('fuel_plugin_builder.utils.os.access', return_value=True)
def test_is_executable_returns_true(self, access_mock, isfile_mock):
file_name = 'file_name'
self.assertTrue(utils.is_executable(file_name))
isfile_mock.assert_called_once_with(file_name)
access_mock.assert_called_once_with(file_name, os.X_OK)
@mock.patch('fuel_plugin_builder.utils.os.path.isfile', return_value=True)
@mock.patch('fuel_plugin_builder.utils.os.access', return_value=False)
def test_is_executable_returns_false(self, access_mock, isfile_mock):
file_name = 'file_name'
self.assertFalse(utils.is_executable(file_name))
isfile_mock.assert_called_once_with(file_name)
access_mock.assert_called_once_with(file_name, os.X_OK)
@mock.patch('fuel_plugin_builder.utils.os')
@mock.patch('fuel_plugin_builder.utils.is_executable', return_value=True)
def test_which_returns_for_absolute_path_exec(self, _, os_mock):
path = '/usr/bin/some_exec'
os_mock.path.split.return_value = ('/usr/bin/', 'some_exec')
self.assertEqual(utils.which(path), path)
@mock.patch('fuel_plugin_builder.utils.is_executable',
side_effect=[False, True])
def test_which_returns_if_exec_in_env_path(self, _):
# some_exec is in /bin directory
path = 'some_exec'
with patch.dict('os.environ', {'PATH': '/usr/bin:/bin'}):
self.assertEqual(utils.which(path), '/bin/some_exec')
@mock.patch('fuel_plugin_builder.utils.is_executable', return_value=False)
def test_which_returns_none(self, _):
with patch.dict('os.environ', {'PATH': '/usr/bin:/bin'}):
self.assertIsNone(utils.which('some_exec'))
def make_process_mock(self, return_code=0):
process_mock = mock.Mock()
process_mock.stdout = ['Stdout line 1', 'Stdout line 2']
process_mock.returncode = return_code
return process_mock
def test_exec_cmd_raises_error_in_case_of_non_zero_exit_code(self):
cmd = 'some command'
return_code = 1
process_mock = self.make_process_mock(return_code=return_code)
with patch.object(subprocess, 'Popen', return_value=process_mock):
self.assertRaisesRegexp(
errors.ExecutedErrorNonZeroExitCode,
'Shell command executed with "{0}" '
'exit code: {1} '.format(return_code, cmd),
utils.exec_cmd, cmd)
def test_exec_cmd(self):
process_mock = self.make_process_mock(return_code=0)
with patch.object(subprocess, 'Popen', return_value=process_mock):
utils.exec_cmd('some command')
process_mock.wait.assert_called_once_with()
@mock.patch('fuel_plugin_builder.utils.os')
def test_create_dir(self, os_mock):
path = '/dir/path'
os_mock.path.isdir.return_value = False
utils.create_dir(path)
os_mock.path.isdir.assert_called_once_with(path)
os_mock.makedirs.assert_called_once_with(path)
@mock.patch('fuel_plugin_builder.utils.os')
def test_create_dir_dont_create_if_created(self, os_mock):
path = '/dir/path'
os_mock.path.isdir.return_value = True
utils.create_dir(path)
os_mock.path.isdir.assert_called_once_with(path)
self.method_was_not_called(os_mock.makedirs)
@mock.patch('fuel_plugin_builder.utils.os.path.lexists', return_value=True)
def test_exists(self, os_exists):
file_path = '/dir/path'
self.assertTrue(utils.exists(file_path))
os_exists.assert_called_once_with(file_path)
@mock.patch('fuel_plugin_builder.utils.os.path.lexists',
return_value=False)
def test_exists_returns_false(self, os_exists):
file_path = '/dir/path'
self.assertFalse(utils.exists(file_path))
os_exists.assert_called_once_with(file_path)
@mock.patch('fuel_plugin_builder.utils.os.path.basename')
def test_basename(self, base_mock):
path = 'some_path'
base_mock.return_value = path
self.assertEqual(utils.basename(path), path)
base_mock.assert_called_once_with(path)
@mock.patch('fuel_plugin_builder.utils.shutil')
def test_copy_file_permissions(self, shutil_mock):
utils.copy_file_permissions('src', 'dst')
shutil_mock.copymode.assert_called_once_with('src', 'dst')
@mock.patch('fuel_plugin_builder.utils.shutil')
@mock.patch('fuel_plugin_builder.utils.os')
def test_remove_file(self, os_mock, shutil_mock):
path = 'file_for_removing'
os_mock.path.isdir.return_value = False
utils.remove(path)
os_mock.remove.assert_called_once_with(path)
self.method_was_not_called(shutil_mock.rmtree)
@mock.patch('fuel_plugin_builder.utils.shutil')
@mock.patch('fuel_plugin_builder.utils.os')
def test_remove_dir(self, os_mock, shutil_mock):
path = 'dir_for_removing'
os_mock.path.isdir.return_value = True
os_mock.path.islink.return_value = False
utils.remove(path)
shutil_mock.rmtree.assert_called_once_with(path)
self.method_was_not_called(os_mock.remove)
@mock.patch('fuel_plugin_builder.utils.shutil')
@mock.patch('fuel_plugin_builder.utils.os')
def test_copy_file(self, os_mock, shutil_mock):
src = '/tmp/soruce_file'
dst = '/tmp/destination_file'
os_mock.path.isdir.return_value = False
utils.copy(src, dst)
shutil_mock.copy.assert_called_once_with(src, dst)
self.method_was_not_called(shutil_mock.copytree)
@mock.patch('fuel_plugin_builder.utils.shutil')
@mock.patch('fuel_plugin_builder.utils.os')
def test_copy_dir(self, os_mock, shutil_mock):
src = '/tmp/soruce_file'
dst = '/tmp/destination_file'
os_mock.path.isdir.return_value = True
utils.copy(src, dst)
shutil_mock.copytree.assert_called_once_with(src, dst, symlinks=True)
self.method_was_not_called(shutil_mock.copy)
@mock.patch('fuel_plugin_builder.utils.copy')
@mock.patch('fuel_plugin_builder.utils.glob',
return_value=['file1', 'file2'])
def test_copy_files_in_dir(self, glob_mock, copy_mock):
mask = 'file*'
dst_dir = '/tmp'
utils.copy_files_in_dir(mask, dst_dir)
glob_mock.assert_called_once_with(mask)
self.assertEqual(
copy_mock.call_args_list,
[mock.call('file1', '/tmp/file1'),
mock.call('file2', '/tmp/file2')])
@mock.patch('fuel_plugin_builder.utils.tarfile')
def test_make_tar_gz(self, tarfile_mock):
src = 'dir'
dst = '/tmp/file.fp'
prefix = 'prefix_dir'
tar_mock = mock.MagicMock()
tarfile_mock.open.return_value = tar_mock
utils.make_tar_gz(src, dst, prefix)
tarfile_mock.open.assert_called_once_with(dst, 'w:gz')
tar_mock.add.assert_called_once_with(src, arcname=prefix)
tar_mock.close.assert_called_once_with()
@mock.patch('fuel_plugin_builder.utils.shutil.move')
@mock.patch('fuel_plugin_builder.utils.glob',
return_value=['file1', 'file2'])
def test_move_files_in_dir(self, glob_mock, move_mock):
mask = 'file*'
dst_dir = '/tmp'
utils.move_files_in_dir(mask, dst_dir)
glob_mock.assert_called_once_with(mask)
self.assertEqual(
move_mock.call_args_list,
[mock.call('file1', '/tmp/file1'),
mock.call('file2', '/tmp/file2')])

View File

@ -14,11 +14,15 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import logging
import os
import shutil
import subprocess
import tarfile
from glob import glob
from mako.template import Template
from fuel_plugin_builder import errors
@ -50,7 +54,6 @@ def which(cmd):
return cmd
for path in os.environ['PATH'].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, cmd)
if is_executable(exe_file):
return exe_file
@ -75,7 +78,6 @@ def exec_cmd(cmd):
for line in child.stdout:
logger.debug(line.rstrip())
child.wait()
exit_code = child.returncode
@ -85,3 +87,125 @@ def exec_cmd(cmd):
'exit code: {1} '.format(exit_code, cmd))
logger.debug(u'Command "{0}" successfully executed'.format(cmd))
def create_dir(dir_path):
"""Creates directory
:param dir_path: directory path
:raises: errors.DirectoryExistsError
"""
logger.debug(u'Creating directory %s', dir_path)
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
def exists(path):
"""Checks if filel is exist
:param str path: path to the file
:returns: True if file is exist, Flase if is not
"""
return os.path.lexists(path)
def basename(path):
"""Basename for path
:param str path: path to the file
:returns: str with filename
"""
return os.path.basename(path)
def render_to_file(src, dst, params):
"""Render mako template and write it to specified file
:param src: path to template
:param dst: path where rendered template will be saved
"""
logger.debug('Render template from {0} to {1} with params: {2}'.format(
src, dst, params))
with open(src, 'r') as f:
template_file = f.read()
with open(dst, 'w') as f:
rendered_file = Template(template_file).render(**params)
f.write(rendered_file)
def copy_file_permissions(src, dst):
"""Copies file permissions
:param str src: source file
:param str dst: destination
"""
shutil.copymode(src, dst)
def remove(path):
"""Remove file or directory
:param path: a file or directory to remove
"""
logger.debug(u'Removing "%s"', path)
if not os.path.lexists(path):
return
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
else:
os.remove(path)
def copy(src, dst):
"""Copy a given file or directory from one place to another.
:param src: copy from
:param dst: copy to
"""
logger.debug(u'Copy from %s to %s', src, dst)
if os.path.isdir(src):
shutil.copytree(src, dst, symlinks=True)
else:
shutil.copy(src, dst)
def copy_files_in_dir(src, dst):
"""Copies file in directory
:param str src: source files
:param str dst: destination directory
"""
logger.debug(u'Copy files in directory %s %s', src, dst)
for f in glob(src):
dst_path = os.path.join(dst, os.path.basename(f))
copy(f, dst_path)
def move_files_in_dir(src, dst):
"""Move files or directories
:param str src: source files or directories
:param str dst: destination directory
"""
logger.debug(u'Move files to directory %s %s', src, dst)
for f in glob(src):
dst_path = os.path.join(dst, os.path.basename(f))
shutil.move(f, dst_path)
def make_tar_gz(dir_path, tar_path, files_prefix):
"""Compress the file in tar.gz archive
:param str dir_path: directory for archiving
:param str tar_path: the name and path to the file
:param str files_prefix: the directory in the tar files where all
of the files are allocated
"""
logger.debug(u'Archive directory %s to file %s', dir_path, tar_path)
tar = tarfile.open(tar_path, 'w:gz')
tar.add(dir_path, arcname=files_prefix)
tar.close()

View File

@ -1,2 +1,4 @@
argparse==1.2.1
six==1.5.2
Mako==0.9.1
PyYAML==3.10

View File

@ -13,7 +13,6 @@
# under the License.
import os
import re
from setuptools import find_packages
from setuptools import setup
@ -28,13 +27,14 @@ def find_requires():
setup(
name='fuel_plugin_builder',
name='fpb',
version='0.1.0',
description='Helps to create and build fuel plugins',
long_description="""Helps to create and build fuel plugins""",
classifiers=[
"Programming Language :: Python",
"Topic :: System :: Software Distribution"],
"Topic :: System :: Software Distribution",
"License :: OSI Approved :: Apache Software License"],
author='Mirantis Inc.',
author_email='product@mirantis.com',
url='http://mirantis.com',
@ -43,7 +43,7 @@ setup(
zip_safe=False,
install_requires=find_requires(),
include_package_data=True,
package_data={'': ['templates/*']},
package_data={'': ['templates/*', 'templates/.gitignore']},
entry_points={
'console_scripts': [
'fpb = fuel_plugin_builder.cli:main']})

View File

@ -9,7 +9,7 @@ install_command = pip install {packages}
setenv = VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/test-requirements.txt
commands =
nosetests {posargs:fuel_plugin}
nosetests {posargs:fuel_plugin_builder}
[tox:jenkins]
downloadcache = ~/cache/pip

122
run_tests.sh Executable file
View File

@ -0,0 +1,122 @@
#!/bin/bash
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
set -eu
function usage {
echo "Usage: $0 [OPTION]..."
echo "Run fuel plugin building"
echo ""
echo " -b, --build Builds all of the plugins"
echo " -B, --no-build Don't run plugins build"
echo " -f, --fpb Run tests for fpb"
echo " -F, --no-fpb Don't run tests for fpb"
echo " -h, --help Print this usage message"
echo ""
echo "Note: with no options specified, the script will try to run "
echo " all available tests with all available checks."
exit
}
function process_options {
for arg in $@; do
case "$arg" in
-h|--help) usage;;
-b|--build) plugins_build=1;;
-B|--no-build) no_plugins_build=1;;
-f|--fpb) fpb_tests=1;;
-F|--no-fpb) no_fpb_tests=1;;
-*) testropts="$testropts $arg";;
*) testrargs="$testrargs $arg"
esac
done
}
# Settings
ROOT=$(dirname `readlink -f $0`)
# Initialize global variables
plugins_build=0
no_plugins_build=0
fpb_tests=0
no_fpb_tests=0
function run_tests {
run_cleanup
# This variable collects all failed tests. It'll be printed in
# the end of this function as a small statistic for user.
local errors=""
# Enable all tests if none was specified skipping all explicitly
# disabled tests.
if [[ $plugins_build -eq 0 && \
$fpb_tests -eq 0 ]]; then
if [ $no_plugins_build -ne 1 ]; then plugins_build=1; fi
if [ $no_fpb_tests -ne 1 ]; then fpb_tests=1; fi
fi
# Run all enabled tests
if [ $plugins_build -eq 1 ]; then
echo "Starting plugins build..."
run_build || errors+=" plugins_build"
fi
if [ $fpb_tests -eq 1 ]; then
echo "Starting fpb testing..."
run_fpb || errors+=" fpb_tests"
fi
if [ -n "$errors" ]; then
echo Failed tests: $errors
exit 1
fi
exit
}
function run_cleanup {
find . -type f -name "*.pyc" -delete
}
function run_fpb {
local result=0
pushd $ROOT/fuel_plugin_builder >> /dev/null
tox || result=1
popd >> /dev/null
return $result
}
# Build all of the plugins with current version of plugins builder
function run_build {
# TODO(eli): add plugins building process plugin
# when there are any plugins
return 0
}
# Parse command line arguments and run the tests
process_options $@
run_tests