...

Text file src/github.com/GoogleCloudPlatform/k8s-config-connector/scripts/dclsampleconverter/test.py

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/scripts/dclsampleconverter

     1# Copyright 2022 Google LLC
     2#
     3# Licensed under the Apache License, Version 2.0 (the "License");
     4# you may not use this file except in compliance with the License.
     5# You may obtain a copy of the License at
     6#
     7#      http://www.apache.org/licenses/LICENSE-2.0
     8#
     9# Unless required by applicable law or agreed to in writing, software
    10# distributed under the License is distributed on an "AS IS" BASIS,
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12# See the License for the specific language governing permissions and
    13# limitations under the License.
    14
    15"""This file contains tests for the dcl sample converter."""
    16
    17import os
    18
    19from absl import logging
    20from absl.testing import absltest
    21
    22import resource
    23import strings
    24
    25
    26class Test(absltest.TestCase):
    27
    28  def test_conversion(self):
    29    print(__file__)
    30    base_dir = f'{os.path.dirname(os.path.abspath(__file__))}/testdata'
    31    for test_case in os.listdir(base_dir):
    32      kcc_testdata_dir = f'{base_dir}/{test_case}/kcc/testdata'
    33      kcc_samples_dir = f'{base_dir}/{test_case}/kcc/samples'
    34
    35      r = resource.Resource(
    36          service='testservice',
    37          name='test_resource',
    38          kcc_kind=strings.snake_to_title('testservice_test_resource'),
    39          dcl_dir=f'{base_dir}/{test_case}/dcl',
    40          kcc_testdata_dir=kcc_testdata_dir,
    41          kcc_samples_dir=kcc_samples_dir,
    42          samples=[],
    43      )
    44      r.load_samples()
    45      r.convert_samples()
    46      r.write_samples()
    47
    48      self.compare_dirs(kcc_samples_dir, f'{kcc_samples_dir}/canonical')
    49      self.compare_dirs(kcc_testdata_dir, f'{kcc_testdata_dir}/canonical')
    50
    51  def compare_dirs(self, created_path: str, canonical_path: str):
    52    for file_name in os.listdir(created_path):
    53      if file_name == 'canonical':
    54        continue
    55      created_file_path = f'{created_path}/{file_name}'
    56      canonical_file_path = f'{canonical_path}/{file_name}'
    57      if os.path.isdir(created_file_path):
    58        self.compare_dirs(created_file_path, canonical_file_path)
    59        continue
    60      with open(canonical_file_path) as canonical_file, open(
    61          created_file_path) as created_file:
    62        logging.info(
    63            f'Comparing files:\n{created_file_path}\n{canonical_file_path}')
    64        self.assertEqual(canonical_file.read(), created_file.read())
    65      os.remove(created_file_path)
    66
    67
    68if __name__ == '__main__':
    69  absltest.main()

View as plain text