def test_model_serialization(self):
        dep = models.Deployment(id='dep-id',
                                created_at='some-time1',
                                updated_at='some-time2',
                                blueprint_id='bp-id',
                                plan={'field': 'value'},
                                permalink=None,
                                workflows={},
                                inputs={},
                                policy_types={},
                                policy_triggers={},
                                groups={},
                                outputs={})

        serialized_dep = dep.to_dict()
        self.assertEquals(11, len(serialized_dep))
        self.assertEquals(dep.id, serialized_dep['id'])
        self.assertEquals(dep.created_at, serialized_dep['created_at'])
        self.assertEquals(dep.updated_at, serialized_dep['updated_at'])
        self.assertEquals(dep.blueprint_id, serialized_dep['blueprint_id'])
        self.assertEquals(dep.permalink, serialized_dep['permalink'])

        deserialized_dep = models.Deployment(**serialized_dep)
        self.assertEquals(dep.id, deserialized_dep.id)
        self.assertEquals(dep.created_at, deserialized_dep.created_at)
        self.assertEquals(dep.updated_at, deserialized_dep.updated_at)
        self.assertEquals(dep.blueprint_id, deserialized_dep.blueprint_id)
        self.assertEquals(dep.permalink, deserialized_dep.permalink)
    def test_get_blueprint_deployments(self):
        now = str(datetime.now())
        blueprint = models.BlueprintState(id='blueprint-id',
                                          created_at=now,
                                          updated_at=now,
                                          plan={'name': 'my-bp'},
                                          source='bp-source')
        storage_manager.instance().put_blueprint('blueprint-id', blueprint)

        deployment1 = models.Deployment(id='dep-1',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-1', deployment1)

        deployment2 = models.Deployment(id='dep-2',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-2', deployment2)

        deployment3 = models.Deployment(id='dep-3',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='another-blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        outputs={})
        storage_manager.instance().put_deployment('dep-3', deployment3)

        blueprint_deployments = storage_manager.instance()\
            .get_blueprint_deployments(
                'blueprint-id')

        self.assertEquals(2, len(blueprint_deployments))
        self.assertEquals(deployment1.__dict__,
                          blueprint_deployments[0].__dict__)
        self.assertEquals(deployment2.__dict__,
                          blueprint_deployments[1].__dict__)
        blueprint = self.get_blueprint(blueprint_id)
        plan = blueprint.plan
        try:
            deployment_plan = tasks.prepare_deployment_plan(plan, inputs)
        except parser_exceptions.MissingRequiredInputError, e:
            raise manager_exceptions.MissingRequiredDeploymentInputError(
                str(e))
        except parser_exceptions.UnknownInputError, e:
            raise manager_exceptions.UnknownDeploymentInputError(str(e))

        now = str(datetime.now())
        new_deployment = models.Deployment(
            id=deployment_id,
            blueprint_id=blueprint_id, created_at=now, updated_at=now,
            workflows=deployment_plan['workflows'],
            inputs=deployment_plan['inputs'],
            policy_types=deployment_plan['policy_types'],
            policy_triggers=deployment_plan['policy_triggers'],
            groups=deployment_plan['groups'],
            outputs=deployment_plan['outputs'])

        self.sm.put_deployment(deployment_id, new_deployment)
        self._create_deployment_nodes(blueprint_id,
                                      deployment_id,
                                      deployment_plan)

        node_instances = deployment_plan['node_instances']
        self._create_deployment_node_instances(deployment_id,
                                               node_instances)
        self._create_deployment_environment(new_deployment, deployment_plan)
        return new_deployment
    def test_get_blueprint_deployments(self):
        now = str(datetime.now())
        sm = storage_manager._get_instance()
        blueprint = models.BlueprintState(id='blueprint-id',
                                          created_at=now,
                                          updated_at=now,
                                          description=None,
                                          plan={'name': 'my-bp'},
                                          source='bp-source',
                                          main_file_name='aaa')
        sm.put_blueprint('blueprint-id', blueprint)

        deployment1 = models.Deployment(id='dep-1',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        description=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        scaling_groups={},
                                        outputs={})
        sm.put_deployment('dep-1', deployment1)

        deployment2 = models.Deployment(id='dep-2',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='blueprint-id',
                                        plan={'name': 'my-bp'},
                                        permalink=None,
                                        description=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        scaling_groups={},
                                        outputs={})
        sm.put_deployment('dep-2', deployment2)

        deployment3 = models.Deployment(id='dep-3',
                                        created_at=now,
                                        updated_at=now,
                                        blueprint_id='another-blueprint-id',
                                        plan={'name': 'my-bp'},
                                        description=None,
                                        permalink=None,
                                        workflows={},
                                        inputs={},
                                        policy_types={},
                                        policy_triggers={},
                                        groups={},
                                        scaling_groups={},
                                        outputs={})
        sm.put_deployment('dep-3', deployment3)

        blueprint_deployments = \
            sm.get_blueprint_deployments('blueprint-id').items

        self.assertEquals(2, len(blueprint_deployments))
        if blueprint_deployments[0].id != deployment1.id:
            blueprint_deployments[0], blueprint_deployments[1] =\
                blueprint_deployments[1], blueprint_deployments[0]
        self.assertEquals(deployment1.to_dict(),
                          blueprint_deployments[0].to_dict())
        self.assertEquals(deployment2.to_dict(),
                          blueprint_deployments[1].to_dict())