Note: this was a take home assignment for a DevOps interview in 2022
Take an IP address and determine if it's in a range of CIDRs
Thank you for the opportunities - to learn, to grow, and to be challenged to produce a higher quality output. I appreciate the time, patience and consideration.
And while I may not be confused for Kent Beck or Brian Okken anytime soon, this exercise has helped me practically pursue improvement.
I understand this process is to help us both see if we're a good fit. So, in an effort to be intellectually honest, I've captured my learnings to date and added notes at: Testing, Test Driven Development, Unit Testing, and Pytest Framework. In completing this exercise, I've only referenced what I could search online.
I fully appreciate you have to build the team to meet the needs of the business. I will always respect that. In the end we both want to be each other's first choice.
I know there are things I need to work on and will continue to sharpen going forward. And am confident I can efficiently manage any gaps. Again, thank you for your time and the opportunity. I look forward to meeting soon.
- Take IP address as a command line argument
- Get JSON data from RIPE Network Coordination Center
- Using the ipv4 block in JSON:
- Determine whether IP address provided on CLI is in any of the CIDRs
- Output a Pass/Fail result
- based on the presence of the IP address in the CIDR ranges
Run the following commands given Python 3.7+ is installed.
Developed on Python Version: 3.9.13 (pyenv local 3.9.13)
OS X & Linux:
# Clone repo from codecharacter GitHub
git clone https://github.com/codecharacter/devops-take-home.git
cd devops-take-home
# (Recommended) Set up virtual environment to safeguard from any package conflicts with current
python3 -m venv venv
source venv/bin/activate
# For running tests, install/restore dependencies from the requirements-dev.txt file
pip install -r requirements-dev.txt
# For running the script without test framework install/restore from requirements.txt
pip install -r requirements.txt
# Navigate to script directory
cd devops-take-home
python main.py [IPv4 address]
Example:
python main.py 2.56.8.1
Example Output for a PASS:
Example Output for a Fail:
Framework: pytest
# In cloned GitHub directory (you may very well be aware of all this)
cd devops-take-home
pytest # Run all tests
pytest tests/[filename].py # Run any individual test
# Pytest coverage report
pytest --cov
- argparse (Std Lib)
- The recommended command-line parsing module in the Python standard library.
- argparse provides options:
- positional arguments,
- default value for arguments,
- help message,
- specifying data types of argument
- ipaddress (Std Lib)
- provides many capabilities to work with IP addresses (input) and networks (CIDRs)
- including relevant to this task:
- checking validity of an IP address (input)
- iterating over all hosts in a particular subnet (CIDR)
- requests (PyPI)
- a simple HTTP library in Python that allows sending requests easily
- abstracts complexities of making requests so focus can be on consuming data & interacting with services
- relevant features:
- keep-alive & connection pooling
- timeouts
- sessions
- responses (PyPI)
- a utility library for mocking out the requests Python library
- including integration with unit test frameworks, and more:
- responses as a pytest fixture
- assertions on declared responses
- pytest (PyPI)
- testing framework used to write tests for this exercise
- alternate testing framework available (not used): unittest
- generally, easy to use, available resources online, usable output
- ability to filter tests
- allows test parametrization
- pytest-cov (PyPI)
- plugin that produces coverage reports
I collected this workflow from one of the training courses I completed and applying when possible.
- Browse a curated list:
- Search Google 2-5 relevant keywords
- Search Stack Overflow
- Search Community Forums: Reddit: r/Python, HackerNews (Algolia), Twitter
- Search PyPI directly . Ask a question on SO, Reddit, Twitter (slower)
- Check download stats if available
- Google/Reddit/SO recommendations
- GitHub 'stars' (visible on PyPI)
- python.libhunt.com popularity indicator
- Is it helpful?
- Is it well-maintained?
- How "successful" does the project look?
- Does it cover what the project does & how to install it?
- What license is the project under?
- Who is the author?
- Skim the changelog/update history
- Is there activity on the bug tracker?
- When was the last commit of the project?
- note: looking to grow in my ability to do this; principles apply
- Does it follow best practices? (formatting, style, comments, docstrings)
- Are there (automated) tests?
- How experienced were the developers who wrote it?
- Would I feel comfortable making small changes if I had to?
- if I had to maintain the library going forward
- Take the (narrowed down) list of candidates and try them out in an interpreter session.
- Does the package install and import cleanly?
- Do I enjoy working with the package?
- argparse
- ipaddress
- requests
- responses
- pytest
- pytest-cov
- __main__
- StackOverflow: How to Test or Mock if __name__ == "__main__" contents
- note: first comment had 28 upvotes, open to learning on this
- Medium: How to test if __name__ == '__main__'
- StackOverflow: How to Test or Mock if __name__ == "__main__" contents
- 2.0
- Submitted 7/1/2022
- 1.0
- Submitted 6/24/2022
- Email: dan@codecharacter.dev
- Website: www.CodeCharacter.dev
- LinkedIn: LinkedIn Profile
- Article: So, What's the Deal with 'Code Character'?