Skip to content

Commit 0ae4cc7

Browse files
authored
Merge pull request #20 from Physsix27/master
Adding Makefile, docs and Tests for util.py & auth.py
2 parents 3471e71 + 8e0695f commit 0ae4cc7

File tree

10 files changed

+232
-3
lines changed

10 files changed

+232
-3
lines changed

Makefile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
TEST_DIR=tests
2+
3+
## Sets up the virtual environment via pipenv.
4+
.PHONY: setup
5+
setup:
6+
pipenv --three install --dev
7+
8+
## Cleans test and packaging outputs.
9+
.PHONY: clean
10+
clean:
11+
rm -rf .coverage htmlcov/ build/ dist/
12+
13+
## Runs tests.
14+
.PHONY: test
15+
test: clean
16+
pipenv run pytest $(TEST_DIR) -v -s --disable-warnings
17+
18+
## Creates coverage report.
19+
.PHONY: coverage
20+
coverage:
21+
pytest --cov=src/ --cov-report=term-missing --cov-report=html --disable-warnings
22+
open htmlcov/index.html || xdg-open htmlcov/index.html
23+
24+
.DEFAULT:
25+
@$(MAKE) help
26+
27+
## This help message.
28+
.PHONY: help
29+
help:
30+
@printf "\nUsage:\n";
31+
32+
@awk '{ \
33+
if ($$0 ~ /^.PHONY: [a-zA-Z\-\_0-9]+$$/) { \
34+
helpCommand = substr($$0, index($$0, ":") + 2); \
35+
if (helpMessage) { \
36+
printf "\033[36m%-20s\033[0m %s\n", \
37+
helpCommand, helpMessage; \
38+
helpMessage = ""; \
39+
} \
40+
} else if ($$0 ~ /^[a-zA-Z\-\_0-9.]+:/) { \
41+
helpCommand = substr($$0, 0, index($$0, ":")); \
42+
if (helpMessage) { \
43+
printf "\033[36m%-20s\033[0m %s\n", \
44+
helpCommand, helpMessage; \
45+
helpMessage = ""; \
46+
} \
47+
} else if ($$0 ~ /^##/) { \
48+
if (helpMessage) { \
49+
helpMessage = helpMessage"\n "substr($$0, 3); \
50+
} else { \
51+
helpMessage = substr($$0, 3); \
52+
} \
53+
} else { \
54+
if (helpMessage) { \
55+
print "\n "helpMessage"\n" \
56+
} \
57+
helpMessage = ""; \
58+
} \
59+
}' \
60+
$(MAKEFILE_LIST)

Pipfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ grpcio = "*"
1616
protobuf = "*"
1717
six = "*"
1818
cryptography = "*"
19+
pytest = "*"
20+
pytest-cov = "*"
21+
pytest-mock = "*"
22+
pytest-coverage = "*"
1923

