#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2018 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 machineslib
import parent
from testlib import *


@allowImage("LibvirtDBus is the only available provider", "ubuntu-1804")
class TestMachinesVirsh(machineslib.TestMachines):

    def removeLibvirtDBus(self):
        m = self.machine
        if ("debian" in self.machine.image) or ("ubuntu" in self.machine.image):
            m.execute("dpkg --remove libvirt-dbus || true")
        else:
            m.execute("rpm -e --nodeps libvirt-dbus || true")

    def setUp(self):
        super().setUp()

        self.removeLibvirtDBus()
        self.provider = "virsh"

    # Test a functionality added exclusively for dbus provider is not visible for virsh provider
    def testAutostart(self):
        b = self.browser

        self.startVm("subVmTest1")

        self.login_and_go("/machines")
        b.wait_in_text("body", "Virtual Machines")
        b.wait_in_text("tbody tr th", "subVmTest1")

        b.click("tbody tr th") # click on the row header
        b.wait_in_text("#vm-subVmTest1-state", "running")
        b.wait_present("#vm-subVmTest1-autostart-checkbox:not(:checked)")


if __name__ == '__main__':
    test_main()
