# Copyright (C) 2014 Canonical Ltd.
# Author: Michael Vogt <michael.vogt@ubuntu.com>

# 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; version 3 of the License.
#
# 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, see <http://www.gnu.org/licenses/>.

"""Integration tests for the click chroot feature."""

import subprocess

from .helpers import (
    require_network,
    require_root,
    ClickTestCase,
)


class TestChroot(ClickTestCase):

    @classmethod
    def setUpClass(cls):
        super(TestChroot, cls).setUpClass()
        require_root()
        require_network()
        cls.arch = subprocess.check_output(
            ["dpkg", "--print-architecture"], universal_newlines=True).strip()
        subprocess.check_call([
            cls.click_binary,
            "chroot", "-a", cls.arch,
            "create"])

    @classmethod
    def tearDownClass(cls):
        subprocess.check_call([
            cls.click_binary,
            "chroot", "-a", cls.arch,
            "destroy"])

    def test_upgrade(self):
        subprocess.check_call([
            self.click_binary, "chroot", "-a", self.arch,
            "upgrade"])

    def test_install(self):
        subprocess.check_call([
            self.click_binary, "chroot", "-a", self.arch,
            "install", "apt-utils"])

    def test_run(self):
        output = subprocess.check_output([
            self.click_binary, "chroot", "-a", self.arch,
            "run", "echo", "hello world"], universal_newlines=True)
        self.assertEqual(output, "hello world\n")

    def test_maint(self):
        output = subprocess.check_output([
            self.click_binary, "chroot", "-a", self.arch,
            "maint", "id"], universal_newlines=True)
        self.assertEqual(output, "uid=0(root) gid=0(root) groups=0(root)\n")

    def test_exists_ok(self):
        subprocess.check_call([
            self.click_binary, "chroot", "-a", self.arch, "exists"])

    def test_exists_no(self):
        with self.assertRaises(subprocess.CalledProcessError):
            subprocess.check_call([
                self.click_binary,
                "chroot", "-a", "arch-that-does-not-exist"])
