util.py 5.13 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2013 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import os
import slapos.util
29
from slapos.util import string_to_boolean
30 31
import tempfile
import unittest
32 33
import shutil
from pwd import getpwnam
34

35
class TestUtil(unittest.TestCase):
36 37 38 39 40 41 42 43 44 45 46 47
  """
  Tests methods available in the slapos.util module.
  """

  def test_mkdir_p_new_directory(self):
    """
    Test that mkdir_p recursively creates a directory.
    """
    root_directory = tempfile.mkdtemp()
    wanted_directory = os.path.join(root_directory, 'foo', 'bar')
    slapos.util.mkdir_p(wanted_directory)
    self.assertTrue(os.path.isdir(wanted_directory))
48
    shutil.rmtree(root_directory)
49 50 51 52 53 54 55 56

  def test_mkdir_already_existing(self):
    """
    Check that mkdir_p doesn't raise if directory already exist.
    """
    root_directory = tempfile.mkdtemp()
    slapos.util.mkdir_p(root_directory)
    self.assertTrue(os.path.isdir(root_directory))
57
    shutil.rmtree(root_directory)
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  def test_chown_directory(self):
    """
    Test that slapos.util.chownDirectory correctly changes owner.
    Note: requires root privileges.
    """
    root_slaptest = tempfile.mkdtemp()
    wanted_directory0 = os.path.join(root_slaptest, 'slap-write0')
    wanted_directory1 = os.path.join(root_slaptest, 'slap-write0', 'write-slap1')
    wanted_directory2 = os.path.join(root_slaptest, 'slap-write0', 'write-slap1', 'write-teste2')
    wanted_directory_mkdir0 = os.makedirs(wanted_directory0, mode=0777)
    wanted_directory_mkdir1 = os.makedirs(wanted_directory1, mode=0777)
    wanted_directory_mkdir2 = os.makedirs(wanted_directory2, mode=0777)
    create_file_txt = tempfile.mkstemp(suffix='.txt', prefix='tmp', dir=wanted_directory2, text=True)
    user = 'nobody'
    try:
      uid = getpwnam(user)[2]
      gid = getpwnam(user)[3]
    except KeyError:
      raise unittest.SkipTest("user %s doesn't exist." % user)
78 79

    if os.getuid() != 0:
80 81
      raise unittest.SkipTest("No root privileges, impossible to chown.")

82 83
    slapos.util.chownDirectory(root_slaptest, uid, gid)

84 85
    uid_check_root_slaptest = os.stat(root_slaptest)[4]
    gid_check_root_slaptest = os.stat(root_slaptest)[5]
86 87
    self.assertEquals(uid, uid_check_root_slaptest)
    self.assertEquals(gid, gid_check_root_slaptest)
88 89 90

    uid_check_wanted_directory0 = os.stat(wanted_directory0)[4]
    gid_check_wanted_directory0 = os.stat(wanted_directory0)[5]
91 92
    self.assertEquals(uid, uid_check_wanted_directory0)
    self.assertEquals(gid, gid_check_wanted_directory0)
93 94 95

    uid_check_wanted_directory1 = os.stat(wanted_directory1)[4]
    gid_check_wanted_directory1 = os.stat(wanted_directory1)[5]
96 97
    self.assertEquals(uid, uid_check_wanted_directory1)
    self.assertEquals(gid, gid_check_wanted_directory1)
98 99 100

    uid_check_wanted_directory2 = os.stat(wanted_directory2)[4]
    gid_check_wanted_directory2 = os.stat(wanted_directory2)[5]
101 102
    self.assertEquals(uid,  uid_check_wanted_directory2)
    self.assertEquals(gid, gid_check_wanted_directory2)
103 104 105

    uid_check_file_txt = os.stat(create_file_txt[1])[4]
    gid_check_file_txt = os.stat(create_file_txt[1])[5]
106 107
    self.assertEquals(uid, uid_check_file_txt)
    self.assertEquals(gid, gid_check_file_txt)
108 109

    shutil.rmtree(root_slaptest)
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  def test_string_to_boolean_with_true_values(self):
    """
    Check that mkdir_p doesn't raise if directory already exist.
    """
    for value in ['true', 'True', 'TRUE']:
      self.assertTrue(string_to_boolean(value))

  def test_string_to_boolean_with_false_values(self):
    """
    Check that mkdir_p doesn't raise if directory already exist.
    """
    for value in ['false', 'False', 'False']:
      self.assertFalse(string_to_boolean(value))

  def test_string_to_boolean_with_incorrect_values(self):
    """
    Check that mkdir_p doesn't raise if directory already exist.
    """
    for value in [True, False, 1, '1', 't', 'tru', 'truelle', 'f', 'fals', 'falsey']:
      self.assertRaises(ValueError, string_to_boolean, value)

132 133
if __name__ == '__main__':
  unittest.main()