Skip to content

fix: Ensure terraform tests have a cache path and logger #3161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

type executor struct {
initMu sync.Locker
binaryPath string
cachePath string
workdir string
Expand Down Expand Up @@ -142,6 +143,19 @@ func (e executor) init(ctx context.Context, logr logger) error {
"-no-color",
"-input=false",
}

// When cache path is set, we must protect against multiple calls
// to `terraform init`.
//
// From the Terraform documentation:
// Note: The plugin cache directory is not guaranteed to be
// concurrency safe. The provider installer's behavior in
// environments with multiple terraform init calls is undefined.
if e.cachePath != "" {
e.initMu.Lock()
defer e.initMu.Unlock()
}

return e.execWriteOutput(ctx, args, e.basicEnv(), outWriter, errWriter)
}

Expand Down
25 changes: 3 additions & 22 deletions provisioner/terraform/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,20 @@
package terraform_test

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/provisioner/terraform"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/provisionersdk/proto"
)

func TestParse(t *testing.T) {
t.Parallel()

// Create an in-memory provisioner to communicate with.
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,
},
})
assert.NoError(t, err)
}()
api := proto.NewDRPCProvisionerClient(provisionersdk.Conn(client))
ctx, api := setupProvisioner(t)

testCases := []struct {
Name string
Expand Down Expand Up @@ -175,7 +155,8 @@ func TestParse(t *testing.T) {
DefaultDestination: &proto.ParameterDestination{
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
},
}},
},
},
},
},
},
Expand Down
10 changes: 7 additions & 3 deletions provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ import (
)

func setupProvisioner(t *testing.T) (context.Context, proto.DRPCProvisionerClient) {
cachePath := t.TempDir()
client, server := provisionersdk.TransportPipe()
ctx, cancelFunc := context.WithCancel(context.Background())
serverErr := make(chan error, 1)
t.Cleanup(func() {
_ = client.Close()
_ = server.Close()
cancelFunc()
err := <-serverErr
assert.NoError(t, err)
})
go func() {
err := terraform.Serve(ctx, &terraform.ServeOptions{
serverErr <- terraform.Serve(ctx, &terraform.ServeOptions{
ServeOptions: &provisionersdk.ServeOptions{
Listener: server,
},
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
CachePath: cachePath,
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
})
assert.NoError(t, err)
}()
api := proto.NewDRPCProvisionerClient(provisionersdk.Conn(client))
return ctx, api
Expand Down
12 changes: 9 additions & 3 deletions provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraform
import (
"context"
"path/filepath"
"sync"

"github.com/cli/safeexec"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -109,15 +110,20 @@ func Serve(ctx context.Context, options *ServeOptions) error {
}

type server struct {
// initMu protects against executors running `terraform init`
// concurrently when cache path is set.
initMu sync.Mutex

binaryPath string
cachePath string
logger slog.Logger
}

func (t server) executor(workdir string) executor {
func (s *server) executor(workdir string) executor {
return executor{
binaryPath: t.binaryPath,
cachePath: t.cachePath,
initMu: &s.initMu,
binaryPath: s.binaryPath,
cachePath: s.cachePath,
workdir: workdir,
}
}