aboutsummaryrefslogtreecommitdiffstats
path: root/test_exmachina.py
blob: b9527f03fed4e4b01049a25fb0a79786457c4a1b (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
79
80
81
82
83
84
85
#!/usr/bin/env python

"""
This file tests the "client side" of the exmachina layer.

To use with secret keys, do the following in seperate terminals:

    $ echo "<key>" | sudo ./exmachina.py -vk
    $ echo "<key>" | ./test_exmachina.py -k

To use without, do the following in seperate terminals:

    $ sudo ./exmachina.py -v
    $ ./test_exmachina.py

Use the init_test.sh script to test shared key passing and privilage seperation
at the same time:

    $ sudo ./init_test.sh
"""

import sys
import socket

import bjsonrpc
import bjsonrpc.connection
from bjsonrpc.exceptions import ServerError

from exmachina import ExMachinaClient

# =============================================================================
# Command line handling
def main():

    secret_key = None
    if sys.argv[-1] == "-k":
        print "waiting for key on stdin..."
        secret_key = sys.stdin.readline()
        print "got it!"

    """
    # both tests together won't work now that server exits after single client
    socket_path = "/tmp/exmachina.sock"
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.connect(socket_path)

    print "========= Testing JSON-RPC connection"
    c = bjsonrpc.connection.Connection(sock)
    if secret_key:
        c.call.authenticate(secret_key)
    print "/*: %s" % c.call.augeas_match("/*")
    print "/augeas/*: %s" % c.call.augeas_match("/augeas/*")
    print "/etc/* files:"
    for name in c.call.augeas_match("/files/etc/*"):
        print "\t%s" % name
    print c.call.initd_status("bluetooth")
    print "hostname: %s" % c.call.augeas_get("/files/etc/hostname/*")
    print "localhost: %s" % c.call.augeas_get("/files/etc/hosts/1/canonical")
    sock.close()
    """

    print "========= Testing user client library"
    client = ExMachinaClient(secret_key=secret_key)
    print client.augeas.match("/files/etc/*")
    #print client.initd.restart("bluetooth")
    try:
        print client.initd.status("greentooth")
        print "ERROR: should have failed above!"
    except ServerError:
        print "(got expected error, good!)"
    print "(expect Error on the above line)"
    print client.initd.status("bluetooth")
    print client.apt.install("pkg_which_does_not_exist")
    print client.apt.remove("pkg_which_does_not_exist")
    #print client.apt.update() # can be slow...
    #print client.misc.set_timezone("UTC") # don't clobber system...
    try:
        print client.misc.set_timezone("whoopie") # should be an error
        print "ERROR: should have failed above!"
    except ServerError:
        print "(got expected error, good!)"
    client.close()

if __name__ == '__main__':
    main()