rally/tests/cmd/test_envutils.py
Julien Vey b74ea2e4f8 Add rally use command
This adds a new group of commands : 'rally use'
the first implemented is 'rally use deployment <deploy_id>'

As it is not possible in python to export globaly an env var,it saves the
variable in a file at ~/.rally/deployment like :
RALLY_DEPLOYMENT=1234abcd

For each command that takes a deploy_id as parameter, this
parameter is now optional.

The algorithm to find the deploy_id is as follows :
- If the --deploy-id param is set, use it
- If the environ contains the key 'RALLY_DEPLOYMENT', use it
- load the content of ~/.rally/deployment and if it contains the key
RALLY_DEPLOYMENT, use it
- Else, raise an InvalidArgumentException

Note that I have not been able to use the default arg in @cliutils.args
because it is called before the effective method call. Setting
deploy_id=envutils.default_deployment_id() has the same problem

Partially implements blueprint: rally-use-command

Change-Id: I1e62e26d259bd9e81f1c008058ee9ce91cc3290c
2014-01-23 22:14:01 +01:00

39 lines
1.4 KiB
Python

# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
# 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
import os
from rally.cmd import envutils
from rally import exceptions
from rally.openstack.common import test
class EnvUtilsTestCase(test.BaseTestCase):
@mock.patch.dict(os.environ, values={'RALLY_DEPLOYMENT': 'my_deploy_id'},
clear=True)
def test_get_deployment_id_in_env(self):
deploy_id = envutils.default_deployment_id()
self.assertEqual('my_deploy_id', deploy_id)
@mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch('rally.cmd.envutils.fileutils.load_env_file')
def test_get_deployment_id_with_exception(self, mock_file):
self.assertRaises(exceptions.InvalidArgumentsException,
envutils.default_deployment_id)
mock_file.assert_called_once_with(os.path.expanduser(
'~/.rally/deployment'))