def test_will_throw_exception_if_service_definition_dont_have_a_service_configurator_key(self):
        single_service_yaml = """
            apache:
                miss_spelled_service_configurator: phoenix.configurators.fake_service_configurator.FakeServiceConfigurator
            """

        with self.assertRaisesRegexp(Exception,
            "Key 'service_configurator' is not found in services configuration of service: 'apache'"):
            service_definitions_from_yaml(single_service_yaml, None)
    def test_will_throw_exception_if_service_configurator_key_dont_name_a_existing_service_configurator(self):
        single_service_yaml = """
            apache:
                service_configurator: NonExistingServiceConfigurator
            """

        with self.assertRaisesRegexp(Exception,
            "Invalid Service Configurator: 'NonExistingServiceConfigurator' in services configuration of service: 'apache'"):
            service_definitions_from_yaml(single_service_yaml, None)
    def test_will_throw_exception_if_service_definition_dont_have_a_puppet_manifest_key(self):
        single_service_yaml = """
            apache:
                puppet_module_directory : puppet
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
            """

        with self.assertRaisesRegexp(Exception,
            "Key 'puppet_manifest' is not defined in services configuration of service: 'apache'"):
            service_definitions_from_yaml(single_service_yaml, os.path.abspath('samples'))
    def test_finds_all_problems_when_loading_multiple_service_definitions(self):
        multiple_service_yaml = """
          apache:
                puppet_manifest : apache.pp
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
          mongo:
                puppet_module_directory : puppet
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
        """

        with self.assertRaisesRegexp(Exception,
            "Key 'puppet_module_directory' is not defined in services configuration of service: 'apache',\nKey 'puppet_manifest' is not defined in services configuration of service: 'mongo'"):
            service_definitions_from_yaml(multiple_service_yaml, os.path.abspath('samples'))
    def test_can_load_single_service_definition(self):
        single_service_yaml = """
            apache:
                puppet_module_directory : puppet
                puppet_manifest : apache.pp
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
                connectivity:
                    - protocol: tcp
                      ports: [ 80 ]
                      allowed: [ WORLD ]
        """

        service_definitions = service_definitions_from_yaml(single_service_yaml,
            os.path.abspath('samples'))
        self.assertEquals(service_definitions['apache'].name, 'apache')
        self.assertEquals(service_definitions['apache'].puppet_module_directory, 'puppet')
        self.assertEquals(service_definitions['apache'].puppet_manifest, 'apache.pp')
    def test_can_load_multiple_service_definitions(self):
        multiple_service_yaml = """
          apache:
                puppet_module_directory : puppet
                puppet_manifest : apache.pp
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
          mongo:
                puppet_module_directory : puppet
                puppet_manifest : mongo.pp
                service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
        """

        service_definitions = service_definitions_from_yaml(multiple_service_yaml,
            os.path.abspath('samples'))
        self.assertEquals(service_definitions['apache'].name, 'apache')
        self.assertEquals(service_definitions['apache'].puppet_manifest, 'apache.pp')
        self.assertEquals(service_definitions['mongo'].name, 'mongo')
        self.assertEquals(service_definitions['mongo'].puppet_manifest, 'mongo.pp')
    def test_can_bundle_directory(self):
        abs_temp_dir = tempfile.mkdtemp()
        temp_rel_dir = tempfile.mkdtemp(dir=abs_temp_dir)
        fakefile = tempfile.mkstemp(dir=temp_rel_dir)

        yaml = """
            apache:
                port : 80
                puppet_module_dir : %s
                service_configurator: phoenix.configurators.fake_service_configurator.FakeServiceConfigurator
                """ % os.path.relpath(temp_rel_dir, abs_temp_dir)

        service_definitions = service_definitions_from_yaml(yaml, abs_temp_dir)
        path_to_bundle = service_definitions['apache'].bundle('puppet_module_dir', 'testbundle.tgz')

        tar = tarfile.open(path_to_bundle)
        # Inside the tarball, the file path will be relative to the fake relative directory we created
        expected_rel_path_to_file = os.path.relpath(fakefile[1], abs_temp_dir)

        self.assertTrue(expected_rel_path_to_file in [entry.path for entry in tar.getmembers()])
  service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
  connectivity:
    - protocol: tcp
      ports: [ 8080, 8081 ]
      allowed: [ WORLD ]
mongo:
  puppet_module_directory : puppet
  puppet_manifest : mongo.pp
  service_configurator: phoenix.configurators.puppet_service_configurator.PuppetServiceConfigurator
  connectivity:
    - protocol: tcp
      ports: [ 27017 ]
      allowed: [ hello_world ]
"""

service_definitions = service_definitions_from_yaml(services_yaml, "samples/")

class TestNodeDefinitions(TestCase):
    def test_will_throw_exception_if_node_type_is_not_set(self):
        env_yaml = """
          dev:
            nodes:
                - ami_id: ami-4dad7424
                  size:   t1.micro
                  credentials_name: test
                  services: [mongo, hello_world]
                  aws_key_name : test
                  invalid_type: phoenix.providers.aws_provider.AWSNodeDefinition
            node_provider:
                class_name: AWSNodeProvider
                public_api_key: AKIAIGBFGAGVPGKLVX4Q