Merge pull request #26709 from rallytime/fix-14612

Ensure VM name is valid before trying to create Linode VM
This commit is contained in:
Mike Place 2015-08-27 14:07:49 -06:00
commit cf487cf0f5
2 changed files with 140 additions and 1 deletions

View File

@ -41,13 +41,13 @@ minion gets new keys and the keys get pre-seeded on the master, and the
Cloning requires a post 2015-02-01 salt-bootstrap.
'''
from __future__ import absolute_import
# pylint: disable=E0102
from __future__ import absolute_import
# Import python libs
import pprint
import logging
import re
import time
from os.path import exists, expanduser
@ -794,6 +794,8 @@ def create(vm_):
'''
Create a single VM from a data dict
'''
if _validate_name(vm_['name']) is False:
return False
salt.utils.cloud.fire_event(
'event',
@ -1020,3 +1022,32 @@ def create(vm_):
)
return ret
def _validate_name(name):
'''
Checks if the provided name fits Linode's labeling parameters.
.. versionadded:: 2015.5.6
name
The VM name to validate
'''
name_length = len(name)
regex = re.compile(r'^[a-zA-Z0-9][A-Za-z0-9_-]*[a-zA-Z0-9]$')
if name_length < 3 or name_length > 48:
ret = False
elif not re.match(regex, name):
ret = False
else:
ret = True
if ret is False:
log.warning(
'A Linode label may only contain ASCII letters or numbers, dashes, and '
'underscores, must begin and end with letters or numbers, and be at least '
'three characters in length.'
)
return ret

View File

@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import Salt Libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import NO_MOCK, NO_MOCK_REASON
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../../')
# Import Salt Libs
from salt.cloud.clouds import linode
@skipIf(NO_MOCK, NO_MOCK_REASON)
class LinodeTestCase(TestCase):
'''
Unit TestCase for the salt.cloud.clouds.linode module.
'''
# _validate_name tests
def test_validate_name_first_character_invalid(self):
'''
Tests when name starts with an invalid character.
'''
# Test when name begins with a hyphen
self.assertFalse(linode._validate_name('-foo'))
# Test when name begins with an underscore
self.assertFalse(linode._validate_name('_foo'))
def test_validate_name_last_character_invalid(self):
'''
Tests when name ends with an invalid character.
'''
# Test when name ends with a hyphen
self.assertFalse(linode._validate_name('foo-'))
# Test when name ends with an underscore
self.assertFalse(linode._validate_name('foo_'))
def test_validate_name_too_short(self):
'''
Tests when name has less than three letters.
'''
# Test when name is an empty string
self.assertFalse(linode._validate_name(''))
# Test when name is two letters long
self.assertFalse(linode._validate_name('ab'))
# Test when name is three letters long (valid)
self.assertTrue(linode._validate_name('abc'))
def test_validate_name_too_long(self):
'''
Tests when name has more than 48 letters.
'''
long_name = '1111-2222-3333-4444-5555-6666-7777-8888-9999-111'
# Test when name is 48 letters long (valid)
self.assertEqual(len(long_name), 48)
self.assertTrue(linode._validate_name(long_name))
# Test when name is more than 48 letters long
long_name += '1'
self.assertEqual(len(long_name), 49)
self.assertFalse(linode._validate_name(long_name))
def test_validate_name_invalid_characters(self):
'''
Tests when name contains invalid characters.
'''
# Test when name contains an invalid character
self.assertFalse(linode._validate_name('foo;bar'))
# Test when name contains non-ascii letters
self.assertFalse(linode._validate_name('fooàààààbar'))
# Test when name contains spaces
self.assertFalse(linode._validate_name('foo bar'))
def test_validate_name_valid_characters(self):
'''
Tests when name contains valid characters.
'''
# Test when name contains letters and numbers
self.assertTrue(linode._validate_name('foo123bar'))
# Test when name contains hyphens
self.assertTrue(linode._validate_name('foo-bar'))
# Test when name contains underscores
self.assertTrue(linode._validate_name('foo_bar'))
# Test when name start and end with numbers
self.assertTrue(linode._validate_name('1foo'))
self.assertTrue(linode._validate_name('foo0'))
if __name__ == '__main__':
from integration import run_tests
run_tests(LinodeTestCase, needs_daemon=False)