Prevent overwrite of input env file
When deploying an environment with --id, it is possible for the input env file to be overwritten. For example, consider the deploy command `bin/deploy.py --quintupleo --id foo -e env-foo.yaml`. Because the output id env file would be named the same as the input env file the input env file would be overwritten and lost. This change prevents such a situation by checking for an input env file named 'env-ID.yaml' and raising an error if one is found.
This commit is contained in:
parent
783843678c
commit
a0fa1b36b3
|
@ -64,8 +64,14 @@ def _parse_args():
|
|||
return parser.parse_args()
|
||||
|
||||
def _process_args(args):
|
||||
if args.id and not args.quintupleo:
|
||||
raise RuntimeError('--id requires --quintupleo')
|
||||
if args.id:
|
||||
if not args.quintupleo:
|
||||
raise RuntimeError('--id requires --quintupleo')
|
||||
id_env = 'env-%s.yaml' % args.id
|
||||
if id_env in args.env:
|
||||
raise ValueError('Input env file "%s" would be overwritten by ID '
|
||||
'env file. Either rename the input file or '
|
||||
'change the deploy ID.' % id_env)
|
||||
if args.role and not args.quintupleo:
|
||||
raise RuntimeError('--role requires --quintupleo')
|
||||
|
||||
|
|
|
@ -26,36 +26,38 @@ from openstack_virtual_baremetal import auth
|
|||
from openstack_virtual_baremetal import deploy
|
||||
|
||||
class TestProcessArgs(unittest.TestCase):
|
||||
def test_basic(self):
|
||||
def _basic_mock_args(self):
|
||||
"""Return a mock with basic args set"""
|
||||
mock_args = mock.Mock()
|
||||
mock_args.name = None
|
||||
mock_args.quintupleo = False
|
||||
mock_args.id = None
|
||||
mock_args.env = []
|
||||
mock_args.role = []
|
||||
return mock_args
|
||||
|
||||
def test_basic(self):
|
||||
mock_args = self._basic_mock_args()
|
||||
name, template = deploy._process_args(mock_args)
|
||||
self.assertEqual('baremetal', name)
|
||||
self.assertEqual('templates/virtual-baremetal.yaml', template)
|
||||
|
||||
def test_name(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.name = 'foo'
|
||||
mock_args.quintupleo = False
|
||||
mock_args.id = None
|
||||
mock_args.role = []
|
||||
name, template = deploy._process_args(mock_args)
|
||||
self.assertEqual('foo', name)
|
||||
self.assertEqual('templates/virtual-baremetal.yaml', template)
|
||||
|
||||
def test_quintupleo(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args.name = None
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.quintupleo = True
|
||||
name, template = deploy._process_args(mock_args)
|
||||
self.assertEqual('quintupleo', name)
|
||||
self.assertEqual('templates/quintupleo.yaml', template)
|
||||
|
||||
def test_quintupleo_name(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.name = 'foo'
|
||||
mock_args.quintupleo = True
|
||||
name, template = deploy._process_args(mock_args)
|
||||
|
@ -63,28 +65,31 @@ class TestProcessArgs(unittest.TestCase):
|
|||
self.assertEqual('templates/quintupleo.yaml', template)
|
||||
|
||||
def test_id_quintupleo(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.id = 'foo'
|
||||
mock_args.quintupleo = False
|
||||
self.assertRaises(RuntimeError, deploy._process_args, mock_args)
|
||||
|
||||
def test_role_quintupleo(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.role = 'foo.yaml'
|
||||
mock_args.id = None
|
||||
mock_args.quintupleo = False
|
||||
self.assertRaises(RuntimeError, deploy._process_args, mock_args)
|
||||
|
||||
def test_maintain_old_default(self):
|
||||
mock_args = mock.Mock()
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.name = 'foo'
|
||||
mock_args.quintupleo = True
|
||||
mock_args.env = []
|
||||
name, template = deploy._process_args(mock_args)
|
||||
self.assertEqual('foo', name)
|
||||
self.assertEqual('templates/quintupleo.yaml', template)
|
||||
self.assertEqual(['env.yaml'], mock_args.env)
|
||||
|
||||
def test_no_overwrite(self):
|
||||
mock_args = self._basic_mock_args()
|
||||
mock_args.quintupleo = True
|
||||
mock_args.id = 'foo'
|
||||
mock_args.env = ['env-foo.yaml']
|
||||
self.assertRaises(ValueError, deploy._process_args, mock_args)
|
||||
|
||||
test_env = u"""parameters:
|
||||
provision_net: provision
|
||||
public_net: public
|
||||
|
|
Loading…
Reference in New Issue