Skip to content

chore(testutil): extract testutil.CreateZip and testutil.CreateTar helpers #15540

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 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
found another one
  • Loading branch information
johnstcn committed Nov 15, 2024
commit a81b5617e1fa022d0458d6db5cb0e41e6495f338
3 changes: 2 additions & 1 deletion provisioner/terraform/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
)

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -380,7 +381,7 @@ func TestParse(t *testing.T) {
t.Parallel()

session := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, testCase.Files),
TemplateSourceArchive: testutil.CreateTar(t, testCase.Files),
})

err := session.Send(&proto.Request{Type: &proto.Request_Parse{Parse: &proto.ParseRequest{}}})
Expand Down
51 changes: 25 additions & 26 deletions provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package terraform_test

import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -28,6 +26,7 @@ import (
"github.com/coder/coder/v2/provisioner/terraform"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
)

type provisionerServeOptions struct {
Expand Down Expand Up @@ -78,24 +77,24 @@ func setupProvisioner(t *testing.T, opts *provisionerServeOptions) (context.Cont
return ctx, api
}

func makeTar(t *testing.T, files map[string]string) []byte {
t.Helper()
var buffer bytes.Buffer
writer := tar.NewWriter(&buffer)
for name, content := range files {
err := writer.WriteHeader(&tar.Header{
Name: name,
Size: int64(len(content)),
Mode: 0o644,
})
require.NoError(t, err)
_, err = writer.Write([]byte(content))
require.NoError(t, err)
}
err := writer.Flush()
require.NoError(t, err)
return buffer.Bytes()
}
// func testutil.CreateTar(t *testing.T, files map[string]string) []byte {
// t.Helper()
// var buffer bytes.Buffer
// writer := tar.NewWriter(&buffer)
// for name, content := range files {
// err := writer.WriteHeader(&tar.Header{
// Name: name,
// Size: int64(len(content)),
// Mode: 0o644,
// })
// require.NoError(t, err)
// _, err = writer.Write([]byte(content))
// require.NoError(t, err)
// }
// err := writer.Flush()
// require.NoError(t, err)
// return buffer.Bytes()
// }

func configure(ctx context.Context, t *testing.T, client proto.DRPCProvisionerClient, config *proto.Config) proto.DRPCProvisioner_SessionClient {
t.Helper()
Expand Down Expand Up @@ -186,7 +185,7 @@ func TestProvision_Cancel(t *testing.T) {
binaryPath: binPath,
})
sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, nil),
TemplateSourceArchive: testutil.CreateTar(t, nil),
})

err = sendPlan(sess, proto.WorkspaceTransition_START)
Expand Down Expand Up @@ -257,7 +256,7 @@ func TestProvision_CancelTimeout(t *testing.T) {
})

sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, nil),
TemplateSourceArchive: testutil.CreateTar(t, nil),
})

// provisioner requires plan before apply, so test cancel with plan.
Expand Down Expand Up @@ -346,7 +345,7 @@ func TestProvision_TextFileBusy(t *testing.T) {
})

sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, nil),
TemplateSourceArchive: testutil.CreateTar(t, nil),
})

err = sendPlan(sess, proto.WorkspaceTransition_START)
Expand Down Expand Up @@ -758,7 +757,7 @@ func TestProvision(t *testing.T) {

ctx, api := setupProvisioner(t, nil)
sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, testCase.Files),
TemplateSourceArchive: testutil.CreateTar(t, testCase.Files),
})

planRequest := &proto.Request{Type: &proto.Request_Plan{Plan: &proto.PlanRequest{
Expand Down Expand Up @@ -863,7 +862,7 @@ func TestProvision_ExtraEnv(t *testing.T) {

ctx, api := setupProvisioner(t, nil)
sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, map[string]string{"main.tf": `resource "null_resource" "A" {}`}),
TemplateSourceArchive: testutil.CreateTar(t, map[string]string{"main.tf": `resource "null_resource" "A" {}`}),
})

err := sendPlan(sess, proto.WorkspaceTransition_START)
Expand Down Expand Up @@ -913,7 +912,7 @@ func TestProvision_SafeEnv(t *testing.T) {

ctx, api := setupProvisioner(t, nil)
sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, map[string]string{"main.tf": echoResource}),
TemplateSourceArchive: testutil.CreateTar(t, map[string]string{"main.tf": echoResource}),
})

err := sendPlan(sess, proto.WorkspaceTransition_START)
Expand Down
2 changes: 1 addition & 1 deletion provisioner/terraform/timings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTimingsFromProvision(t *testing.T) {
binaryPath: fakeBin,
})
sess := configure(ctx, t, api, &proto.Config{
TemplateSourceArchive: makeTar(t, nil),
TemplateSourceArchive: testutil.CreateTar(t, nil),
})

ctx, cancel := context.WithTimeout(ctx, testutil.WaitLong)
Expand Down
Loading