Skip to content

Commit

Permalink
Merge branch 'main' of github.com:k1LoW/runn
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Dec 14, 2024
2 parents da5ceab + 3f68bcd commit 6add4c2
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 38 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## [v0.124.0](https://github.com/k1LoW/runn/compare/v0.123.1...v0.124.0) - 2024-12-13
### Breaking Changes 🛠
- Use goccy/go-yaml only by @k1LoW in https://github.com/k1LoW/runn/pull/1088
- STDIN value is handled as `runn.stdin`, not as Runn ID(s). by @k1LoW in https://github.com/k1LoW/runn/pull/1108
### Fix bug 🐛
- Fix race condition in cdp.go by @k1LoW in https://github.com/k1LoW/runn/pull/1112
### Other Changes
- chore(deps): bump golang.org/x/crypto from 0.29.0 to 0.31.0 by @dependabot in https://github.com/k1LoW/runn/pull/1110
- chore(deps): bump the dependencies group across 1 directory with 11 updates by @dependabot in https://github.com/k1LoW/runn/pull/1113
- Fix lint warn by @k1LoW in https://github.com/k1LoW/runn/pull/1114

## [v0.123.1](https://github.com/k1LoW/runn/compare/v0.123.0...v0.123.1) - 2024-12-10
### Fix bug 🐛
- Update maskedio by @k1LoW in https://github.com/k1LoW/runn/pull/1105
Expand Down
22 changes: 19 additions & 3 deletions capture/runbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ func (c *cRunbook) setRunner(name string, value any) {
}
exist := false
for _, rnr := range r.Runners {
if rnr.Key.(string) == name {
n, ok := rnr.Key.(string)
if !ok {
continue
}
if n == name {
exist = true
}
}
Expand Down Expand Up @@ -585,11 +589,23 @@ func (r *runbook) replaceLatestStep(rep yaml.MapSlice) {
}

func headersAndMessages(step yaml.MapSlice) yaml.MapSlice {
return step[0].Value.(yaml.MapSlice)[0].Value.(yaml.MapSlice)
s, ok := step[0].Value.(yaml.MapSlice)
if !ok {
return nil
}
ss, ok := s[0].Value.(yaml.MapSlice)
if !ok {
return nil
}
return ss
}

func replaceHeadersAndMessages(step, hb yaml.MapSlice) yaml.MapSlice {
step[0].Value.(yaml.MapSlice)[0].Value = hb
s, ok := step[0].Value.(yaml.MapSlice)
if !ok {
return nil
}
s[0].Value = hb
return step
}

Expand Down
4 changes: 2 additions & 2 deletions dbg.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (d *dbg) attach(ctx context.Context, s *step) error {

if d.quit {
s.parent.skipped = true
return errStepSkiped
return errStepSkipped
}
if !d.enable {
return nil
Expand Down Expand Up @@ -184,7 +184,7 @@ L:
// quit
d.quit = true
s.parent.skipped = true
return errStepSkiped
return errStepSkipped
case dbgCmdPrint, dbgCmdPrintShort:
// print
if len(cmd) != 2 {
Expand Down
14 changes: 4 additions & 10 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package flags
import (
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/k1LoW/duration"
"github.com/k1LoW/runn"
"github.com/k1LoW/runn/capture"
"github.com/mattn/go-isatty"
"github.com/spf13/cast"
)

Expand Down Expand Up @@ -106,15 +104,11 @@ func (f *Flags) ToOpts() ([]runn.Option, error) {
runn.Attach(f.Attach),
}

// runbook ID
if !isatty.IsTerminal(os.Stdin.Fd()) {
// From stdin
b, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
opts = append(opts, runn.RunID(string(b)))
// STDIN
if err := runn.SetStdin(os.Stdin); err != nil {
return nil, err
}

// From flags
opts = append(opts, runn.RunID(f.RunIDs...))

Expand Down
6 changes: 5 additions & 1 deletion grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,11 @@ L:
rnr.refc = nil

d[grpcStoreMessagesKey] = messages
if h, err := stream.Header(); len(d[grpcStoreHeaderKey].(metadata.MD)) == 0 && err == nil {
mmd, ok := d[grpcStoreHeaderKey].(metadata.MD)
if !ok {
return fmt.Errorf("failed to cast headers: %s", d[grpcStoreHeaderKey])
}
if h, err := stream.Header(); len(mmd) == 0 && err == nil {
d[grpcStoreHeaderKey] = h
}
t, ok := dcopy(stream.Trailer()).(metadata.MD)
Expand Down
15 changes: 12 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ func newHTTPRunner(name, endpoint string) (*httpRunner, error) {
if err != nil {
return nil, err
}
tp, ok := http.DefaultTransport.(*http.Transport)
if !ok {
return nil, fmt.Errorf("failed to cast: %v", http.DefaultTransport)
}

return &httpRunner{
name: name,
endpoint: u,
client: &http.Client{
Transport: http.DefaultTransport.(*http.Transport).Clone(),
Transport: tp.Clone(),
Timeout: time.Second * 30,
},
validator: newNopValidator(),
Expand Down Expand Up @@ -172,7 +177,7 @@ func (r *httpRequest) encodeBody() (io.Reader, error) {
case string:
return strings.NewReader(v), nil
case []byte:
return bytes.NewBuffer(r.body.([]byte)), nil
return bytes.NewBuffer(v), nil
case []any:
arr, ok := r.body.([]any)
if !ok {
Expand Down Expand Up @@ -419,7 +424,11 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
switch {
case rnr.client != nil:
if rnr.client.Transport == nil {
rnr.client.Transport = http.DefaultTransport.(*http.Transport).Clone()
tp, ok := http.DefaultTransport.(*http.Transport)
if !ok {
return fmt.Errorf("failed to cast: %v", http.DefaultTransport)
}
rnr.client.Transport = tp.Clone()
}
if ts, ok := rnr.client.Transport.(*http.Transport); ok {
existingConfig := ts.TLSClientConfig
Expand Down
12 changes: 10 additions & 2 deletions include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func TestIncludeRunnerRun(t *testing.T) {
}
}
{
got := len(o.store.steps[0]["steps"].([]map[string]any))
steps, ok := o.store.steps[0]["steps"].([]map[string]any)
if !ok {
t.Errorf("failed to cast: %v", o.store.steps[0]["steps"])
}
got := len(steps)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
Expand All @@ -60,7 +64,11 @@ func TestIncludeRunnerRun(t *testing.T) {
}
}
{
got := len(o.store.steps[0]["vars"].(map[string]any))
vars, ok := o.store.steps[0]["vars"].(map[string]any)
if !ok {
t.Errorf("failed to cast: %v", o.store.steps[0]["vars"])
}
got := len(vars)
if want := len(tt.vars); got != want {
t.Errorf("got %v\nwant %v", got, want)
}
Expand Down
18 changes: 11 additions & 7 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/spf13/cast"
)

var errStepSkiped = errors.New("step skipped")
var errStepSkipped = errors.New("step skipped")
var ErrFailFast = errors.New("fail fast")

var _ otchkiss.Requester = (*operatorN)(nil)
Expand Down Expand Up @@ -176,7 +176,7 @@ func (op *operator) runStep(ctx context.Context, idx int, s *step) error {
} else {
op.Debugf(yellow("Skip on %s\n"), op.stepName(idx))
}
return errStepSkiped
return errStepSkipped
}
}
if s.desc != "" {
Expand Down Expand Up @@ -276,7 +276,7 @@ func (op *operator) runStep(ctx context.Context, idx int, s *step) error {
if op.skipTest {
op.Debugf(yellow("Skip %q on %s\n"), testRunnerKey, op.stepName(idx))
if !run {
return errStepSkiped
return errStepSkipped
}
return nil
}
Expand Down Expand Up @@ -523,7 +523,11 @@ func New(opts ...Option) (*operator, error) {
}
}
if len(hostRules) > 0 {
v.client.Transport.(*http.Transport).DialContext = hostRules.dialContextFunc()
tp, ok := v.client.Transport.(*http.Transport)
if !ok {
return nil, fmt.Errorf("failed to cast: %v", v.client.Transport)
}
tp.DialContext = hostRules.dialContextFunc()
}
op.httpRunners[k] = v
}
Expand Down Expand Up @@ -1190,7 +1194,7 @@ func (op *operator) runInternal(ctx context.Context) (rerr error) {
force := op.force
for i, s := range op.steps {
if failed && !force {
s.setResult(errStepSkiped)
s.setResult(errStepSkipped)
op.recordNotRun(i)
if err := op.recordResultToLatest(resultSkipped); err != nil {
return err
Expand All @@ -1200,7 +1204,7 @@ func (op *operator) runInternal(ctx context.Context) (rerr error) {
err := op.runStep(ctx, i, s)
s.setResult(err)
switch {
case errors.Is(errStepSkiped, err):
case errors.Is(errStepSkipped, err):
op.recordNotRun(i)
if err := op.recordResultToLatest(resultSkipped); err != nil {
return err
Expand Down Expand Up @@ -1295,7 +1299,7 @@ func (op *operator) skip() error {
op.Debugf(yellow("Skip %s\n"), op.desc)
op.skipped = true
for i, s := range op.steps {
s.setResult(errStepSkiped)
s.setResult(errStepSkipped)
op.recordNotRun(i)
if err := op.recordResultToLatest(resultSkipped); err != nil {
return err
Expand Down
51 changes: 51 additions & 0 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,57 @@ func TestSortWithNeeds(t *testing.T) {
}
}

func TestStdin(t *testing.T) {
tests := []struct {
in string
want string
}{
{"hello", "hello\n"},
{`{"hello":"world"}`, "{\n \"hello\": \"world\"\n}\n"},
}
ctx := context.Background()
book := "testdata/book/stdin.yml"
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "stdin")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := f.Close(); err != nil {
t.Error(err)
}
})
if _, err := f.WriteString(tt.in); err != nil {
t.Fatal(err)
}
if _, err := f.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
if err := SetStdin(f); err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
opts := []Option{
Book(book),
Stdout(buf),
Scopes(ScopeAllowRunExec),
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err != nil {
t.Error(err)
}
got := buf.String()
if got != tt.want {
t.Errorf("want %q, got %q", tt.want, got)
}
})
}
}

func tenOps(t *testing.T) []*operator {
t.Helper()
var ops []*operator
Expand Down
18 changes: 15 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func parseHTTPRequest(v map[string]any) (*httpRequest, error) {
req.headers.Add(k, v)
case []any:
for _, vv := range v {
req.headers.Add(k, vv.(string))
svv, ok := vv.(string)
if !ok {
return nil, fmt.Errorf("invalid request: %s", string(part))
}
req.headers.Add(k, svv)
}
default:
return nil, fmt.Errorf("invalid request: %s", string(part))
Expand Down Expand Up @@ -157,7 +161,11 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
if err != nil {
return nil, err
}
svc, mth, err := parseServiceAndMethod(pe.(string))
pes, ok := pe.(string)
if !ok {
return nil, fmt.Errorf("invalid request: %s", string(part))
}
svc, mth, err := parseServiceAndMethod(pes)
if err != nil {
return nil, err
}
Expand All @@ -183,7 +191,11 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
req.headers.Append(k, v)
case []any:
for _, vv := range v {
req.headers.Append(k, vv.(string))
svv, ok := vv.(string)
if !ok {
return nil, fmt.Errorf("invalid request: %s", string(part))
}
req.headers.Append(k, svv)
}
default:
return nil, fmt.Errorf("invalid request: %s", string(part))
Expand Down
2 changes: 1 addition & 1 deletion step.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *step) setResult(err error) {
if s.includeRunner != nil {
runResults = s.includeRunner.runResults
}
if errors.Is(errStepSkiped, err) {
if errors.Is(errStepSkipped, err) {
s.result = &StepResult{ID: s.runbookID(), Key: s.key, Desc: s.desc, Skipped: true, Err: nil, IncludedRunResults: runResults}
return
}
Expand Down
Loading

0 comments on commit 6add4c2

Please sign in to comment.