#!/usr/bin/python3

# 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/>.

import parent
from testlib import *

@nondestructive
@skipImage("No tuned packaged", "fedora-coreos")
@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestTuned(MachineCase):

    def testBasic(self):
        self.allow_restart_journal_messages()

        b = self.browser
        m = self.machine

        recommended_profile = m.execute("tuned-adm recommend").strip()

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

        m.execute("systemctl stop tuned")
        m.execute("systemctl disable tuned")
        self.addCleanup(m.execute, "tuned-adm auto_profile") # Enable and set default profile

        # Login and check initial state

        self.login_and_go("/system")

        def check_status_tooltip(expected):
            b.mouse("#system-info-performance span", "mouseover")
            b.wait_text("#system-info-performance .tooltip-inner", expected)
            # move mouse away again, to let the tooltip disappear and update
            b.mouse("#system-info-performance span", "mouseout")
            b.wait_not_present("#system-info-performance .tooltip-inner")

        b.wait_text('#system-info-performance .action-trigger', "none")
        check_status_tooltip("Tuned is not running")

        # Start tuned manually. The recommended profile will be activated automatically.
        m.execute("systemctl start tuned")
        b.wait_text('#system-info-performance .action-trigger', recommended_profile)
        check_status_tooltip("This system is using the recommended profile")

        # Disabled when not authorized
        b.relogin('/system', superuser=False)
        trigger_selector = '#system-info-performance .action-trigger:disabled'
        b.wait_text(trigger_selector, recommended_profile)
        b.relogin('/system', superuser=True)

        # Switch the profile

        b.click('#system-info-performance .action-trigger')
        title_selector = ".pf-c-modal-box__header:contains('Change performance profile')"
        b.wait_visible(title_selector)
        body_selector = ".pf-c-modal-box:contains('Change performance profile')"
        b.wait_visible("{0} .list-group-item.active p".format(body_selector))

        # Make sure we see the recommended profile and its badges
        b.wait_in_text("{0} .list-group-item.active p".format(body_selector), recommended_profile)
        b.wait_visible("{0} .list-group-item.active .pf-c-label:contains('recommended')".format(body_selector))
        b.wait_visible("{0} .list-group-item.active .pf-c-label:contains('active')".format(body_selector))

        b.click("{0} .list-group-item p:contains('balanced')".format(body_selector))
        b.wait_visible("{0} .list-group-item.active p:contains('balanced')".format(body_selector))

        b.click("{0} button.apply".format(body_selector))
        b.wait_not_present(title_selector)

        b.wait_text('#system-info-performance .action-trigger', "balanced")
        check_status_tooltip("This system is using a custom profile")

        # Check the status of tuned, it should show the profile
        output = m.execute("tuned-adm active 2>&1 || true")
        self.assertIn("balanced", output)
        output = m.execute("systemctl status tuned")
        self.assertIn("enabled;", output)

        # Now disable tuned
        b.click('#system-info-performance .action-trigger')
        b.wait_visible(title_selector)
        b.wait_visible("{0} .list-group-item.active p".format(body_selector))

        b.click("{0} .list-group-item p:contains('None')".format(body_selector))
        b.wait_visible("{0} .list-group-item.active p:contains('None')".format(body_selector))
        b.click("{0} button.apply".format(body_selector))
        b.wait_not_present(title_selector)

        b.wait_text('#system-info-performance .action-trigger', "none")

        # Check the status of tuned, it should show disabled
        output = m.execute("tuned-adm active 2>&1 || true")
        self.assertIn("No current active profile", output)
        output = m.execute("systemctl status tuned")
        self.assertIn("disabled;", output)

        # Click on the button again
        b.click('#system-info-performance .action-trigger')
        b.wait_visible(title_selector)

        # Tuned should still be disabled
        output = m.execute("systemctl status tuned")
        self.assertIn("disabled;", output)


if __name__ == '__main__':
    test_main()
