Debian Codegraph @

tests/test_cvdupdate.py

raw
1 import json
2 from pathlib import Path
3 import shutil
4
5 from tests.fixtures.revert import revert_homedir
6
7 from cvdupdate.cvdupdate import CVDUpdate
8
9 def test_instantiation(revert_homedir):
10 c = CVDUpdate()
11
12 def test_alternate_config_locations(revert_homedir, tmp_path):
13 ''' Test that we can save config and state files to alternative locations '''
14 # ensure we're starting with a clean slate
15 default_cvdupdate_dir = Path.home() / '.cvdupdate'
16 assert not default_cvdupdate_dir.exists()
17
18 # set the config file to be in pytests's /tmp/pytest-*
19 config_file_path = tmp_path / 'config.json'
20 c = CVDUpdate(config=config_file_path)
21
22 # verify the file is created and has default config data inside
23 assert config_file_path.exists()
24 txt = config_file_path.read_text()
25 assert txt
26 config_file_json = json.loads(txt)
27 assert config_file_json == c.config
28 # state file value will differ, so blank it out for comparing
29 c.config['state file'] = ''
30 assert c.config == c.default_config
31
32 # verify the state file is created and has default data inside
33 state_file_path = tmp_path / 'state.json'
34 assert state_file_path.exists()
35 txt = state_file_path.read_text()
36 assert txt
37 state_file_json = json.loads(txt)
38 assert state_file_json == c.state
39 # again, uuid will differ, so toss it out
40 del c.state['uuid']
41 assert c.state == c.default_state
42
43 # ~/.cvdupdate exists, because we haven't changed the logdir location
44 # but that's all it should have in it
45 default_cvdupdate_dir = Path.home() / '.cvdupdate'
46 assert default_cvdupdate_dir.exists()
47 children = list(default_cvdupdate_dir.iterdir())
48 assert len(children) == 1
49 assert children[0] == default_cvdupdate_dir / 'logs'
50
51
52 def test_default_config_not_mutated(revert_homedir, tmp_path):
53 ''' default_config and default_state are both class-level attributes
54 Ensure that when we copy these, we are actually copying them and not simply reassigning
55 note that this typically won't be a problem in normal usage,
56 but it was a problem during testing and was really annoying to track down
57 '''
58 a = CVDUpdate()
59 config_file_path = tmp_path / 'config.json'
60 # set the config file to be in pytests /tmp/pytest-*
61 b = CVDUpdate(config=config_file_path)
62
63 assert all(val == b.config[key] for key,val in a.config.items() if key != 'state file')
64 assert id(a.config) != id(b.config)
65 assert id(a.default_config) == id(b.default_config) == id(CVDUpdate.default_config)
66 assert a.state != b.state
67 assert id(a.default_state) == id(b.default_state)
68
69
70 def test_existing_state_migrates_successfully(revert_homedir):
71 ''' specifically test migrating an existing config.json to config + state.json'''
72 default_cvdupdate_dir = Path.home() / '.cvdupdate'
73 default_cvdupdate_dir.mkdir(parents=True)
74
75 # create a .cvdupdate/config.json which also contains dbs definitions
76 old_config_file = 'tests/files/v1.0.2.config.json'
77 old_config_json = json.loads(Path(old_config_file).read_text())
78 old_config_json["log directory"] = str(default_cvdupdate_dir / 'logs')
79 old_config_json["db directory"] = str(default_cvdupdate_dir / 'database')
80
81 with (default_cvdupdate_dir / 'config.json').open('w') as test_config:
82 json.dump(old_config_json, test_config)
83
84 # create cvdupdate object, which will read config.json and split state into state.json
85 a = CVDUpdate()
86
87 new_config_json = old_config_json
88
89 # create expected state.json contents by copying the bits that move
90 new_state_json = {}
91 new_state_json['dbs'] = old_config_json['dbs']
92 new_state_json['uuid'] = old_config_json['uuid']
93
94 # new update the config.json contents
95 del new_config_json['dbs']
96 del new_config_json['uuid']
97 new_config_json['state file'] = str(default_cvdupdate_dir / 'state.json')
98
99 # compare actual result with expected transform
100 with open(default_cvdupdate_dir / 'config.json') as config:
101 assert new_config_json == json.loads(config.read())
102 with open(default_cvdupdate_dir / 'state.json') as state:
103 from pprint import pprint
104 assert new_state_json == json.loads(state.read())