# Path: slo_generator/migrations/migrator.py
# def slo_config_v1tov2(slo_config,
#                       shared_config={},
#                       shared_exporters=[],
#                       quiet=False,
#                       verbose=0):
#     """Process old SLO config v1 and generate SLO config v2.
# 
#     Args:
#         slo_config (dict): SLO Config v1.
#         shared_config (dict): SLO Generator config.
#         shared_exporters (list): Shared exporters keys to add to SLO configs.
#         quiet (bool): If true, do not ask for user input.
#         verbose (int): Verbose level.
# 
#     Returns:
#         dict: SLO Config v2.
#     """
#     # SLO config v2 skeleton
#     slo_config_v2 = OrderedDict(copy.deepcopy(SLO_CONFIG_SCHEMA))
#     slo_config_v2['apiVersion'] = 'sre.google.com/v2'
#     slo_config_v2['kind'] = 'ServiceLevelObjective'
#     missing_keys = [
#         key for key in ['service_name', 'feature_name', 'slo_name', 'backend']
#         if key not in slo_config
#     ]
#     if missing_keys:
#         click.secho(
#             f'Invalid SLO configuration: missing key(s) {missing_keys}.',
#             fg='red')
#         return None
# 
#     # Get fields from old config
#     slo_metadata_name_fmt = '{service_name}-{feature_name}-{slo_name}'
#     slo_metadata_name = slo_metadata_name_fmt.format(**slo_config)
#     slo_description = slo_config.pop('slo_description')
#     slo_target = slo_config.pop('slo_target')
#     service_level_indicator = slo_config['backend'].pop('measurement', {})
#     backend = slo_config['backend']
#     method = backend.pop('method')
#     exporters = slo_config.get('exporters', [])
#     if isinstance(exporters, dict):  # single exporter, deprecated
#         exporters = [exporters]
# 
#     # Fill spec
#     slo_config_v2['metadata']['name'] = slo_metadata_name
#     slo_config_v2['metadata']['labels'] = {
#         'service_name': slo_config['service_name'],
#         'feature_name': slo_config['feature_name'],
#         'slo_name': slo_config['slo_name'],
#     }
#     other_labels = {
#         k: v for k, v in slo_config.items() if k not in
#         ['service_name', 'feature_name', 'slo_name', 'backend', 'exporters']
#     }
#     slo_config_v2['metadata']['labels'].update(other_labels)
#     slo_config_v2['spec']['description'] = slo_description
#     slo_config_v2['spec']['goal'] = slo_target
# 
#     # Process backend
#     backend = OrderedDict(backend)
#     backend_key = add_to_shared_config(backend,
#                                        shared_config,
#                                        'backends',
#                                        quiet=quiet)
#     slo_config_v2['spec']['backend'] = backend_key
#     slo_config_v2['spec']['method'] = method
# 
#     # If exporter not in general config, add it and add an alias for the
#     # exporter. Refer to the alias in the SLO config file.
#     for exporter in exporters:
#         exporter = OrderedDict(exporter)
#         exp_key = add_to_shared_config(exporter,
#                                        shared_config,
#                                        'exporters',
#                                        quiet=quiet)
#         slo_config_v2['spec']['exporters'].append(exp_key)
# 
#     # Add shared exporters to slo config
#     for exp_key in shared_exporters:
#         slo_config_v2['spec']['exporters'].append(exp_key)
# 
#     # Fill spec
#     slo_config_v2['spec']['service_level_indicator'] = service_level_indicator
# 
#     if verbose > 0:
#         pprint.pprint(dict(slo_config_v2))
#     return dict(slo_config_v2)
# 
# Path: tests/unit/test_stubs.py
# def load_fixture(filename, ctx=os.environ):
#     """Load a fixture from the test/fixtures/ directory and replace context
#     environmental variables in it.
# 
#     Args:
#         filename (str): Filename of the fixture to load.
#         ctx (dict): Context dictionary (env variables).
# 
#     Returns:
#         dict: Loaded fixture.
#     """
#     path = get_fixture_path(filename)
#     return load_config(path, ctx=ctx)


import unittest
from slo_generator.migrations.migrator import slo_config_v1tov2
from .test_stubs import load_fixture
# Copyright 2019 Google Inc.
#
# 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.




class TestMigrator(unittest.TestCase):

    def setUp(self):
        self.slo_config_v1 = load_fixture('slo_config_v1.yaml')

