# Copyright 2017 The Armada Authors.
#
# 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.

import copy
import json
import os
import yaml

import testtools

from armada.handlers.override import Override
from armada.exceptions import override_exceptions
from armada import const


class OverrideTestCase(testtools.TestCase):
    def setUp(self):
        super(OverrideTestCase, self).setUp()
        self.basepath = os.path.join(os.path.dirname(__file__))
        self.base_manifest = '{}/templates/base.yaml'.format(self.basepath)

    def test_update_manifests_no_overrides_and_values_valid(self):
        with open(self.base_manifest) as f:
            documents = list(yaml.safe_load_all(f.read()))
            ovr = Override(documents)
            ovr.update_manifests()
            # no updates since no overrides and values provided
            self.assertEqual(documents, ovr.documents)

    def test_update_manifests_with_values_valid(self):
        original = "{}/templates/override-{}.yaml".format(self.basepath, '01')
        values_yaml = "{}/templates/override-{}-expected.yaml".format(
            self.basepath, '01')
        with open(original) as f, open(values_yaml) as g:
            original_documents = list(yaml.safe_load_all(f.read()))
            documents_copy = copy.deepcopy(original_documents)
            values_documents = list(yaml.safe_load_all(g.read()))
        ovr = Override(original_documents, None, [values_yaml])
        ovr.update_manifests()
        # updating values changed the original document
        self.assertNotEqual(original_documents, documents_copy)
        # verifying that these documents have the same value now
        self.assertEqual(original_documents, values_documents)

    def test_update_manifests_with_values_and_overrides_valid(self):
        values_yaml = "{}/templates/override-{}-expected.yaml".format(
            self.basepath, '01')
        comparison_yaml = "{}/templates/override-{}-expected.yaml".format(
            self.basepath, '03')

        with open(self.base_manifest) as f, open(values_yaml) as g:
            original_documents = list(yaml.safe_load_all(f.read()))
            documents_copy = copy.deepcopy(original_documents)
            values_documents = list(yaml.safe_load_all(g.read()))

        override = ('manifest:simple-armada:release_prefix='
                    'overridden',)

        # Case 1: Checking if primitive gets updated.
        ovr = Override(original_documents, override, [values_yaml])
        ovr.update_manifests()

        # updating values changed the original document
        self.assertNotEqual(original_documents, documents_copy)
        # since overrides done, these documents aren't same anymore
        self.assertNotEqual(original_documents, values_documents)
        target_doc = [x
                      for x
                      in ovr.documents
                      if x.get('metadata').get('name') == 'simple-armada'][0]
        self.assertEqual('overridden',
                         target_doc['data']['release_prefix'])

        override = ('manifest:simple-armada:chart_groups='
                    'blog-group3,blog-group4',)

        # Case 2: Checking if list gets updated.
        ovr = Override(original_documents, override, [values_yaml])
        ovr.update_manifests()
        # updating values changed the original document
        self.assertNotEqual(original_documents, documents_copy)
        # since overrides done, these documents aren't same anymore
        self.assertNotEqual(original_documents, values_documents)
        with open(comparison_yaml) as c:
            comparison_documents = list(yaml.safe_load_all(c.read()))
        # verifying that the override is correct
        self.assertEqual(original_documents[2]['data']['chart_groups'],
                         comparison_documents[0]['data']['chart_groups'])

    def test_update_manifests_invalid_override_format(self):
        with open(self.base_manifest) as f:
            original_documents = list(yaml.safe_load_all(f.read()))

        original_documents[-1]['data']['test'] = {'foo': 'bar'}
        override = ('manifest:simple-armada:test='
                    '{"foo": "bar"}',)

        ovr = Override(original_documents, override, [])
        self.assertRaises(json.decoder.JSONDecodeError, ovr.update_manifests)

    def test_load_yaml_file(self):
        with open(self.base_manifest) as f:
            documents = list(yaml.safe_load_all(f.read()))
            ovr = Override(documents)
            value = ovr._load_yaml_file(self.base_manifest)
            self.assertIsInstance(value, list)

    def test_find_document_type_valid(self):
        with open(self.base_manifest) as f:
            documents = list(yaml.safe_load_all(f.read()))
            ovr = Override(documents)
            test_group = ovr.find_document_type('chart_group')
            self.assertEqual(test_group, const.DOCUMENT_GROUP)

            test_chart = ovr.find_document_type('chart')
            self.assertEqual(test_chart, const.DOCUMENT_CHART)

            test_manifest = ovr.find_document_type('manifest')
            self.assertEqual(test_manifest, const.DOCUMENT_MANIFEST)

    def test_update_chart_document_valid(self):
        with open(self.base_manifest) as f:
            documents = list(yaml.safe_load_all(f.read()))
        documents_modified = copy.deepcopy(documents)

        # Case 1: Checking if primitives get updated
        documents_modified[0]['data']['chart_name'] = 'modified'

        # starting out, both doc have different values for data
        self.assertNotEqual(documents[0], documents_modified[0])

        ovr = Override(documents)
        # update with document values with the modified ones
        ovr.update_chart_document(documents_modified[0])

        # after the update, both documents are equal
        self.assertEqual(ovr.documents[0]['data']['chart_name'],
                         documents_modified[0]['data']['chart_name'])
        self.assertEqual(ovr.documents[0], documents_modified[0])

        # Case 2: Checking if dictionaries get updated
        documents_modified[0]['data']['values'] = {'foo': 'bar'}

        ovr.update_chart_document(documents_modified[0])

        # after the update, both documents are equal
        self.assertEqual(ovr.documents[0]['data']['values'],
                         documents_modified[0]['data']['values'])
        self.assertEqual(ovr.documents[0], documents_modified[0])

        # Case 3: Checking if lists get updated
        documents_modified[0]['data']['dependencies'] = ['foo', 'bar']

        ovr.update_chart_document(documents_modified[0])

        # after the update, both documents are equal
        self.assertEqual(['foo', 'bar'],
                         ovr.documents[0]['data']['dependencies'])
        self.assertEqual(documents_modified[0]['data']['dependencies'],
                         ovr.documents[0]['data']['dependencies'])
        self.assertEqual(ovr.documents[0], documents_modified[0])

    def test_update_chart_document_keys_not_removed_with_override(self):
        with open(self.base_manifest) as f:
            documents = list(yaml.safe_load_all(f.read()))

        documents_modified = copy.deepcopy(documents)
        del documents_modified[0]['data']['chart_name']

        # verify both doc have different values for data
        self.assertNotEqual(documents[0], documents_modified[0])

        ovr = Override(documents)
        # update with document values with the modified ones
        ovr.update_chart_document(documents_modified[0])

        self.assertIn('chart_name', ovr.documents[0]['data'])
        self.assertNotEqual(ovr.documents[0], documents_modified[0])

    def test_update_chart_group_document_valid(self):