rally/tests/test_fileutils.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

48 lines
2.0 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 import fileutils
from tests import test
class FileUtilsTestCase(test.TestCase):
@mock.patch('os.path.exists')
@mock.patch.dict('os.environ', values={}, clear=True)
def test_load_env_vile(self, mock_path):
file_data = ["FAKE_ENV=fake_env\n"]
mock_path.return_value = True
with mock.patch('rally.fileutils.open', mock.mock_open(
read_data=file_data), create=True) as mock_file:
mock_file.return_value.readlines.return_value = file_data
fileutils.load_env_file('path_to_file')
self.assertIn('FAKE_ENV', os.environ)
mock_file.return_value.readlines.assert_called_once_with()
@mock.patch('os.path.exists')
def test_update_env_file(self, mock_path):
file_data = ["FAKE_ENV=old_value\n", "FAKE_ENV2=any\n"]
mock_path.return_value = True
with mock.patch('rally.fileutils.open', mock.mock_open(
read_data=file_data), create=True) as mock_file:
mock_file.return_value.readlines.return_value = file_data
fileutils.update_env_file('path_to_file', 'FAKE_ENV', 'new_value')
calls = [mock.call('FAKE_ENV2=any\n'), mock.call(
'FAKE_ENV=new_value')]
mock_file.return_value.readlines.assert_called_once_with()
mock_file.return_value.write.assert_has_calls(calls)