mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
* refactor: Rename ProjectParameter to ProjectVersionParameter This was confusing with ParameterValue before. It still is a bit, but this should help distinguish scope. * Add project version resources table * Allow project parameters to optionally have user and workspace * Add dry run for provisioners * Add resource detection on project import
182 lines
4.1 KiB
Go
182 lines
4.1 KiB
Go
//go:build linux
|
|
|
|
package terraform_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/provisioner/terraform"
|
|
"github.com/coder/coder/provisionersdk"
|
|
"github.com/coder/coder/provisionersdk/proto"
|
|
)
|
|
|
|
func TestProvision(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client, server := provisionersdk.TransportPipe()
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
t.Cleanup(func() {
|
|
_ = client.Close()
|
|
_ = server.Close()
|
|
cancelFunc()
|
|
})
|
|
go func() {
|
|
err := terraform.Serve(ctx, &terraform.ServeOptions{
|
|
ServeOptions: &provisionersdk.ServeOptions{
|
|
Listener: server,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
}()
|
|
api := proto.NewDRPCProvisionerClient(provisionersdk.Conn(client))
|
|
|
|
for _, testCase := range []struct {
|
|
Name string
|
|
Files map[string]string
|
|
Request *proto.Provision_Request
|
|
Response *proto.Provision_Response
|
|
Error bool
|
|
}{{
|
|
Name: "single-variable",
|
|
Files: map[string]string{
|
|
"main.tf": `variable "A" {
|
|
description = "Testing!"
|
|
}`,
|
|
},
|
|
Request: &proto.Provision_Request{
|
|
ParameterValues: []*proto.ParameterValue{{
|
|
DestinationScheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
|
|
Name: "A",
|
|
Value: "example",
|
|
}},
|
|
},
|
|
Response: &proto.Provision_Response{
|
|
Type: &proto.Provision_Response_Complete{
|
|
Complete: &proto.Provision_Complete{},
|
|
},
|
|
},
|
|
}, {
|
|
Name: "missing-variable",
|
|
Files: map[string]string{
|
|
"main.tf": `variable "A" {
|
|
}`,
|
|
},
|
|
Error: true,
|
|
}, {
|
|
Name: "single-resource",
|
|
Files: map[string]string{
|
|
"main.tf": `resource "null_resource" "A" {}`,
|
|
},
|
|
Response: &proto.Provision_Response{
|
|
Type: &proto.Provision_Response_Complete{
|
|
Complete: &proto.Provision_Complete{
|
|
Resources: []*proto.Resource{{
|
|
Name: "A",
|
|
Type: "null_resource",
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
}, {
|
|
Name: "invalid-sourcecode",
|
|
Files: map[string]string{
|
|
"main.tf": `a`,
|
|
},
|
|
Error: true,
|
|
}, {
|
|
Name: "dryrun-single-resource",
|
|
Files: map[string]string{
|
|
"main.tf": `resource "null_resource" "A" {}`,
|
|
},
|
|
Request: &proto.Provision_Request{
|
|
DryRun: true,
|
|
},
|
|
Response: &proto.Provision_Response{
|
|
Type: &proto.Provision_Response_Complete{
|
|
Complete: &proto.Provision_Complete{
|
|
Resources: []*proto.Resource{{
|
|
Name: "A",
|
|
Type: "null_resource",
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
}, {
|
|
Name: "dryrun-conditional-single-resource",
|
|
Files: map[string]string{
|
|
"main.tf": `
|
|
variable "test" {
|
|
default = "no"
|
|
}
|
|
resource "null_resource" "A" {
|
|
count = var.test == "yes" ? 1 : 0
|
|
}`,
|
|
},
|
|
Response: &proto.Provision_Response{
|
|
Type: &proto.Provision_Response_Complete{
|
|
Complete: &proto.Provision_Complete{
|
|
Resources: nil,
|
|
},
|
|
},
|
|
},
|
|
}} {
|
|
testCase := testCase
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := t.TempDir()
|
|
for path, content := range testCase.Files {
|
|
err := os.WriteFile(filepath.Join(directory, path), []byte(content), 0600)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
request := &proto.Provision_Request{
|
|
Directory: directory,
|
|
}
|
|
if testCase.Request != nil {
|
|
request.ParameterValues = testCase.Request.ParameterValues
|
|
request.State = testCase.Request.State
|
|
request.DryRun = testCase.Request.DryRun
|
|
}
|
|
response, err := api.Provision(ctx, request)
|
|
require.NoError(t, err)
|
|
for {
|
|
msg, err := response.Recv()
|
|
if msg != nil && msg.GetLog() != nil {
|
|
t.Logf("log: [%s] %s", msg.GetLog().Level, msg.GetLog().Output)
|
|
continue
|
|
}
|
|
if testCase.Error {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
|
|
if msg.GetComplete() == nil {
|
|
continue
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
if !request.DryRun {
|
|
require.Greater(t, len(msg.GetComplete().State), 0)
|
|
}
|
|
|
|
resourcesGot, err := json.Marshal(msg.GetComplete().Resources)
|
|
require.NoError(t, err)
|
|
|
|
resourcesWant, err := json.Marshal(testCase.Response.GetComplete().Resources)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(resourcesWant), string(resourcesGot))
|
|
break
|
|
}
|
|
})
|
|
}
|
|
}
|