|
| 1 | +# Copyright 2021 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import mock |
| 18 | + |
| 19 | +from google.cloud.bigtable import enums |
| 20 | + |
| 21 | + |
| 22 | +EncryptionType = enums.EncryptionInfo.EncryptionType |
| 23 | +_STATUS_CODE = 123 |
| 24 | +_STATUS_MESSAGE = "message" |
| 25 | +_KMS_KEY_VERSION = 345 |
| 26 | + |
| 27 | + |
| 28 | +def _make_status_pb(code=_STATUS_CODE, message=_STATUS_MESSAGE): |
| 29 | + from google.rpc.status_pb2 import Status |
| 30 | + |
| 31 | + return Status(code=code, message=message) |
| 32 | + |
| 33 | + |
| 34 | +def _make_status(code=_STATUS_CODE, message=_STATUS_MESSAGE): |
| 35 | + from google.cloud.bigtable.error import Status |
| 36 | + |
| 37 | + status_pb = _make_status_pb(code=code, message=message) |
| 38 | + return Status(status_pb) |
| 39 | + |
| 40 | + |
| 41 | +def _make_info_pb( |
| 42 | + encryption_type=EncryptionType.GOOGLE_DEFAULT_ENCRYPTION, |
| 43 | + code=_STATUS_CODE, |
| 44 | + message=_STATUS_MESSAGE, |
| 45 | + kms_key_version=_KMS_KEY_VERSION, |
| 46 | +): |
| 47 | + encryption_status = _make_status_pb(code=code, message=message) |
| 48 | + |
| 49 | + spec = ["encryption_type", "encryption_status", "kms_key_version"] |
| 50 | + return mock.Mock( |
| 51 | + spec=spec, |
| 52 | + encryption_type=encryption_type, |
| 53 | + encryption_status=encryption_status, |
| 54 | + kms_key_version=kms_key_version, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class TestEncryptionInfo(unittest.TestCase): |
| 59 | + @staticmethod |
| 60 | + def _get_target_class(): |
| 61 | + from google.cloud.bigtable.encryption_info import EncryptionInfo |
| 62 | + |
| 63 | + return EncryptionInfo |
| 64 | + |
| 65 | + def _make_one(self, encryption_type, encryption_status, kms_key_version): |
| 66 | + return self._get_target_class()( |
| 67 | + encryption_type, encryption_status, kms_key_version, |
| 68 | + ) |
| 69 | + |
| 70 | + def _make_one_defaults( |
| 71 | + self, |
| 72 | + encryption_type=EncryptionType.GOOGLE_DEFAULT_ENCRYPTION, |
| 73 | + code=_STATUS_CODE, |
| 74 | + message=_STATUS_MESSAGE, |
| 75 | + kms_key_version=_KMS_KEY_VERSION, |
| 76 | + ): |
| 77 | + encryption_status = _make_status(code=code, message=message) |
| 78 | + return self._make_one(encryption_type, encryption_status, kms_key_version) |
| 79 | + |
| 80 | + def test__from_pb(self): |
| 81 | + klass = self._get_target_class() |
| 82 | + info_pb = _make_info_pb() |
| 83 | + |
| 84 | + info = klass._from_pb(info_pb) |
| 85 | + |
| 86 | + self.assertEqual( |
| 87 | + info.encryption_type, EncryptionType.GOOGLE_DEFAULT_ENCRYPTION, |
| 88 | + ) |
| 89 | + self.assertEqual(info.encryption_status.code, _STATUS_CODE) |
| 90 | + self.assertEqual(info.encryption_status.message, _STATUS_MESSAGE) |
| 91 | + self.assertEqual(info.kms_key_version, _KMS_KEY_VERSION) |
| 92 | + |
| 93 | + def test_ctor(self): |
| 94 | + encryption_type = EncryptionType.GOOGLE_DEFAULT_ENCRYPTION |
| 95 | + encryption_status = _make_status() |
| 96 | + |
| 97 | + info = self._make_one( |
| 98 | + encryption_type=encryption_type, |
| 99 | + encryption_status=encryption_status, |
| 100 | + kms_key_version=_KMS_KEY_VERSION, |
| 101 | + ) |
| 102 | + |
| 103 | + self.assertEqual(info.encryption_type, encryption_type) |
| 104 | + self.assertEqual(info.encryption_status, encryption_status) |
| 105 | + self.assertEqual(info.kms_key_version, _KMS_KEY_VERSION) |
| 106 | + |
| 107 | + def test___eq___identity(self): |
| 108 | + info = self._make_one_defaults() |
| 109 | + self.assertTrue(info == info) |
| 110 | + |
| 111 | + def test___eq___wrong_type(self): |
| 112 | + info = self._make_one_defaults() |
| 113 | + other = object() |
| 114 | + self.assertFalse(info == other) |
| 115 | + |
| 116 | + def test___eq___same_values(self): |
| 117 | + info = self._make_one_defaults() |
| 118 | + other = self._make_one_defaults() |
| 119 | + self.assertTrue(info == other) |
| 120 | + |
| 121 | + def test___eq___different_encryption_type(self): |
| 122 | + info = self._make_one_defaults() |
| 123 | + other = self._make_one_defaults( |
| 124 | + encryption_type=EncryptionType.CUSTOMER_MANAGED_ENCRYPTION, |
| 125 | + ) |
| 126 | + self.assertFalse(info == other) |
| 127 | + |
| 128 | + def test___eq___different_encryption_status(self): |
| 129 | + info = self._make_one_defaults() |
| 130 | + other = self._make_one_defaults(code=456) |
| 131 | + self.assertFalse(info == other) |
| 132 | + |
| 133 | + def test___eq___different_kms_key_version(self): |
| 134 | + info = self._make_one_defaults() |
| 135 | + other = self._make_one_defaults(kms_key_version=789) |
| 136 | + self.assertFalse(info == other) |
| 137 | + |
| 138 | + def test___ne___identity(self): |
| 139 | + info = self._make_one_defaults() |
| 140 | + self.assertFalse(info != info) |
| 141 | + |
| 142 | + def test___ne___wrong_type(self): |
| 143 | + info = self._make_one_defaults() |
| 144 | + other = object() |
| 145 | + self.assertTrue(info != other) |
| 146 | + |
| 147 | + def test___ne___same_values(self): |
| 148 | + info = self._make_one_defaults() |
| 149 | + other = self._make_one_defaults() |
| 150 | + self.assertFalse(info != other) |
| 151 | + |
| 152 | + def test___ne___different_encryption_type(self): |
| 153 | + info = self._make_one_defaults() |
| 154 | + other = self._make_one_defaults( |
| 155 | + encryption_type=EncryptionType.CUSTOMER_MANAGED_ENCRYPTION, |
| 156 | + ) |
| 157 | + self.assertTrue(info != other) |
| 158 | + |
| 159 | + def test___ne___different_encryption_status(self): |
| 160 | + info = self._make_one_defaults() |
| 161 | + other = self._make_one_defaults(code=456) |
| 162 | + self.assertTrue(info != other) |
| 163 | + |
| 164 | + def test___ne___different_kms_key_version(self): |
| 165 | + info = self._make_one_defaults() |
| 166 | + other = self._make_one_defaults(kms_key_version=789) |
| 167 | + self.assertTrue(info != other) |
0 commit comments