4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
29 from rift
.tasklets
.rwstagingmgr
.store
import StagingFileStore
30 from rift
.mano
.utils
.project
import ManoProject
, DEFAULT_PROJECT
33 gi
.require_version('RwStagingMgmtYang', '1.0')
34 from gi
.repository
import (
38 class MockTasklet(object):
40 self
.log
= logging
.getLogger()
42 project
= ManoProject(self
.log
, name
=DEFAULT_PROJECT
)
43 project
.publisher
= None
44 self
.projects
[project
.name
] = project
46 def set_delegate(self
, store
):
47 self
.projects
[DEFAULT_PROJECT
].publisher
= store
50 class TestSerializer(unittest
.TestCase
):
52 def test_staging_area_create(self
):
54 1. Verify a valid id is created
55 2. if the folder and meta files were created.
56 3. Verify if the meta file has been created.
59 tmp_dir
= tempfile
.mkdtemp()
60 tasklet
= MockTasklet()
61 store
= StagingFileStore(tasklet
, root_dir
=tmp_dir
)
63 mock_model
= RwStagingMgmtYang
.StagingArea
.from_dict({})
64 stg
= store
.create_staging_area(mock_model
)
65 mock_id
= stg
.model
.area_id
67 assert mock_id
== store
.get_staging_area(mock_id
).model
.area_id
68 area_path
= os
.path
.join(store
.root_dir
, mock_id
)
70 assert os
.path
.isdir(area_path
)
72 def test_staging_area_remove(self
):
74 1. Verify a valid id is created
75 2. if the folder and meta files were created.
76 3. Verify if the meta file has been created.
79 tmp_dir
= tempfile
.mkdtemp()
80 tasklet
= MockTasklet()
81 store
= StagingFileStore(tasklet
, root_dir
=tmp_dir
)
83 mock_model
= RwStagingMgmtYang
.StagingArea
.from_dict({})
84 # get the wrapped mock model
85 mock_model
= store
.create_staging_area(mock_model
)
86 mock_id
= mock_model
.model
.area_id
87 area_path
= os
.path
.join(store
.root_dir
, mock_id
)
89 # check if dir actually exists
90 assert os
.path
.isdir(area_path
)
91 store
.remove_staging_area(mock_model
)
93 assert not os
.path
.isdir(area_path
)
95 def main(argv
=sys
.argv
[1:]):
96 logging
.basicConfig(format
='TEST %(message)s')
98 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
99 parser
= argparse
.ArgumentParser()
100 parser
.add_argument('-v', '--verbose', action
='store_true')
101 parser
.add_argument('-n', '--no-runner', action
='store_true')
103 args
, unknown
= parser
.parse_known_args(argv
)
107 # Set the global logging level
108 logging
.getLogger().setLevel(logging
.DEBUG
if args
.verbose
else logging
.ERROR
)
110 # The unittest framework requires a program name, so use the name of this
111 # file instead (we do not want to have to pass a fake program name to main
112 # when this is called from the interpreter).
113 unittest
.main(argv
=[__file__
] + unknown
+ ["-v"], testRunner
=runner
)
115 if __name__
== '__main__':