Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
phobologic committed May 3, 2015
1 parent f91530f commit cf502b1
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
65 changes: 65 additions & 0 deletions stacker/tests/test_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest

from stacker.plan import BlueprintContext, Plan


def generate_definition(base_name, _id):
return {
"name": "%s.%d" % (base_name, _id),
"class_path": "stacker.blueprints.%s.%s" % (base_name,
base_name.upper()),
"parameters": {
"ExternalParameter": "fakeStack2::FakeParameter",
"InstanceType": "m3.medium",
"AZCount": 2,
},
"requires": ["fakeStack"]
}


class TestBlueprintContext(unittest.TestCase):
def setUp(self):
self.context = BlueprintContext(**generate_definition('vpc', 1))

def test_status(self):
self.assertFalse(self.context.submitted)
self.assertFalse(self.context.completed)
self.context.submit()
self.assertTrue(self.context.submitted)
self.assertFalse(self.context.completed)
self.context.complete()
self.assertTrue(self.context.submitted)
self.assertTrue(self.context.completed)

def test_requires(self):
self.assertIn('fakeStack', self.context.requires)
self.assertIn('fakeStack2', self.context.requires)


class TestPlan(unittest.TestCase):
def setUp(self):
self.plan = Plan()
for i in range(4):
self.plan.add(generate_definition('vpc', i))

def test_add(self):
first_id = 'vpc.1'
self.assertIn(first_id, self.plan)
self.assertIsInstance(self.plan[first_id], BlueprintContext)

def test_status(self):
self.assertEqual(len(self.plan.list_submitted()), 0)
self.assertEqual(len(self.plan.list_completed()), 0)
self.assertEqual(len(self.plan.list_pending()), 4)
self.plan.submit('vpc.1')
self.assertEqual(len(self.plan.list_submitted()), 1)
self.assertEqual(len(self.plan.list_completed()), 0)
self.assertEqual(len(self.plan.list_pending()), 4)
self.plan.complete('vpc.1')
self.assertEqual(len(self.plan.list_submitted()), 0)
self.assertEqual(len(self.plan.list_completed()), 1)
self.assertEqual(len(self.plan.list_pending()), 3)
self.assertFalse(self.plan.completed)
for i in range(4):
self.plan.complete("vpc.%d" % i)
self.assertTrue(self.plan.completed)
49 changes: 49 additions & 0 deletions stacker/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest

import string
import os

from stacker.util import (
get_bucket_location, cf_safe_name, load_object_from_string,
camel_to_snake)

regions = ['us-east-1', 'cn-north-1', 'ap-northeast-1', 'eu-west-1',
'ap-southeast-1', 'ap-southeast-2', 'us-west-2', 'us-gov-west-1',
'us-west-1', 'eu-central-1', 'sa-east-1']


class TestUtil(unittest.TestCase):
def test_get_bucket_location(self):
for r in regions:
expected = r
if r == "us-east-1":
expected = ""
self.assertEqual(get_bucket_location(r), expected)

def test_cf_safe_name(self):
tests = (
('abc-def', 'AbcDef'),
('GhI', 'GhI'),
('jKlm.noP', 'JKlmNoP')
)
for t in tests:
self.assertEqual(cf_safe_name(t[0]), t[1])

def test_load_object_from_string(self):
tests = (
('string.Template', string.Template),
('os.path.basename', os.path.basename),
('string.letters', string.letters)
)
for t in tests:
self.assertIs(load_object_from_string(t[0]), t[1])

def test_camel_to_snake(self):
tests = (
('TestTemplate', 'test_template'),
('testTemplate', 'test_template'),
('test_Template', 'test__template'),
('testtemplate', 'testtemplate'),
)
for t in tests:
self.assertEqual(camel_to_snake(t[0]), t[1])

0 comments on commit cf502b1

Please sign in to comment.