Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/integration/node_client.py
blob: 4d5188373eec802284bbdd401f6f3a1c8eec4f62 (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
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
#!/usr/bin/env python
# sugar-lint: disable

import os
import json
import signal
import shutil
import zipfile
from os.path import exists, join

from __init__ import tests, src_root

from sugar_network.client import IPCClient, Client
from sugar_network.toolkit import coroutine, util


class NodeClientTest(tests.Test):

    def setUp(self):
        tests.Test.setUp(self)

        os.makedirs('mnt')
        util.cptree(src_root + '/tests/data/node', 'node')
        self.client_pid = None

        self.node_pid = self.popen([join(src_root, 'sugar-network-node'), '-F', 'start',
            '--port=8100', '--data-root=node', '--cachedir=tmp', '-DDD',
            '--rundir=run', '--stats-node-step=0',
            ])
        coroutine.sleep(2)

    def tearDown(self):
        self.waitpid(self.node_pid, signal.SIGINT)
        if not self.client_pid:
            self.waitpid(self.client_pid, signal.SIGINT)
        tests.Test.tearDown(self)

    def test_CloneContext(self):
        context = self.cli(['POST', '/context'], stdin={
            'type': 'activity',
            'title': 'title1',
            'summary': 'summary',
            'description': 'description',
            })
        impl = self.cli(['POST', '/implementation'], stdin={
            'context': context,
            'license': 'GPLv3+',
            'version': '1',
            'stability': 'stable',
            'notes': '',
            'spec': {
                '*-*': {
                    'commands': {
                        'activity': {
                            'exec': 'true',
                            },
                        },
                    'extract': 'topdir',
                    },
                },
            })
        bundle = zipfile.ZipFile('bundle', 'w')
        bundle.writestr('/topdir/probe', 'ok')
        bundle.close()
        self.cli(['PUT', '/implementation/%s/data' % impl, '--post-file=bundle'])

        self.cli(['PUT', '/context/%s' % context, 'cmd=clone', '-jd1'])
        assert exists('client/Activities/topdir/probe')
        self.assertEqual('ok', file('client/Activities/topdir/probe').read())

    def test_FavoriteContext(self):
        context = self.cli(['POST', '/context'], stdin={
            'type': 'activity',
            'title': 'title1',
            'summary': 'summary',
            'description': 'description',
            })

        path = 'client/db/context/%s/%s/favorite' % (context[:2], context)
        assert not exists(path)

        self.cli(['PUT', '/context/%s' % context, 'cmd=favorite', '-jdtrue'])

        assert exists(path)
        self.assertEqual(True, json.load(file(path))['value'])

    def test_UsecaseOOB(self):
        privkey_path = '.sugar/default/owner.key'
        pubkey_path = '.sugar/default/owner.key.pub'
        os.unlink(privkey_path)
        os.unlink(pubkey_path)

        deplist = self.cli(['GET', '/context/activity', 'cmd=deplist', 'repo=Fedora-14', '--anonymous', '--no-dbus', '--porcelain'])
        assert not exists(privkey_path)
        assert not exists(pubkey_path)
        self.assertEqual(
                sorted(['dep1.rpm', 'dep2.rpm', 'dep3.rpm']),
                sorted(deplist.split('\n')))

        self.cli(['PUT', '/context/context', '--anonymous', 'cmd=clone', 'nodeps=1', 'stability=stable', '-jd', '1'])
        assert not exists(privkey_path)
        assert not exists(pubkey_path)

        self.cli(['PUT', '/context/context', '--anonymous', 'cmd=favorite', '-jd', 'true'])
        assert not exists(privkey_path)
        assert not exists(pubkey_path)
        assert exists('Activities/Chat.activity/activity/activity.info')
        self.assertEqual(True, json.load(file('client/db/context/co/context/favorite'))['value'])

    def cli(self, cmd, stdin=None):
        cmd = ['sugar-network', '--local-root=client', '--ipc-port=5101', '--api-url=http://127.0.0.1:8100', '-DDD'] + cmd

        if '--anonymous' not in cmd and not self.client_pid:
            self.client_pid = self.popen([join(src_root, 'sugar-network-client'),
                '-DDDF', 'start',
                '--activity-dirs=client/Activities', '--local-root=client',
                '--mounts-root=mnt', '--cachedir=tmp', '--ipc-port=5101',
                '--api-url=http://127.0.0.1:8100',
                ])
            coroutine.sleep(2)
            ipc = Client('http://127.0.0.1:5101')
            if ipc.get(cmd='status')['route'] == 'offline':
                self.wait_for_events(ipc, event='inline', state='online').wait()

        result = util.assert_call(cmd, stdin=json.dumps(stdin))
        if result and '--porcelain' not in cmd:
            result = json.loads(result)
        return result


if __name__ == '__main__':
    tests.main()