Beispiel #1
0
    def test_advanced_s3_example(self):
        region = "us-east-1"
        bucket_name = "pitfall-advanced-example"

        config = [
            PulumiConfigurationKey(name='aws:region', value=region),
            PulumiConfigurationKey(name='s3-bucket-name', value=bucket_name)
        ]

        plugins = [PulumiPlugin(kind='resource', name='aws', version='v1.7.0')]

        opts = PulumiIntegrationTestOptions(verbose=True,
                                            cleanup=True,
                                            preview=False,
                                            up=False,
                                            destroy=False)

        directory = Path(__file__)

        with PulumiIntegrationTest(directory=directory,
                                   config=config,
                                   plugins=plugins,
                                   opts=opts) as t:
            # execute `pulumi preview`
            t.preview.execute()

            # verify that 3 resources will be provisioned
            pulumi_steps = t.preview.steps
            self.assertEqual(len(pulumi_steps), 3)

            for step in pulumi_steps:
                self.assertEqual("create", step.op)

            # execute `pulumi up`
            t.up.execute()

            # verify that 3 resources have been provisioned
            resources = t.state.resources
            self.assertEqual(resources.providers["pulumi:providers:aws"], 3)

            # get the bucket_name and website_url from the stack outputs
            stack_outputs = t.get_stack_outputs()

            # verify that the bucket was provisioned
            bucket = stack_outputs["bucket_name"]

            s3 = boto3.client(service_name="s3")
            s3.head_bucket(Bucket=bucket)

            # verify that the bucket is hosting a website and serving the uploaded index.html file
            index_html_file = Path('./index.html')

            url = stack_outputs["website_url"]

            r = requests.get(url, timeout=5)
            self.assertEqual(r.status_code, 200)
            self.assertEqual(r.text, index_html_file.read_text())

            # execute `pulumi destroy`
            t.destroy.execute()
Beispiel #2
0
    def test_basic_s3_example(self):
        region = "us-east-1"
        bucket_name = "pitfall-basic-example"

        config = [
            PulumiConfigurationKey(name='aws:region', value=region),
            PulumiConfigurationKey(name='s3-bucket-name', value=bucket_name)
        ]

        plugins = [PulumiPlugin(kind='resource', name='aws', version='v1.7.0')]

        opts = PulumiIntegrationTestOptions(verbose=True,
                                            cleanup=True,
                                            preview=True,
                                            up=True,
                                            destroy=True)

        directory = Path(__file__)

        with PulumiIntegrationTest(directory=directory,
                                   config=config,
                                   plugins=plugins,
                                   opts=opts) as t:
            # get the name of the bucket from the stack's outputs
            stack_outputs = t.get_stack_outputs()

            bucket = stack_outputs["bucket_name"]

            # use boto3 to perform verifications
            s3 = boto3.client(service_name="s3")

            # verify that the bucket was provisioned in us-east-1
            r = s3.head_bucket(Bucket=bucket)

            bucket_region = r["ResponseMetadata"]["HTTPHeaders"][
                "x-amz-bucket-region"]
            self.assertEqual(region, bucket_region)

            # verify that the bucket has the required tags set
            required_tags = {"CreatedBy", "PulumiProject", "PulumiStack"}

            r = s3.get_bucket_tagging(Bucket=bucket)

            set_tags = {i["Key"]: i["Value"] for i in r["TagSet"]}

            self.assertTrue(required_tags <= set(set_tags))
Beispiel #3
0
    def setUpClass(cls):
        cls.region = utils.get_random_region()
        print("[DEBUG] Using AWS Region: ", cls.region)

        cls.vpc_cidr = "10.0.0.0/16"
        vpc_name = "pitfall-test-vpc"
        subnets = 2
        prefix = 20

        config = [
            PulumiConfigurationKey(name='aws:region', value=cls.region),
            PulumiConfigurationKey(name='environment', value='test'),
            PulumiConfigurationKey(name='billing-project',
                                   value='integration-testing'),
            PulumiConfigurationKey(name='vpc-name', value=vpc_name),
            PulumiConfigurationKey(name='vpc-cidr', value=cls.vpc_cidr),
            PulumiConfigurationKey(name='subnets', value=subnets),
            PulumiConfigurationKey(name='prefix', value=prefix),
        ]

        plugins = [PulumiPlugin(kind='resource', name='aws', version='v1.7.0')]

        opts = PulumiIntegrationTestOptions(verbose=False,
                                            cleanup=False,
                                            preview=True,
                                            up=True,
                                            destroy=True)

        directory = Path(__file__)

        cls.t = PulumiIntegrationTest(directory=directory,
                                      config=config,
                                      plugins=plugins,
                                      opts=opts)
        cls.t.__enter__()
Beispiel #4
0
    def test_aws_provision_s3_bucket_with_auto_cleanup_destroy(self):
        region      = utils.get_random_region()
        bucket_name = f"pitfall-test-bucket-1"

        config = [
            PulumiConfigurationKey(name='aws:region', value=region),
            PulumiConfigurationKey(name='s3-bucket-name', value=bucket_name),
            PulumiConfigurationKey(name='environment', value='test'),
            PulumiConfigurationKey(name='owner', value='@bincyber'),
            PulumiConfigurationKey(name='billing-project', value='integration-testing'),
            PulumiConfigurationKey(name='customer', value=b'ACME Corp', encrypted=True)
        ]

        provisioned_bucket_name = None

        opts = PulumiIntegrationTestOptions(cleanup=True, preview=True, up=True, destroy=True)

        with PulumiIntegrationTest(directory=self.dir, config=config, plugins=self.plugins, opts=opts) as integration_test:
            outputs = integration_test.get_stack_outputs()

            provisioned_bucket_name = outputs["s3_bucket_name"]
            self.assertTrue(provisioned_bucket_name.startswith(bucket_name))

        # verify that the S3 bucket has been deleted
        with self.assertRaises(botocore.exceptions.ClientError):
            self.s3.head_bucket(Bucket=provisioned_bucket_name)