2024
[requires]
2125
python_version = "3.6"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ git clone https://github.com/cisco-ie/cisco-gnmi-python.git
154154
cd cisco-gnmi-python
155155
# If pipenv not installed, install!
156156
pip install --user pipenv
157-
# Now use pipenv
157+
# Now use Makefile...
158+
make setup
159+
# Or pipenv manually if make not present
158160
pipenv --three install --dev
159161
# Enter virtual environment
160162
pipenv shell
161-
# Do your thing.
163+
# Work work
162164
exit
163165
```
164166

hygiene.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
# Script which autoformats (black) and lints (pylint) code
33
echo "Running black ..."
4-
FORMAT_COMMAND="black --safe --verbose --exclude proto src/cisco_gnmi"
4+
FORMAT_COMMAND="black --safe --verbose --exclude proto src/cisco_gnmi tests/"
55
if black &> /dev/null; then
66
eval $FORMAT_COMMAND
77
elif pipenv run black &> /dev/null; then

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ ipaddress==1.0.22 ; python_version < '3'
99
protobuf==3.9.2
1010
pycparser==2.19
1111
six==1.12.0
12+
pytest==5.2.1
13+
pytest-cov==2.8.1
14+
pytest-mock==1.11.1
15+
coverage==4.5.4

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
"twine",
6464
"setuptools",
6565
"wheel",
66+
"pytest",
67+
"pytest-cov",
68+
"pytest-mock",
69+
"coverage",
6670
],
6771
},
6872
)

tests/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Tests
2+
3+
Clone the project and enter the project directory:
4+
```
5+
git clone git@github.com:cisco-ie/cisco-gnmi-python.git
6+
cd cisco-gnmi-python
7+
```
8+
9+
## Pre-requisite
10+
11+
- Setup the developer virtual environment
12+
```
13+
make setup
14+
```
15+
16+
## Usage
17+
18+
### How to run tests
19+
20+
```
21+
make test
22+
```
23+
24+
### How to open test coverage
25+
26+
```
27+
make coverage
28+
```

tests/__init__.py

Whitespace-only changes.

tests/test_auth.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
from unittest import mock
3+
from src.cisco_gnmi.auth import CiscoAuthPlugin
4+
5+
6+
def test_call():
7+
username = "grpc-username"
8+
password = "grpc-password"
9+
10+
mock_call = mock.MagicMock(spec=CiscoAuthPlugin.__call__)
11+
12+
instance = CiscoAuthPlugin(username, password)
13+
result = instance.__call__(
14+
[(username, "testUsr"), (password, "testPass")], CiscoAuthPlugin
15+
)
16+
mock_call.assert_not_called()

tests/test_util.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import pytest
2+
from pytest_mock import mocker
3+
from src.cisco_gnmi.proto import gnmi_pb2
4+
from src.cisco_gnmi import util
5+
from src.cisco_gnmi.util import urlparse, ssl, x509, default_backend
6+
7+
8+
def test_gen_target_netloc_valueerror():
9+
with pytest.raises(ValueError):
10+
util.gen_target_netloc("http://www.test.com", "test_prefix://", 8000)
11+
12+
13+
def test_gen_target_netloc_parsed_none():
14+
15+
mock_netloc = "//www.testing.com:8080"
16+
mock_parsed_netloc = urlparse(mock_netloc)
17+
18+
result = util.gen_target_netloc("www.testing.com", "http://", 8080)
19+
assert mock_parsed_netloc == result
20+
21+
22+
def test_validate_proto_enum_exception_one():
23+
24+
enum = gnmi_pb2.SubscriptionMode
25+
26+
with pytest.raises(Exception):
27+
util.validate_proto_enum("test", "INVALID_VALUE", "test", enum)
28+
29+
30+
def test_validate_proto_enum_exception_two():
31+
32+
enum = gnmi_pb2.SubscriptionMode
33+
fake_subset = [3]
34+
35+
with pytest.raises(Exception):
36+
util.validate_proto_enum("test", 2, "test", enum, subset=fake_subset)
37+
38+
39+
def test_validate_proto_enum_exception_three():
40+
41+
enum = gnmi_pb2.SubscriptionMode
42+
fake_subset = ["ON_CHANGE", "SAMPLE"]
43+
44+
with pytest.raises(Exception):
45+
util.validate_proto_enum(
46+
"test", "TARGET_DEFINED", "test", enum, subset=fake_subset
47+
)
48+
49+
50+
def test_validate_proto_enum_element_in_subset_one():
51+
52+
enum = gnmi_pb2.SubscriptionMode
53+
fake_subset = ["ON_CHANGE", "SAMPLE"]
54+
55+
result = util.validate_proto_enum("test", 2, "test", enum, subset=fake_subset)
56+
assert 2 == result
57+
58+
59+
def test_validate_proto_enum_element_in_subset_two():
60+
61+
enum = gnmi_pb2.SubscriptionMode
62+
fake_subset = [2, 0]
63+
64+
result = util.validate_proto_enum(
65+
"test", "TARGET_DEFINED", "test", enum, subset=fake_subset
66+
)
67+
assert 0 == result
68+
69+
70+
def test_validate_proto_enum_value_returned_one():
71+
72+
enum = gnmi_pb2.SubscriptionMode
73+
74+
result = util.validate_proto_enum("test", "ON_CHANGE", "test", enum)
75+
assert 1 == result
76+
77+
78+
def test_validate_proto_enum_value_returned_two():
79+
80+
enum = gnmi_pb2.SubscriptionMode
81+
82+
result = util.validate_proto_enum("test", 1, "test", enum)
83+
assert 1 == result
84+
85+
86+
def test_get_cert_from_target():
87+
88+
target_netloc = {"hostname": "cisco.com", "port": 443}
89+
90+
expected_ssl_cert = ssl.get_server_certificate(
91+
(target_netloc.get("hostname"), target_netloc.get("port"))
92+
)
93+
94+
expected_ssl_cert.encode("utf-8")
95+
96+
target = util.gen_target_netloc("cisco.com:443")
97+
result = util.get_cert_from_target((target)).decode("utf-8")
98+
99+
assert expected_ssl_cert == result
100+
101+
102+
def test_get_cn_from_cert_returned_value_invalid_entry(mocker):
103+
104+
mock_cert_parsed = mocker.patch.object(x509, "load_pem_x509_certificate")
105+
result = util.get_cn_from_cert("INVALID_ENTRY")
106+
107+
assert None == result
108+
109+
110+
def test_get_cn_from_cert_returned_value(mocker):
111+
pass

0 commit comments

Comments
 (0)