1
1
import logging
2
2
import os
3
+ import shutil
3
4
import sys
5
+ import tempfile
4
6
import unittest
5
7
6
8
from tuf .api import metadata
11
13
logger = logging .getLogger (__name__ )
12
14
13
15
class TestMetadataBundle (unittest .TestCase ):
16
+ @classmethod
17
+ def setUpClass (cls ):
18
+ cls .temporary_directory = tempfile .mkdtemp (dir = os .getcwd ())
19
+
20
+ @classmethod
21
+ def tearDownClass (cls ):
22
+ shutil .rmtree (cls .temporary_directory )
23
+
24
+ def setUp (self ):
25
+ # copy metadata to "local repo"
26
+ shutil .copytree (
27
+ os .path .join (os .getcwd (), 'repository_data' , 'repository' , 'metadata' ),
28
+ self .temporary_directory ,
29
+ dirs_exist_ok = True
30
+ )
31
+
14
32
def test_local_load (self ):
15
- repo_dir = os .path .join (os .getcwd (), 'repository_data' , 'repository' , 'metadata' )
16
33
17
34
# test loading all local metadata succesfully
18
- bundle = MetadataBundle (repo_dir )
35
+ bundle = MetadataBundle (self . temporary_directory )
19
36
bundle .root_update_finished ()
20
37
self .assertTrue (bundle .load_local_timestamp ())
21
38
self .assertTrue (bundle .load_local_snapshot ())
@@ -24,7 +41,7 @@ def test_local_load(self):
24
41
self .assertTrue (bundle .load_local_delegated_targets ('role2' ,'role1' ))
25
42
26
43
# Make sure loading metadata without its "dependencies" fails
27
- bundle = MetadataBundle (repo_dir )
44
+ bundle = MetadataBundle (self . temporary_directory )
28
45
29
46
with self .assertRaises (RuntimeError ):
30
47
bundle .load_local_timestamp ()
@@ -43,6 +60,42 @@ def test_local_load(self):
43
60
self .assertTrue (bundle .load_local_delegated_targets ('role1' ,'targets' ))
44
61
self .assertTrue (bundle .load_local_delegated_targets ('role2' ,'role1' ))
45
62
63
+ def test_update (self ):
64
+ remote_dir = os .path .join (os .getcwd (), 'repository_data' , 'repository' , 'metadata' )
65
+
66
+ # remove all but root.json from local repo
67
+ os .remove (os .path .join (self .temporary_directory , "timestamp.json" ))
68
+ os .remove (os .path .join (self .temporary_directory , "snapshot.json" ))
69
+ os .remove (os .path .join (self .temporary_directory , "targets.json" ))
70
+ os .remove (os .path .join (self .temporary_directory , "role1.json" ))
71
+ os .remove (os .path .join (self .temporary_directory , "role2.json" ))
72
+
73
+ # test updating metadata succesfully
74
+ bundle = MetadataBundle (self .temporary_directory )
75
+ bundle .root_update_finished ()
76
+
77
+ with open (os .path .join (remote_dir , "timestamp.json" ), "rb" ) as f :
78
+ bundle .update_timestamp (f .read ())
79
+ with open (os .path .join (remote_dir , "snapshot.json" ), "rb" ) as f :
80
+ bundle .update_snapshot (f .read ())
81
+ with open (os .path .join (remote_dir , "targets.json" ), "rb" ) as f :
82
+ bundle .update_targets (f .read ())
83
+ with open (os .path .join (remote_dir , "role1.json" ), "rb" ) as f :
84
+ bundle .update_delegated_targets (f .read (), "role1" , "targets" )
85
+ with open (os .path .join (remote_dir , "role2.json" ), "rb" ) as f :
86
+ bundle .update_delegated_targets (f .read (), "role2" , "role1" )
87
+
88
+ # test loading the metadata (that should now be locally available)
89
+ bundle = MetadataBundle (self .temporary_directory )
90
+ bundle .root_update_finished ()
91
+ self .assertTrue (bundle .load_local_timestamp ())
92
+ self .assertTrue (bundle .load_local_snapshot ())
93
+ self .assertTrue (bundle .load_local_targets ())
94
+ self .assertTrue (bundle .load_local_delegated_targets ('role1' ,'targets' ))
95
+ self .assertTrue (bundle .load_local_delegated_targets ('role2' ,'role1' ))
96
+
97
+ # TODO test loading one version, then updating to new versions of each metadata
98
+
46
99
47
100
if __name__ == '__main__' :
48
101
utils .configure_test_logging (sys .argv )
0 commit comments