<s> import datetime <EOL> import logging <EOL> import mock <EOL> import os <EOL> import unittest2 <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> from compass . utils import setting_wrapper as setting <EOL> reload ( setting ) <EOL> from base import BaseTest <EOL> from compass . db . api import adapter as adapter_api <EOL> from compass . db . api import adapter_holder as adapter <EOL> from compass . db . api import database <EOL> from compass . db . api import metadata as metadata_api <EOL> from compass . db . api import metadata_holder as metadata <EOL> from compass . db . api import user as user_api <EOL> from compass . db import exception <EOL> from compass . utils import flags <EOL> from compass . utils import logsetting <EOL> from compass . utils import util <EOL> class MetadataTestCase ( unittest2 . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( MetadataTestCase , self ) . setUp ( ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> os . environ [ '<STR_LIT>' ] = os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> '<STR_LIT:data>' <EOL> ) <EOL> reload ( setting ) <EOL> database . init ( '<STR_LIT>' ) <EOL> database . create_db ( ) <EOL> adapter . load_adapters ( force_reload = True ) <EOL> metadata . load_metadatas ( force_reload = True ) <EOL> adapter . load_flavors ( force_reload = True ) <EOL> self . user_object = ( <EOL> user_api . get_user_object ( <EOL> setting . COMPASS_ADMIN_EMAIL <EOL> ) <EOL> ) <EOL> self . adapter_object = adapter . list_adapters ( self . user_object ) <EOL> test_adapter = None <EOL> for adapter_obj in self . adapter_object : <EOL> if adapter_obj [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> self . adapter_id = adapter_obj [ '<STR_LIT:id>' ] <EOL> test_adapter = adapter_obj <EOL> break <EOL> self . os_id = None <EOL> if test_adapter [ '<STR_LIT>' ] : <EOL> for supported_os in test_adapter [ '<STR_LIT>' ] : <EOL> self . os_id = supported_os [ '<STR_LIT>' ] <EOL> break <EOL> for flavor in test_adapter [ '<STR_LIT>' ] : <EOL> if flavor [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> self . flavor_id = flavor [ '<STR_LIT:id>' ] <EOL> break <EOL> def tearDown ( self ) : <EOL> super ( MetadataTestCase , self ) . setUp ( ) <EOL> reload ( setting ) <EOL> database . drop_db ( ) <EOL> class TestGetPackageMetadata ( MetadataTestCase ) : <EOL> def setUp ( self ) : <EOL> self . backup_load_configs = util . load_configs <EOL> def mock_load_configs ( config_dir , * args , ** kwargs ) : <EOL> if config_dir != setting . PACKAGE_METADATA_DIR : <EOL> return self . backup_load_configs ( <EOL> config_dir , * args , ** kwargs <EOL> ) <EOL> config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:username>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT:username>' , <EOL> '<STR_LIT>' : '<STR_LIT:username>' <EOL> } <EOL> } , <EOL> '<STR_LIT:password>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' <EOL> } <EOL> } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } <EOL> } <EOL> return [ config ] <EOL> util . load_configs = mock . Mock ( side_effect = mock_load_configs ) <EOL> super ( TestGetPackageMetadata , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> util . load_configs = self . backup_load_configs <EOL> super ( TestGetPackageMetadata , self ) . tearDown ( ) <EOL> def test_get_package_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> package_metadata = metadata . get_package_metadata ( <EOL> self . adapter_id , <EOL> user = self . user_object <EOL> ) <EOL> expected = [ ] <EOL> for k , v in package_metadata [ '<STR_LIT>' ] . iteritems ( ) : <EOL> expected . append ( k ) <EOL> self . assertIsNotNone ( package_metadata ) <EOL> self . assertIn ( '<STR_LIT>' , expected ) <EOL> def test_adapter_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> exception . RecordNotExists , <EOL> metadata . get_package_metadata , <EOL> <NUM_LIT> , <EOL> user = self . user_object <EOL> ) <EOL> class TestGetOsMetadata ( MetadataTestCase ) : <EOL> def setUp ( self ) : <EOL> self . backup_load_configs = util . load_configs <EOL> def mock_load_configs ( config_dir , * args , ** kwargs ) : <EOL> if config_dir != setting . OS_METADATA_DIR : <EOL> return self . backup_load_configs ( <EOL> config_dir , * args , ** kwargs <EOL> ) <EOL> config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:test>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } <EOL> } <EOL> return [ config ] <EOL> util . load_configs = mock . Mock ( side_effect = mock_load_configs ) <EOL> super ( TestGetOsMetadata , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> util . load_configs = self . backup_load_configs <EOL> super ( TestGetOsMetadata , self ) . tearDown ( ) <EOL> def test_get_os_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> os_metadata = metadata . get_os_metadata ( <EOL> self . os_id , <EOL> user = self . user_object <EOL> ) <EOL> expected = [ ] <EOL> for k , v in os_metadata [ '<STR_LIT>' ] . iteritems ( ) : <EOL> expected . append ( k ) <EOL> self . assertIsNotNone ( os_metadata ) <EOL> self . assertIn ( '<STR_LIT>' , expected ) <EOL> def test_os_non_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> exception . RecordNotExists , <EOL> metadata . get_os_metadata , <EOL> <NUM_LIT> , <EOL> user = self . user_object <EOL> ) <EOL> class TestGetFlavorMetadata ( MetadataTestCase ) : <EOL> def setUp ( self ) : <EOL> self . backup_load_configs = util . load_configs <EOL> def mock_load_configs ( config_dir , * args , ** kwargs ) : <EOL> if config_dir != setting . FLAVOR_METADATA_DIR : <EOL> return self . backup_load_configs ( <EOL> config_dir , * args , ** kwargs <EOL> ) <EOL> config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } <EOL> } <EOL> } <EOL> return [ config ] <EOL> util . load_configs = mock . Mock ( side_effect = mock_load_configs ) <EOL> super ( TestGetFlavorMetadata , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> util . load_configs = self . backup_load_configs <EOL> super ( TestGetFlavorMetadata , self ) . tearDown ( ) <EOL> def test_get_flavor_metadata ( self ) : <EOL> flavor_metadata = metadata . get_flavor_metadata ( <EOL> self . flavor_id , <EOL> user = self . user_object <EOL> ) <EOL> self . assertIsNotNone ( flavor_metadata ) <EOL> self . assertTrue ( <EOL> '<STR_LIT>' in flavor_metadata [ '<STR_LIT>' ] . keys ( ) <EOL> ) <EOL> class TestGetPackageOsMetadata ( MetadataTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestGetPackageOsMetadata , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestGetPackageOsMetadata , self ) . tearDown ( ) <EOL> def test_get_package_os_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> package_os_metadata = metadata . get_package_os_metadata ( <EOL> self . adapter_id , <EOL> self . os_id , <EOL> user = self . user_object <EOL> ) <EOL> self . assertIsNotNone ( package_os_metadata ) <EOL> def test_invalid_parameter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> exception . InvalidParameter , <EOL> metadata . get_package_os_metadata , <EOL> self . adapter_id , <EOL> <NUM_LIT> , <EOL> user = self . user_object <EOL> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> flags . init ( ) <EOL> logsetting . init ( ) <EOL> unittest2 . main ( ) </s>
