This file is indexed.

/usr/share/pyshared/slapos/tests/entry.py is in slapos-node-unofficial 0.35.1-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2012 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 advised 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 shutil
import slapos.entry as entry
import sys
import tempfile
import unittest


class BasicMixin:

  def setUp(self):
    self._tempdir = tempfile.mkdtemp()
    self._original_sys_argv = sys.argv
    sys.argv = ['entry']

  def tearDown(self):
    shutil.rmtree(self._tempdir, True)
    sys.argv = self._original_sys_argv


class TestcheckSlaposCfg (BasicMixin, unittest.TestCase):
  """
  Tests on checkSlaposCfg function
  """

  def test_slapos_cfg_detection_when_no_configuration_given(self):
    """
    If no configuration file is given then False is return
    """
    sys.argv = ['entry', '--logfile', '/log/file']
    self.assertFalse(entry.checkSlaposCfg())

  def test_slapos_cfg_detection_when_file_does_not_exists(self):
    """
    If given configuration file does not exists then False is return
    """
    slapos_cfg = os.path.join(self._tempdir, 'slapos.cfg')
    sys.argv = ['entry', slapos_cfg]
    self.assertFalse(entry.checkSlaposCfg())

  def test_slapos_cfg_detection_when_no_slapos_section(self):
    """
    If given configuration file does not have slapos section
    then False is return
    """
    slapos_cfg = os.path.join(self._tempdir, 'slapos.cfg')
    open(slapos_cfg, 'w').write('[slapformat]')
    sys.argv = ['entry', slapos_cfg]
    self.assertFalse(entry.checkSlaposCfg())

  def test_slapos_cfg_detection_when_correct(self):
    """
    If given configuration file have slapos section
    then True is return
    """
    slapos_cfg = os.path.join(self._tempdir, 'slapos.cfg')
    open(slapos_cfg, 'w').write('[slapos]')
    sys.argv.append(slapos_cfg)
    self.assertTrue(entry.checkSlaposCfg())


class TestcheckOption (BasicMixin, unittest.TestCase):

  def test_present_option_is_not_added(self):
    """
    If the option is already there do not add it
    """
    sys.argv += ['-vc', '--logfile', '/opt/slapos/slapos.log']
    original_sysargv = sys.argv
    entry.checkOption("--logfile /opt/slapos/format.log")
    self.assertEqual(original_sysargv, sys.argv)

  def test_missing_option_is_added(self):
    """
    If the option is not there add it
    """
    sys.argv += ['-vc', '--pidfile', '/opt/slapos/slapos.pid']
    original_sysargv = sys.argv
    option = "--logfile /opt/slapgrid/slapformat.log"
    entry.checkOption(option)
    self.assertNotEqual(original_sysargv, sys.argv)
    self.assertTrue(option in " ".join(sys.argv))


class TestCall (BasicMixin, unittest.TestCase):
  """
  Testing call function
  """
  def test_config_and_option_are_added(self):
    """
    Test missing options and config are added
    """
    sys.argv += ['-vc']
    original_sysargv = sys.argv

    def fun():
      return 0

    options = ["--logfile /opt/slapos/logfile",
               "--pidfile /opt/slapos/pidfile"]
    config = '/etc/opt/slapos/slapos.cfg'
    try:
      entry.call(fun, config=config, option=options)
    except SystemExit, e:
      self.assertEqual(e[0], 0)
    self.assertNotEqual(original_sysargv, sys.argv)
    for x in options:
      self.assertTrue(x in " ".join(sys.argv))
    self.assertEqual(config, sys.argv[1])

  def test_config_and_missing_option_are_added(self):
    """
    Test missing options and config are added but do not replace
    already present option
    """
    missing_option = "--logfile /opt/slapos/logfile"
    present_option = "--pidfile /opt/slapos/pidfile"
    default_present_option = "--pidfile /opt/slapos/pidfile.default"
    sys.argv += ['-vc', present_option]
    original_sysargv = sys.argv

    def fun():
      return 0

    options = [default_present_option, missing_option]
    config = '/etc/opt/slapos/slapos.cfg'
    try:
      entry.call(fun, config=config, option=options)
    except SystemExit, e:
      self.assertEqual(e[0], 0)
    self.assertNotEqual(original_sysargv, sys.argv)
    for x in (missing_option, present_option):
      self.assertTrue(x in " ".join(sys.argv))
    self.assertFalse(default_present_option in " ".join(sys.argv))
    self.assertEqual(config, sys.argv[1])

  def test_present_config_and_option_are_not_added(self):
    """
    Test already present options and config are not added
    """
    present_option = "--pidfile /opt/slapos/pidfile"
    default_present_option = "--pidfile /opt/slapos/pidfile.default"
    slapos_cfg = os.path.join(self._tempdir, 'slapos.cfg')
    open(slapos_cfg, 'w').write('[slapos]')
    sys.argv += ['-vc', slapos_cfg, present_option.split()[0],
                 present_option.split()[1]]
    original_sysargv = sys.argv

    def fun():
      return 0

    options = [default_present_option]
    config = '/etc/opt/slapos/slapos.cfg'
    try:
      entry.call(fun, config=config, option=options)
    except SystemExit, e:
      self.assertEqual(e[0], 0)

    self.assertEqual(original_sysargv, sys.argv)