Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): expose customer managed encryption key for ML models #9302

Prev Previous commit
Next Next commit
feat(bigquery): refactor test class of encryption configuration and c…
…hange location in key
  • Loading branch information
HemangChothani committed Oct 14, 2019
commit c70562293c3ab1dc84b90cb232e8db810a56aee6
2 changes: 1 addition & 1 deletion bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TestClient(unittest.TestCase):
TABLE_ID = "TABLE_ID"
MODEL_ID = "MODEL_ID"
TABLE_REF = DatasetReference(PROJECT, DS_ID).table(TABLE_ID)
KMS_KEY_NAME = "projects/1/locations/global/keyRings/1/cryptoKeys/1"
KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"
LOCATION = "us-central"

@staticmethod
Expand Down
97 changes: 97 additions & 0 deletions bigquery/tests/unit/test_encryption_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import unittest
tswast marked this conversation as resolved.
Show resolved Hide resolved
import mock


class TestEncryptionConfiguration(unittest.TestCase):
KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"

@staticmethod
def _get_target_class():
from google.cloud.bigquery.encryption_configuration import (
EncryptionConfiguration,
)

return EncryptionConfiguration

def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor_defaults(self):
encryption_config = self._make_one()
self.assertIsNone(encryption_config.kms_key_name)

def test_ctor_with_key(self):
encryption_config = self._make_one(kms_key_name=self.KMS_KEY_NAME)
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)

def test_kms_key_name_setter(self):
encryption_config = self._make_one()
self.assertIsNone(encryption_config.kms_key_name)
encryption_config.kms_key_name = self.KMS_KEY_NAME
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)
encryption_config.kms_key_name = None
self.assertIsNone(encryption_config.kms_key_name)

def test_from_api_repr(self):
RESOURCE = {"kmsKeyName": self.KMS_KEY_NAME}
klass = self._get_target_class()
encryption_config = klass.from_api_repr(RESOURCE)
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)

def test_to_api_repr(self):
encryption_config = self._make_one(kms_key_name=self.KMS_KEY_NAME)
resource = encryption_config.to_api_repr()
self.assertEqual(resource, {"kmsKeyName": self.KMS_KEY_NAME})

def test___eq___wrong_type(self):
encryption_config = self._make_one()
other = object()
self.assertNotEqual(encryption_config, other)
self.assertEqual(encryption_config, mock.ANY)

def test___eq___kms_key_name_mismatch(self):
encryption_config = self._make_one()
other = self._make_one(self.KMS_KEY_NAME)
self.assertNotEqual(encryption_config, other)

def test___eq___hit(self):
encryption_config = self._make_one(self.KMS_KEY_NAME)
other = self._make_one(self.KMS_KEY_NAME)
self.assertEqual(encryption_config, other)

def test___ne___wrong_type(self):
encryption_config = self._make_one()
other = object()
self.assertNotEqual(encryption_config, other)
self.assertEqual(encryption_config, mock.ANY)

def test___ne___same_value(self):
encryption_config1 = self._make_one(self.KMS_KEY_NAME)
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
# unittest ``assertEqual`` uses ``==`` not ``!=``.
comparison_val = encryption_config1 != encryption_config2
self.assertFalse(comparison_val)

def test___ne___different_values(self):
encryption_config1 = self._make_one()
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
self.assertNotEqual(encryption_config1, encryption_config2)

def test___hash__set_equality(self):
encryption_config1 = self._make_one(self.KMS_KEY_NAME)
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
set_one = {encryption_config1, encryption_config2}
set_two = {encryption_config1, encryption_config2}
self.assertEqual(set_one, set_two)

def test___hash__not_equals(self):
encryption_config1 = self._make_one()
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
set_one = {encryption_config1}
set_two = {encryption_config2}
self.assertNotEqual(set_one, set_two)

def test___repr__(self):
encryption_config = self._make_one(self.KMS_KEY_NAME)
expected = "EncryptionConfiguration({})".format(self.KMS_KEY_NAME)
self.assertEqual(repr(encryption_config), expected)
2 changes: 1 addition & 1 deletion bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ class _Base(object):
TABLE_ID = "table_id"
TABLE_REF = TableReference(DS_REF, TABLE_ID)
JOB_ID = "JOB_ID"
KMS_KEY_NAME = "projects/1/locations/global/keyRings/1/cryptoKeys/1"
KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"

