def _update(args):
    """Update the Django project on GKE."""
    try:
        update.main(args)
    except Exception as e:
        django_cloud_deploy.crash_handling.handle_crash(
            e, 'django-cloud-deploy update')
Esempio n. 2
0
    def test_deploy_and_update_new_project(self):
        # Generate unique resource names
        fake_superuser_name = 'admin'
        fake_password = '******'

        cloud_storage_bucket_name = utils.get_resource_name('bucket')
        django_project_name = utils.get_resource_name('project', delimiter='')

        # Generate names we hardcode for users
        image_tag = '/'.join(['gcr.io', self.project_id, django_project_name])
        service_account_email = '{}@{}.iam.gserviceaccount.com'.format(
            self._FAKE_CLOUDSQL_SERVICE_ACCOUNT['id'], self.project_id)
        member = 'serviceAccount:{}'.format(service_account_email)

        with self.clean_up_cluster(django_project_name), \
                self.clean_up_bucket(cloud_storage_bucket_name), \
                self.clean_up_docker_image(image_tag), \
                self.delete_service_account(service_account_email), \
                self.reset_iam_policy(member, self._CLOUDSQL_ROLES), \
                self.clean_up_sql_instance(django_project_name + '-instance'):

            test_io = io.TestIO()
            test_io.answers.append(self.project_id)  # project_id
            test_io.password_answers.append(fake_password)  # database password
            # database password again
            test_io.password_answers.append(fake_password)
            test_io.answers.append(self.project_dir)  # django_directory_path

            # The Django local directory is created with tempfile.mkdtemp().
            # So when we get this prompt, it exists already. We need to
            # overwrite it.
            test_io.answers.append('Y')
            test_io.answers.append(django_project_name)  # django_project_name
            test_io.answers.append('')  # django_app_name
            # django_superuser_login
            test_io.answers.append(fake_superuser_name)
            # django_superuser_password
            test_io.password_answers.append(fake_password)
            # django_superuser_password again
            test_io.password_answers.append(fake_password)
            test_io.answers.append('')  # django_superuser_email

            fake_service_accounts = {
                'cloud_sql': [self._FAKE_CLOUDSQL_SERVICE_ACCOUNT]
            }

            arguments = types.SimpleNamespace(
                credentials=self.credentials,
                use_existing_project=True,
                bucket_name=cloud_storage_bucket_name,
                service_accounts=fake_service_accounts,
                backend='gke')
            url = new.main(arguments, test_io)

            # Assert answers are all used.
            self.assertEqual(len(test_io.answers), 0)
            self.assertEqual(len(test_io.password_answers), 0)

            # Setup Selenium
            chrome_options = options.Options()
            chrome_options.add_argument('--headless')  # No browser
            driver = webdriver.Chrome(options=chrome_options)

            # Assert the web app is available
            driver.get(url)
            self.assertIn('Hello from the Cloud!', driver.page_source)

            # Assert the web app admin page is available
            admin_url = urllib.parse.urljoin(url, '/admin')
            driver.get(admin_url)
            self.assertEqual(driver.title, 'Log in | Django site admin')

            # Log in with superuser name and password
            username = driver.find_element_by_id('id_username')
            password = driver.find_element_by_id('id_password')
            username.send_keys(fake_superuser_name)
            password.send_keys(fake_password)
            driver.find_element_by_css_selector(
                '.submit-row [value="Log in"]').click()
            self.assertEqual(driver.title,
                             'Site administration | Django site admin')

            object_path = 'static/admin/css/base.css'
            object_url = 'http://storage.googleapis.com/{}/{}'.format(
                cloud_storage_bucket_name, object_path)
            response = requests.get(object_url)

            # Assert the static content is successfully uploaded
            self.assertIn('DJANGO', response.text)

            # Assert the deployed app is using static content from the GCS
            # bucket
            self.assertIn(cloud_storage_bucket_name, driver.page_source)

            # Test update command
            test_io = io.TestIO()
            test_io.password_answers.append(fake_password)  # database password
            test_io.answers.append(self.project_dir)  # django_directory_path

            view_file_path = os.path.join(self.project_dir, 'home', 'views.py')
            with open(view_file_path) as view_file:
                file_content = view_file.read()
                file_content = file_content.replace('Hello', 'Hello1')

            with open(view_file_path, 'w') as view_file:
                view_file.write(file_content)
            arguments = types.SimpleNamespace(credentials=self.credentials)

            update.main(arguments, test_io)

            # Assert answers are all used.
            self.assertEqual(len(test_io.answers), 0)
            self.assertEqual(len(test_io.password_answers), 0)

            response = requests.get(url)
            self.assertIn('Hello1 from the Cloud!', response.text)
