Exemplo n.º 1
0
 def test_cache_token(self, m_sh):
     token = 'TOKEN VALUE'
     m_sh.return_value = token
     OpenStack.token = None
     o = OpenStack()
     #
     # Only for OVH
     #
     o.provider = 'something'
     assert False == o.cache_token()
     o.provider = 'ovh'
     #
     # Set the environment with the token
     #
     assert 'OS_TOKEN_VALUE' not in os.environ
     assert 'OS_TOKEN_EXPIRES' not in os.environ
     assert True == o.cache_token()
     m_sh.assert_called_with('openstack -q token issue -c id -f value')
     assert token == os.environ['OS_TOKEN_VALUE']
     assert token == OpenStack.token
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires
     #
     # Reset after it expires
     #
     token_expires = int(time.time()) - 2000
     OpenStack.token_expires = token_expires
     assert True == o.cache_token()
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires
Exemplo n.º 2
0
 def test_cache_token(self, m_sh):
     token = 'TOKEN VALUE'
     m_sh.return_value = token
     OpenStack.token = None
     o = OpenStack()
     #
     # Only for OVH
     #
     o.provider = 'something'
     assert False == o.cache_token()
     o.provider = 'ovh'
     #
     # Set the environment with the token
     #
     assert 'OS_TOKEN_VALUE' not in os.environ
     assert 'OS_TOKEN_EXPIRES' not in os.environ
     assert True == o.cache_token()
     m_sh.assert_called_with('openstack -q token issue -c id -f value')
     assert token == os.environ['OS_TOKEN_VALUE']
     assert token == OpenStack.token
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires
     #
     # Reset after it expires
     #
     token_expires = int(time.time()) - 2000
     OpenStack.token_expires = token_expires
     assert True == o.cache_token()
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires
Exemplo n.º 3
0
 def test_cache_token_from_environment(self, m_sh):
     OpenStack.token = None
     o = OpenStack()
     o.provider = 'ovh'
     token = 'TOKEN VALUE'
     os.environ['OS_TOKEN_VALUE'] = token
     token_expires = int(time.time()) + OpenStack.token_cache_duration
     os.environ['OS_TOKEN_EXPIRES'] = str(token_expires)
     assert True == o.cache_token()
     assert token == OpenStack.token
     assert token_expires == OpenStack.token_expires
     m_sh.assert_not_called()
Exemplo n.º 4
0
 def test_cache_token_from_environment(self, m_sh):
     OpenStack.token = None
     o = OpenStack()
     o.provider = 'ovh'
     token = 'TOKEN VALUE'
     os.environ['OS_TOKEN_VALUE'] = token
     token_expires = int(time.time()) + OpenStack.token_cache_duration
     os.environ['OS_TOKEN_EXPIRES'] = str(token_expires)
     assert True == o.cache_token()
     assert token == OpenStack.token
     assert token_expires == OpenStack.token_expires
     m_sh.assert_not_called()
Exemplo n.º 5
0
 def test_get_os_url(self):
     o = OpenStack()
     #
     # Only for OVH
     #
     o.provider = 'something'
     assert "" == o.get_os_url("server ")
     o.provider = 'ovh'
     assert "" == o.get_os_url("unknown ")
     type2cmd = {
         'compute': ('server', 'flavor'),
         'network': ('ip', 'security', 'network'),
         'image': ('image',),
         'volume': ('volume',),
     }
     os.environ['OS_REGION_NAME'] = 'REGION'
     os.environ['OS_TENANT_ID'] = 'TENANT'
     for (type, cmds) in type2cmd.iteritems():
         for cmd in cmds:
             assert ("//" + type) in o.get_os_url(cmd + " ")
     for type in type2cmd.keys():
         assert ("//" + type) in o.get_os_url("whatever ", type=type)
Exemplo n.º 6
0
 def test_get_os_url(self):
     o = OpenStack()
     #
     # Only for OVH
     #
     o.provider = 'something'
     assert "" == o.get_os_url("server ")
     o.provider = 'ovh'
     assert "" == o.get_os_url("unknown ")
     type2cmd = {
         'compute': ('server', 'flavor'),
         'network': ('ip', 'security', 'network'),
         'image': ('image',),
         'volume': ('volume',),
     }
     os.environ['OS_REGION_NAME'] = 'REGION'
     os.environ['OS_TENANT_ID'] = 'TENANT'
     for (type, cmds) in type2cmd.iteritems():
         for cmd in cmds:
             assert ("//" + type) in o.get_os_url(cmd + " ")
     for type in type2cmd.keys():
         assert ("//" + type) in o.get_os_url("whatever ", type=type)
Exemplo n.º 7
0
 def test_cache_token_expired_environment(self, m_sh):
     token = 'TOKEN VALUE'
     m_sh.return_value = token
     OpenStack.token = None
     o = OpenStack()
     o.provider = 'ovh'
     os.environ['OS_TOKEN_VALUE'] = token
     token_expires = int(time.time()) - 2000
     os.environ['OS_TOKEN_EXPIRES'] = str(token_expires)
     assert True == o.cache_token()
     m_sh.assert_called_with('openstack -q token issue -c id -f value')
     assert token == os.environ['OS_TOKEN_VALUE']
     assert token == OpenStack.token
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires
Exemplo n.º 8
0
 def test_cache_token_expired_environment(self, m_sh):
     token = 'TOKEN VALUE'
     m_sh.return_value = token
     OpenStack.token = None
     o = OpenStack()
     o.provider = 'ovh'
     os.environ['OS_TOKEN_VALUE'] = token
     token_expires = int(time.time()) - 2000
     os.environ['OS_TOKEN_EXPIRES'] = str(token_expires)
     assert True == o.cache_token()
     m_sh.assert_called_with('openstack -q token issue -c id -f value')
     assert token == os.environ['OS_TOKEN_VALUE']
     assert token == OpenStack.token
     assert time.time() < int(os.environ['OS_TOKEN_EXPIRES'])
     assert time.time() < OpenStack.token_expires