Skip to content

Commit

Permalink
Adding function tests for protocol v5 and protocol v6 providers (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Feb 1, 2024
1 parent 6653ce6 commit 9ddc360
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/protocolprovider/function_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package protocol

import (
"context"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type functionBool struct{}

func (f functionBool) CallFunction(context.Context, *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
value, err := tfprotov5.NewDynamicValue(tftypes.Bool, tftypes.NewValue(tftypes.Bool, true))

if err != nil {
return nil, err
}

return &tfprotov5.CallFunctionResponse{
Result: &value,
}, nil
}

func (f functionBool) GetFunctions(context.Context, *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
panic("implement me")
}
36 changes: 36 additions & 0 deletions internal/protocolprovider/function_bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package protocol

import (
"testing"

"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/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAccV5FunctionBool(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
//nolint:unparam // False positive in unparam related to map: https://github.com/mvdan/unparam/issues/40
"corner": func() (tfprotov5.ProviderServer, error) {
return Server(), nil
},
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::corner::bool(true)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Bool(true)),
},
},
},
})
}
32 changes: 32 additions & 0 deletions internal/protocolprovider/function_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package protocol

import (
"context"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
)

type errUnsupportedFunction string

func (e errUnsupportedFunction) Error() string {
return "unsupported function: " + string(e)
}

type functionRouter map[string]tfprotov5.FunctionServer

func (f functionRouter) CallFunction(ctx context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
fu, ok := f[req.Name]

if !ok {
return nil, errUnsupportedFunction(req.Name)
}

return fu.CallFunction(ctx, req)
}

func (f functionRouter) GetFunctions(ctx context.Context, req *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
panic("not implemented")
}
20 changes: 20 additions & 0 deletions internal/protocolprovider/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-provider-corner/internal/backend"
)

Expand All @@ -17,9 +18,11 @@ type server struct {
providerMetaSchema *tfprotov5.Schema
resourceSchemas map[string]*tfprotov5.Schema
dataSourceSchemas map[string]*tfprotov5.Schema
functions map[string]*tfprotov5.Function

resourceRouter
dataSourceRouter
functionRouter

client *backend.Client
}
Expand Down Expand Up @@ -59,6 +62,7 @@ func (s *server) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProvid
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ServerCapabilities: s.serverCapabilities(),
Functions: s.functions,
}, nil
}

Expand Down Expand Up @@ -117,5 +121,21 @@ func Server() tfprotov5.ProviderServer {
dataSourceRouter: dataSourceRouter{
"corner_time": dataSourceTime{},
},
functions: map[string]*tfprotov5.Function{
"bool": {
Parameters: []*tfprotov5.FunctionParameter{
{
Name: "param",
Type: tftypes.Bool,
},
},
Return: &tfprotov5.FunctionReturn{
Type: tftypes.Bool,
},
},
},
functionRouter: functionRouter{
"bool": functionBool{},
},
}
}
29 changes: 29 additions & 0 deletions internal/protocolv6provider/function_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package protocolv6

import (
"context"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type functionBool struct{}

func (f functionBool) CallFunction(context.Context, *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
value, err := tfprotov6.NewDynamicValue(tftypes.Bool, tftypes.NewValue(tftypes.Bool, true))

if err != nil {
return nil, err
}

return &tfprotov6.CallFunctionResponse{
Result: &value,
}, nil
}

func (f functionBool) GetFunctions(context.Context, *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
panic("implement me")
}
36 changes: 36 additions & 0 deletions internal/protocolv6provider/function_bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package protocolv6

import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"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/tfversion"
)

func TestAccV6FunctionBool(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
//nolint:unparam // False positive in unparam related to map: https://github.com/mvdan/unparam/issues/40
"corner": func() (tfprotov6.ProviderServer, error) {
return Server(), nil
},
},
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::corner::bool(true)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue("test", knownvalue.Bool(true)),
},
},
},
})
}
32 changes: 32 additions & 0 deletions internal/protocolv6provider/function_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package protocolv6

import (
"context"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

type errUnsupportedFunction string

func (e errUnsupportedFunction) Error() string {
return "unsupported function: " + string(e)
}

type functionRouter map[string]tfprotov6.FunctionServer

func (f functionRouter) CallFunction(ctx context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
fu, ok := f[req.Name]

if !ok {
return nil, errUnsupportedFunction(req.Name)
}

return fu.CallFunction(ctx, req)
}

func (f functionRouter) GetFunctions(ctx context.Context, req *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
panic("not implemented")
}
20 changes: 20 additions & 0 deletions internal/protocolv6provider/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-provider-corner/internal/backend"
)

Expand All @@ -17,9 +18,11 @@ type server struct {
providerMetaSchema *tfprotov6.Schema
resourceSchemas map[string]*tfprotov6.Schema
dataSourceSchemas map[string]*tfprotov6.Schema
functions map[string]*tfprotov6.Function

resourceRouter
dataSourceRouter
functionRouter

client *backend.Client
}
Expand Down Expand Up @@ -59,6 +62,7 @@ func (s *server) GetProviderSchema(ctx context.Context, req *tfprotov6.GetProvid
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ServerCapabilities: s.serverCapabilities(),
Functions: s.functions,
}, nil
}

Expand Down Expand Up @@ -117,5 +121,21 @@ func Server() tfprotov6.ProviderServer {
dataSourceRouter: dataSourceRouter{
"corner_v6_time": dataSourceTime{},
},
functions: map[string]*tfprotov6.Function{
"bool": {
Parameters: []*tfprotov6.FunctionParameter{
{
Name: "param",
Type: tftypes.Bool,
},
},
Return: &tfprotov6.FunctionReturn{
Type: tftypes.Bool,
},
},
},
functionRouter: functionRouter{
"bool": functionBool{},
},
}
}

0 comments on commit 9ddc360

Please sign in to comment.