~didrocks/ubuntuone-client/use_result_var

« back to all changes in this revision

Viewing changes to tests/platform/linux/eventlog/test_zglog.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-11 16:18:11 UTC
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: james.westby@ubuntu.com-20110211161811-n18dj9lde7dxqjzr
Tags: upstream-1.5.4
ImportĀ upstreamĀ versionĀ 1.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Author: Alejandro J. Cura <alecu@canonical.com>
 
4
#
 
5
# Copyright 2010 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
"""Tests for the Zeitgeist logging."""
 
19
 
 
20
import os
 
21
import shutil
 
22
import subprocess
 
23
import tempfile
 
24
import time
 
25
 
 
26
from distutils.spawn import find_executable
 
27
 
 
28
from twisted.internet.defer import Deferred
 
29
from zeitgeist.client import ZeitgeistClient
 
30
from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
 
31
 
 
32
from tests.platform.linux.test_dbus import DBusTwistedTestCase
 
33
from ubuntuone.eventlog.zglog import ZeitgeistLogger
 
34
 
 
35
SRCDIR = os.environ.get('SRCDIR', os.getcwd())
 
36
 
 
37
 
 
38
class NotFoundError(Exception):
 
39
    """Not found error."""
 
40
 
 
41
 
 
42
class ZeitgeistNotStartedTests(DBusTwistedTestCase):
 
43
    """Tests for the zeitgeist logging module."""
 
44
 
 
45
    def test_log_does_not_err_when_daemon_not_started(self):
 
46
        """zeitgeist-daemon was not started."""
 
47
        timestamp = int(time.time() * 1000)
 
48
        subject = Subject.new_for_values(
 
49
                uri="file:///tmp/folder1",
 
50
                interpretation=Interpretation.FOLDER,
 
51
                manifestation=Manifestation.FILE_DATA_OBJECT,
 
52
                origin="ubuntuone:uuid",
 
53
                mimetype="inode/directory",
 
54
                text="sample folder"
 
55
        )
 
56
        sample_event = Event.new_for_values(
 
57
            timestamp=timestamp,
 
58
            interpretation=Interpretation.ACCESS_EVENT,
 
59
            manifestation=Manifestation.USER_ACTIVITY,
 
60
            actor="mailto:sample_subject",
 
61
            subjects=[subject]
 
62
        )
 
63
 
 
64
        zg = ZeitgeistLogger()
 
65
        d = zg.log(sample_event)
 
66
        self.assertEqual(zg.client, None)
 
67
 
 
68
        def verify(result):
 
69
            """Stored result is the empty list, because zg not available."""
 
70
            self.assertEqual(result, [])
 
71
            return result
 
72
 
 
73
        d.addCallback(verify)
 
74
        return d
 
75
 
 
76
def wait_zeitgeist_started(seconds=10):
 
77
    """Wait a few seconds until zg is started, or fail if it can't."""
 
78
    client = None
 
79
    count = 0
 
80
    while client is None:
 
81
        count += 1
 
82
        try:
 
83
            client = ZeitgeistClient()
 
84
            break
 
85
        except RuntimeError:
 
86
            if count > seconds*10:
 
87
                raise
 
88
            time.sleep(0.1)
 
89
 
 
90
 
 
91
class ZeitgeistTestCase(DBusTwistedTestCase):
 
92
    """Tests for the zeitgeist logging module."""
 
93
 
 
94
    def setUp(self):
 
95
        super(ZeitgeistTestCase, self).setUp()
 
96
        zgdaemon = find_executable("zeitgeist-daemon")
 
97
        if not zgdaemon:
 
98
            raise NotFoundError("zeitgeist-daemon was not found.")
 
99
 
 
100
        tempfolder = tempfile.mkdtemp(prefix="test-u1-zeitgeist-")
 
101
        tempstdout = tempfile.TemporaryFile(prefix="test-u1-stdout-")
 
102
        tempstderr = tempfile.TemporaryFile(prefix="test-u1-stderr-")
 
103
        os.environ["ZEITGEIST_DATA_PATH"] = tempfolder
 
104
        p = subprocess.Popen([zgdaemon], bufsize=4096, stdout=tempstdout,
 
105
                             stderr=tempstderr)
 
106
        def cleanup():
 
107
            """Wait for the process to finish."""
 
108
            p.terminate()
 
109
            p.wait()
 
110
            del(os.environ["ZEITGEIST_DATA_PATH"])
 
111
            shutil.rmtree(tempfolder)
 
112
 
 
113
        wait_zeitgeist_started()
 
114
        self.addCleanup(cleanup)
 
115
 
 
116
    def test_log_records_the_event(self):
 
117
        """The log method records the event in zg."""
 
118
        timestamp = int(time.time() * 1000)
 
119
        subject = Subject.new_for_values(
 
120
                uri="file:///tmp/folder1",
 
121
                interpretation=Interpretation.FOLDER,
 
122
                manifestation=Manifestation.FILE_DATA_OBJECT,
 
123
                origin="ubuntuone:uuid",
 
124
                mimetype="inode/directory",
 
125
                text="sample folder"
 
126
        )
 
127
        sample_event = Event.new_for_values(
 
128
            timestamp=timestamp,
 
129
            interpretation=Interpretation.ACCESS_EVENT,
 
130
            manifestation=Manifestation.USER_ACTIVITY,
 
131
            actor="mailto:sample_subject",
 
132
            subjects=[subject]
 
133
        )
 
134
 
 
135
        sample_template = Event.new_for_values()
 
136
 
 
137
        zg = ZeitgeistLogger()
 
138
        self.assertNotEqual(zg.client, None)
 
139
 
 
140
        d2 = Deferred()
 
141
 
 
142
        def logged(id_list):
 
143
            """The event was logged to zeitgeist."""
 
144
 
 
145
            def events_found(event_list):
 
146
                """zg returned the list of events."""
 
147
                self.assertEqual(event_list[0].id, id_list[0])
 
148
                d2.callback("ok")
 
149
 
 
150
            zg.client.find_events_for_template(sample_template, events_found)
 
151
 
 
152
        d = zg.log(sample_event)
 
153
        d.addCallbacks(logged, d2.errback)
 
154
 
 
155
        return d2