<commit_before>"""
This module provides testing functionality of the BigTable Init Action.

Test logic:
1. Create test table and fill it with some data by injecting commands into hbase shell.
2. Validate from local station that BigTable has test table created with right data.

Note:
    Test REQUIRES cbt tool installed which provides CLI access to BigTable instances.
    See: https://cloud.google.com/bigtable/docs/cbt-overview
"""
import os
import pkg_resources

from absl.testing import absltest
from absl.testing import parameterized

from integration_tests.dataproc_test_case import DataprocTestCase


class BigTableTestCase(DataprocTestCase):
    COMPONENT = 'bigtable'
    INIT_ACTIONS = ['bigtable/bigtable.sh']
    TEST_SCRIPT_FILE_NAME = "run_hbase_commands.py"

    def __init__(self, method_name='runTest'):
        super().__init__(method_name)
        self.metadata = None
        self.db_name = None
        self.bigtable_zone = "us-central1-f"

    def setUp(self):
        super().setUp()
        self.db_name = "test-bt-{}-{}".format(self.datetime_str(),
                                              self.random_str())
        self.metadata = "bigtable-instance={},bigtable-project={}".format(
            self.db_name, self.PROJECT)

        self.assert_command(
            'gcloud bigtable instances create {}'
            ' --cluster {} --cluster-zone {}'
            ' --display-name={} --instance-type=DEVELOPMENT'.format(
                self.db_name, self.db_name, self.bigtable_zone, self.db_name))

    def tearDown(self):
        super().tearDown()
        self.assert_command('gcloud bigtable instances delete {}'.format(
            self.db_name))

    def _validate_bigtable(self):
        _, stdout, _ = self.assert_command(
            'cbt -instance {} count test-bigtable '.format(self.db_name))
        self.assertEqual(
            int(float(stdout)), 4, "Invalid BigTable instance count")

    def verify_instance(self, name):
        self.upload_test_file(
            os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                self.TEST_SCRIPT_FILE_NAME), name)
        self.assert_instance_command(
            name, "python {}".format(self.TEST_SCRIPT_FILE_NAME))
        self._validate_bigtable()

    @parameterized.parameters(
        ("SINGLE", ["m"]),
        ("STANDARD", ["m"]),
        ("HA", ["m-0"]),
    )
    def test_bigtable(self, configuration, machine_suffixes):
        if self.getImageOs() == 'centos':
            self.skipTest("Not supported in CentOS-based images")

        if self.getImageVersion() >= pkg_resources.parse_version("2.0"):
            self.skipTest("Not supported in the 2.0+ images")

        self.createCluster(
            configuration, self.INIT_ACTIONS, metadata=self.metadata)

        for machine_suffix in machine_suffixes:
            self.verify_instance("{}-{}".format(self.getClusterName(),
                                                machine_suffix))


if __name__ == '__main__':
    absltest.main()<commit_msg>Update test_bigtable.py (#924)<commit_after>"""
This module provides testing functionality of the BigTable Init Action.

Test logic:
1. Create test table and fill it with some data by injecting commands into hbase shell.
2. Validate from local station that BigTable has test table created with right data.

Note:
    Test REQUIRES cbt tool installed which provides CLI access to BigTable instances.
    See: https://cloud.google.com/bigtable/docs/cbt-overview
"""
import os
import pkg_resources

from absl.testing import absltest
from absl.testing import parameterized

from integration_tests.dataproc_test_case import DataprocTestCase


class BigTableTestCase(DataprocTestCase):
    COMPONENT = 'bigtable'
    INIT_ACTIONS = ['bigtable/bigtable.sh']
    TEST_SCRIPT_FILE_NAME = "run_hbase_commands.py"

    def __init__(self, method_name='runTest'):
        super().__init__(method_name)
        self.metadata = None
        self.db_name = None
        self.bigtable_zone = "us-central1-f"

    def setUp(self):
        super().setUp()
        self.db_name = "test-bt-{}-{}".format(self.datetime_str(),
                                              self.random_str())
        self.metadata = "bigtable-instance={},bigtable-project={}".format(
            self.db_name, self.PROJECT)

        self.assert_command(
            'gcloud bigtable instances create {}'
            ' --cluster {} --cluster-zone {}'
            ' --display-name={} --instance-type=DEVELOPMENT'.format(
                self.db_name, self.db_name, self.bigtable_zone, self.db_name))

    def tearDown(self):
        super().tearDown()
        self.assert_command('gcloud bigtable instances delete {}'.format(
            self.db_name))

    def _validate_bigtable(self):
        _, stdout, _ = self.assert_command(
            'cbt -instance {} count test-bigtable '.format(self.db_name))
        self.assertEqual(
            int(float(stdout)), 4, "Invalid BigTable instance count")

    def verify_instance(self, name):
        self.upload_test_file(
            os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                self.TEST_SCRIPT_FILE_NAME), name)
        self.assert_instance_command(
            name, "python {}".format(self.TEST_SCRIPT_FILE_NAME))
        self._validate_bigtable()

    @parameterized.parameters(
        ("SINGLE", ["m"]),
        ("STANDARD", ["m"]),
        ("HA", ["m-0"]),
    )
    def test_bigtable(self, configuration, machine_suffixes):
        if self.getImageVersion() >= pkg_resources.parse_version("2.0"):
            self.skipTest("Not supported in the 2.0+ images")

        self.createCluster(
            configuration, self.INIT_ACTIONS, metadata=self.metadata)

        for machine_suffix in machine_suffixes:
            self.verify_instance("{}-{}".format(self.getClusterName(),
                                                machine_suffix))


if __name__ == '__main__':
    absltest.main()
{'fraction_non_alphanumeric': 0.11507671781187458, 'fraction_numerical': 0.003502334889926618, 'mean_word_length': 2.542232722976964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7215905', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1704, 'n_words': 383}