Beispiel #5
0
    def test_e2e_using_localstack_with_autodestroy(self):
        bucket_name = f"pitfall-localstack-test-bucket-1"

        config = [
            PulumiConfigurationKey(name='local-mode', value=True),
            PulumiConfigurationKey(name='s3-bucket-name', value=bucket_name),
            PulumiConfigurationKey(name='environment', value='test'),
            PulumiConfigurationKey(name='owner', value='@bincyber'),
            PulumiConfigurationKey(name='billing-project',
                                   value='integration-testing'),
            PulumiConfigurationKey(name='customer',
                                   value=b'ACME Corp',
                                   encrypted=True)
        ]

        plugins = [PulumiPlugin(kind='resource', name='aws', version='v1.7.0')]

        provisioned_bucket_name = None

        opts = PulumiIntegrationTestOptions(cleanup=True,
                                            preview=True,
                                            up=True,
                                            destroy=True)

        with PulumiIntegrationTest(directory=self.dir,
                                   config=config,
                                   plugins=plugins,
                                   opts=opts) as integration_test:
            outputs = integration_test.get_stack_outputs()

            provisioned_bucket_name = outputs["s3_bucket_name"]
            self.assertTrue(provisioned_bucket_name.startswith(bucket_name))

        # verify that the S3 bucket has been deleted
        with self.assertRaises(botocore.exceptions.ClientError):
            self.s3.head_bucket(Bucket=provisioned_bucket_name)
Beispiel #6
0
    def test_aws_provision_s3_bucket_without_auto_cleanup_destroy(self):
        region = "us-east-1"
        bucket_name = f"pitfall-test-bucket-2"

        config = [
            PulumiConfigurationKey(name='aws:region', value=region),
            PulumiConfigurationKey(name='s3-bucket-name', value=bucket_name),
            PulumiConfigurationKey(name='environment', value='test'),
            PulumiConfigurationKey(name='owner', value='@bincyber'),
            PulumiConfigurationKey(name='billing-project', value='integration-testing'),
            PulumiConfigurationKey(name='customer', value=b'ACME Corp', encrypted=True)
        ]

        opts = PulumiIntegrationTestOptions(verbose=True, cleanup=False, preview=False, destroy=False)

        with PulumiIntegrationTest(directory=self.dir, config=config, plugins=self.plugins, opts=opts) as integration_test:
            integration_test.preview.execute()

            pulumi_step = integration_test.preview.steps[0]
            self.assertEqual("create", pulumi_step.op)
            self.assertEqual("aws:s3/bucket:Bucket", pulumi_step.new_state_type)
            self.assertTrue(pulumi_step.new_state_inputs["bucket"].startswith(bucket_name))
            self.assertEqual("private", pulumi_step.new_state_inputs["acl"])

            integration_test.up.execute()

            outputs = integration_test.get_stack_outputs()

            # verify that the S3 bucket exists using boto3
            provisioned_bucket_name = outputs["s3_bucket_name"]

            response = self.s3.head_bucket(Bucket=provisioned_bucket_name)
            self.assertIs(dict, type(response))

            expected = response["ResponseMetadata"]["HTTPHeaders"]["x-amz-bucket-region"]
            actual   = region
            self.assertEqual(expected, actual)

            # verify required tags have been set by checking the state file
            resources = integration_test.state.resources

            s3_bucket_resource = resources.lookup(key="type", value="aws:s3/bucket:Bucket")[0]
            self.assertEqual("aws:s3/bucket:Bucket", s3_bucket_resource.type)

            required_tags = {"CreatedBy", "CreatedOn", "Environment", "BillingProject", "Owner"}
            self.assertTrue(required_tags <= set(s3_bucket_resource.outputs["tags"]))

            # upload __main__.py to the S3 bucket
            filename       = "__main__.py"
            file           = self.dir.parent.joinpath(filename)
            content_length = len(file.read_bytes())
            content_type   = "application/x-python-code"

            self.s3.put_object(
                ACL="private",
                Bucket=provisioned_bucket_name,
                Body=file.read_bytes(),
                Key=filename,
                ContentLength=content_length,
                ContentType=content_type
            )

            response = self.s3.head_object(
                Bucket=provisioned_bucket_name,
                Key=filename
            )

            expected = response["ContentLength"]
            actual   = content_length
            self.assertEqual(expected, actual)

            expected = response["ContentType"]
            actual   = content_type
            self.assertEqual(expected, actual)

            # execute `pulumi up` again for idempotency test
            integration_test.up.execute(expect_no_changes=True)

            # execute `pulumi destroy` to delete S3 bucket
            integration_test.destroy.execute()

            # wait for bucket deletion
            time.sleep(5)

            # verify that the S3 bucket has been deleted
            with self.assertRaises(botocore.exceptions.ClientError):
                self.s3.head_bucket(Bucket=provisioned_bucket_name)