Skip to content

Commit

Permalink
Adding Provider-Defined Function Testing (#210)
Browse files Browse the repository at this point in the history
* Adding function tests for framework protocol 5 & 6 and protocol 6 mux providers (#202)

* Adding copyright headers (#202)

* Linting (#202)

* Adding function tests for protocol v5 and protocol v6 providers (#202)

* Amending provider defined function tests for number until bug fix on go.cty is released (#202)

* Adding function testing for mux providers (#202)

* Adding function testing for tf6to5provider (#202)

* Adding copyright headers (#202)

* Bumping terraform-plugin-framework version to latest (#202)

Latest terraform-plugin-framework contains fix for:
  * hashicorp/terraform-plugin-framework#914
  * hashicorp/terraform-plugin-framework#919

* Bumping terraform-plugin-testing to v1.7.0 (#202)

* Refactoring to return function.FuncError (#202)

* Updates following code review (#202)
  • Loading branch information
bendbennett committed Mar 6, 2024
1 parent f2d0b55 commit eac47aa
Show file tree
Hide file tree
Showing 61 changed files with 3,655 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/framework5provider/bool_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var _ function.Function = BoolFunction{}

func NewBoolFunction() function.Function {
return &BoolFunction{}
}

type BoolFunction struct{}

func (f BoolFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "bool"
}

func (f BoolFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Parameters: []function.Parameter{
function.BoolParameter{},
},
Return: function.BoolReturn{},
}
}

func (f BoolFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var arg bool

resp.Error = req.Arguments.Get(ctx, &arg)

resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, arg))
}
90 changes: 90 additions & 0 deletions internal/framework5provider/bool_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestBoolFunction_known(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::bool(true)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Bool(true)),
},
},
},
})
}

func TestBoolFunction_null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::bool(null)
}`,
ExpectError: regexp.MustCompile("Invalid function argument"),
},
},
})
}

func TestBoolFunction_unknown(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
input = provider::framework::bool(true)
}
output "test" {
value = terraform_data.test.output
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownOutputValue("test"),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Bool(true)),
},
},
},
})
}
39 changes: 39 additions & 0 deletions internal/framework5provider/float64_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var _ function.Function = Float64Function{}

func NewFloat64Function() function.Function {
return &Float64Function{}
}

type Float64Function struct{}

func (f Float64Function) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "float64"
}

func (f Float64Function) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Parameters: []function.Parameter{
function.Float64Parameter{},
},
Return: function.Float64Return{},
}
}

func (f Float64Function) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var arg float64

resp.Error = req.Arguments.Get(ctx, &arg)

resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, arg))
}
90 changes: 90 additions & 0 deletions internal/framework5provider/float64_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestFloat64Function_known(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::float64(1.23)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Float64Exact(1.23)),
},
},
},
})
}

func TestFloat64Function_null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::float64(null)
}`,
ExpectError: regexp.MustCompile("Invalid function argument"),
},
},
})
}

func TestFloat64Function_unknown(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
input = provider::framework::float64(1.23)
}
output "test" {
value = terraform_data.test.output
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownOutputValue("test"),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Float64Exact(1.23)),
},
},
},
})
}
39 changes: 39 additions & 0 deletions internal/framework5provider/int64_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var _ function.Function = Int64Function{}

func NewInt64Function() function.Function {
return &Int64Function{}
}

type Int64Function struct{}

func (f Int64Function) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "int64"
}

func (f Int64Function) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Parameters: []function.Parameter{
function.Int64Parameter{},
},
Return: function.Int64Return{},
}
}

func (f Int64Function) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var arg int64

resp.Error = req.Arguments.Get(ctx, &arg)

resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, arg))
}
90 changes: 90 additions & 0 deletions internal/framework5provider/int64_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package framework

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestInt64Function_known(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::int64(123)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Int64Exact(123)),
},
},
},
})
}

func TestInt64Function_null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::framework::int64(null)
}`,
ExpectError: regexp.MustCompile("Invalid function argument"),
},
},
})
}

func TestInt64Function_unknown(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
input = provider::framework::int64(123)
}
output "test" {
value = terraform_data.test.output
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownOutputValue("test"),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Int64Exact(123)),
},
},
},
})
}

0 comments on commit eac47aa

Please sign in to comment.