Esempio n. 1
0
 def test_cloudformation_create_and_update(self):
     cloudformation = CloudFormationBuddy(self.test_deploy_ctx)
     s3 = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
     temp_dir = tempfile.mkdtemp()
     try:
         template = ParentTestCase._get_resource_path("cloudformation/aws-resources.template")
         parameter_file = ParentTestCase._get_resource_path("cloudformation/aws-resources.parameters.json")
         self.test_deploy_ctx['RANDOM'] = self.randomWord(5)
         parameter_file_rendered = self.test_deploy_ctx.render_template(parameter_file,temp_dir)
         template_file_url = s3.upload(file=template)
         self.assertFalse(cloudformation.does_stack_exist(), "Failed to identify reality")
         cloudformation.create_stack(template_file_url=template_file_url,
                                     parameter_file=parameter_file_rendered)
         self.assertTrue(cloudformation.does_stack_exist(), "Failed to identify reality")
         self.assertEqual(cloudformation.get_stack_status(), "CREATE_COMPLETE", "Failed to identify create complete")
         cloudformation.create_change_set(template_file_url=template_file_url,
                                          parameter_file=parameter_file_rendered)
         self.assertFalse(cloudformation.should_execute_change_set(),"Failed to id noop changeset")
         cloudformation.delete_change_set()
         self.test_deploy_ctx['RANDOM'] = self.randomWord(5)
         parameter_file_rendered = self.test_deploy_ctx.render_template(parameter_file,temp_dir)
         cloudformation.create_change_set(template_file_url=template_file_url,
                                          parameter_file=parameter_file_rendered)
         self.assertEqual(cloudformation.get_change_set_status(refresh=True), "CREATE_COMPLETE",
                          "Did not get expected cs status")
         self.assertTrue(cloudformation.should_execute_change_set(),"Failed to id good changeset")
         if not cloudformation.should_execute_change_set():
             self.fail("Did not want to execute changeset")
         else:
             cloudformation.execute_change_set()
         cloudformation.log_stack_status()
     finally:
         super(CloudFormationTestCase, self).clean(cloudformation)
         super(CloudFormationTestCase, self).clean_s3(s3)
         super(CloudFormationTestCase, self).clean_dir(temp_dir)
Esempio n. 2
0
 def test_s3_template(self):
     s3 = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
     template = ParentTestCase._get_resource_path(
         "template_tests/test-template.zip")
     s3.upload(file=template)
     key = s3._get_upload_bucket_key_name(template, key_name=None)
     s3_url = "s3://{bucket}/{key}".format(
         bucket=self.test_deploy_ctx.cf_bucket_name, key=key)
     self._validate_template(self.test_deploy_ctx.template_manager, {
         "type": "s3",
         "location": s3_url
     })
Esempio n. 3
0
 def test_cloudformation_deploy(self):
     template = ParentTestCase._get_resource_path("cloudformation/aws-resources.template")
     parameter_file = ParentTestCase._get_resource_path("cloudformation/aws-resources.parameters.json")
     config_templates = ParentTestCase._get_resource_path("cloudformation/config/")
     deploy = CloudFormationDeploy(self.test_deploy_ctx.stack_name, LocalTemplate(template, parameter_file, config_templates), self.test_deploy_ctx)
     deploy.do_deploy(dry_run=False)
     cloudformation = CloudFormationBuddy(self.test_deploy_ctx)
     s3 = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
     try:
         self.assertTrue(cloudformation.does_stack_exist(), "Failed to create stack")
         self.assertEqual(s3.get_file_as_string("install_template.sh"),"foo-bar-{}".format(self.run_random_word),"Did not render config template")
     finally:
         super(CloudFormationTestCase, self).clean(cloudformation)
         super(CloudFormationTestCase, self).clean_s3(s3)
Esempio n. 4
0
 def test_zip_download(self):
     s3_buddy = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
     compress = ParentTestCase._get_resource_path(
         "s3_tests/test_compress.json.zip")
     s3_buddy.upload(compress)
     s3_url = "s3://{bucket}/{key}".format(
         bucket=self.test_deploy_ctx.cf_bucket_name,
         key=s3_buddy._get_upload_bucket_key_name(
             file=None, key_name="test_compress.json.zip"))
     temp_dir = tempfile.mkdtemp()
     test_file = os.path.join(temp_dir, "test_compress.json")
     try:
         s3.download_zip_from_s3_url(s3_url, temp_dir)
         self.assertTrue(os.path.exists(test_file),
                         "Failed to decompress file")
     finally:
         self.clean_dir(temp_dir)
         self.clean_s3(s3_buddy)
