Jelajahi Sumber

Tutorial 10: Testing.

Frédéric G. MARAND 4 bulan lalu
induk
melakukan
c56a5fa8e3

+ 1 - 1
internal/provider/coffees_data_source.go

@@ -154,7 +154,7 @@ func (d *coffeesDataSource) Read(ctx context.Context, req datasource.ReadRequest
 			Teaser:      types.StringValue(coffee.Teaser),
 			Description: types.StringValue(coffee.Description),
 			Price:       types.Float64Value(coffee.Price),
-			Image:       types.StringValue(coffee.Image),
+			Image:       types.StringValue(coffee.Image + ""),
 			Ingredients: make([]coffeesIngredientsModel, 0, len(coffee.Ingredient)), // Not in tutorial, why ?
 		}
 

+ 33 - 0
internal/provider/coffees_data_source_test.go

@@ -0,0 +1,33 @@
+package provider
+
+import (
+	"testing"
+
+	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
+)
+
+func TestAccCoffeesDataSource(t *testing.T) {
+	resource.Test(t, resource.TestCase{
+		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
+		Steps: []resource.TestStep{
+			// Read testing
+			{
+				Config: providerConfig + `data "hashicups_coffees" "test" {}`,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					// Verify number of coffees returned.
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.#", "9"), // # : number of blocks
+					// Verify the first coffee to ensure all attributes are set.
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.%", "7"), // % : number of attributes
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.description", ""),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.id", "1"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.image", "/hashicorp.png"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.ingredients.#", "1"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.ingredients.0.id", "6"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.name", "HCP Aeropress"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.price", "200"),
+					resource.TestCheckResourceAttr("data.hashicups_coffees.test", "coffees.0.teaser", "Automation in a cup"),
+				),
+			},
+		},
+	})
+}

+ 45 - 0
internal/provider/compute_tax_function_test.go

@@ -0,0 +1,45 @@
+package provider
+
+import (
+	"regexp"
+	"testing"
+
+	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
+	"github.com/hashicorp/terraform-plugin-testing/tfversion"
+)
+
+func TestComputeTaxFunction_Known(t *testing.T) {
+	// Should only be used for resource that don't have any external dependencies.
+	resource.UnitTest(t, resource.TestCase{
+		TerraformVersionChecks: []tfversion.TerraformVersionCheck{
+			tfversion.SkipBelow(tfversion.Version1_8_0), // Functions were introduced in 1.8.0
+		},
+		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
+		Steps: []resource.TestStep{
+			{
+				Config: `output "test" {
+          value = provider::hashicups::compute_tax(5.00, 0.085)
+        }`,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					resource.TestCheckOutput("test", "5.43")), // 5*1.085 == 5.425
+			},
+		},
+	})
+}
+
+func TestComputeTaxFunction_Null(t *testing.T) {
+	resource.UnitTest(t, resource.TestCase{
+		TerraformVersionChecks: []tfversion.TerraformVersionCheck{
+			tfversion.SkipBelow(tfversion.Version1_8_0),
+		},
+		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
+		Steps: []resource.TestStep{
+			{
+				Config: `output "test" {
+          value = provider::hashicups::compute_tax(null, 0.085)
+        }`,
+				ExpectError: regexp.MustCompile(`argument must not be null`),
+			},
+		},
+	})
+}

+ 0 - 0
internal/provider/example_data_source_test.go → internal/provider/example_data_source_test.notgo


+ 0 - 0
internal/provider/example_function_test.go → internal/provider/example_function_test.notgo


+ 0 - 0
internal/provider/example_resource_test.go → internal/provider/example_resource_test.notgo


+ 89 - 0
internal/provider/order_resource_test.go

@@ -0,0 +1,89 @@
+package provider
+
+import (
+	"testing"
+
+	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
+)
+
+// Resource acceptance testing verifies that the entire resource lifecycle,
+// such as the Create, Read, Update, and Delete functionality, along with import capabilities.
+//
+// The testing framework automatically handles destroying test resources
+// and returning any errors as a final step,
+// regardless of whether there is a destroy step explicitly written.
+func TestAccOrderResource(t *testing.T) {
+	resource.Test(t, resource.TestCase{
+		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
+		Steps: []resource.TestStep{
+			// Create and Read testing
+			{
+				Config: providerConfig + `
+resource "hashicups_order" "test" {
+  items = [
+    {
+      coffee = {
+        id = 1
+      }
+      quantity = 2
+    },
+  ]
+}
+`,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					// Verify number of items.
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.#", "1"),
+					// Verify first order item.
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.quantity", "2"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.id", "1"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.description", ""),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.image", "/hashicorp.png"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.name", "HCP Aeropress"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.price", "200"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.teaser", "Automation in a cup"),
+					// Verify dynamic values have any value set in the state.
+					resource.TestCheckResourceAttrSet("hashicups_order.test", "id"),
+					resource.TestCheckResourceAttrSet("hashicups_order.test", "last_updated"),
+				),
+			},
+			// ImportState testing.
+			{
+				// No Config
+				ResourceName:      "hashicups_order.test",
+				ImportState:       true,
+				ImportStateVerify: true,
+				// The last_updated attribute does not exist in the HashiCups
+				// API, therefore there is no value for it during import.
+				ImportStateVerifyIgnore: []string{"last_updated"},
+			},
+			// Update and Read testing.
+			{
+				Config: providerConfig + `
+resource "hashicups_order" "test" {
+  items = [
+    {
+      coffee = {
+        id = 2
+      }
+      quantity = 2
+    },
+  ]
+}
+`,
+				Check: resource.ComposeAggregateTestCheckFunc(
+					// Verify first order item updated (reuse previous state since this is the same test).
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.#", "1"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.quantity", "2"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.id", "2"),
+					// Verify first coffee item has Computed attributes updated.
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.description", ""),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.image", "/packer.png"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.name", "Packer Spiced Latte"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.price", "350"),
+					resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.teaser", "Packed with goodness to spice up your images"),
+				),
+				// Delete testing automatically occurs in TestCase.
+			},
+		},
+	})
+}

+ 18 - 1
internal/provider/provider_test.go

@@ -7,16 +7,33 @@ import (
 	"github.com/hashicorp/terraform-plugin-go/tfprotov6"
 )
 
+const (
+	// providerConfig is a shared configuration to combine with the actual
+	// test configuration so the HashiCups client is properly configured.
+	// It is also possible to use the HASHICUPS_ environment variables instead,
+	// such as updating the Makefile and running the testing through that tool.
+	//
+	// language=terraform
+	providerConfig = `
+provider "hashicups" {
+	username = "education"
+	password = "test123"
+	host     = "http://localhost:19090"
+}
+`
+)
+
 // testAccProtoV6ProviderFactories are used to instantiate a provider during
 // acceptance testing. The factory function will be invoked for every Terraform
 // CLI command executed to create a provider server to which the CLI can
 // reattach.
 var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
-	"scaffolding": providerserver.NewProtocol6WithError(New("test")()),
+	"hashicups": providerserver.NewProtocol6WithError(New("test")()),
 }
 
 func testAccPreCheck(t *testing.T) {
 	// You can add code here to run prior to any test case execution, for example assertions
 	// about the appropriate environment variables being set are common to see in a pre-check
 	// function.
+	return
 }