Skip to content

Commit 6e6bf90

Browse files
authored
Fix config output and add config and command tests (#163)
1 parent e210c7e commit 6e6bf90

3 files changed

Lines changed: 134 additions & 2 deletions

File tree

alertaclient/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class Config:
2525
def __init__(self, config_file):
2626
self.options = default_config
2727
self.parser = configparser.RawConfigParser(defaults=self.options)
28-
config_file = config_file or os.environ.get('ALERTA_CONF_FILE') or self.options['config_file']
29-
self.parser.read(os.path.expanduser(config_file))
28+
29+
self.options['config_file'] = config_file or os.environ.get('ALERTA_CONF_FILE') or self.options['config_file']
30+
self.parser.read(os.path.expanduser(self.options['config_file']))
3031

3132
def get_config_for_profle(self, profile=None):
3233
want_profile = profile or os.environ.get('ALERTA_DEFAULT_PROFILE') or self.parser.defaults().get('profile')
@@ -44,6 +45,10 @@ def get_config_for_profle(self, profile=None):
4445
except (ValueError, AttributeError):
4546
self.options[opt] = self.parser.get('DEFAULT', opt)
4647

48+
self.options['profile'] = want_profile
49+
self.options['endpoint'] = os.environ.get('ALERTA_ENDPOINT', self.options['endpoint'])
50+
self.options['key'] = os.environ.get('ALERTA_API_KEY', self.options['key'])
51+
4752
def get_remote_config(self):
4853
config_url = '{}/config'.format(self.options['endpoint'])
4954
try:

tests/test_commands.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
3+
import requests_mock
4+
from click.testing import CliRunner
5+
6+
from alertaclient.api import Client
7+
from alertaclient.commands.cmd_whoami import cli as whoami
8+
from alertaclient.config import Config
9+
10+
11+
class CommandsTestCase(unittest.TestCase):
12+
13+
def setUp(self):
14+
self.client = Client()
15+
16+
config = Config(config_file=None)
17+
self.obj = config.options
18+
self.obj['client'] = self.client
19+
20+
self.runner = CliRunner()
21+
22+
@requests_mock.mock()
23+
def test_whoami_cmd(self, m):
24+
25+
whoami_response = """
26+
{
27+
"aud": "736147134702-glkb1pesv716j1utg4llg7c3rr7nnhli.apps.googleusercontent.com",
28+
"customers": [],
29+
"email": "admin@alerta.io",
30+
"exp": 1543264150,
31+
"iat": 1542054550,
32+
"iss": "https://alerta-api.herokuapp.com/",
33+
"jti": "54bfe4fd-5ed3-4d43-abc5-09cea407af94",
34+
"name": "admin@alerta.io",
35+
"nbf": 1542054550,
36+
"preferred_username": "admin@alerta.io",
37+
"provider": "basic",
38+
"roles": [
39+
"admin"
40+
],
41+
"scope": "admin read write",
42+
"sub": "df97c902-9b66-42f1-97b8-efb37ad942d6"
43+
}
44+
"""
45+
46+
m.get('/userinfo', text=whoami_response)
47+
result = self.runner.invoke(whoami, ['-u'], obj=self.obj)
48+
self.assertIn('preferred_username : admin@alerta.io', result.output)
49+
self.assertEqual(result.exit_code, 0)

tests/test_config.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import contextlib
2+
import os
3+
import unittest
4+
5+
from alertaclient.api import Client
6+
from alertaclient.config import Config
7+
8+
9+
@contextlib.contextmanager
10+
def mod_env(*remove, **update):
11+
"""
12+
See https://stackoverflow.com/questions/2059482#34333710
13+
14+
Temporarily updates the ``os.environ`` dictionary in-place.
15+
16+
The ``os.environ`` dictionary is updated in-place so that the modification
17+
is sure to work in all situations.
18+
19+
:param remove: Environment variables to remove.
20+
:param update: Dictionary of environment variables and values to add/update.
21+
"""
22+
env = os.environ
23+
update = update or {}
24+
remove = remove or []
25+
26+
# List of environment variables being updated or removed.
27+
stomped = (set(update.keys()) | set(remove)) & set(env.keys())
28+
# Environment variables and values to restore on exit.
29+
update_after = {k: env[k] for k in stomped}
30+
# Environment variables and values to remove on exit.
31+
remove_after = frozenset(k for k in update if k not in env)
32+
33+
try:
34+
env.update(update)
35+
[env.pop(k, None) for k in remove]
36+
yield
37+
finally:
38+
env.update(update_after)
39+
[env.pop(k) for k in remove_after]
40+
41+
42+
class CommandsTestCase(unittest.TestCase):
43+
44+
def setUp(self):
45+
pass
46+
47+
def test_env_vars(self):
48+
49+
config = Config(config_file=None)
50+
self.assertEqual(config.options['config_file'], '~/.alerta.conf')
51+
52+
with mod_env(
53+
ALERTA_CONF_FILE='~/.alerta.test.conf',
54+
ALERTA_DEFAULT_PROFILE='test-profile',
55+
ALERTA_ENDPOINT='http://foo/bar/baz',
56+
ALERTA_API_KEY='test-key',
57+
REQUESTS_CA_BUNDLE='',
58+
CLICOLOR='',
59+
DEBUG='1'
60+
):
61+
62+
# conf file
63+
config = Config(config_file=None)
64+
self.assertEqual(config.options['config_file'], '~/.alerta.test.conf', os.environ)
65+
config = Config(config_file='/dev/null')
66+
self.assertEqual(config.options['config_file'], '/dev/null')
67+
68+
# profile
69+
config = Config(config_file=None)
70+
config.get_config_for_profle()
71+
self.assertEqual(config.options['profile'], 'test-profile', os.environ)
72+
73+
# endpoint
74+
self.client = Client()
75+
self.assertEqual(self.client.endpoint, 'http://foo/bar/baz')
76+
77+
# api key
78+
self.assertEqual(config.options['key'], 'test-key')

0 commit comments

Comments
 (0)