|
| 1 | +# Copyright 2015 Google Inc. 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 unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class TestChanges(unittest2.TestCase): |
| 19 | + |
| 20 | + def _getTargetClass(self): |
| 21 | + from gcloud.dns.changes import Changes |
| 22 | + return Changes |
| 23 | + |
| 24 | + def _makeOne(self, *args, **kw): |
| 25 | + return self._getTargetClass()(*args, **kw) |
| 26 | + |
| 27 | + def test_ctor(self): |
| 28 | + zone = _Zone() |
| 29 | + |
| 30 | + changes = self._makeOne(zone) |
| 31 | + |
| 32 | + self.assertTrue(changes.zone is zone) |
| 33 | + self.assertEqual(changes.name, None) |
| 34 | + self.assertEqual(changes.status, None) |
| 35 | + self.assertEqual(changes.started, None) |
| 36 | + self.assertEqual(list(changes.additions), []) |
| 37 | + self.assertEqual(list(changes.deletions), []) |
| 38 | + |
| 39 | + def test_from_api_repr_missing_additions_deletions(self): |
| 40 | + from gcloud._helpers import UTC |
| 41 | + from gcloud._helpers import _NOW |
| 42 | + from gcloud._helpers import _RFC3339_MICROS |
| 43 | + zone = _Zone() |
| 44 | + NAME = 'CHANGES_ID' |
| 45 | + WHEN = _NOW().replace(tzinfo=UTC) |
| 46 | + WHEN_STR = WHEN.strftime(_RFC3339_MICROS) |
| 47 | + RESOURCE = { |
| 48 | + 'kind': 'dns#change', |
| 49 | + 'id': NAME, |
| 50 | + 'status': 'pending', |
| 51 | + 'startTime': WHEN_STR, |
| 52 | + } |
| 53 | + zone = _Zone() |
| 54 | + klass = self._getTargetClass() |
| 55 | + changes = klass.from_api_repr(RESOURCE, zone=zone) |
| 56 | + |
| 57 | + self.assertEqual(changes.name, NAME) |
| 58 | + self.assertEqual(changes.started, WHEN) |
| 59 | + self.assertEqual(changes.status, 'pending') |
| 60 | + |
| 61 | + self.assertEqual(len(changes.additions), 0) |
| 62 | + self.assertEqual(len(changes.deletions), 0) |
| 63 | + |
| 64 | + def test_from_api_repr(self): |
| 65 | + from gcloud._helpers import UTC |
| 66 | + from gcloud._helpers import _NOW |
| 67 | + from gcloud._helpers import _RFC3339_MICROS |
| 68 | + NAME = 'CHANGES_ID' |
| 69 | + WHEN = _NOW().replace(tzinfo=UTC) |
| 70 | + WHEN_STR = WHEN.strftime(_RFC3339_MICROS) |
| 71 | + RESOURCE = { |
| 72 | + 'kind': 'dns#change', |
| 73 | + 'id': NAME, |
| 74 | + 'startTime': WHEN_STR, |
| 75 | + 'status': 'done', |
| 76 | + 'additions' : [ |
| 77 | + {'name': 'test.example.com', |
| 78 | + 'type': 'CNAME', |
| 79 | + 'ttl': '3600', |
| 80 | + 'rrdatas': ['www.example.com']}, |
| 81 | + ], |
| 82 | + 'deletions': [ |
| 83 | + {'name': 'test.example.com', |
| 84 | + 'type': 'CNAME', |
| 85 | + 'ttl': '86400', |
| 86 | + 'rrdatas': ['other.example.com']}, |
| 87 | + ], |
| 88 | + } |
| 89 | + zone = _Zone() |
| 90 | + klass = self._getTargetClass() |
| 91 | + changes = klass.from_api_repr(RESOURCE, zone=zone) |
| 92 | + |
| 93 | + self.assertEqual(changes.name, NAME) |
| 94 | + self.assertEqual(changes.started, WHEN) |
| 95 | + self.assertEqual(changes.status, 'done') |
| 96 | + |
| 97 | + self.assertEqual(len(changes.additions), 1) |
| 98 | + rrs = changes.additions[0] |
| 99 | + self.assertEqual(rrs.name, 'test.example.com') |
| 100 | + self.assertEqual(rrs.record_type, 'CNAME') |
| 101 | + self.assertEqual(rrs.ttl, 3600) |
| 102 | + self.assertEqual(rrs.rrdatas, ['www.example.com']) |
| 103 | + self.assertTrue(rrs.zone is zone) |
| 104 | + |
| 105 | + self.assertEqual(len(changes.deletions), 1) |
| 106 | + rrs = changes.deletions[0] |
| 107 | + self.assertEqual(rrs.name, 'test.example.com') |
| 108 | + self.assertEqual(rrs.record_type, 'CNAME') |
| 109 | + self.assertEqual(rrs.ttl, 86400) |
| 110 | + self.assertEqual(rrs.rrdatas, ['other.example.com']) |
| 111 | + self.assertTrue(rrs.zone is zone) |
| 112 | + |
| 113 | + def test_add_record_set_invalid_value(self): |
| 114 | + zone = _Zone() |
| 115 | + changes = self._makeOne(zone) |
| 116 | + |
| 117 | + with self.assertRaises(ValueError): |
| 118 | + changes.add_record_set(object()) |
| 119 | + |
| 120 | + def test_add_record_set(self): |
| 121 | + from gcloud.dns.resource_record_set import ResourceRecordSet |
| 122 | + zone = _Zone() |
| 123 | + changes = self._makeOne(zone) |
| 124 | + rrs = ResourceRecordSet('test.example.com', 'CNAME', 3600, |
| 125 | + ['www.example.com'], zone) |
| 126 | + changes.add_record_set(rrs) |
| 127 | + self.assertEqual(list(changes.additions), [rrs]) |
| 128 | + |
| 129 | + def test_delete_record_set_invalid_value(self): |
| 130 | + zone = _Zone() |
| 131 | + changes = self._makeOne(zone) |
| 132 | + |
| 133 | + with self.assertRaises(ValueError): |
| 134 | + changes.delete_record_set(object()) |
| 135 | + |
| 136 | + def test_delete_record_set(self): |
| 137 | + from gcloud.dns.resource_record_set import ResourceRecordSet |
| 138 | + zone = _Zone() |
| 139 | + changes = self._makeOne(zone) |
| 140 | + rrs = ResourceRecordSet('test.example.com', 'CNAME', 3600, |
| 141 | + ['www.example.com'], zone) |
| 142 | + changes.delete_record_set(rrs) |
| 143 | + self.assertEqual(list(changes.deletions), [rrs]) |
| 144 | + |
| 145 | + |
| 146 | +class _Zone(object): |
| 147 | + pass |
0 commit comments