Exemple #1
0
    def test_execute_plan_no_persist(self):
        """Test execute plan with no persistent graph."""
        context = Context(config=self.config)
        context.put_persistent_graph = mock.MagicMock()
        vpc = Stack(definition=generate_definition("vpc", 1), context=context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=context,
        )

        calls = []

        def _launch_stack(stack, status=None):
            calls.append(stack.fqn)
            return COMPLETE

        graph = Graph.from_steps(
            [Step(vpc, _launch_stack),
             Step(bastion, _launch_stack)])
        plan = Plan(description="Test", graph=graph, context=context)

        plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1", "namespace-bastion.1"])
        context.put_persistent_graph.assert_not_called()
Exemple #2
0
    def test_execute_plan_locked(self):
        """Test execute plan locked.

        Locked stacks still need to have their requires evaluated when
        they're being created.

        """
        vpc = Stack(definition=generate_definition("vpc", 1),
                    context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            locked=True,
            context=self.context,
        )

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            return COMPLETE

        graph = Graph.from_steps([Step(vpc, fn), Step(bastion, fn)])
        plan = Plan(description="Test", graph=graph)
        plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1", "namespace-bastion.1"])
Exemple #3
0
    def test_execute_plan_exception(self):
        """Test execute plan exception."""
        vpc = Stack(definition=generate_definition("vpc", 1),
                    context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            if stack.name == vpc_step.name:
                raise ValueError("Boom")
            return COMPLETE

        vpc_step = Step(vpc, fn)
        bastion_step = Step(bastion, fn)

        graph = Graph.from_steps([vpc_step, bastion_step])
        plan = Plan(description="Test", graph=graph)

        with self.assertRaises(PlanFailed):
            plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1"])
        self.assertEqual(vpc_step.status, FAILED)
Exemple #4
0
    def test_execute_plan_filtered(self):
        """Test execute plan filtered."""
        vpc = Stack(definition=generate_definition("vpc", 1),
                    context=self.context)
        db = Stack(
            definition=generate_definition("db", 1, requires=[vpc.name]),
            context=self.context,
        )
        app = Stack(
            definition=generate_definition("app", 1, requires=[db.name]),
            context=self.context,
        )

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            return COMPLETE

        context = mock.MagicMock()
        context.persistent_graph_locked = False
        context.stack_names = ["db.1"]
        graph = Graph.from_steps([Step(vpc, fn), Step(db, fn), Step(app, fn)])
        plan = Plan(context=context, description="Test", graph=graph)
        plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1", "namespace-db.1"])
Exemple #5
0
    def test_execute_plan_failed(self):
        """Test execute plan failed."""
        vpc = Stack(definition=generate_definition("vpc", 1),
                    context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )
        db = Stack(definition=generate_definition("db", 1),
                   context=self.context)

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            if stack.name == vpc_step.name:
                return FAILED
            return COMPLETE

        vpc_step = Step(vpc, fn)
        bastion_step = Step(bastion, fn)
        db_step = Step(db, fn)

        graph = Graph.from_steps([vpc_step, bastion_step, db_step])
        plan = Plan(description="Test", graph=graph)
        with self.assertRaises(PlanFailed):
            plan.execute(walk)

        calls.sort()

        self.assertEqual(calls, ["namespace-db.1", "namespace-vpc.1"])
Exemple #6
0
    def test_execute_plan_cancelled(self):
        """Test execute plan cancelled."""
        vpc = Stack(definition=generate_definition("vpc", 1),
                    context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            if stack.fqn == vpc_step.name:
                raise CancelExecution
            return COMPLETE

        vpc_step = Step(vpc, fn)
        bastion_step = Step(bastion, fn)

        graph = Graph.from_steps([vpc_step, bastion_step])
        plan = Plan(description="Test", graph=graph)
        plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1", "namespace-bastion.1"])
Exemple #7
0
    def test_execute_plan_skipped(self):
        """Test execute plan skipped."""
        vpc = Stack(definition=generate_definition('vpc', 1),
                    context=self.context)
        bastion = Stack(definition=generate_definition('bastion',
                                                       1,
                                                       requires=[vpc.name]),
                        context=self.context)

        calls = []

        def fn(stack, status=None):
            calls.append(stack.fqn)
            if stack.fqn == vpc_step.name:
                return SKIPPED
            return COMPLETE

        vpc_step = Step(vpc, fn)
        bastion_step = Step(bastion, fn)

        graph = Graph.from_steps([vpc_step, bastion_step])
        plan = Plan(description="Test", graph=graph)
        plan.execute(walk)

        self.assertEqual(calls, ['namespace-vpc.1', 'namespace-bastion.1'])
Exemple #8
0
    def test_plan_targeted(self):
        """Test plan targeted."""
        context = Context(config=self.config)
        vpc = Stack(definition=generate_definition("vpc", 1), context=context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=context,
        )
        context.stack_names = [vpc.name]
        graph = Graph.from_steps([Step(vpc, fn=None), Step(bastion, fn=None)])
        plan = Plan(description="Test", graph=graph, context=context)

        self.assertEqual({vpc.name: set()}, plan.graph.to_dict())
Exemple #9
0
class TestStep(unittest.TestCase):
    """Tests for runway.cfngin.plan.Step."""

    def setUp(self) -> None:
        """Run before tests."""
        stack = mock.MagicMock()
        stack.name = "stack"
        stack.fqn = "namespace-stack"
        self.step = Step(stack=stack, fn=None)

    def test_status(self) -> None:
        """Test status."""
        self.assertFalse(self.step.submitted)
        self.assertFalse(self.step.completed)

        self.step.submit()
        self.assertEqual(self.step.status, SUBMITTED)
        self.assertTrue(self.step.submitted)
        self.assertFalse(self.step.completed)

        self.step.complete()
        self.assertEqual(self.step.status, COMPLETE)
        self.assertNotEqual(self.step.status, SUBMITTED)
        self.assertTrue(self.step.submitted)
        self.assertTrue(self.step.completed)

        self.assertNotEqual(self.step.status, True)
        self.assertNotEqual(self.step.status, False)
        self.assertNotEqual(self.step.status, "banana")

    def test_from_stack_name(self) -> None:
        """Return step from step name."""
        context = mock_context()
        stack_name = "test-stack"
        result = Step.from_stack_name(stack_name, context)

        self.assertIsInstance(result, Step)
        self.assertEqual(stack_name, result.stack.name)

    def test_from_persistent_graph(self) -> None:
        """Return list of steps from graph dict."""
        context = mock_context()
        graph_dict: Dict[str, Any] = {"stack1": [], "stack2": ["stack1"]}
        result = Step.from_persistent_graph(graph_dict, context)

        self.assertEqual(2, len(result))
        self.assertIsInstance(result, list)

        for step in result:
            self.assertIsInstance(step, Step)
            self.assertIn(step.stack.name, graph_dict.keys())
Exemple #10
0
    def test_plan_reverse(self) -> None:
        """Test plan reverse."""
        vpc = Stack(definition=generate_definition("vpc", 1), context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )
        graph = Graph.from_steps([Step(vpc, fn=None), Step(bastion, fn=None)])
        plan = Plan(description="Test", graph=graph, reverse=True)

        # order is different between python2/3 so can't compare dicts
        result_graph_dict = plan.graph.to_dict()
        self.assertEqual(set(), result_graph_dict.get("bastion.1"))
        self.assertEqual(set(["bastion.1"]), result_graph_dict.get("vpc.1"))
Exemple #11
0
    def test_plan(self) -> None:
        """Test plan."""
        vpc = Stack(definition=generate_definition("vpc", 1), context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )

        graph = Graph.from_steps([Step(vpc, fn=None), Step(bastion, fn=None)])
        plan = Plan(description="Test", graph=graph)

        self.assertEqual(
            plan.graph.to_dict(), {"bastion.1": set(["vpc.1"]), "vpc.1": set([])}
        )
Exemple #12
0
    def test_generate_plan_persist_destroy(
            self, mock_graph_tags: PropertyMock) -> None:
        """Test generate plan persist destroy."""
        mock_graph_tags.return_value = {}
        context = self._get_context(
            extra_config_args={"persistent_graph_key": "test.json"})
        context._persistent_graph = Graph.from_steps(
            [Step.from_stack_name("removed", context)])
        deploy_action = deploy.Action(context=context)
        plan = cast(Plan,
                    deploy_action._Action__generate_plan())  # type: ignore

        self.assertIsInstance(plan, Plan)
        self.assertEqual(deploy.Action.DESCRIPTION, plan.description)
        mock_graph_tags.assert_called_once()
        # order is different between python2/3 so can't compare dicts
        result_graph_dict = plan.graph.to_dict()
        self.assertEqual(5, len(result_graph_dict))
        self.assertEqual(set(), result_graph_dict["other"])
        self.assertEqual(set(), result_graph_dict["removed"])
        self.assertEqual(set(), result_graph_dict["vpc"])
        self.assertEqual(set(["vpc"]), result_graph_dict["bastion"])
        self.assertEqual(set(["bastion", "vpc"]), result_graph_dict["db"])
        self.assertEqual(deploy_action._destroy_stack,
                         plan.graph.steps["removed"].fn)
        self.assertEqual(deploy_action._launch_stack,
                         plan.graph.steps["vpc"].fn)
        self.assertEqual(deploy_action._launch_stack,
                         plan.graph.steps["bastion"].fn)
        self.assertEqual(deploy_action._launch_stack,
                         plan.graph.steps["db"].fn)
        self.assertEqual(deploy_action._launch_stack,
                         plan.graph.steps["other"].fn)
Exemple #13
0
    def test_generate_plan_with_persist_no_lock_req(self, mock_stack_action,
                                                    mock_tags):
        """Test generate plan with persist no lock req."""
        mock_stack_action.return_value = MagicMock()
        mock_tags.return_value = {}
        context = mock_context(namespace="test",
                               extra_config_args=self.config_persist,
                               region=self.region)
        persist_step = Step.from_stack_name("removed", context)
        context._persistent_graph = Graph.from_steps([persist_step])
        action = BaseAction(
            context=context,
            provider_builder=MockProviderBuilder(self.provider,
                                                 region=self.region),
        )

        plan = action._generate_plan(include_persistent_graph=True,
                                     require_unlocked=False)

        self.assertIsInstance(plan, Plan)
        mock_tags.assert_called_once()
        # order is different between python2/3 so can't compare dicts
        result_graph_dict = plan.graph.to_dict()
        self.assertEqual(3, len(result_graph_dict))
        self.assertEqual(set(), result_graph_dict["stack1"])
        self.assertEqual(set(["stack1"]), result_graph_dict["stack2"])
        self.assertEqual(set(), result_graph_dict["removed"])
        self.assertEqual(BaseAction.DESCRIPTION, plan.description)
        self.assertFalse(plan.require_unlocked)
Exemple #14
0
    def test_generate_plan_persist_destroy(self, mock_graph_tags):
        """Test generate plan persist destroy."""
        mock_graph_tags.return_value = {}
        context = self._get_context(
            extra_config_args={'persistent_graph_key': 'test.json'})
        context._persistent_graph = Graph.from_steps(
            [Step.from_stack_name('removed', context)])
        build_action = build.Action(context=context)
        plan = build_action._Action__generate_plan()

        self.assertIsInstance(plan, Plan)
        self.assertEqual(build.Action.DESCRIPTION, plan.description)
        mock_graph_tags.assert_called_once()
        # order is different between python2/3 so can't compare dicts
        result_graph_dict = plan.graph.to_dict()
        self.assertEqual(5, len(result_graph_dict))
        self.assertEqual(set(), result_graph_dict['other'])
        self.assertEqual(set(), result_graph_dict['removed'])
        self.assertEqual(set(), result_graph_dict['vpc'])
        self.assertEqual(set(['vpc']), result_graph_dict['bastion'])
        self.assertEqual(set(['bastion', 'vpc']), result_graph_dict['db'])
        self.assertEqual(build_action._destroy_stack,
                         plan.graph.steps['removed'].fn)
        self.assertEqual(build_action._launch_stack,
                         plan.graph.steps['vpc'].fn)
        self.assertEqual(build_action._launch_stack,
                         plan.graph.steps['bastion'].fn)
        self.assertEqual(build_action._launch_stack, plan.graph.steps['db'].fn)
        self.assertEqual(build_action._launch_stack,
                         plan.graph.steps['other'].fn)
Exemple #15
0
    def test_plan(self):
        """Test plan."""
        vpc = Stack(definition=generate_definition('vpc', 1),
                    context=self.context)
        bastion = Stack(definition=generate_definition('bastion',
                                                       1,
                                                       requires=[vpc.name]),
                        context=self.context)

        graph = Graph.from_steps([Step(vpc, fn=None), Step(bastion, fn=None)])
        plan = Plan(description="Test", graph=graph)

        self.assertEqual(plan.graph.to_dict(), {
            'bastion.1': set(['vpc.1']),
            'vpc.1': set([])
        })
Exemple #16
0
    def test_execute_plan(self) -> None:
        """Test execute plan."""
        context = CfnginContext(config=self.config)
        context.put_persistent_graph = mock.MagicMock()
        vpc = Stack(definition=generate_definition("vpc", 1), context=context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=context,
        )
        removed = Stack(
            definition=generate_definition("removed", 1, requires=[]), context=context
        )
        context._persistent_graph = Graph.from_steps([Step(removed)])

        calls: List[str] = []

        def _launch_stack(stack: Stack, status: Optional[Status] = None) -> Status:
            calls.append(stack.fqn)
            return COMPLETE

        def _destroy_stack(stack: Stack, status: Optional[Status] = None) -> Status:
            calls.append(stack.fqn)
            return COMPLETE

        graph = Graph.from_steps(
            [
                Step(removed, fn=_destroy_stack),
                Step(vpc, fn=_launch_stack),
                Step(bastion, fn=_launch_stack),
            ]
        )
        plan = Plan(description="Test", graph=graph, context=context)
        plan.context._persistent_graph_lock_code = plan.lock_code  # type: ignore
        plan.execute(walk)

        # the order these are appended changes between python2/3
        self.assertIn("namespace-vpc.1", calls)
        self.assertIn("namespace-bastion.1", calls)
        self.assertIn("namespace-removed.1", calls)
        context.put_persistent_graph.assert_called()

        # order is different between python2/3 so can't compare dicts
        result_graph_dict = context.persistent_graph.to_dict()  # type: ignore
        self.assertEqual(2, len(result_graph_dict))
        self.assertEqual(set(), result_graph_dict.get("vpc.1"))
        self.assertEqual(set(["vpc.1"]), result_graph_dict.get("bastion.1"))
        self.assertIsNone(result_graph_dict.get("namespace-removed.1"))
Exemple #17
0
    def test_from_stack_name(self):
        """Return step from step name."""
        context = mock_context()
        stack_name = "test-stack"
        result = Step.from_stack_name(stack_name, context)

        self.assertIsInstance(result, Step)
        self.assertEqual(stack_name, result.stack.name)
Exemple #18
0
    def test_execute_plan(self):
        """Test execute plan."""
        context = Context(config=self.config)
        context.put_persistent_graph = mock.MagicMock()
        vpc = Stack(definition=generate_definition('vpc', 1), context=context)
        bastion = Stack(definition=generate_definition('bastion',
                                                       1,
                                                       requires=[vpc.name]),
                        context=context)
        removed = Stack(definition=generate_definition('removed',
                                                       1,
                                                       requires=[]),
                        context=context)
        context._persistent_graph = Graph.from_steps([removed])

        calls = []

        def _launch_stack(stack, status=None):
            calls.append(stack.fqn)
            return COMPLETE

        def _destroy_stack(stack, status=None):
            calls.append(stack.fqn)
            return COMPLETE

        graph = Graph.from_steps([
            Step(removed, _destroy_stack),
            Step(vpc, _launch_stack),
            Step(bastion, _launch_stack)
        ])
        plan = Plan(description="Test", graph=graph, context=context)
        plan.context._persistent_graph_lock_code = plan.lock_code
        plan.execute(walk)

        # the order these are appended changes between python2/3
        self.assertIn('namespace-vpc.1', calls)
        self.assertIn('namespace-bastion.1', calls)
        self.assertIn('namespace-removed.1', calls)
        context.put_persistent_graph.assert_called()

        # order is different between python2/3 so can't compare dicts
        result_graph_dict = context.persistent_graph.to_dict()
        self.assertEqual(2, len(result_graph_dict))
        self.assertEqual(set(), result_graph_dict.get('vpc.1'))
        self.assertEqual(set(['vpc.1']), result_graph_dict.get('bastion.1'))
        self.assertIsNone(result_graph_dict.get('namespace-removed.1'))
Exemple #19
0
    def test_build_graph_cyclic_dependencies(self):
        """Test build graph cyclic dependencies."""
        vpc = Stack(definition=generate_definition('vpc', 1),
                    context=self.context)
        db = Stack(definition=generate_definition('db', 1, requires=['app.1']),
                   context=self.context)
        app = Stack(definition=generate_definition('app', 1,
                                                   requires=['db.1']),
                    context=self.context)

        with self.assertRaises(GraphError) as expected:
            Graph.from_steps(
                [Step(vpc, None),
                 Step(db, None),
                 Step(app, None)])
        message = ("Error detected when adding 'db.1' "
                   "as a dependency of 'app.1': graph is "
                   "not acyclic")
        self.assertEqual(str(expected.exception), message)
Exemple #20
0
    def test_from_persistent_graph(self):
        """Return list of steps from graph dict."""
        context = mock_context()
        graph_dict = {"stack1": [], "stack2": ["stack1"]}
        result = Step.from_persistent_graph(graph_dict, context)

        self.assertEqual(2, len(result))
        self.assertIsInstance(result, list)

        for step in result:
            self.assertIsInstance(step, Step)
            self.assertIn(step.stack.name, graph_dict.keys())
Exemple #21
0
    def test_build_graph_missing_dependency(self):
        """Test build graph missing dependency."""
        bastion = Stack(definition=generate_definition('bastion',
                                                       1,
                                                       requires=['vpc.1']),
                        context=self.context)

        with self.assertRaises(GraphError) as expected:
            Graph.from_steps([Step(bastion, None)])
        message_starts = ("Error detected when adding 'vpc.1' "
                          "as a dependency of 'bastion.1':")
        message_contains = "dependent node vpc.1 does not exist"
        self.assertTrue(str(expected.exception).startswith(message_starts))
        self.assertTrue(message_contains in str(expected.exception))
Exemple #22
0
    def test_run_persist(self, mock_execute, mock_unlock, mock_lock,
                         mock_graph_tags):
        """Test run persist."""
        mock_graph_tags.return_value = {}
        context = self._get_context(
            extra_config_args={'persistent_graph_key': 'test.json'})
        context._persistent_graph = Graph.from_steps(
            [Step.from_stack_name('removed', context)])
        build_action = build.Action(context=context)
        build_action.run()

        mock_graph_tags.assert_called_once()
        mock_lock.assert_called_once()
        mock_execute.assert_called_once()
        mock_unlock.assert_called_once()
Exemple #23
0
    def test_execute_plan_skipped(self) -> None:
        """Test execute plan skipped."""
        vpc = Stack(definition=generate_definition("vpc", 1), context=self.context)
        bastion = Stack(
            definition=generate_definition("bastion", 1, requires=[vpc.name]),
            context=self.context,
        )

        calls: List[str] = []

        def fn(stack: Stack, status: Optional[Status] = None) -> Status:
            calls.append(stack.fqn)
            if stack.fqn == vpc_step.name:
                return SKIPPED
            return COMPLETE

        vpc_step = Step(vpc, fn=fn)
        bastion_step = Step(bastion, fn=fn)

        graph = Graph.from_steps([vpc_step, bastion_step])
        plan = Plan(description="Test", graph=graph)
        plan.execute(walk)

        self.assertEqual(calls, ["namespace-vpc.1", "namespace-bastion.1"])
Exemple #24
0
    def test_run_persist(self, mock_execute, mock_unlock, mock_lock, mock_graph_tags):
        """Test run persist."""
        mock_graph_tags.return_value = {}
        context = self._get_context(
            extra_config_args={"persistent_graph_key": "test.json"}
        )
        context._persistent_graph = Graph.from_steps(
            [Step.from_stack_name("removed", context)]
        )
        destroy_action = destroy.Action(context=context)
        destroy_action.run(force=True)

        mock_graph_tags.assert_called_once()
        mock_lock.assert_called_once()
        mock_execute.assert_called_once()
        mock_unlock.assert_called_once()
Exemple #25
0
    def test_dump(self) -> None:
        """Test dump."""
        requires: List[str] = []
        steps: List[Step] = []

        for i in range(5):
            overrides = {
                "variables": {
                    "PublicSubnets": "1",
                    "SshKeyName": "1",
                    "PrivateSubnets": "1",
                    "Random": "${noop something}",
                },
                "requires": requires,
            }

            stack = Stack(
                definition=generate_definition("vpc", i, **overrides),
                context=self.context,
            )
            requires = [stack.name]

            steps += [Step(stack)]

        graph = Graph.from_steps(steps)
        plan = Plan(description="Test", graph=graph)

        tmp_dir = tempfile.mkdtemp()
        try:
            plan.dump(directory=tmp_dir, context=self.context)

            for step in plan.steps:
                template_path = os.path.join(
                    tmp_dir, stack_template_key_name(step.stack.blueprint)  # type: ignore
                )
                self.assertTrue(os.path.isfile(template_path))
        finally:
            shutil.rmtree(tmp_dir)
Exemple #26
0
 def setUp(self):
     """Run before tests."""
     self.context = mock_context()
     self.graph_dict = {"stack1": [], "stack2": ["stack1"]}
     self.graph_dict_expected = {"stack1": set(), "stack2": set(["stack1"])}
     self.steps = Step.from_persistent_graph(self.graph_dict, self.context)
Exemple #27
0
 def setUp(self):
     """Run before tests."""
     self.context = mock_context()
     self.graph_dict = {'stack1': [], 'stack2': ['stack1']}
     self.graph_dict_expected = {'stack1': set(), 'stack2': set(['stack1'])}
     self.steps = Step.from_persistent_graph(self.graph_dict, self.context)
Exemple #28
0
 def setUp(self):
     """Run before tests."""
     stack = mock.MagicMock()
     stack.name = "stack"
     stack.fqn = "namespace-stack"
     self.step = Step(stack=stack, fn=None)