#!/usr/bin/python
# -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2015 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/>.

from testlib import *
import unittest
import os

@unittest.skipIf("debian" in os.environ.get("TEST_OS", ""), "No tuned on Debian.")
@unittest.skipIf("fedora-atomic" in os.environ.get("TEST_OS", ""), "No tuned on Fedora Atomic.")
class TestTuned(MachineCase):
    def testBasic(self):
        b = self.browser
        m = self.machine

        # Stop tuned in case it is running by default, as on RHEL.

        m.execute("systemctl stop tuned")

        # Login and check initial state

        self.login_and_go("/system")

        b.wait_text('#system-info-performance button', "none")
        b.wait_attr('#system-info-performance [data-toggle="popover"]', 'data-content',
                    "Tuned is not running")

        # Start tuned manually and hit the info button to force an
        # update

        m.execute("systemctl start tuned")
        b.click('#system-info-performance [data-toggle="popover"]')
        if m.image == "rhel-atomic":
            b.wait_text('#system-info-performance button', "atomic-guest")
        else:
            b.wait_text('#system-info-performance button', "virtual-guest")
        b.wait_attr('#system-info-performance [data-toggle="popover"]', 'data-content',
                    "This system is using the recommended profile")

        # Switch the profile

        b.click('#system-info-performance button')
        b.wait_present('#tuned-dialog')
        b.wait_visible('#tuned-dialog')
        if m.image == "rhel-atomic":
            b.wait_in_text('#tuned-dialog .list-group-item.active', "atomic-guest (recommended)")
        else:
            b.wait_in_text('#tuned-dialog .list-group-item.active', "virtual-guest (recommended)")
        b.click('#tuned-dialog [data-profile="balanced"]')
        b.wait_in_text('#tuned-dialog .list-group-item.active', "balanced")
        b.click('#tuned-dialog button.apply')
        b.wait_not_present('#tuned-dialog')

        b.wait_text('#system-info-performance button', "balanced")
        b.wait_attr('#system-info-performance [data-toggle="popover"]', 'data-content',
                    "This system is using a custom profile")

if __name__ == '__main__':
    test_main()
