#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import sys
import time
import unittest

import parent
from testlib import *


@skipPackage("example")
class TestExample(MachineCase):

    @unittest.expectedFailure
    def testFail(self):
        self.login_and_go("/system")
        self.assertFalse(True)

    @unittest.skip
    def testSkip(self):
        self.login_and_go("/system")
        self.assertFalse(True)

    def testRaiseSkip(self):
        raise unittest.SkipTest("dynamic skip")

    @nondestructive
    def testNondestructive(self):
        self.login_and_go("/system")

    def testBasic(self):
        self.login_and_go("/system")


@nondestructive
class TestNondestructiveExample(MachineCase):

    def testOne(self):
        self.assertEqual(self.machine.execute("whoami").strip(), "root")
        # existing file
        self.machine.execute("echo original > /etc/someconfig")
        self.restore_file("/etc/someconfig")
        self.machine.execute("echo changed > /etc/someconfig")
        # nonexisting file
        self.restore_file("/var/lib/cockpittest.txt")
        self.machine.execute("echo data > /var/lib/cockpittest.txt")

        # existing dir
        self.machine.execute("mkdir -p /var/lib/existing_dir && echo hello > /var/lib/existing_dir/original")
        self.restore_dir("/var/lib/existing_dir")
        self.machine.execute("rm /var/lib/existing_dir/original && echo pwned > /var/lib/existing_dir/new")
        # nonexisting dir
        self.restore_dir("/var/lib/cockpittestnew")
        self.machine.execute("mkdir -p /var/lib/cockpittestnew && echo hello > /var/lib/cockpittestnew/cruft")

        # NSS is backed up by default
        self.machine.execute("useradd cockpittest")
        # marker that VM stays running in between tests
        self.machine.execute("touch /root/the_file")

    def testTwo(self):
        self.assertIn("usr", self.machine.execute("ls -l /").strip())
        # testOne correctly restored existing file
        self.assertEqual("original", self.machine.execute("cat /etc/someconfig").strip())
        self.machine.execute("rm /etc/someconfig")
        # testOne correctly cleaned up nonexisting file
        self.machine.execute("test ! -e /var/lib/cockpittest.txt")

        # testOne correctly restored existing dir
        self.assertEqual("original", self.machine.execute("ls /var/lib/existing_dir").strip())
        self.assertEqual("hello\n", self.machine.execute("cat /var/lib/existing_dir/original"))
        self.machine.execute("rm -r /var/lib/existing_dir")
        # testOne correctly removed nonexisting dir
        self.machine.execute("test ! -e /var/lib/cockpittestnew")

        # NSS/home got restored
        self.assertNotIn("cockpittest", self.machine.execute("cat /etc/passwd"))
        self.machine.execute("test ! -e /home/cockpittest")

        # machine was not reset after testOne
        self.machine.execute("[ -f /root/the_file ] || exit 1")


class TestSimple(unittest.TestCase):

    def testOne(self):
        for i in range(0, 10):
            time.sleep(0.100)
            if i % 2:
                sys.stderr.write(">1%i\n" % i)
            else:
                print(">2", i)
        self.assertTrue(True)

    @unittest.expectedFailure
    def testTwo(self):
        self.assertFalse(True)

    def testThree(self):
        self.assertTrue(True)


if __name__ == '__main__':
    test_main()
