-from ganeti import serializer
-from ganeti.utils import IsProcessAlive, RunCmd, \
- RemoveFile, MatchNameComponent, FormatUnit, \
- ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
- ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
- SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \
- TailFile, ForceDictType, SafeEncode, IsNormAbsPath, FormatTime, \
- UnescapeAndSplit, RunParts, PathJoin, HostInfo
-
-from ganeti.errors import LockError, UnitParseError, GenericError, \
- ProgrammerError, OpPrereqError
-
-
-class TestIsProcessAlive(unittest.TestCase):
- """Testing case for IsProcessAlive"""
-
- def testExists(self):
- mypid = os.getpid()
- self.assert_(IsProcessAlive(mypid),
- "can't find myself running")
-
- def testNotExisting(self):
- pid_non_existing = os.fork()
- if pid_non_existing == 0:
- os._exit(0)
- elif pid_non_existing < 0:
- raise SystemError("can't fork")
- os.waitpid(pid_non_existing, 0)
- self.assert_(not IsProcessAlive(pid_non_existing),
- "nonexisting process detected")
-
-
-class TestPidFileFunctions(unittest.TestCase):
- """Tests for WritePidFile, RemovePidFile and ReadPidFile"""
-
- def setUp(self):
- self.dir = tempfile.mkdtemp()
- self.f_dpn = lambda name: os.path.join(self.dir, "%s.pid" % name)
- utils.DaemonPidFileName = self.f_dpn
-
- def testPidFileFunctions(self):
- pid_file = self.f_dpn('test')
- utils.WritePidFile('test')
- self.failUnless(os.path.exists(pid_file),
- "PID file should have been created")
- read_pid = utils.ReadPidFile(pid_file)
- self.failUnlessEqual(read_pid, os.getpid())
- self.failUnless(utils.IsProcessAlive(read_pid))
- self.failUnlessRaises(GenericError, utils.WritePidFile, 'test')
- utils.RemovePidFile('test')
- self.failIf(os.path.exists(pid_file),
- "PID file should not exist anymore")
- self.failUnlessEqual(utils.ReadPidFile(pid_file), 0,
- "ReadPidFile should return 0 for missing pid file")
- fh = open(pid_file, "w")
- fh.write("blah\n")
- fh.close()
- self.failUnlessEqual(utils.ReadPidFile(pid_file), 0,
- "ReadPidFile should return 0 for invalid pid file")
- utils.RemovePidFile('test')
- self.failIf(os.path.exists(pid_file),
- "PID file should not exist anymore")
-
- def testKill(self):
- pid_file = self.f_dpn('child')
- r_fd, w_fd = os.pipe()
- new_pid = os.fork()
- if new_pid == 0: #child
- utils.WritePidFile('child')
- os.write(w_fd, 'a')
- signal.pause()
- os._exit(0)
- return
- # else we are in the parent
- # wait until the child has written the pid file
- os.read(r_fd, 1)
- read_pid = utils.ReadPidFile(pid_file)
- self.failUnlessEqual(read_pid, new_pid)
- self.failUnless(utils.IsProcessAlive(new_pid))
- utils.KillProcess(new_pid, waitpid=True)
- self.failIf(utils.IsProcessAlive(new_pid))
- utils.RemovePidFile('child')
- self.failUnlessRaises(ProgrammerError, utils.KillProcess, 0)
-
- def tearDown(self):
- for name in os.listdir(self.dir):
- os.unlink(os.path.join(self.dir, name))
- os.rmdir(self.dir)
-
-
-class TestRunCmd(testutils.GanetiTestCase):
- """Testing case for the RunCmd function"""
-
- def setUp(self):
- testutils.GanetiTestCase.setUp(self)
- self.magic = time.ctime() + " ganeti test"
- self.fname = self._CreateTempFile()
-
- def testOk(self):
- """Test successful exit code"""
- result = RunCmd("/bin/sh -c 'exit 0'")
- self.assertEqual(result.exit_code, 0)
- self.assertEqual(result.output, "")
-
- def testFail(self):
- """Test fail exit code"""
- result = RunCmd("/bin/sh -c 'exit 1'")
- self.assertEqual(result.exit_code, 1)
- self.assertEqual(result.output, "")
-
- def testStdout(self):
- """Test standard output"""
- cmd = 'echo -n "%s"' % self.magic
- result = RunCmd("/bin/sh -c '%s'" % cmd)
- self.assertEqual(result.stdout, self.magic)
- result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname)
- self.assertEqual(result.output, "")
- self.assertFileContent(self.fname, self.magic)
-
- def testStderr(self):
- """Test standard error"""
- cmd = 'echo -n "%s"' % self.magic
- result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd)
- self.assertEqual(result.stderr, self.magic)
- result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd, output=self.fname)
- self.assertEqual(result.output, "")
- self.assertFileContent(self.fname, self.magic)
-
- def testCombined(self):
- """Test combined output"""
- cmd = 'echo -n "A%s"; echo -n "B%s" 1>&2' % (self.magic, self.magic)
- expected = "A" + self.magic + "B" + self.magic
- result = RunCmd("/bin/sh -c '%s'" % cmd)
- self.assertEqual(result.output, expected)
- result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname)
- self.assertEqual(result.output, "")
- self.assertFileContent(self.fname, expected)
-
- def testSignal(self):
- """Test signal"""
- result = RunCmd(["python", "-c", "import os; os.kill(os.getpid(), 15)"])
- self.assertEqual(result.signal, 15)
- self.assertEqual(result.output, "")
-
- def testListRun(self):
- """Test list runs"""
- result = RunCmd(["true"])
- self.assertEqual(result.signal, None)
- self.assertEqual(result.exit_code, 0)
- result = RunCmd(["/bin/sh", "-c", "exit 1"])
- self.assertEqual(result.signal, None)
- self.assertEqual(result.exit_code, 1)
- result = RunCmd(["echo", "-n", self.magic])
- self.assertEqual(result.signal, None)
- self.assertEqual(result.exit_code, 0)
- self.assertEqual(result.stdout, self.magic)
-
- def testFileEmptyOutput(self):
- """Test file output"""
- result = RunCmd(["true"], output=self.fname)
- self.assertEqual(result.signal, None)
- self.assertEqual(result.exit_code, 0)
- self.assertFileContent(self.fname, "")
-
- def testLang(self):
- """Test locale environment"""
- old_env = os.environ.copy()
- try:
- os.environ["LANG"] = "en_US.UTF-8"
- os.environ["LC_ALL"] = "en_US.UTF-8"
- result = RunCmd(["locale"])
- for line in result.output.splitlines():
- key, value = line.split("=", 1)
- # Ignore these variables, they're overridden by LC_ALL
- if key == "LANG" or key == "LANGUAGE":
- continue
- self.failIf(value and value != "C" and value != '"C"',
- "Variable %s is set to the invalid value '%s'" % (key, value))
- finally:
- os.environ = old_env
-
- def testDefaultCwd(self):
- """Test default working directory"""
- self.failUnlessEqual(RunCmd(["pwd"]).stdout.strip(), "/")
-
- def testCwd(self):
- """Test default working directory"""
- self.failUnlessEqual(RunCmd(["pwd"], cwd="/").stdout.strip(), "/")
- self.failUnlessEqual(RunCmd(["pwd"], cwd="/tmp").stdout.strip(), "/tmp")
- cwd = os.getcwd()
- self.failUnlessEqual(RunCmd(["pwd"], cwd=cwd).stdout.strip(), cwd)
-
- def testResetEnv(self):
- """Test environment reset functionality"""
- self.failUnlessEqual(RunCmd(["env"], reset_env=True).stdout.strip(), "")
- self.failUnlessEqual(RunCmd(["env"], reset_env=True,
- env={"FOO": "bar",}).stdout.strip(), "FOO=bar")
-
-
-class TestRunParts(unittest.TestCase):
- """Testing case for the RunParts function"""
-
- def setUp(self):
- self.rundir = tempfile.mkdtemp(prefix="ganeti-test", suffix=".tmp")
-
- def tearDown(self):
- shutil.rmtree(self.rundir)
-
- def testEmpty(self):
- """Test on an empty dir"""
- self.failUnlessEqual(RunParts(self.rundir, reset_env=True), [])
-
- def testSkipWrongName(self):
- """Test that wrong files are skipped"""
- fname = os.path.join(self.rundir, "00test.dot")
- utils.WriteFile(fname, data="")
- os.chmod(fname, stat.S_IREAD | stat.S_IEXEC)
- relname = os.path.basename(fname)
- self.failUnlessEqual(RunParts(self.rundir, reset_env=True),
- [(relname, constants.RUNPARTS_SKIP, None)])
-
- def testSkipNonExec(self):
- """Test that non executable files are skipped"""
- fname = os.path.join(self.rundir, "00test")
- utils.WriteFile(fname, data="")
- relname = os.path.basename(fname)
- self.failUnlessEqual(RunParts(self.rundir, reset_env=True),
- [(relname, constants.RUNPARTS_SKIP, None)])
-
- def testError(self):
- """Test error on a broken executable"""
- fname = os.path.join(self.rundir, "00test")
- utils.WriteFile(fname, data="")
- os.chmod(fname, stat.S_IREAD | stat.S_IEXEC)
- (relname, status, error) = RunParts(self.rundir, reset_env=True)[0]
- self.failUnlessEqual(relname, os.path.basename(fname))
- self.failUnlessEqual(status, constants.RUNPARTS_ERR)
- self.failUnless(error)
-
- def testSorted(self):
- """Test executions are sorted"""
- files = []
- files.append(os.path.join(self.rundir, "64test"))
- files.append(os.path.join(self.rundir, "00test"))
- files.append(os.path.join(self.rundir, "42test"))
-
- for fname in files:
- utils.WriteFile(fname, data="")
-
- results = RunParts(self.rundir, reset_env=True)
-
- for fname in sorted(files):
- self.failUnlessEqual(os.path.basename(fname), results.pop(0)[0])
-
- def testOk(self):
- """Test correct execution"""
- fname = os.path.join(self.rundir, "00test")
- utils.WriteFile(fname, data="#!/bin/sh\n\necho -n ciao")
- os.chmod(fname, stat.S_IREAD | stat.S_IEXEC)
- (relname, status, runresult) = RunParts(self.rundir, reset_env=True)[0]
- self.failUnlessEqual(relname, os.path.basename(fname))
- self.failUnlessEqual(status, constants.RUNPARTS_RUN)
- self.failUnlessEqual(runresult.stdout, "ciao")
-
- def testRunFail(self):
- """Test correct execution, with run failure"""
- fname = os.path.join(self.rundir, "00test")
- utils.WriteFile(fname, data="#!/bin/sh\n\nexit 1")
- os.chmod(fname, stat.S_IREAD | stat.S_IEXEC)
- (relname, status, runresult) = RunParts(self.rundir, reset_env=True)[0]
- self.failUnlessEqual(relname, os.path.basename(fname))
- self.failUnlessEqual(status, constants.RUNPARTS_RUN)
- self.failUnlessEqual(runresult.exit_code, 1)
- self.failUnless(runresult.failed)
-
- def testRunMix(self):
- files = []
- files.append(os.path.join(self.rundir, "00test"))
- files.append(os.path.join(self.rundir, "42test"))
- files.append(os.path.join(self.rundir, "64test"))
- files.append(os.path.join(self.rundir, "99test"))
-
- files.sort()
-
- # 1st has errors in execution
- utils.WriteFile(files[0], data="#!/bin/sh\n\nexit 1")
- os.chmod(files[0], stat.S_IREAD | stat.S_IEXEC)
-
- # 2nd is skipped
- utils.WriteFile(files[1], data="")
-
- # 3rd cannot execute properly
- utils.WriteFile(files[2], data="")
- os.chmod(files[2], stat.S_IREAD | stat.S_IEXEC)
-
- # 4th execs
- utils.WriteFile(files[3], data="#!/bin/sh\n\necho -n ciao")
- os.chmod(files[3], stat.S_IREAD | stat.S_IEXEC)
-
- results = RunParts(self.rundir, reset_env=True)
-
- (relname, status, runresult) = results[0]
- self.failUnlessEqual(relname, os.path.basename(files[0]))
- self.failUnlessEqual(status, constants.RUNPARTS_RUN)
- self.failUnlessEqual(runresult.exit_code, 1)
- self.failUnless(runresult.failed)
-
- (relname, status, runresult) = results[1]
- self.failUnlessEqual(relname, os.path.basename(files[1]))
- self.failUnlessEqual(status, constants.RUNPARTS_SKIP)
- self.failUnlessEqual(runresult, None)
-
- (relname, status, runresult) = results[2]
- self.failUnlessEqual(relname, os.path.basename(files[2]))
- self.failUnlessEqual(status, constants.RUNPARTS_ERR)
- self.failUnless(runresult)
-
- (relname, status, runresult) = results[3]
- self.failUnlessEqual(relname, os.path.basename(files[3]))
- self.failUnlessEqual(status, constants.RUNPARTS_RUN)
- self.failUnlessEqual(runresult.output, "ciao")
- self.failUnlessEqual(runresult.exit_code, 0)
- self.failUnless(not runresult.failed)
-
-
-class TestStartDaemon(testutils.GanetiTestCase):
- def setUp(self):
- self.tmpdir = tempfile.mkdtemp(prefix="ganeti-test")
- self.tmpfile = os.path.join(self.tmpdir, "test")
-
- def tearDown(self):
- shutil.rmtree(self.tmpdir)
-
- def testShell(self):
- utils.StartDaemon("echo Hello World > %s" % self.tmpfile)
- self._wait(self.tmpfile, 60.0, "Hello World")
-
- def testShellOutput(self):
- utils.StartDaemon("echo Hello World", output=self.tmpfile)
- self._wait(self.tmpfile, 60.0, "Hello World")
-
- def testNoShellNoOutput(self):
- utils.StartDaemon(["pwd"])
-
- def testNoShellNoOutputTouch(self):
- testfile = os.path.join(self.tmpdir, "check")
- self.failIf(os.path.exists(testfile))
- utils.StartDaemon(["touch", testfile])
- self._wait(testfile, 60.0, "")
-
- def testNoShellOutput(self):
- utils.StartDaemon(["pwd"], output=self.tmpfile)
- self._wait(self.tmpfile, 60.0, "/")
-
- def testNoShellOutputCwd(self):
- utils.StartDaemon(["pwd"], output=self.tmpfile, cwd=os.getcwd())
- self._wait(self.tmpfile, 60.0, os.getcwd())
-
- def testShellEnv(self):
- utils.StartDaemon("echo \"$GNT_TEST_VAR\"", output=self.tmpfile,
- env={ "GNT_TEST_VAR": "Hello World", })
- self._wait(self.tmpfile, 60.0, "Hello World")
-
- def testNoShellEnv(self):
- utils.StartDaemon(["printenv", "GNT_TEST_VAR"], output=self.tmpfile,
- env={ "GNT_TEST_VAR": "Hello World", })
- self._wait(self.tmpfile, 60.0, "Hello World")
-
- def testOutputFd(self):
- fd = os.open(self.tmpfile, os.O_WRONLY | os.O_CREAT)
- try:
- utils.StartDaemon(["pwd"], output_fd=fd, cwd=os.getcwd())
- finally:
- os.close(fd)
- self._wait(self.tmpfile, 60.0, os.getcwd())
-
- def testPid(self):
- pid = utils.StartDaemon("echo $$ > %s" % self.tmpfile)
- self._wait(self.tmpfile, 60.0, str(pid))
-
- def testPidFile(self):
- pidfile = os.path.join(self.tmpdir, "pid")
- checkfile = os.path.join(self.tmpdir, "abort")
-
- pid = utils.StartDaemon("while sleep 5; do :; done", pidfile=pidfile,
- output=self.tmpfile)
- try:
- fd = os.open(pidfile, os.O_RDONLY)
- try:
- # Check file is locked
- self.assertRaises(errors.LockError, utils.LockFile, fd)
-
- pidtext = os.read(fd, 100)
- finally:
- os.close(fd)
-
- self.assertEqual(int(pidtext.strip()), pid)
-
- self.assert_(utils.IsProcessAlive(pid))
- finally:
- # No matter what happens, kill daemon
- utils.KillProcess(pid, timeout=5.0, waitpid=False)
- self.failIf(utils.IsProcessAlive(pid))
-
- self.assertEqual(utils.ReadFile(self.tmpfile), "")
-
- def _wait(self, path, timeout, expected):
- # Due to the asynchronous nature of daemon processes, polling is necessary.
- # A timeout makes sure the test doesn't hang forever.
- def _CheckFile():
- if not (os.path.isfile(path) and
- utils.ReadFile(path).strip() == expected):
- raise utils.RetryAgain()
-
- try:
- utils.Retry(_CheckFile, (0.01, 1.5, 1.0), timeout)
- except utils.RetryTimeout:
- self.fail("Apparently the daemon didn't run in %s seconds and/or"
- " didn't write the correct output" % timeout)
-
- def testError(self):
- self.assertRaises(errors.OpExecError, utils.StartDaemon,
- ["./does-NOT-EXIST/here/0123456789"])
- self.assertRaises(errors.OpExecError, utils.StartDaemon,
- ["./does-NOT-EXIST/here/0123456789"],
- output=os.path.join(self.tmpdir, "DIR/NOT/EXIST"))
- self.assertRaises(errors.OpExecError, utils.StartDaemon,
- ["./does-NOT-EXIST/here/0123456789"],
- cwd=os.path.join(self.tmpdir, "DIR/NOT/EXIST"))
- self.assertRaises(errors.OpExecError, utils.StartDaemon,
- ["./does-NOT-EXIST/here/0123456789"],
- output=os.path.join(self.tmpdir, "DIR/NOT/EXIST"))
-
- fd = os.open(self.tmpfile, os.O_WRONLY | os.O_CREAT)
- try:
- self.assertRaises(errors.ProgrammerError, utils.StartDaemon,
- ["./does-NOT-EXIST/here/0123456789"],
- output=self.tmpfile, output_fd=fd)
- finally:
- os.close(fd)
-
-
-class TestSetCloseOnExecFlag(unittest.TestCase):
- """Tests for SetCloseOnExecFlag"""
-
- def setUp(self):
- self.tmpfile = tempfile.TemporaryFile()
-
- def testEnable(self):
- utils.SetCloseOnExecFlag(self.tmpfile.fileno(), True)
- self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
- fcntl.FD_CLOEXEC)
-
- def testDisable(self):
- utils.SetCloseOnExecFlag(self.tmpfile.fileno(), False)
- self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
- fcntl.FD_CLOEXEC)
-
-
-class TestSetNonblockFlag(unittest.TestCase):
- def setUp(self):
- self.tmpfile = tempfile.TemporaryFile()
-
- def testEnable(self):
- utils.SetNonblockFlag(self.tmpfile.fileno(), True)
- self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
- os.O_NONBLOCK)
-
- def testDisable(self):
- utils.SetNonblockFlag(self.tmpfile.fileno(), False)
- self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
- os.O_NONBLOCK)
-