Esempio n. 1
0
 def test_refresh_token(self):
     with patch('os.environ', {SERVICE_HOST_ENV_NAME: 'a', SERVICE_PORT_ENV_NAME: '1'}),\
             patch('os.path.isfile', Mock(side_effect=[True, True, False, True, True, True])),\
             patch.object(builtins, 'open', Mock(side_effect=[
                 mock_open(read_data='cert')(), mock_open(read_data='a')(),
                 mock_open()(), mock_open(read_data='b')(), mock_open(read_data='c')()])):
         k8s_config.load_incluster_config(
             token_refresh_interval=datetime.timedelta(milliseconds=100))
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer a')
         time.sleep(0.1)
         # token file doesn't exist
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer a')
         # token file is empty
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer a')
         # token refreshed
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer b')
         time.sleep(0.1)
         # token refreshed
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer c')
         # no need to refresh token
         self.assertEqual(k8s_config.headers.get('authorization'),
                          'Bearer c')
Esempio n. 2
0
    def test_load_incluster_config(self):
        for env in ({}, {SERVICE_HOST_ENV_NAME: '', SERVICE_PORT_ENV_NAME: ''}):
            with patch('os.environ', env):
                self.assertRaises(k8s_config.ConfigException, k8s_config.load_incluster_config)

        with patch('os.environ', {SERVICE_HOST_ENV_NAME: 'a', SERVICE_PORT_ENV_NAME: '1'}),\
                patch('os.path.isfile', Mock(side_effect=[False, True, True, False, True, True, True, True])),\
                patch.object(builtins, 'open', Mock(side_effect=[
                    mock_open()(), mock_open(read_data='a')(), mock_open(read_data='a')(),
                    mock_open()(), mock_open(read_data='a')(), mock_open(read_data='a')()])):
            for _ in range(0, 4):
                self.assertRaises(k8s_config.ConfigException, k8s_config.load_incluster_config)
            k8s_config.load_incluster_config()
            self.assertEqual(k8s_config.server, 'https://a:1')