def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
Expand Down
97 changes: 1 addition & 96 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,101 +70,6 @@ def _verifySchema(self, schema, resource):
self._verify_field(field, r_field)


class TestEncryptionConfiguration(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please keep one test to ensure that from google.cloud.bigquery.table import EncryptionConfiguration continues to work? I don't want to make this feature into breaking change.

KMS_KEY_NAME = "projects/1/locations/global/keyRings/1/cryptoKeys/1"

@staticmethod
def _get_target_class():
from google.cloud.bigquery.encryption_configuration import (
EncryptionConfiguration,
)

return EncryptionConfiguration

def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor_defaults(self):
encryption_config = self._make_one()
self.assertIsNone(encryption_config.kms_key_name)

def test_ctor_with_key(self):
encryption_config = self._make_one(kms_key_name=self.KMS_KEY_NAME)
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)

def test_kms_key_name_setter(self):
encryption_config = self._make_one()
self.assertIsNone(encryption_config.kms_key_name)
encryption_config.kms_key_name = self.KMS_KEY_NAME
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)
encryption_config.kms_key_name = None
self.assertIsNone(encryption_config.kms_key_name)

def test_from_api_repr(self):
RESOURCE = {"kmsKeyName": self.KMS_KEY_NAME}
klass = self._get_target_class()
encryption_config = klass.from_api_repr(RESOURCE)
self.assertEqual(encryption_config.kms_key_name, self.KMS_KEY_NAME)

def test_to_api_repr(self):
encryption_config = self._make_one(kms_key_name=self.KMS_KEY_NAME)
resource = encryption_config.to_api_repr()
self.assertEqual(resource, {"kmsKeyName": self.KMS_KEY_NAME})

def test___eq___wrong_type(self):
encryption_config = self._make_one()
other = object()
self.assertNotEqual(encryption_config, other)
self.assertEqual(encryption_config, mock.ANY)

def test___eq___kms_key_name_mismatch(self):
encryption_config = self._make_one()
other = self._make_one(self.KMS_KEY_NAME)
self.assertNotEqual(encryption_config, other)

def test___eq___hit(self):
encryption_config = self._make_one(self.KMS_KEY_NAME)
other = self._make_one(self.KMS_KEY_NAME)
self.assertEqual(encryption_config, other)

def test___ne___wrong_type(self):
encryption_config = self._make_one()
other = object()
self.assertNotEqual(encryption_config, other)
self.assertEqual(encryption_config, mock.ANY)

def test___ne___same_value(self):
encryption_config1 = self._make_one(self.KMS_KEY_NAME)
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
# unittest ``assertEqual`` uses ``==`` not ``!=``.
comparison_val = encryption_config1 != encryption_config2
self.assertFalse(comparison_val)

def test___ne___different_values(self):
encryption_config1 = self._make_one()
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
self.assertNotEqual(encryption_config1, encryption_config2)

def test___hash__set_equality(self):
encryption_config1 = self._make_one(self.KMS_KEY_NAME)
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
set_one = {encryption_config1, encryption_config2}
set_two = {encryption_config1, encryption_config2}
self.assertEqual(set_one, set_two)

def test___hash__not_equals(self):
encryption_config1 = self._make_one()
encryption_config2 = self._make_one(self.KMS_KEY_NAME)
set_one = {encryption_config1}
set_two = {encryption_config2}
self.assertNotEqual(set_one, set_two)

def test___repr__(self):
encryption_config = self._make_one(self.KMS_KEY_NAME)
expected = "EncryptionConfiguration({})".format(self.KMS_KEY_NAME)
self.assertEqual(repr(encryption_config), expected)


class TestTableReference(unittest.TestCase):
@staticmethod
def _get_target_class():
Expand Down Expand Up @@ -341,7 +246,7 @@ class TestTable(unittest.TestCase, _SchemaBase):
PROJECT = "prahj-ekt"
DS_ID = "dataset-name"
TABLE_NAME = "table-name"
KMS_KEY_NAME = "projects/1/locations/global/keyRings/1/cryptoKeys/1"
KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"

@staticmethod
def _get_target_class():
Expand Down