Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 717e4c2

Browse files
committed
some different jank
1 parent 40f45a6 commit 717e4c2

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

cli/create_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func TestCreate(t *testing.T) {
266266
Description: "description 1",
267267
DefaultSource: &proto.ParameterSource{
268268
Scheme: proto.ParameterSource_DATA,
269-
Value: "exit 0",
269+
Value: echo.ParameterSucceed(),
270270
},
271271
DefaultDestination: &proto.ParameterDestination{
272272
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
@@ -287,7 +287,7 @@ func TestCreate(t *testing.T) {
287287

288288
tempDir := t.TempDir()
289289
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
290-
_, _ = parameterFile.WriteString(fmt.Sprintf("%s: %q", echo.ParameterExecKey, "exit 1"))
290+
_, _ = parameterFile.WriteString(fmt.Sprintf("%s: %q", echo.ParameterExecKey, echo.ParameterError("fail")))
291291

292292
// The template import job should end up failed, but we need it to be
293293
// succeeded so the dry-run can begin.

coderd/templateversions_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,10 @@ func TestTemplateVersionDryRun(t *testing.T) {
578578
job, err := client.CreateTemplateVersionDryRun(context.Background(), version.ID, codersdk.CreateTemplateVersionDryRunRequest{
579579
ParameterValues: []codersdk.CreateParameterRequest{
580580
{
581-
Name: echo.ParameterExecKey,
582-
SourceValue: "tail -f /dev/null",
581+
Name: echo.ParameterExecKey,
582+
// Sleep for a ridiculously long time so we don't accidentally complete
583+
// before we successfully cancel the job.
584+
SourceValue: echo.ParameterSleep(time.Minute * 5),
583585
},
584586
},
585587
})
@@ -659,7 +661,7 @@ func TestTemplateVersionDryRun(t *testing.T) {
659661
ParameterValues: []codersdk.CreateParameterRequest{
660662
{
661663
Name: echo.ParameterExecKey,
662-
SourceValue: "tail -f /dev/null",
664+
SourceValue: echo.ParameterSleep(time.Minute * 5),
663665
},
664666
},
665667
})

coderd/workspaces_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,10 @@ func TestPostWorkspaceBuild(t *testing.T) {
629629
func(req *codersdk.CreateWorkspaceRequest) {
630630
req.ParameterValues = []codersdk.CreateParameterRequest{
631631
{
632-
Name: echo.ParameterExecKey,
633-
SourceValue: "tail -f /dev/null",
632+
Name: echo.ParameterExecKey,
633+
SourceValue: echo.ParameterSleep(time.Minute * 5),
634+
SourceScheme: codersdk.ParameterSourceSchemeData,
635+
DestinationScheme: codersdk.ParameterDestinationSchemeProvisionerVariable,
634636
},
635637
}
636638
})

provisioner/echo/serve.go

+37-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8-
"os/exec"
98
"path/filepath"
109
"strings"
10+
"time"
1111

1212
"golang.org/x/xerrors"
1313
protobuf "google.golang.org/protobuf/proto"
@@ -20,8 +20,28 @@ import (
2020

2121
const (
2222
ParameterExecKey = "echo.exec"
23+
24+
sleepKey = "sleep"
25+
errorKey = "error"
26+
successKey = "success"
2327
)
2428

29+
func ParameterError(s string) string {
30+
return formatExecValue(errorKey, s)
31+
}
32+
33+
func ParameterSleep(dur time.Duration) string {
34+
return formatExecValue(sleepKey, dur.String())
35+
}
36+
37+
func ParameterSucceed() string {
38+
return formatExecValue(successKey, "")
39+
}
40+
41+
func formatExecValue(key, value string) string {
42+
return fmt.Sprintf("%s=%s", key, value)
43+
}
44+
2545
var (
2646
// ParseComplete is a helper to indicate an empty parse completion.
2747
ParseComplete = []*proto.Parse_Response{{
@@ -91,15 +111,22 @@ func (e *echo) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
91111

92112
for _, param := range request.ParameterValues {
93113
if param.Name == ParameterExecKey {
94-
// #nosec G204
95-
cmd := exec.Command("/bin/sh", "-c", param.Value)
96-
out, err := cmd.CombinedOutput()
97-
if err != nil {
98-
return xerrors.Errorf("exec %q returned %q: %w",
99-
strings.Join(cmd.Args, " "),
100-
string(out),
101-
err,
102-
)
114+
toks := strings.Split(param.Value, "=")
115+
if len(toks) < 2 {
116+
break
117+
}
118+
119+
switch toks[0] {
120+
case sleepKey:
121+
dur, err := time.ParseDuration(toks[1])
122+
if err != nil {
123+
return xerrors.Errorf("parse duration: %w", err)
124+
}
125+
time.Sleep(dur)
126+
case errorKey:
127+
return xerrors.Errorf("returning error: %v", toks[1])
128+
default:
129+
// Do nothing
103130
}
104131
}
105132
}

0 commit comments

Comments
 (0)