test_check_free_disk_space.py 3.63 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2018 Vifib SARL and Contributors. All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#
# 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.
#
##############################################################################

28 29
from slapos.test.promise.plugin import TestPromisePluginMixin
from slapos.grid.promise import PromiseError
30 31 32 33
import os
import sqlite3
from slapos.test.promise import data
from slapos.promise.check_free_disk import getFreeSpace
34
from slapos.grid.promise import PromiseError
35

36
class TestCheckFreeDiskSpace(TestPromisePluginMixin):
37 38

  def setUp(self):
39 40 41 42
    TestPromisePluginMixin.setUp(self)
    log_folder = os.path.join(self.partition_dir, 'var/log')
    os.makedirs(log_folder)

43
    self.db_file = '/tmp/collector.db'
44
    self.base_path = "/".join(data.__file__.split("/")[:-1])
45 46 47 48 49 50

    # populate db
    self.conn = sqlite3.connect(self.db_file)
    f = open(self.base_path+"/disktest.sql")
    sql = f.read()
    self.conn.executescript(sql)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    self.conn.close()

    self.promise_name = "check-free-disk-space.py"
    self.th_file = os.path.join(self.partition_dir, 'min-disk-value')
    with open(self.th_file, 'w') as f:
      f.write('2048')

    content = """from slapos.promise.plugin.check_free_disk_space import RunPromise

extra_config_dict = {
  'collectordb': '%(collectordb)s',
  'threshold-file': '%(th_file)s',
  'test-check-date': '2017-10-02',
}
""" % {'collectordb': self.db_file, 'th_file': self.th_file}
    self.writePromise(self.promise_name, content)

  def tearDown(self):
    TestPromisePluginMixin.tearDown(self)
    if os.path.exists(self.db_file):
      os.remove(self.db_file)
72 73 74 75 76 77 78 79

  def test_check_disk(self):
    self.assertEquals(288739385344,
      getFreeSpace('/dev/sda1', '/tmp', '2017-10-02', '09:27'))

  def test_check_free_disk_with_unavailable_dates(self):
    self.assertEquals(0, getFreeSpace('/', '/tmp', '18:00', '2017-09-14'))

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  def test_disk_space_ok(self):
    self.configureLauncher()
    self.launcher.run()
    result = self.getPromiseResult(self.promise_name)
    self.assertEquals(result['result']['failed'], False)
    self.assertEquals(result['result']['message'], "Disk usage: OK")

  def test_disk_space_nok(self):
    with open(self.th_file, 'w') as f:
      f.write('298927494144')
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    result = self.getPromiseResult(self.promise_name)
    self.assertEquals(result['result']['failed'], True)
    self.assertEquals(result['result']['message'], "Free disk space low: remaining 269.1 G (threshold: 291921381.0 G)")
96 97 98

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