Skip to content

Commit

Permalink
Randomize administrative password for new server creation.
Browse files Browse the repository at this point in the history
Fixes #164
  • Loading branch information
Samuel A. Falvo II committed Feb 20, 2015
1 parent 8921442 commit b6ba180
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
10 changes: 6 additions & 4 deletions mimic/model/nova_objects.py
Expand Up @@ -6,8 +6,11 @@
from random import randrange
from json import loads, dumps

from mimic.util.helper import seconds_to_timestamp
from mimic.util.helper import invalid_resource
from mimic.util.helper import (
seconds_to_timestamp,
invalid_resource,
random_string,
)

from twisted.web.http import ACCEPTED, NOT_FOUND

Expand Down Expand Up @@ -161,8 +164,7 @@ def from_creation_request_json(cls, collection, creation_json,
disk_config="AUTO",
# ^ TODO: https://github.com/rackerlabs/mimic/issues/163
status="ACTIVE",
admin_password="testpassword",
# ^ TODO: https://github.com/rackerlabs/mimic/issues/164
admin_password=random_string(12),
)
collection.servers.append(self)
return self
Expand Down
6 changes: 6 additions & 0 deletions mimic/test/test_nova.py
Expand Up @@ -72,6 +72,10 @@ def test_create_server(self):
"""
self.assertEqual(self.create_server_response.code, 202)
self.assertTrue(type(self.server_id), unicode)
self.assertNotEqual(
self.create_server_response_body['server']['adminPass'],
"testpassword"
)
validate_link_json(self, self.create_server_response_body['server'])

def test_list_servers(self):
Expand Down Expand Up @@ -131,6 +135,8 @@ def test_get_server(self):
self.server_id)
self.assertEqual(
get_server_response_body['server']['status'], 'ACTIVE')
admin_password = get_server_response_body['server'].get('adminPass', None)
self.assertEqual(admin_password, None)
self.validate_server_detail_json(get_server_response_body['server'])

def test_get_server_negative(self):
Expand Down
21 changes: 20 additions & 1 deletion mimic/util/helper.py
Expand Up @@ -7,14 +7,33 @@
"""
import os
from datetime import datetime, timedelta
from random import randint
from random import choice, randint

from six import text_type


fmt = '%Y-%m-%dT%H:%M:%S.%fZ'


def random_string(length, selectable=None):
"""
Create a random string of the specified length.
:param int length: How long the string must be.
:param str selectable: If left unspecified, the random character selection
will be taken from uppercase and lowercase letters, digits, and a few
punctuation marks. Otherwise, the characters will be taken from the
string provided.
:returns: A string of length `length`.
"""
selectable = selectable or (
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890!@#$%^&*():;.,<>"
)
return ''.join([choice(selectable) for _ in xrange(length)])


def random_ipv4(*numbers):
"""
Return a random IPv4 address - parts of the IP address can be provided.
Expand Down

0 comments on commit b6ba180

Please sign in to comment.