Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 7a6eb51

Browse files
Add code formatting (#36)
* feat: feat: added job which checks if code is formatted correctly in CircleCi, code formatted using black * fixing typo
1 parent bd03c04 commit 7a6eb51

12 files changed

+163
-75
lines changed

.circleci/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ jobs:
4040
path: build-container
4141
destination: build-container
4242

43+
format-code:
44+
docker:
45+
- image: circleci/python:3.8.6
46+
steps:
47+
- checkout
48+
- run:
49+
name: "Check Formatting"
50+
command: |
51+
pip install black
52+
black --check ./
53+
4354
release:
4455
docker:
4556
- image: circleci/node:12
@@ -118,6 +129,8 @@ workflows:
118129
version: 2
119130
build_test:
120131
jobs:
132+
- format-code
133+
121134
- build:
122135
context:
123136
- gdi-github

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dmypy.json
130130

131131
# Pyre type checker
132132
.pyre/
133+
134+
# Intellij
135+
.idea

tests/conftest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
pytest_plugins = ['tests.snmp_utils.arguments_setup', 'tests.snmp_utils.fixtures.splunk_setup',
2-
'tests.snmp_utils.fixtures.splunk_docker_setup', 'tests.snmp_utils.fixtures.mongo_docker_setup',
3-
'tests.snmp_utils.fixtures.snmp_simulator_docker_setup',
4-
'tests.snmp_utils.fixtures.rabbitmq_docker_setup']
1+
pytest_plugins = [
2+
"tests.snmp_utils.arguments_setup",
3+
"tests.snmp_utils.fixtures.splunk_setup",
4+
"tests.snmp_utils.fixtures.splunk_docker_setup",
5+
"tests.snmp_utils.fixtures.mongo_docker_setup",
6+
"tests.snmp_utils.fixtures.snmp_simulator_docker_setup",
7+
"tests.snmp_utils.fixtures.rabbitmq_docker_setup",
8+
]

tests/mongo_integration_test.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
# TODO as for now this is just a demo on how to use WalkedHostsRepository
55
# TODO final version should use fixures to run MongoDB in Docker
66
def main():
7-
mongo_config = {'host': 'localhost', 'port': 27017, 'database': 'snmp_poller', 'collection': 'walked_hosts'}
7+
mongo_config = {
8+
"host": "localhost",
9+
"port": 27017,
10+
"database": "snmp_poller",
11+
"collection": "walked_hosts",
12+
}
813

914
try:
1015
mongo = WalkedHostsRepository(mongo_config)
1116
except Exception:
12-
print('Exception!')
17+
print("Exception!")
1318

1419
mongo.clear()
15-
mongo.add_host('192.168.0.2')
16-
print(mongo.contains_host('192.168.0.2'))
20+
mongo.add_host("192.168.0.2")
21+
print(mongo.contains_host("192.168.0.2"))
1722

18-
mongo.delete_host('192.168.0.2')
19-
print(mongo.contains_host('192.168.0.2'))
23+
mongo.delete_host("192.168.0.2")
24+
print(mongo.contains_host("192.168.0.2"))
2025

2126

22-
if __name__ == '__main__':
27+
if __name__ == "__main__":
2328
main()

tests/snmp_utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import logging.config
22

3-
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')
3+
logging.basicConfig(
4+
level=logging.INFO, format="%(asctime)s | %(name)s | %(levelname)s | %(message)s"
5+
)

tests/snmp_utils/arguments_setup.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
def pytest_addoption(parser):
2-
group = parser.getgroup('splunk-addon')
2+
group = parser.getgroup("splunk-addon")
33
# NOTE: for 'splunk-type' you can either specify 'external' or 'docker'
4-
group.addoption('--splunk_type', action='store', dest='splunk_type', default='docker', help='Type of Splunk')
5-
group.addoption('--splunk_host', action='store', dest='splunk_host', default='127.0.0.1',
6-
help='Address of the Splunk Server')
7-
group.addoption('--splunk_port', action='store', dest='splunk_port', default='8089', help='Splunk rest port')
8-
group.addoption('--splunk_user', action='store', dest='splunk_user', default='admin', help='Splunk login user')
9-
group.addoption('--splunk_password', action='store', dest='splunk_password', default='Changed@11',
10-
help='Splunk password')
11-
group.addoption('--connect_max_retries', action='store', dest='splunk_connect_max_retries', default='600',
12-
help='Max number of tries when connecting to Splunk before giving up')
4+
group.addoption(
5+
"--splunk_type",
6+
action="store",
7+
dest="splunk_type",
8+
default="docker",
9+
help="Type of Splunk",
10+
)
11+
group.addoption(
12+
"--splunk_host",
13+
action="store",
14+
dest="splunk_host",
15+
default="127.0.0.1",
16+
help="Address of the Splunk Server",
17+
)
18+
group.addoption(
19+
"--splunk_port",
20+
action="store",
21+
dest="splunk_port",
22+
default="8089",
23+
help="Splunk rest port",
24+
)
25+
group.addoption(
26+
"--splunk_user",
27+
action="store",
28+
dest="splunk_user",
29+
default="admin",
30+
help="Splunk login user",
31+
)
32+
group.addoption(
33+
"--splunk_password",
34+
action="store",
35+
dest="splunk_password",
36+
default="Changed@11",
37+
help="Splunk password",
38+
)
39+
group.addoption(
40+
"--connect_max_retries",
41+
action="store",
42+
dest="splunk_connect_max_retries",
43+
default="600",
44+
help="Max number of tries when connecting to Splunk before giving up",
45+
)

tests/snmp_utils/fixtures/mongo_docker_setup.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ def is_mongo_running(mongo_config):
1212
return client.is_connected()
1313

1414

15-
@pytest.fixture(scope='session')
15+
@pytest.fixture(scope="session")
1616
def mongo_service(request, docker_services):
17-
service_name = 'mongo'
17+
service_name = "mongo"
1818
docker_services.start(service_name)
1919
port = docker_services.port_for(service_name, 27017)
20-
configuration = {'host': 'localhost', 'port': port, 'database': 'snmp_poller', 'collection': 'walked_hosts'}
21-
22-
docker_services.wait_until_responsive(timeout=180.0, pause=1.0, check=lambda: is_mongo_running(configuration))
23-
logger.info(f'Initialized {service_name} from Docker with {configuration}')
20+
configuration = {
21+
"host": "localhost",
22+
"port": port,
23+
"database": "snmp_poller",
24+
"collection": "walked_hosts",
25+
}
26+
27+
docker_services.wait_until_responsive(
28+
timeout=180.0, pause=1.0, check=lambda: is_mongo_running(configuration)
29+
)
30+
logger.info(f"Initialized {service_name} from Docker with {configuration}")

tests/snmp_utils/fixtures/rabbitmq_docker_setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ def is_rabbitmq_running(mongo_config):
1111
return True
1212

1313

14-
@pytest.fixture(scope='session')
14+
@pytest.fixture(scope="session")
1515
def rabbitmq_service(request, docker_services):
16-
service_name = 'rabbitmq'
16+
service_name = "rabbitmq"
1717
docker_services.start(service_name)
1818
port = docker_services.port_for(service_name, 5672)
1919
configuration = {}
2020

21-
docker_services.wait_until_responsive(timeout=180.0, pause=1.0, check=lambda: is_rabbitmq_running(configuration))
22-
logger.info(f'Initialized {service_name} from Docker with {configuration}')
21+
docker_services.wait_until_responsive(
22+
timeout=180.0, pause=1.0, check=lambda: is_rabbitmq_running(configuration)
23+
)
24+
logger.info(f"Initialized {service_name} from Docker with {configuration}")

tests/snmp_utils/fixtures/snmp_simulator_docker_setup.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77

88
def is_snmp_simulator_running(snmp_config):
99
import pysnmp.hlapi as snmp
10-
iterator = snmp.getCmd(snmp.SnmpEngine(), snmp.CommunityData('public'),
11-
snmp.UdpTransportTarget((snmp_config['host'], snmp_config['port'])),
12-
snmp.ContextData(),
13-
snmp.ObjectType(snmp.ObjectIdentity('1.3.6.1.2.1.1.1.0')))
10+
11+
iterator = snmp.getCmd(
12+
snmp.SnmpEngine(),
13+
snmp.CommunityData("public"),
14+
snmp.UdpTransportTarget((snmp_config["host"], snmp_config["port"])),
15+
snmp.ContextData(),
16+
snmp.ObjectType(snmp.ObjectIdentity("1.3.6.1.2.1.1.1.0")),
17+
)
1418
error_indication, error_status, error_index, var_binds = next(iterator)
1519
return True if error_indication is None else False
1620

1721

18-
@pytest.fixture(scope='session')
22+
@pytest.fixture(scope="session")
1923
def snmp_simulator_service(request, docker_services):
20-
service_name = 'snmpsim'
24+
service_name = "snmpsim"
2125
docker_services.start(service_name)
22-
snmp_config = {'host': 'localhost', 'port': 161}
23-
docker_services.wait_until_responsive(timeout=180.0, pause=1.0,
24-
check=lambda: is_snmp_simulator_running(snmp_config))
25-
logger.info(f'Initialized {service_name} from Docker with {snmp_config}')
26+
snmp_config = {"host": "localhost", "port": 161}
27+
docker_services.wait_until_responsive(
28+
timeout=180.0, pause=1.0, check=lambda: is_snmp_simulator_running(snmp_config)
29+
)
30+
logger.info(f"Initialized {service_name} from Docker with {snmp_config}")

tests/snmp_utils/fixtures/splunk_docker_setup.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,46 @@
88

99

1010
def is_responsive_splunk(splunk_conn_config):
11-
logger.debug(f'Calling with parameters {splunk_conn_config}')
11+
logger.debug(f"Calling with parameters {splunk_conn_config}")
1212
try:
13-
connection_params = {'username': splunk_conn_config['username'], 'password': splunk_conn_config['password'],
14-
'host': splunk_conn_config['host'], 'port': splunk_conn_config['port']}
13+
connection_params = {
14+
"username": splunk_conn_config["username"],
15+
"password": splunk_conn_config["password"],
16+
"host": splunk_conn_config["host"],
17+
"port": splunk_conn_config["port"],
18+
}
1519
client.connect(**connection_params)
16-
logger.info(f'Connected to Splunk!')
20+
logger.info(f"Connected to Splunk!")
1721
return True
1822
except Exception:
1923
return False
2024

2125

22-
@pytest.fixture(scope='session')
26+
@pytest.fixture(scope="session")
2327
def docker_compose_files(pytestconfig):
2428
# This took me hours to figure. We must return a collection!
25-
return [os.path.join(str(pytestconfig.rootdir), 'docker-compose.yml')]
29+
return [os.path.join(str(pytestconfig.rootdir), "docker-compose.yml")]
2630

2731

28-
@pytest.fixture(scope='session')
32+
@pytest.fixture(scope="session")
2933
def splunk_docker_configuration(request, docker_services):
30-
service_name = 'splunk'
34+
service_name = "splunk"
3135
docker_services.start(service_name)
32-
default_port = request.config.getoption('splunk_port')
36+
default_port = request.config.getoption("splunk_port")
3337
port = docker_services.port_for(service_name, int(default_port))
3438

35-
configuration = {'host': docker_services.docker_ip, 'port': port,
36-
'username': request.config.getoption('splunk_user'),
37-
'password': request.config.getoption('splunk_password'),
38-
'connect_max_retries': request.config.getoption('splunk_connect_max_retries')}
39+
configuration = {
40+
"host": docker_services.docker_ip,
41+
"port": port,
42+
"username": request.config.getoption("splunk_user"),
43+
"password": request.config.getoption("splunk_password"),
44+
"connect_max_retries": request.config.getoption("splunk_connect_max_retries"),
45+
}
3946

4047
logger.info(configuration)
41-
docker_services.wait_until_responsive(timeout=180.0, pause=1.0, check=lambda: is_responsive_splunk(configuration))
42-
logger.info(f'Initialized {service_name} from Docker')
48+
docker_services.wait_until_responsive(
49+
timeout=180.0, pause=1.0, check=lambda: is_responsive_splunk(configuration)
50+
)
51+
logger.info(f"Initialized {service_name} from Docker")
4352

4453
return configuration

0 commit comments

Comments
 (0)