From c9f97fde747495e139bbbceb978bfdbd7cd3399e Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Fri, 14 Jun 2024 13:29:21 +0200 Subject: [PATCH] feat: fixed tests --- .../plugin-terraform/test/generator.test.ts | 57 +++- .../test/testdata/expected/base.tf | 40 ++- .../base_add_amplience_is_managed_switch.tf | 265 ++++++++++++++++++ .../testdata/expected/base_repo_for_each.tf | 35 ++- .../expected/base_without_provider.tf | 40 ++- 5 files changed, 413 insertions(+), 24 deletions(-) create mode 100644 packages/plugin-terraform/test/testdata/expected/base_add_amplience_is_managed_switch.tf diff --git a/packages/plugin-terraform/test/generator.test.ts b/packages/plugin-terraform/test/generator.test.ts index 2989570..a757019 100644 --- a/packages/plugin-terraform/test/generator.test.ts +++ b/packages/plugin-terraform/test/generator.test.ts @@ -51,7 +51,6 @@ it.each([{ graphqlFile: "base" }])( // Normally we can deal with this by configuring "prettier" in the codegen config. // But in this unittest we test the raw result. // Therefore the expected output is also formatted ugly. - expect(terraformResult.toString().replace(/\r/g, "")).toEqual( expected.replace(/\r/g, "") + "\n", ); @@ -168,3 +167,59 @@ it.each([{ graphqlFile: "base" }])( ); }, ); + +it.each([{ graphqlFile: "base" }])( + "correct Terraform file for $graphqlFile WITH managed variable", + async ({ graphqlFile }) => { + const schema = buildSchema( + addToSchema + + fs.readFileSync(`./test/testdata/${graphqlFile}.graphql`, "utf8"), + ); + const expected = fs.readFileSync( + `./test/testdata/expected/${graphqlFile}_add_amplience_is_managed_switch.tf`, + "utf8", + ); + + const terraformResult = await plugin(schema, [], { + hostname: "https://schema-examples.com", + add_amplience_is_managed_switch: true, + visualization: [ + { + for_each: 'var.variables["VISUALIZATION_HOST"]', + label: "visualization.key", + templated_uri: + "${visualization.value}/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}", + default: false, + }, + { + label: "Localhost with layout", + templated_uri: + "http://localhost:3000/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}", + default: false, + }, + { + label: "Localhost without layout", + templated_uri: + "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}", + default: false, + }, + ], + content_repositories: { + website1: 'var.variables["CONTENT_REPO1_ID"]', + website2: 'var.variables["CONTENT_REPO2_ID"]', + }, + slot_repositories: { + slot1: 'var.variables["SLOT_REPO1_ID"]', + slot2: 'var.variables["SLOT_REPO2_ID"]', + }, + }); + + // Note: the terraform generator does output the terraform code with ugly formatting. + // Normally we can deal with this by configuring "prettier" in the codegen config. + // But in this unittest we test the raw result. + // Therefore the expected output is also formatted ugly. + expect(terraformResult.toString().replace(/\r/g, "")).toEqual( + expected.replace(/\r/g, "") + "\n", + ); + }, +); diff --git a/packages/plugin-terraform/test/testdata/expected/base.tf b/packages/plugin-terraform/test/testdata/expected/base.tf index 93f918e..d72bcb8 100644 --- a/packages/plugin-terraform/test/testdata/expected/base.tf +++ b/packages/plugin-terraform/test/testdata/expected/base.tf @@ -30,9 +30,12 @@ auto_sync = true } resource "amplience_content_type" "test"{ -content_type_uri = amplience_content_type_schema.test.schema_id +content_type_uri = "https://schema-examples.com/test" label = "Test" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test +] } resource "amplience_content_type_assignment" "test"{ @@ -48,9 +51,12 @@ auto_sync = true } resource "amplience_content_type" "test_other_repository"{ -content_type_uri = amplience_content_type_schema.test_other_repository.schema_id +content_type_uri = "https://schema-examples.com/test-other-repository" label = "Test Other Repository" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_other_repository +] } resource "amplience_content_type_assignment" "test_other_repository"{ @@ -66,9 +72,12 @@ auto_sync = true } resource "amplience_content_type" "test_slot"{ -content_type_uri = amplience_content_type_schema.test_slot.schema_id +content_type_uri = "https://schema-examples.com/test-slot" label = "Test Slot" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_slot +] } resource "amplience_content_type_assignment" "test_slot"{ @@ -84,7 +93,7 @@ auto_sync = true } resource "amplience_content_type" "test_visualizations"{ -content_type_uri = amplience_content_type_schema.test_visualizations.schema_id +content_type_uri = "https://schema-examples.com/test-visualizations" label = "Test Visualizations" status = "ACTIVE" dynamic"visualization" { @@ -105,6 +114,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_visualizations +] } resource "amplience_content_type_assignment" "test_visualizations"{ @@ -120,7 +132,7 @@ auto_sync = true } resource "amplience_content_type" "test_icon"{ -content_type_uri = amplience_content_type_schema.test_icon.schema_id +content_type_uri = "https://schema-examples.com/test-icon" label = "Test Icon" icon { size = 256 @@ -145,6 +157,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_icon +] } resource "amplience_content_type_assignment" "test_icon"{ @@ -160,9 +175,12 @@ auto_sync = true } resource "amplience_content_type" "test_auto_sync_true"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_true.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-true" label = "Test Auto Sync True" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_true +] } resource "amplience_content_type_assignment" "test_auto_sync_true"{ @@ -178,9 +196,12 @@ auto_sync = false } resource "amplience_content_type" "test_auto_sync_false"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_false.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-false" label = "Test Auto Sync False" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_false +] } resource "amplience_content_type_assignment" "test_auto_sync_false"{ @@ -196,9 +217,12 @@ auto_sync = true } resource "amplience_content_type" "test_no_auto_sync"{ -content_type_uri = amplience_content_type_schema.test_no_auto_sync.schema_id +content_type_uri = "https://schema-examples.com/test-no-auto-sync" label = "Test No Auto Sync" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_no_auto_sync +] } resource "amplience_content_type_assignment" "test_no_auto_sync"{ diff --git a/packages/plugin-terraform/test/testdata/expected/base_add_amplience_is_managed_switch.tf b/packages/plugin-terraform/test/testdata/expected/base_add_amplience_is_managed_switch.tf new file mode 100644 index 0000000..444a2b9 --- /dev/null +++ b/packages/plugin-terraform/test/testdata/expected/base_add_amplience_is_managed_switch.tf @@ -0,0 +1,265 @@ +terraform { +required_providers { +amplience = { +source = "labd/amplience" +} +} +} + +data "amplience_content_repository" "website1"{ +id = var.variables["CONTENT_REPO1_ID"] +count = var.amplience_is_managed ? 1 : 0 +} + +data "amplience_content_repository" "website2"{ +id = var.variables["CONTENT_REPO2_ID"] +count = var.amplience_is_managed ? 1 : 0 +} + +data "amplience_content_repository" "slot1"{ +id = var.variables["SLOT_REPO1_ID"] +count = var.amplience_is_managed ? 1 : 0 +} + +data "amplience_content_repository" "slot2"{ +id = var.variables["SLOT_REPO2_ID"] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test"{ +body = file("${path.module}/schemas/test.json") +schema_id = "https://schema-examples.com/test" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test"{ +content_type_uri = "https://schema-examples.com/test" +label = "Test" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test"{ +content_type_id = amplience_content_type.test.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_other_repository"{ +body = file("${path.module}/schemas/test-other-repository.json") +schema_id = "https://schema-examples.com/test-other-repository" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_other_repository"{ +content_type_uri = "https://schema-examples.com/test-other-repository" +label = "Test Other Repository" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_other_repository +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_other_repository"{ +content_type_id = amplience_content_type.test_other_repository.id +repository_id = data.amplience_content_repository.website2.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_slot"{ +body = file("${path.module}/schemas/test-slot.json") +schema_id = "https://schema-examples.com/test-slot" +validation_level = "SLOT" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_slot"{ +content_type_uri = "https://schema-examples.com/test-slot" +label = "Test Slot" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_slot +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_slot"{ +content_type_id = amplience_content_type.test_slot.id +repository_id = data.amplience_content_repository.slot1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_visualizations"{ +body = file("${path.module}/schemas/test-visualizations.json") +schema_id = "https://schema-examples.com/test-visualizations" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_visualizations"{ +content_type_uri = "https://schema-examples.com/test-visualizations" +label = "Test Visualizations" +status = "ACTIVE" +dynamic"visualization" { +for_each = var.variables["VISUALIZATION_HOST"] +content { +label = visualization.key +templated_uri = "${visualization.value}/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +} +visualization { +label = "Localhost with layout" +templated_uri = "http://localhost:3000/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +visualization { +label = "Localhost without layout" +templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +depends_on = [ +amplience_content_type_schema.test_visualizations +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_visualizations"{ +content_type_id = amplience_content_type.test_visualizations.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_icon"{ +body = file("${path.module}/schemas/test-icon.json") +schema_id = "https://schema-examples.com/test-icon" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_icon"{ +content_type_uri = "https://schema-examples.com/test-icon" +label = "Test Icon" +icon { +size = 256 +url = "icon.png" +} +status = "ACTIVE" +dynamic"visualization" { +for_each = var.variables["VISUALIZATION_HOST"] +content { +label = visualization.key +templated_uri = "${visualization.value}/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +} +visualization { +label = "Localhost with layout" +templated_uri = "http://localhost:3000/preview/with-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +visualization { +label = "Localhost without layout" +templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" +default = false +} +depends_on = [ +amplience_content_type_schema.test_icon +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_icon"{ +content_type_id = amplience_content_type.test_icon.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_auto_sync_true"{ +body = file("${path.module}/schemas/test-auto-sync-true.json") +schema_id = "https://schema-examples.com/test-auto-sync-true" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_auto_sync_true"{ +content_type_uri = "https://schema-examples.com/test-auto-sync-true" +label = "Test Auto Sync True" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_true +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_auto_sync_true"{ +content_type_id = amplience_content_type.test_auto_sync_true.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_auto_sync_false"{ +body = file("${path.module}/schemas/test-auto-sync-false.json") +schema_id = "https://schema-examples.com/test-auto-sync-false" +validation_level = "CONTENT_TYPE" +auto_sync = false +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_auto_sync_false"{ +content_type_uri = "https://schema-examples.com/test-auto-sync-false" +label = "Test Auto Sync False" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_false +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_auto_sync_false"{ +content_type_id = amplience_content_type.test_auto_sync_false.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_schema" "test_no_auto_sync"{ +body = file("${path.module}/schemas/test-no-auto-sync.json") +schema_id = "https://schema-examples.com/test-no-auto-sync" +validation_level = "CONTENT_TYPE" +auto_sync = true +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type" "test_no_auto_sync"{ +content_type_uri = "https://schema-examples.com/test-no-auto-sync" +label = "Test No Auto Sync" +status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_no_auto_sync +] +count = var.amplience_is_managed ? 1 : 0 +} + +resource "amplience_content_type_assignment" "test_no_auto_sync"{ +content_type_id = amplience_content_type.test_no_auto_sync.id +repository_id = data.amplience_content_repository.website1.id +count = var.amplience_is_managed ? 1 : 0 +} + +variable "amplience_is_managed"{ +type = bool +default = true +description = "Set to false to disable all the Amplience resources" +} diff --git a/packages/plugin-terraform/test/testdata/expected/base_repo_for_each.tf b/packages/plugin-terraform/test/testdata/expected/base_repo_for_each.tf index 459b5d9..1da1fb4 100644 --- a/packages/plugin-terraform/test/testdata/expected/base_repo_for_each.tf +++ b/packages/plugin-terraform/test/testdata/expected/base_repo_for_each.tf @@ -14,9 +14,12 @@ auto_sync = true } resource "amplience_content_type" "test"{ -content_type_uri = amplience_content_type_schema.test.schema_id +content_type_uri = "https://schema-examples.com/test" label = "Test" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test +] } resource "amplience_content_type_assignment" "test"{ @@ -33,9 +36,12 @@ auto_sync = true } resource "amplience_content_type" "test_slot"{ -content_type_uri = amplience_content_type_schema.test_slot.schema_id +content_type_uri = "https://schema-examples.com/test-slot" label = "Test Slot" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_slot +] } resource "amplience_content_type_assignment" "test_slot"{ @@ -52,7 +58,7 @@ auto_sync = true } resource "amplience_content_type" "test_visualizations"{ -content_type_uri = amplience_content_type_schema.test_visualizations.schema_id +content_type_uri = "https://schema-examples.com/test-visualizations" label = "Test Visualizations" status = "ACTIVE" dynamic"visualization" { @@ -73,6 +79,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_visualizations +] } resource "amplience_content_type_assignment" "test_visualizations"{ @@ -89,7 +98,7 @@ auto_sync = true } resource "amplience_content_type" "test_icon"{ -content_type_uri = amplience_content_type_schema.test_icon.schema_id +content_type_uri = "https://schema-examples.com/test-icon" label = "Test Icon" icon { size = 256 @@ -114,6 +123,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_icon +] } resource "amplience_content_type_assignment" "test_icon"{ @@ -130,9 +142,12 @@ auto_sync = true } resource "amplience_content_type" "test_auto_sync_true"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_true.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-true" label = "Test Auto Sync True" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_true +] } resource "amplience_content_type_assignment" "test_auto_sync_true"{ @@ -149,9 +164,12 @@ auto_sync = false } resource "amplience_content_type" "test_auto_sync_false"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_false.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-false" label = "Test Auto Sync False" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_false +] } resource "amplience_content_type_assignment" "test_auto_sync_false"{ @@ -168,9 +186,12 @@ auto_sync = true } resource "amplience_content_type" "test_no_auto_sync"{ -content_type_uri = amplience_content_type_schema.test_no_auto_sync.schema_id +content_type_uri = "https://schema-examples.com/test-no-auto-sync" label = "Test No Auto Sync" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_no_auto_sync +] } resource "amplience_content_type_assignment" "test_no_auto_sync"{ diff --git a/packages/plugin-terraform/test/testdata/expected/base_without_provider.tf b/packages/plugin-terraform/test/testdata/expected/base_without_provider.tf index a210180..e208e62 100644 --- a/packages/plugin-terraform/test/testdata/expected/base_without_provider.tf +++ b/packages/plugin-terraform/test/testdata/expected/base_without_provider.tf @@ -22,9 +22,12 @@ auto_sync = true } resource "amplience_content_type" "test"{ -content_type_uri = amplience_content_type_schema.test.schema_id +content_type_uri = "https://schema-examples.com/test" label = "Test" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test +] } resource "amplience_content_type_assignment" "test"{ @@ -40,9 +43,12 @@ auto_sync = true } resource "amplience_content_type" "test_other_repository"{ -content_type_uri = amplience_content_type_schema.test_other_repository.schema_id +content_type_uri = "https://schema-examples.com/test-other-repository" label = "Test Other Repository" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_other_repository +] } resource "amplience_content_type_assignment" "test_other_repository"{ @@ -58,9 +64,12 @@ auto_sync = true } resource "amplience_content_type" "test_slot"{ -content_type_uri = amplience_content_type_schema.test_slot.schema_id +content_type_uri = "https://schema-examples.com/test-slot" label = "Test Slot" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_slot +] } resource "amplience_content_type_assignment" "test_slot"{ @@ -76,7 +85,7 @@ auto_sync = true } resource "amplience_content_type" "test_visualizations"{ -content_type_uri = amplience_content_type_schema.test_visualizations.schema_id +content_type_uri = "https://schema-examples.com/test-visualizations" label = "Test Visualizations" status = "ACTIVE" dynamic"visualization" { @@ -97,6 +106,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_visualizations +] } resource "amplience_content_type_assignment" "test_visualizations"{ @@ -112,7 +124,7 @@ auto_sync = true } resource "amplience_content_type" "test_icon"{ -content_type_uri = amplience_content_type_schema.test_icon.schema_id +content_type_uri = "https://schema-examples.com/test-icon" label = "Test Icon" icon { size = 256 @@ -137,6 +149,9 @@ label = "Localhost without layout" templated_uri = "http://localhost:3000/preview/without-layout?vse={{vse.domain}}&content={{content.sys.id}}" default = false } +depends_on = [ +amplience_content_type_schema.test_icon +] } resource "amplience_content_type_assignment" "test_icon"{ @@ -152,9 +167,12 @@ auto_sync = true } resource "amplience_content_type" "test_auto_sync_true"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_true.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-true" label = "Test Auto Sync True" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_true +] } resource "amplience_content_type_assignment" "test_auto_sync_true"{ @@ -170,9 +188,12 @@ auto_sync = false } resource "amplience_content_type" "test_auto_sync_false"{ -content_type_uri = amplience_content_type_schema.test_auto_sync_false.schema_id +content_type_uri = "https://schema-examples.com/test-auto-sync-false" label = "Test Auto Sync False" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_auto_sync_false +] } resource "amplience_content_type_assignment" "test_auto_sync_false"{ @@ -188,9 +209,12 @@ auto_sync = true } resource "amplience_content_type" "test_no_auto_sync"{ -content_type_uri = amplience_content_type_schema.test_no_auto_sync.schema_id +content_type_uri = "https://schema-examples.com/test-no-auto-sync" label = "Test No Auto Sync" status = "ACTIVE" +depends_on = [ +amplience_content_type_schema.test_no_auto_sync +] } resource "amplience_content_type_assignment" "test_no_auto_sync"{