#!/usr/bin/env python
# -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# ex: set expandtab softtabstop=4 shiftwidth=4:
#
# Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016  Contributor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for testing the reconfigure command."""


import os
import re
from datetime import datetime

import unittest

if __name__ == "__main__":
    from broker import utils
    utils.import_depends()

from broker.brokertest import TestBrokerCommand
from broker.grntest import VerifyGrnsMixin
from broker.notificationtest import VerifyNotificationsMixin


class TestReconfigure(VerifyGrnsMixin, VerifyNotificationsMixin,
                      TestBrokerCommand):
    linux_version_prev = None
    linux_version_curr = None

    # Note that some tests for reconfigure --list appear in
    # test_make_aquilon.py.

    @classmethod
    def setUpClass(cls):
        super(TestReconfigure, cls).setUpClass()
        cls.linux_version_prev = cls.config.get("unittest",
                                                "linux_version_prev")
        cls.linux_version_curr = cls.config.get("unittest",
                                                "linux_version_curr")

    def test_1000_edit_machine_plenary(self):
        # "aq reconfigure" should refresh the machine plenary. We verify that by
        # intentionally breaking it first.
        path = self.plenary_name("machine", "americas", "ut", "ut9",
                                 "ut9s03p45")
        with open(path, "a") as fp:
            fp.write('\n"broken" = "template";\n')

        command = ["cat", "--machine=ut9s03p45"]
        out = self.commandtest(command)
        self.matchoutput(out, '"broken" = "template";', command)

    def test_1001_reconfigure_aquilon95(self):
        command = ["reconfigure", "--hostname=aquilon95.aqd-unittest.ms.com"]
        self.successtest(command)

    def test_1002_verify_machine_plenary(self):
        command = ["cat", "--machine=ut9s03p45"]
        out = self.commandtest(command)
        self.matchclean(out, "broken", command)
        self.matchoutput(out, '"sysloc/room" = "utroom2";', command)
        self.matchoutput(out, '"sysloc/bunker" = "bucket2.ut";', command)
        self.matchoutput(out, '"sysloc/building" = "ut";', command)
        self.matchoutput(out, '"sysloc/city" = "ny";', command)
        self.matchoutput(out, '"sysloc/continent" = "na";', command)
        self.matchoutput(out, '"sysloc/country" = "us";', command)

    def test_1010_reconfigurelist_grn_pre(self):
        hosts = ["aquilon95.aqd-unittest.ms.com",
                 "aquilon91.aqd-unittest.ms.com"]
        for h in hosts:
            command = "show host --hostname %s" % h
            out = self.commandtest(command.split(" "))
            self.matchoutput(out, "Owned by GRN: grn:/ms/ei/aquilon/unittest", command)

    def test_1011_list_grn(self):