# Path: slo_generator/migrations/migrator.py
# def slo_config_v1tov2(slo_config,
#                       shared_config={},
#                       shared_exporters=[],
#                       quiet=False,
#                       verbose=0):
#     """Process old SLO config v1 and generate SLO config v2.
# 
#     Args:
#         slo_config (dict): SLO Config v1.
#         shared_config (dict): SLO Generator config.
#         shared_exporters (list): Shared exporters keys to add to SLO configs.
#         quiet (bool): If true, do not ask for user input.
#         verbose (int): Verbose level.
# 
#     Returns:
#         dict: SLO Config v2.
#     """
#     # SLO config v2 skeleton
#     slo_config_v2 = OrderedDict(copy.deepcopy(SLO_CONFIG_SCHEMA))
#     slo_config_v2['apiVersion'] = 'sre.google.com/v2'
#     slo_config_v2['kind'] = 'ServiceLevelObjective'
#     missing_keys = [
#         key for key in ['service_name', 'feature_name', 'slo_name', 'backend']
#         if key not in slo_config
#     ]
#     if missing_keys:
#         click.secho(
#             f'Invalid SLO configuration: missing key(s) {missing_keys}.',
#             fg='red')
#         return None
# 
#     # Get fields from old config
#     slo_metadata_name_fmt = '{service_name}-{feature_name}-{slo_name}'
#     slo_metadata_name = slo_metadata_name_fmt.format(**slo_config)
#     slo_description = slo_config.pop('slo_description')
#     slo_target = slo_config.pop('slo_target')
#     service_level_indicator = slo_config['backend'].pop('measurement', {})
#     backend = slo_config['backend']
#     method = backend.pop('method')
#     exporters = slo_config.get('exporters', [])
#     if isinstance(exporters, dict):  # single exporter, deprecated
#         exporters = [exporters]
# 
#     # Fill spec
#     slo_config_v2['metadata']['name'] = slo_metadata_name
#     slo_config_v2['metadata']['labels'] = {
#         'service_name': slo_config['service_name'],
#         'feature_name': slo_config['feature_name'],
#         'slo_name': slo_config['slo_name'],
#     }
#     other_labels = {
#         k: v for k, v in slo_config.items() if k not in
#         ['service_name', 'feature_name', 'slo_name', 'backend', 'exporters']
#     }
#     slo_config_v2['metadata']['labels'].update(other_labels)
#     slo_config_v2['spec']['description'] = slo_description
#     slo_config_v2['spec']['goal'] = slo_target
# 
#     # Process backend
#     backend = OrderedDict(backend)
#     backend_key = add_to_shared_config(backend,
#                                        shared_config,
#                                        'backends',
#                                        quiet=quiet)
#     slo_config_v2['spec']['backend'] = backend_key
#     slo_config_v2['spec']['method'] = method
# 
#     # If exporter not in general config, add it and add an alias for the
#     # exporter. Refer to the alias in the SLO config file.
#     for exporter in exporters:
#         exporter = OrderedDict(exporter)
#         exp_key = add_to_shared_config(exporter,
#                                        shared_config,
#                                        'exporters',
#                                        quiet=quiet)
#         slo_config_v2['spec']['exporters'].append(exp_key)
# 
#     # Add shared exporters to slo config
#     for exp_key in shared_exporters:
#         slo_config_v2['spec']['exporters'].append(exp_key)
# 
#     # Fill spec
#     slo_config_v2['spec']['service_level_indicator'] = service_level_indicator
# 
#     if verbose > 0:
#         pprint.pprint(dict(slo_config_v2))
#     return dict(slo_config_v2)
# 
# Path: tests/unit/test_stubs.py
# def load_fixture(filename, ctx=os.environ):
#     """Load a fixture from the test/fixtures/ directory and replace context
#     environmental variables in it.
# 
#     Args:
#         filename (str): Filename of the fixture to load.
#         ctx (dict): Context dictionary (env variables).
# 
#     Returns:
#         dict: Loaded fixture.
#     """
#     path = get_fixture_path(filename)
#     return load_config(path, ctx=ctx)


# Path: tests/unit/test_migrate.py
import unittest
from slo_generator.migrations.migrator import slo_config_v1tov2
from .test_stubs import load_fixture

# Copyright 2019 Google Inc.
#
# 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.




class TestMigrator(unittest.TestCase):

    def setUp(self):
        self.slo_config_v1 = load_fixture('slo_config_v1.yaml')

        self.slo_config_v2 = load_fixture('slo_config_v2.yaml')