source: main/waeup.sirp/trunk/src/waeup/sirp/images/tests/test_container.py @ 6293

Last change on this file since 6293 was 6293, checked in by uli, 13 years ago

Add (at least temporarily) some components for storing and maintaining images.

File size: 4.1 KB
Line 
1##
2## test_container.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Jun  7 12:11:06 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22import os
23import logging
24import unittest
25from cStringIO import StringIO
26from waeup.sirp.images.container import ImagesContainer, ImagesContainerPlugin
27
28class FakeSite(dict):
29    pass
30
31class ImagesContainerPluginTests(unittest.TestCase):
32    def create_logger(self):
33        # create a logger suitable for local tests.
34        test_logger = logging.getLogger('waeup.sirp.images.testlogger')
35        log = StringIO()
36        handler = logging.StreamHandler(log)
37        handler.setLevel(logging.DEBUG)
38        test_logger.addHandler(handler)
39        test_logger.setLevel(logging.DEBUG)
40        self.logger = test_logger
41        self.log = log
42        self.handler = handler
43        return self.logger
44
45    def remove_logger(self):
46        del self.handler
47        del self.logger
48        del self.log
49        pass
50
51    def get_log(self):
52        self.log.seek(0)
53        return self.log.read()
54
55    def setUp(self):
56        self.create_logger()
57        return
58
59    def tearDown(self):
60        self.remove_logger()
61        return
62
63    # Real tests start here...
64    def test_pluginsetup(self):
65        # Make sure we can add ImagesContainer to sites.
66        site = FakeSite()
67        plugin = ImagesContainerPlugin()
68        plugin.setup(site, 'blah', self.logger)
69        self.assertTrue('images' in site.keys())
70        log = self.get_log()
71        self.assertTrue('Installed images container.' in log)
72        return
73
74    def test_update_new(self):
75        # Run update on a site without images container.
76        site = FakeSite()
77        plugin = ImagesContainerPlugin()
78        plugin.update(site, 'blah', self.logger)
79        self.assertTrue('images' in site.keys())
80        log = self.get_log()
81        self.assertTrue('Updating site at <Unnamed Site>' in log)
82        self.assertTrue('Installed images container.' in log)
83        return
84
85    def test_update_outdated(self):
86        # Run update on a site with outdated images container.
87        site = FakeSite()
88        root = object() # # This is not a proper images container
89        site['images'] = root
90        plugin = ImagesContainerPlugin()
91        plugin.update(site, 'blah', self.logger)
92        self.assertTrue(site['images'] is not root)
93        self.assertTrue(isinstance(site['images'], ImagesContainer))
94        log = self.get_log()
95        self.assertTrue('Outdated images folder detected' in log)
96        self.assertTrue('Updating site at <Unnamed Site>' in log)
97        self.assertTrue('Installed images container.' in log)
98        return
99
100    def test_update_uptodate(self):
101        # Run update on a site with proper images root.
102        site = FakeSite()
103        root = ImagesContainer()
104        site['images'] = root
105        plugin = ImagesContainerPlugin()
106        plugin.update(site, 'blah', self.logger)
107        self.assertTrue(site['images'] is root)
108        log = self.get_log()
109        self.assertTrue('Updating site at <Unnamed Site>' in log)
110        self.assertTrue('Nothing to do' in log)
111        return
112
113    def test_update_log(self):
114        # Check that sitename is used in log messages on updates.
115        site = FakeSite()
116        site.__name__ = 'my_site'
117        plugin = ImagesContainerPlugin()
118        plugin.update(site, 'blah', self.logger)
119        log = self.get_log()
120        self.assertTrue('Updating site at my_site.' in log)
121        return
122
Note: See TracBrowser for help on using the repository browser.