1 #!/usr/bin/env python2.6
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken 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 Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 # This file is used to test reading and processing of config files
28 from shinken_test
import unittest
30 from shinken
.daemon
import InvalidPidDir
, InvalidWorkDir
31 from shinken
.pyro_wrapper
import PortNotFree
33 from shinken
.daemons
.pollerdaemon
import Poller
34 # all shinken-* services are subclassing Daemon so we only need to test one normally...
38 # and we can use its default/dev config :
39 pollerconfig
= "../etc/pollerd.ini"
43 class Test_Daemon_Bad_Start(unittest
.TestCase
):
45 def gen_invalid_directory(self
, f
):
46 basedir
= "/invalid_directory42/" + str(random
.randint(0,100))
47 while os
.path
.exists(basedir
):
48 basedir
= os
.path
.join(basedir
, str(random
.randint(0,100)))
49 return os
.path
.join(basedir
, f
)
51 def get_login_and_group(self
, p
):
54 except OSError: # on some rare case, we can have a problem here
55 # so bypass it and keep default value
57 p
.user
= p
.group
= user
59 def get_poller_daemon(self
):
61 p
= Poller(pollerconfig
, False, True, False, None)
63 p
.port
= 0 # let's here choose automatic port attribution..
64 self
.get_login_and_group(p
)
67 def test_bad_piddir(self
):
68 print("Testing bad piddir ... mypid=%d" % (os
.getpid()))
69 p
= self
.get_poller_daemon()
70 p
.pidfile
= self
.gen_invalid_directory(p
.pidfile
)
71 self
.assertRaises(InvalidPidDir
, p
.do_daemon_init_and_start
)
74 def test_bad_workdir(self
):
75 print("Testing bad workdir ... mypid=%d" % (os
.getpid()))
76 p
= self
.get_poller_daemon()
77 p
.workdir
= self
.gen_invalid_directory(p
.workdir
)
78 self
.assertRaises(InvalidWorkDir
, p
.do_daemon_init_and_start
)
81 def test_port_not_free(self
):
83 print("Testing port not free ... mypid=%d" % (os
.getpid()))
84 p1
= self
.get_poller_daemon()
85 p1
.do_daemon_init_and_start()
86 os
.unlink(p1
.pidfile
) ## so that second poller will not see first started poller
87 p2
= self
.get_poller_daemon()
88 p2
.port
= p1
.pyro_daemon
.port
89 self
.assertRaises(PortNotFree
, p2
.do_daemon_init_and_start
)
92 if __name__
== '__main__':