summaryrefslogtreecommitdiffstats
path: root/src/logicalfile/test/test_base.py
blob: 05e7bb29ce2373964e93264bcb2a8d53ffa7b584 (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
# Copyright (C) 2013-2014 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Jan Synacek <jsynacek@redhat.com>

import os
import shutil
import subprocess
import pyudev

from lmi.test import cimbase

class LogicalFileTestBase(cimbase.CIMTestCase):
    """
    Base class for all LogicalFile tests.
    """

    @classmethod
    def setUpClass(cls):
        cimbase.CIMTestCase.setUpClass.im_func(cls)
        cls.testdir = os.environ.get("LMI_LOGICALFILE_TESTDIR", "/var/tmp/logicalfile-tests")
        cls.selinux_enabled = True
        try:
            rc = subprocess.call(['selinuxenabled'])
            if rc != 0: cls.selinux_enabled = False
        except:
            cls.selinux_enabled = False
        ctx = pyudev.Context()
        sb = os.stat(os.path.realpath(cls.testdir + "/.."))
        device = pyudev.Device.from_device_number(ctx, "block", sb.st_dev)
        dev_name = device.get("ID_FS_UUID_ENC")
        if not dev_name:
            cls.fsname = "DEVICE=" + device.get("DEVNAME")
        else:
            cls.fsname = "UUID=" + dev_name

    def setUp(self):
        pass

    def tearDown(self):
        pass


class Configuration():
    """
    Ini-like configuration.

    {group : [(key1, value1), (key2, value2), ...]}

    """
    def __init__(self, path=None):
        self.configuration = dict()
        self.path = path
        if path:
            self.backupfile = path + '.backup_openlmi_logicalfile'
        self.backup = None
        self.original_path = None

    def add(self, group, options):
        if self.configuration.has_key(group):
            self.configuration[group].extend(options)
        else:
            self.configuration[group] = options

    def save(self):
        def get_original_path(path):
            """
            Compute already existing path when creating a directory. Returns
            path to original directory or the whole path.

            Example:
            Suppose there already exists '/tmp/stuff/' directory.
            get_original_path('/tmp/stuff/new/directory/file.txt') will return '/tmp/stuff/'.
            get_original_path('/tmp/stuff/file.txt') will return '/tmp/stuff/file.txt'.
            """
            (head, tail) = os.path.split(path)
            while (head != ''):
                if os.path.exists(head):
                    return os.path.join(head, tail)
                (head, tail) = os.path.split(head)
            return None

        if os.path.exists(self.path):
            # backup
            self.backup = self.backupfile
            shutil.move(self.path, self.backupfile)
        else:
            # create new
            self.original_path = get_original_path(self.path)
            d = os.path.dirname(self.path)
            if (self.path != self.original_path):
                os.makedirs(os.path.dirname(self.path))

        f = open(self.path, 'w')
        for group in self.configuration.keys():
            f.write('[' + group + ']' + '\n')
            for (key, val) in self.configuration[group]:
                f.write(key + '=' + val + '\n')
        f.close()

    def dispose(self):
        if self.backup:
            shutil.move(self.backupfile, self.path)
        elif self.original_path:
            if self.path != self.original_path:
                shutil.rmtree(self.original_path)
            else:
                os.unlink(self.path)
        self.backup = None
        self.original_path = None