statecheck

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package statecheck contains the state check interface, request/response structs, and common state check implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckStateRequest

type CheckStateRequest struct {
	// State represents a parsed state file, retrieved via the `terraform show -json` command.
	State *tfjson.State
}

CheckStateRequest is a request for an invoke of the CheckState function.

type CheckStateResponse

type CheckStateResponse struct {
	// Error is used to report the failure of a state check assertion and is combined with other StateCheck errors
	// to be reported as a test failure.
	Error error
}

CheckStateResponse is a response to an invoke of the CheckState function.

type StateCheck

type StateCheck interface {
	// CheckState should perform the state check.
	CheckState(context.Context, CheckStateRequest, *CheckStateResponse)
}

StateCheck defines an interface for implementing test logic that checks a state file and then returns an error if the state file does not match what is expected.

func ExpectKnownOutputValue

func ExpectKnownOutputValue(outputAddress string, knownValue knownvalue.Check) StateCheck

ExpectKnownOutputValue returns a state check that asserts that the specified value has a known type, and value.

Example
package main

import (
	"testing"

	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/hashicorp/terraform-plugin-testing/knownvalue"
	"github.com/hashicorp/terraform-plugin-testing/statecheck"
)

func main() {
	// A typical test would accept *testing.T as a function parameter, for instance `func TestSomething(t *testing.T) { ... }`.
	t := &testing.T{}
	t.Parallel()

	resource.Test(t, resource.TestCase{
		// Provider definition omitted.
		Steps: []resource.TestStep{
			{
				Config: `resource "test_resource" "one" {
		          bool_attribute = true
		        }

		        output bool_output {
		          value = test_resource.one.bool_attribute
		        }
		        `,
				ConfigStateChecks: []statecheck.StateCheck{
					statecheck.ExpectKnownOutputValue(
						"bool_output",
						knownvalue.Bool(true),
					),
				},
			},
		},
	})
}
Output:

func ExpectKnownOutputValueAtPath

func ExpectKnownOutputValueAtPath(outputAddress string, outputPath tfjsonpath.Path, knownValue knownvalue.Check) StateCheck

ExpectKnownOutputValueAtPath returns a state check that asserts that the specified output at the given path has a known type and value.

Example
package main

import (
	"testing"

	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/hashicorp/terraform-plugin-testing/knownvalue"
	"github.com/hashicorp/terraform-plugin-testing/statecheck"
	"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func main() {
	// A typical test would accept *testing.T as a function parameter, for instance `func TestSomething(t *testing.T) { ... }`.
	t := &testing.T{}
	t.Parallel()

	resource.Test(t, resource.TestCase{
		// Provider definition omitted.
		Steps: []resource.TestStep{
			{
				Config: `resource "test_resource" "one" {
		          bool_attribute = true
		        }

		        output test_resource_one_output {
		          value = test_resource.one
		        }
		        `,
				ConfigStateChecks: []statecheck.StateCheck{
					statecheck.ExpectKnownOutputValueAtPath(
						"test_resource_one_output",
						tfjsonpath.New("bool_attribute"),
						knownvalue.Bool(true),
					),
				},
			},
		},
	})
}
Output:

func ExpectKnownValue

func ExpectKnownValue(resourceAddress string, attributePath tfjsonpath.Path, knownValue knownvalue.Check) StateCheck

ExpectKnownValue returns a state check that asserts that the specified attribute at the given resource has a known type and value.

Example
package main

import (
	"testing"

	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/hashicorp/terraform-plugin-testing/knownvalue"
	"github.com/hashicorp/terraform-plugin-testing/statecheck"
	"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func main() {
	// A typical test would accept *testing.T as a function parameter, for instance `func TestSomething(t *testing.T) { ... }`.
	t := &testing.T{}
	t.Parallel()

	resource.Test(t, resource.TestCase{
		// Provider definition omitted.
		Steps: []resource.TestStep{
			{
				Config: `resource "test_resource" "one" {
		          bool_attribute = true
		        }
		        `,
				ConfigStateChecks: []statecheck.StateCheck{
					statecheck.ExpectKnownValue(
						"test_resource.one",
						tfjsonpath.New("bool_attribute"),
						knownvalue.Bool(true),
					),
				},
			},
		},
	})
}
Output:

func ExpectSensitiveValue

func ExpectSensitiveValue(resourceAddress string, attributePath tfjsonpath.Path) StateCheck

ExpectSensitiveValue returns a state check that asserts that the specified attribute at the given resource has a sensitive value.

Due to implementation differences between the terraform-plugin-sdk and the terraform-plugin-framework, representation of sensitive values may differ. For example, terraform-plugin-sdk based providers may have less precise representations of sensitive values, such as marking whole maps as sensitive rather than individual element values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL