github.com/aws-cloudformation/cloudformation-cli-go-plugin@v1.2.0/tests/plugin/codegen_test.py (about)

     1  # pylint: disable=redefined-outer-name,protected-access
     2  import pytest
     3  
     4  from pathlib import Path
     5  from rpdk.core.exceptions import DownstreamError
     6  from rpdk.core.project import Project
     7  from rpdk.go.codegen import GoLanguagePlugin
     8  from shutil import copyfile
     9  from unittest.mock import patch
    10  
    11  TYPE_NAME = "foo::bar::baz"
    12  
    13  TEST_TARGET_INFO = {
    14      "My::Example::Resource": {
    15          "TargetName": "My::Example::Resource",
    16          "TargetType": "RESOURCE",
    17          "Schema": {
    18              "typeName": "My::Example::Resource",
    19              "additionalProperties": False,
    20              "properties": {
    21                  "Id": {"type": "string"},
    22                  "Tags": {
    23                      "type": "array",
    24                      "uniqueItems": False,
    25                      "items": {"$ref": "#/definitions/Tag"},
    26                  },
    27              },
    28              "required": [],
    29              "definitions": {
    30                  "Tag": {
    31                      "type": "object",
    32                      "additionalProperties": False,
    33                      "properties": {
    34                          "Value": {"type": "string"},
    35                          "Key": {"type": "string"},
    36                      },
    37                      "required": ["Value", "Key"],
    38                  }
    39              },
    40          },
    41          "ProvisioningType": "FULLY_MUTTABLE",
    42          "IsCfnRegistrySupportedType": True,
    43          "SchemaFileAvailable": True,
    44      },
    45      "My::Other::Resource": {
    46          "TargetName": "My::Other::Resource",
    47          "TargetType": "RESOURCE",
    48          "Schema": {
    49              "typeName": "My::Other::Resource",
    50              "additionalProperties": False,
    51              "properties": {
    52                  "Id": {"type": "string"},
    53                  "Tags": {
    54                      "type": "array",
    55                      "uniqueItems": False,
    56                      "items": {"$ref": "#/definitions/Tag"},
    57                  },
    58              },
    59              "required": [],
    60              "definitions": {
    61                  "Tag": {
    62                      "type": "object",
    63                      "additionalProperties": False,
    64                      "properties": {
    65                          "Value": {"type": "string"},
    66                          "Key": {"type": "string"},
    67                      },
    68                      "required": ["Value", "Key"],
    69                  }
    70              },
    71          },
    72          "ProvisioningType": "NOT_PROVISIONABLE",
    73          "IsCfnRegistrySupportedType": False,
    74          "SchemaFileAvailable": True,
    75      },
    76  }
    77  
    78  
    79  @pytest.fixture
    80  def plugin():
    81      return GoLanguagePlugin()
    82  
    83  
    84  @pytest.fixture
    85  def resource_project(tmp_path):
    86      project = Project(root=tmp_path)
    87  
    88      patch_plugins = patch.dict(
    89          "rpdk.core.plugin_registry.PLUGIN_REGISTRY",
    90          {"go": lambda: GoLanguagePlugin},
    91          clear=True,
    92      )
    93      patch_wizard = patch(
    94          "rpdk.go.codegen.input_with_validation", autospec=True, side_effect=[False]
    95      )
    96      with patch_plugins, patch_wizard:
    97          project.init(TYPE_NAME, "go")
    98      return project
    99  
   100  
   101  def get_files_in_project(project):
   102      return {
   103          str(child.relative_to(project.root)): child for child in project.root.rglob("*")
   104      }
   105  
   106  
   107  def test_initialize_resource(resource_project):
   108      assert resource_project.settings == {
   109          "import_path": "False",
   110          "protocolVersion": "2.0.0",
   111      }
   112  
   113      files = get_files_in_project(resource_project)
   114      assert set(files) == {
   115          ".gitignore",
   116          ".rpdk-config",
   117          "Makefile",
   118          "README.md",
   119          "cmd",
   120          "cmd/resource",
   121          "cmd/resource/resource.go",
   122          "foo-bar-baz.json",
   123          "go.mod",
   124          "internal",
   125          "example_inputs/inputs_1_invalid.json",
   126          "example_inputs/inputs_1_update.json",
   127          "example_inputs/inputs_1_create.json",
   128          "example_inputs",
   129          "template.yml",
   130      }
   131  
   132      readme = files["README.md"].read_text()
   133      assert resource_project.type_name in readme
   134  
   135      assert resource_project.entrypoint in files["template.yml"].read_text()
   136  
   137  
   138  def test_generate_resource(resource_project):
   139      resource_project.load_schema()
   140      before = get_files_in_project(resource_project)
   141      resource_project.generate()
   142      after = get_files_in_project(resource_project)
   143      files = after.keys() - before.keys() - {"resource-role.yaml"}
   144  
   145      assert files == {
   146          "makebuild",
   147          "cmd/main.go",
   148          "cmd/resource/config.go",
   149          "cmd/resource/model.go",
   150      }
   151  
   152  
   153  def test_generate_resource_go_failure(resource_project):
   154      resource_project.load_schema()
   155  
   156      with patch("rpdk.go.codegen.subprocess_run") as mock_subprocess:
   157          mock_subprocess.side_effect = FileNotFoundError()
   158          with pytest.raises(DownstreamError, match="go fmt failed"):
   159              resource_project.generate()
   160  
   161  
   162  def test_generate_resource_with_type_configuration(tmp_path):
   163      type_name = "schema::with::typeconfiguration"
   164      project = Project(root=tmp_path)
   165  
   166      patch_plugins = patch.dict(
   167          "rpdk.core.plugin_registry.PLUGIN_REGISTRY",
   168          {"go": lambda: GoLanguagePlugin},
   169          clear=True,
   170      )
   171      patch_wizard = patch(
   172          "rpdk.go.codegen.input_with_validation", autospec=True, side_effect=[False]
   173      )
   174      with patch_plugins, patch_wizard:
   175          project.init(type_name, "go")
   176  
   177      copyfile(
   178          str(Path.cwd() / "tests/data/schema-with-typeconfiguration.json"),
   179          str(project.root / "schema-with-typeconfiguration.json"),
   180      )
   181      project.type_info = ("schema", "with", "typeconfiguration")
   182      project.load_schema()
   183      project.load_configuration_schema()
   184      project.generate()
   185  
   186      type_configuration_schema_file = project.root / "schema-with-typeconfiguration.json"
   187      assert type_configuration_schema_file.is_file()