File: test.py

package info (click to toggle)
pssh 2.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 456 kB
  • sloc: python: 1,552; makefile: 15
file content (303 lines) | stat: -rw-r--r-- 13,029 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python

# Copyright (c) 2009, Andrew McNabb
# Copyright (c) 2003-2008, Brent N. Chun

import os
import subprocess
import sys
import shutil
import tempfile
import time
import unittest

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

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 PsshTest(unittest.TestCase):
    def setUp(self):
        self.outDir = tempfile.mkdtemp()
        self.errDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.errDir)
        shutil.rmtree(self.outDir)

    def testShortOpts(self):
        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pssh -h %s -l %s -p 64 -o %s -e %s -t 60 -v -P -i uptime < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            stdout = open("%s/%s" % (self.outDir, host)).read()
            self.assert_(stdout.find("load average") != -1)

    def testLongOpts(self):
        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pssh --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 --verbose --print --inline uptime < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            stdout = open("%s/%s" % (self.outDir, host)).read()
            self.assert_(stdout.find("load average") != -1)

    def testStderr(self):
        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pssh -h %s -l %s -p 64 -o %s -e %s -t 60 -v -P -i ls /doesnotexist < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            stdout = open("%s/%s" % (self.outDir, host)).read()
            self.assertEqual(stdout, "")
            stderr = open("%s/%s" % (self.errDir, host)).read()
            self.assert_(stderr.find("No such file or directory") != -1)

class PscpTest(unittest.TestCase):
    def setUp(self):
        self.outDir = tempfile.mkdtemp()
        self.errDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.errDir)
        shutil.rmtree(self.outDir)
        try:
            os.remove("/tmp/pssh.test")
        except OSError:
            pass

    def testShortOpts(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pscp -h %s -l %s -p 64 -o %s -e %s -t 60 /etc/hosts /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            cmd = "ssh %s@%s cat /tmp/pssh.test" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/etc/hosts").read())

    def testLongOpts(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pscp --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 /etc/hosts /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            cmd = "ssh %s@%s cat /tmp/pssh.test" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/etc/hosts").read())

    def testRecursive(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pscp -r -h %s -l %s -p 64 -o %s -e %s -t 60 /etc/init.d /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        files = os.popen("ls -R /etc/init.d | sed 1d | sort").read().strip()
        for host in g_hosts:
            cmd = "ssh %s@%s ls -R /tmp/pssh.test | sed 1d | sort" % (g_user, host)
            data = os.popen(cmd).read().strip()
            self.assertEqual(data, files)

class PslurpTest(unittest.TestCase):
    def setUp(self):
        self.outDir = tempfile.mkdtemp()
        self.errDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.errDir)
        shutil.rmtree(self.outDir)

    def testShortOpts(self):
        if os.path.exists("/tmp/pssh.test"):
            try:
                os.remove("/tmp/pssh.test")
            except OSError:
                shutil.rmtree("/tmp/pssh.test")

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pslurp -L /tmp/pssh.test -h %s -l %s -p 64 -o %s -e %s -t 60 /etc/hosts hosts < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)

        for host in g_hosts:
            cmd = "ssh %s@%s cat /etc/hosts" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/tmp/pssh.test/%s/hosts" % host).read())

    def testLongOpts(self):
        if os.path.exists("/tmp/pssh.test"):
            try:
                os.remove("/tmp/pssh.test")
            except OSError:
                shutil.rmtree("/tmp/pssh.test")

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pslurp --localdir=/tmp/pssh.test --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 /etc/hosts hosts < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)

        for host in g_hosts:
            cmd = "ssh %s@%s cat /etc/hosts" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/tmp/pssh.test/%s/hosts" % host).read())

    def testRecursive(self):
        if os.path.exists("/tmp/pssh.test"):
            try:
                os.remove("/tmp/pssh.test")
            except OSError:
                shutil.rmtree("/tmp/pssh.test")

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pslurp -r -L /tmp/pssh.test -h %s -l %s -p 64 -o %s -e %s -t 60 /etc/init.d init.d < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)

        for host in g_hosts:
            cmd = "ssh %s@%s ls -R /etc/init.d | sed 1d | sort" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, os.popen("ls -R /tmp/pssh.test/%s/init.d | sed 1d | sort" % host).read())

class PrsyncTest(unittest.TestCase):
    def setUp(self):
        self.outDir = tempfile.mkdtemp()
        self.errDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.errDir)
        shutil.rmtree(self.outDir)

    def testShortOpts(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/prsync -h %s -l %s -p 64 -o %s -e %s -t 60 -a -z /etc/hosts /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            cmd = "ssh %s@%s cat /tmp/pssh.test" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/etc/hosts").read())

    def testLongOpts(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/prsync --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 --archive --compress /etc/hosts /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        for host in g_hosts:
            cmd = "ssh %s@%s cat /tmp/pssh.test" % (g_user, host)
            data = os.popen(cmd).read()
            self.assertEqual(data, open("/etc/hosts").read())

    def testRecursive(self):
        for host in g_hosts:
            cmd = "ssh %s@%s rm -rf /tmp/pssh.test" % (g_user, host)
            rv = subprocess.call(cmd, shell=True)
            self.assertEqual(rv, 0)

        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/prsync -r -h %s -l %s -p 64 -o %s -e %s -t 60 -a -z /etc/init.d/ /tmp/pssh.test < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)
        files = os.popen("ls -R /etc/init.d | sed 1d | sort").read().strip()
        for host in g_hosts:
            cmd = "ssh %s@%s ls -R /tmp/pssh.test | sed 1d | sort" % (g_user, host)
            data = os.popen(cmd).read().strip()
            self.assertEqual(data, files)

class PnukeTest(unittest.TestCase):
    def setUp(self):
        self.outDir = tempfile.mkdtemp()
        self.errDir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.errDir)
        shutil.rmtree(self.outDir)

    def testShortOpts(self):
        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pssh -h %s -l %s -p 64 -o %s -e %s -t 60 -v sleep 60 < /dev/null &" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        subprocess.call(cmd, shell=True)
        time.sleep(5)

        cmd = "%s/bin/pnuke -h %s -l %s -p 64 -o %s -e %s -t 60 -v sleep < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        print(cmd)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)

    def testLongOpts(self):
        hostsFile = tempfile.NamedTemporaryFile()
        hostsFile.write("".join(map(lambda x: "%s\n" % x, g_hosts)))
        hostsFile.flush()
        cmd = "%s/bin/pssh --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 --verbose sleep 60 < /dev/null &" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        subprocess.call(cmd, shell=True)
        time.sleep(5)

        cmd = "%s/bin/pnuke --hosts=%s --user=%s --par=64 --outdir=%s --errdir=%s --timeout=60 --verbose sleep < /dev/null" % (basedir, hostsFile.name, g_user, self.outDir, self.errDir)
        print(cmd)
        rv = subprocess.call(cmd, shell=True)
        self.assertEqual(rv, 0)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(PsshTest, "test"))
    suite.addTest(unittest.makeSuite(PscpTest, "test"))
    suite.addTest(unittest.makeSuite(PslurpTest, "test"))
    suite.addTest(unittest.makeSuite(PrsyncTest, "test"))
    suite.addTest(unittest.makeSuite(PnukeTest, "test"))
    unittest.TextTestRunner().run(suite)