summaryrefslogtreecommitdiff
path: root/systest
blob: bf650b5cc9b545cf90683accc8f9bb122cc5430d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python

import cliapp
import logging
import re
import subprocess
import unittest

import systest


class DebianBaseTests(systest.TestCase):

    def test_only_ssh_port(self):
        out = self.hostcmd(['nmap', self.settings['target']])
        ports = [line.split()[0]
                 for line in out.splitlines()
                 if ' open ' in line]
        self.assertEqual(ports, ['22/tcp'])

    def test_ssh_login(self):
        user = self.settings['user']
        out = self.hostcmd(['ssh', '-l', user, self.settings['target'], 'id'])
        self.assertMatches(r'^uid=1000\(%s\)' % user, out)

    def test_simple_dns_lookup(self):
        out = self.targetcmd(['host', 'www.debian.org'])
        self.assert_('www.debian.org' in out)
        
    def test_ping_localhost(self):
        self.targetcmd(['ping', '-c1', 'localhost'])
        
    def test_ping6_localhost(self):
        self.targetcmd(['ping6', '-c1', 'ip6-localhost'])
        
    def test_cat(self):
        out = self.targetcmd(['cat'], stdin='foo')
        self.assertEqual(out, 'foo')

#    def test_sudo(self):
#        out = self.targetcmd(['sudo', 'id'], 
#                             stdin=self.settings['user-password'])
#        self.assertMatches(r'^uid=0\(root\)', out)



class SystemTest(cliapp.Application):

    def add_settings(self):
        self.settings.boolean(['verbose', 'v'], 
                              'print names of tests when run')
        self.settings.string(['target'], 'target domain name or IP address')
        self.settings.string(['user'], 'user on target')
        self.settings.string(['user-password'], 'password for target user')

    def process_args(self, args):
        loader = unittest.defaultTestLoader
        loader.suiteClass = self.create_suite
        suite = loader.loadTestsFromTestCase(DebianBaseTests)
        unittest.TextTestRunner().run(suite)

    def create_suite(self, tests):
        for test in tests:
            test.settings = self.settings
        suite = unittest.TestSuite(tests)
        return suite

    def mangle(self, testname):
        return 'test_' + testname.replace('-', '_')
    
    def unmangle(self, methodname):
        assert methodname.startswith('test_')
        methodname = methodname[len('test_'):]
        return methodname.replace('_', '-')


if __name__ == '__main__':
    SystemTest().run()