File: test_api.py

package info (click to toggle)
parallax 1.0.1-3~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 244 kB
  • sloc: python: 1,149; sh: 106; makefile: 11
file content (88 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python

# Copyright (c) 2013, Kristoffer Gronlund

import os
import sys
import unittest
import tempfile
import shutil

basedir, bin = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
sys.path.insert(0, "%s" % basedir)

print(basedir)

import parallax as para

if os.getenv("TEST_HOSTS") is None:
    raise Exception("Must define TEST_HOSTS")
g_hosts = os.getenv("TEST_HOSTS").split()

if os.getenv("TEST_USER") is None:
    raise Exception("Must define TEST_USER")
g_user = os.getenv("TEST_USER")


class CallTest(unittest.TestCase):
    def testSimpleCall(self):
        opts = para.Options()
        opts.default_user = g_user
        for host, result in para.call(g_hosts, "ls -l /", opts).items():
            if isinstance(result, para.Error):
                raise result
            rc, out, err = result
            self.assertEqual(rc, 0)
            self.assert_(len(out) > 0)

    def testUptime(self):
        opts = para.Options()
        opts.default_user = g_user
        for host, result in para.call(g_hosts, "uptime", opts).items():
            if isinstance(result, para.Error):
                raise result
            rc, out, err = result
            self.assertEqual(rc, 0)
            self.assert_(out.decode("utf8").find("load average") != -1)

    def testFailingCall(self):
        opts = para.Options()
        opts.default_user = g_user
        for host, result in para.call(g_hosts, "touch /foofoo/barbar/jfikjfdj", opts).items():
            self.assert_(isinstance(result, para.Error))
            self.assert_(str(result).find('with error code') != -1)


class CopySlurpTest(unittest.TestCase):
    def setUp(self):
        self.tmpDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tmpDir)

    def testCopyFile(self):
        opts = para.Options()
        opts.default_user = g_user
        opts.localdir = self.tmpDir
        by_host = para.copy(g_hosts, "/etc/hosts", "/tmp/para.test", opts)
        for host, result in by_host.items():
            if isinstance(result, para.Error):
                raise result
            rc, _, _ = result
            self.assertEqual(rc, 0)

        by_host = para.slurp(g_hosts, "/tmp/para.test", "para.test", opts)
        for host, result in by_host.items():
            if isinstance(result, para.Error):
                raise result
            rc, _, _, path = result
            self.assertEqual(rc, 0)
            self.assert_(path.endswith('%s/para.test' % (host)))

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(CallTest, "test"))
    suite.addTest(unittest.makeSuite(CopySlurpTest, "test"))
    result = unittest.TextTestRunner().run(suite)
    if not result.wasSuccessful():
        sys.exit(1)