Esempio n. 5
0
    def test_file_operations(self):

        s3_buddy = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
        try:
            changeset = ParentTestCase._get_resource_path(
                "cloudformation/sample_changeset.json")
            s3_buddy.upload(changeset)
            with open(changeset, 'r') as cs:
                change_set_string = cs.read()
                self.assertEqual(
                    s3_buddy.get_file_as_string('sample_changeset.json'),
                    change_set_string, "Failed to get file as string")
            self.assertEqual(
                s3_buddy._get_s3_object(
                    'sample_changeset.json')['ContentType'],
                'application/json', "Did not persist correct content type")
        finally:
            self.clean_s3(s3_buddy)
Esempio n. 6
0
 def _validate_s3(self, deploy_ctx):
     s3_buddy = CloudFormationDeployS3Buddy(deploy_ctx)
     try:
         self.assertEqual(s3_buddy.key_root_path,
                          self.test_deploy_ctx.cf_deploy_resource_path,
                          "Did not init correct path")
         self.assertEqual(s3_buddy.bucket.name,
                          self.test_deploy_ctx.cf_bucket_name,
                          "Did not init correct bucket")
         if deploy_ctx.region == 'us-east-1':
             base = 's3'
         else:
             base = "s3-{}".format(deploy_ctx.region)
         self.assertEqual(
             s3_buddy.url_base,
             "https://{}.amazonaws.com/unit-test-foo-cloudformation-deploy-resources"
             .format(base), "Did not init correct url")
     finally:
         self.clean_s3(s3_buddy)
Esempio n. 7
0
 def test_s3_deploy(self):
     s3_buddy = CloudFormationDeployS3Buddy(self.test_deploy_ctx)
     try:
         compress = ParentTestCase._get_resource_path(
             "s3_tests/test_compress.json.zip")
         s3_buddy.upload(compress)
         s3_url = "{bucket}/{key}".format(
             bucket=self.test_deploy_ctx.cf_bucket_name,
             key=s3_buddy._get_upload_bucket_key_name(
                 file=None, key_name="test_compress.json.zip"))
         # s3 deploy appends zip to it
         s3d = S3Deploy(artifact_id="test_compress.json",
                        location=s3_url[:s3_url.rfind("/")],
                        ctx=self.test_deploy_ctx)
         s3d.cloud_formation_buddy = TestCloudFormationBuddy(
             self.test_deploy_ctx)
         s3d.do_deploy()
         self.assertEqual(s3_buddy.get_file_as_string('test_compress.json'),
                          "test-compress", "Failed to get file as string")
     finally:
         self.clean_s3(s3_buddy)
Esempio n. 8
0
 def _internal_deploy(self, dry_run):
     # Initialize our buddies
     s3 = CloudFormationDeployS3Buddy(self.deploy_ctx)
     cloud_formation = CloudFormationBuddy(self.deploy_ctx)
     if dry_run:
         self.validate()
         return
     # Upload our template to s3 to make things a bit easier and keep a record
     template_file_url = s3.upload(file=(self.template_file))
     # Upload all of our config files to S3 rendering any variables
     config_files = self.get_rendered_config_files()
     for rendered in config_files:
         s3.upload(file=rendered)
     # render our parameter files
     parameter_file_rendered = self.get_rendered_param_file()
     # see if we are updating or creating
     if cloud_formation.should_create_change_set():
         cloud_formation.create_change_set(
             template_file_url=template_file_url,
             parameter_file=parameter_file_rendered)
         # make sure it is available and that there are no special conditions
         if cloud_formation.should_execute_change_set():
             print_utility.progress(
                 "Updating existing stack with ChangeSet - {}".format(
                     self.stack_name))
             cloud_formation.execute_change_set()
         else:
             print_utility.warn("No computed changes for stack - {}".format(
                 self.stack_name))
             # if there are no changes then clean up and exit
             cloud_formation.delete_change_set()
             return
     else:
         print_utility.progress("Creating new stack - {}".format(
             self.stack_name))
         cloud_formation.create_stack(
             template_file_url=template_file_url,
             parameter_file=parameter_file_rendered)