Skip to content

Commit b136ea6

Browse files
authored
add ability to pickle tftest instance objects (#56)
* add test_pickle.py * add skip duner method cond and __*state__ methods * add linting changes * linting changes attempt 2 * lint changes tftest.py
1 parent 33f21d5 commit b136ea6

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

test/test_pickle.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2019 Google LLC
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+
# https://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+
"Test serialization of tftest instances"
16+
17+
import pytest
18+
import tftest
19+
import pickle
20+
21+
22+
def assert_pickle_flow(obj):
23+
"""Ensures instance is the same after being pickled"""
24+
pickled_obj = pickle.dumps(obj)
25+
pickled_obj = pickle.loads(pickled_obj)
26+
assert isinstance(pickled_obj, type(obj))
27+
28+
29+
@pytest.fixture(scope="module")
30+
def tf(fixtures_dir):
31+
tf = tftest.TerraformTest("plan_no_resource_changes", fixtures_dir)
32+
tf.setup()
33+
return tf
34+
35+
36+
def test_setup(fixtures_dir):
37+
tf = tftest.TerraformTest("plan_no_resource_changes", fixtures_dir)
38+
expected = tf.setup()
39+
assert_pickle_flow(expected)
40+
41+
42+
def test_plan(tf):
43+
expected = tf.plan(output=True)
44+
assert_pickle_flow(expected)
45+
46+
47+
def test_apply(tf):
48+
expected = tf.apply()
49+
assert_pickle_flow(expected)
50+
51+
52+
def test_output(tf):
53+
expected = tf.output()
54+
assert_pickle_flow(expected)
55+
56+
57+
def test_state_pull(tf):
58+
expected = tf.state_pull()
59+
assert_pickle_flow(expected)

tftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def __init__(self, raw):
174174
self.sensitive = tuple(k for k, v in raw.items() if v.get('sensitive'))
175175

176176
def __getattr__(self, name):
177+
if isinstance(name, str) and name[:2] == name[-2:] == '__':
178+
# skip non-existing dunder method lookups
179+
raise AttributeError(name)
177180
return getattr(self._raw, name)
178181

179182
def __getitem__(self, name):
@@ -241,6 +244,12 @@ def modules(self):
241244
def __getattr__(self, name):
242245
return self._raw[name]
243246

247+
def __getstate__(self):
248+
return self.__dict__
249+
250+
def __setstate__(self, d):
251+
self.__dict__.update(d)
252+
244253

245254
class TerraformState(TerraformJSONBase):
246255
"Minimal wrapper for Terraform state JSON format."
@@ -264,6 +273,12 @@ def resources(self):
264273
def __getattr__(self, name):
265274
return self._raw[name]
266275

276+
def __getstate__(self):
277+
return self.__dict__
278+
279+
def __setstate__(self, d):
280+
self.__dict__.update(d)
281+
267282

268283
class TerraformTest(object):
269284
"""Helper class for use in testing Terraform modules.

0 commit comments

Comments
 (0)