Esempio n. 3
0
    def test_cloudify_and_update_new_project(self, unused_mock):
        # Generate unique resource names
        fake_superuser_name = 'admin'
        fake_password = '******'

        cloud_storage_bucket_name = utils.get_resource_name('bucket')
        database_instance_name = utils.get_resource_name('sql-instance')
        service_name = utils.get_resource_name('svc', delimiter='')
        cluster_name = utils.get_resource_name('cluster')

        # Generate names we hardcode for users
        service_account_email = '{}@{}.iam.gserviceaccount.com'.format(
            self._FAKE_CLOUDSQL_SERVICE_ACCOUNT['id'], self.project_id)
        member = 'serviceAccount:{}'.format(service_account_email)

        settings_path = os.path.join(self.project_dir, 'mysite', 'settings.py')
        requirements_path = os.path.join(self.project_dir, 'requirements.txt')
        with self.clean_up_cluster(cluster_name), \
                self.clean_up_bucket(cloud_storage_bucket_name), \
                self.delete_service_account(service_account_email), \
                self.reset_iam_policy(member, self._CLOUDSQL_ROLES), \
                self.clean_up_sql_instance(database_instance_name), \
                self.clean_up_appengine_service(service_name):

            test_io = e2e_utils.create_cloudify_command_io(
                self.project_id, self.project_dir, requirements_path,
                settings_path)

            fake_service_accounts = {
                'cloud_sql': [self._FAKE_CLOUDSQL_SERVICE_ACCOUNT]
            }

            arguments = types.SimpleNamespace(
                credentials=self.credentials,
                use_existing_project=True,
                bucket_name=cloud_storage_bucket_name,
                service_accounts=fake_service_accounts,
                appengine_service_name=service_name,
                cluster_name=cluster_name,
                database_instance_name=database_instance_name,
                backend='gke')
            url = cloudify.main(arguments, test_io)

            # Assert answers are all used.
            self.assertEqual(len(test_io.answers), 0)
            self.assertEqual(len(test_io.password_answers), 0)

            # Setup Selenium
            chrome_options = options.Options()
            chrome_options.add_argument('--headless')  # No browser
            driver = webdriver.Chrome(options=chrome_options)

            # Assert the web app is available
            driver.get(url)
            self.assertIn('Hello from the Cloud!', driver.page_source)

            # Assert the web app admin page is available
            admin_url = urllib.parse.urljoin(url, '/admin')
            driver.get(admin_url)
            self.assertEqual(driver.title, 'Log in | Django site admin')

            # Log in with superuser name and password
            username = driver.find_element_by_id('id_username')
            password = driver.find_element_by_id('id_password')
            username.send_keys(fake_superuser_name)
            password.send_keys(fake_password)
            driver.find_element_by_css_selector(
                '.submit-row [value="Log in"]').click()
            self.assertEqual(driver.title,
                             'Site administration | Django site admin')

            # Assert the static content is successfully uploaded
            object_path = 'static/admin/css/base.css'
            object_url = 'http://storage.googleapis.com/{}/{}'.format(
                cloud_storage_bucket_name, object_path)
            response = requests.get(object_url)
            self.assertIn('DJANGO', response.text)

            # Assert the deployed app is using static content from the GCS
            # bucket
            self.assertIn(cloud_storage_bucket_name, driver.page_source)

            # Test update command
            test_io = e2e_utils.create_update_command_io(self.project_dir)
            view_file_path = os.path.join(self.project_dir, 'polls',
                                          'views.py')
            with open(view_file_path) as view_file:
                file_content = view_file.read()
                file_content = file_content.replace('Hello', 'Hello1')

            with open(view_file_path, 'w') as view_file:
                view_file.write(file_content)
            arguments = types.SimpleNamespace(
                credentials=self.credentials,
                cluster_name=cluster_name,
                database_instance_name=database_instance_name)

            update.main(arguments, test_io)

            # Assert answers are all used.
            self.assertEqual(len(test_io.answers), 0)
            self.assertEqual(len(test_io.password_answers), 0)

            # This call is flaky without retry. Sometimes this call is made
            # after the pod is ready but before the http server is ready.
            response = e2e_utils.get_with_retry(url)
            self.assertIn('Hello1 from the Cloud!', response.text)
Esempio n. 4
0
def _update(args):
    """Update the Django project on GKE."""
    update.main(args)