Skip to content
Francois Levaux-Tiffreau edited this page Sep 23, 2016 · 4 revisions

Welcome to the KerbMinder wiki!

This is where we'll put stuff that don't belong to README.md

Today, we're 2 contributors:

@pmbuko created KerbMinder while @ftiff started to contribute to it from v1.3 (where we stopped worrying about Active Directory).

❓ You can contact us on #kerbminder @ macadmins (Slack)

Contributing

Create Virtual Env and test

Before you push any commit, make sure you run nosetests. The easiest way to do it is to create a virtual environment. Here's how:

git clone https://github.com/pmbuko/KerbMinder.git
cd KerbMinder
sudo easy_install pip
sudo pip install virtualenv
virtualenv build/venv --system-site-packages
source build/venv/bin/activate
pip install -r requirements.txt
nosetests --with-coverage --cover-package=KerbMinder Library/Application\ Support/crankd/

It should look like this:

(venv)tall:KerbMinder fti$ nosetests --with-coverage --cover-package=KerbMinder Library/Application\ Support/crankd/
.............
Name            Stmts   Miss  Cover   Missing
---------------------------------------------
KerbMinder.py     293    157    46%   245-248, 285-286, 295-310, 314-317, 321, 325, 329-338, 343-352, 356-362, 371-375, 379-386, 390-395, 399-404, 408-413, 416, 419, 430-444, 453-471, 477-492, 584-596, 610-611, 618-670, 676
----------------------------------------------------------------------
Ran 13 tests in 0.058s

OK
(venv)tall:KerbMinder fti$

This means there's chances you didn't break anything. 46% means the tests cover 46% of the code. Feel free to contribute.

Patch & Mocks

I found the best way to test in this situation is to patch the functions. Here's an example. I want to test this:

def domain_dig_check(domain):
    """Checks if AD domain is accessible by looking for SRV records for LDAP in DNS.
    Returns True if it can ping the domain, otherwise exits.
    """
    dig = subprocess.check_output(['dig', '-t', 'srv', '_ldap._tcp.' + domain])
    if 'ANSWER SECTION' not in dig:
        log_print('Domain not accessible. Exiting.')
        sys.exit(0)
    log_print('Domain is accessible.')
    return True

Here, I want to make sure the script exits if there's no "ANSWER SECTION". It would be quite a pain to change the environment every time I want to test. For example, @pmbuko uses Active Directory, and @ftiff don't. How do we work together ? By patching & Mocking this ! Let's patch subprocess.check_output to return a standard DNS response without "ANSWER SECTION".

// If it starts with test_, it will be tested
def test_domain_dig_check_notok(self):

    // Now my return value
    _return_value = """
; <<>> DiG 9.8.3-P1 <<>> -t srv _ldap._tcp.test.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 696
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;_ldap._tcp.test.com.    IN    SRV
;; AUTHORITY SECTION:
.            10800    IN    SOA    a.root-servers.net. nstld.verisign-grs.com. 2015101900 1800 900 604800 86400
;; Query time: 21 msec
;; SERVER: 10.0.0.2#53(10.0.0.2)
;; WHEN: Mon Oct 17 11:20:54 2015
"""

        // We patch subprocess.check_output to return the _return_value.
        with patch('subprocess.check_output', return_value = _return_value) as check_output:

            // does the function domain_dig_check("TEST.COM") raises SystemExit ?
            nose.tools.assert_raises(SystemExit, domain_dig_check, "TEST.COM")

        // Let's make sure it was called with the right parameters.
        check_output.assert_called_with(['dig', '-t', 'srv', '_ldap._tcp.TEST.COM’])

Is it more clear? It can become pretty messy. But I think it's the best way to do unit testing. I'd love your input -- @ftiff

TODO (To be discussed)

  • Stop using Pashua
  • Make calls to kinit and klist native
  • Stop using crankd