Skip to content
Merged

Dev #12

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
2 changes: 2 additions & 0 deletions cmd/cli/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ var cmdPlanCheck = &cobra.Command{
planManifest, err := extractPlanManifest(loadedManifests)
if err != nil {
cli.Errorf("Failed to check plan manifest: %v", err)
return
}

if err := validatePlan(planManifest); err != nil {
cli.Errorf("Failed to check plan: %v", err)
return
}

cli.Successf("Successfully checked plan manifest")
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/pterm/pterm v0.12.80
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
github.com/tidwall/gjson v1.18.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down Expand Up @@ -76,6 +77,8 @@ require (
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.etcd.io/bbolt v1.4.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
Expand Down
2 changes: 1 addition & 1 deletion internal/core/manifests/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (

const (
PlanManifestKind = "Plan"
ValuesManifestLind = "Values"
ValuesManifestKind = "Values"
ServerManifestKind = "Server"
ServiceManifestKind = "Service"
HttpTestManifestKind = "HttpTest"
Expand Down
17 changes: 7 additions & 10 deletions internal/core/manifests/kinds/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"time"

"github.com/apiqube/cli/internal/core/runner/hooks"

"github.com/apiqube/cli/internal/core/manifests/utils"
"github.com/google/uuid"

Expand Down Expand Up @@ -35,19 +37,14 @@ type Stage struct {
Parallel bool `yaml:"parallel,omitempty" json:"parallel,omitempty"`
Params map[string]any `yaml:"params,omitempty" json:"params,omitempty" validate:"omitempty"`
Mode string `yaml:"mode,omitempty" json:"mode,omitempty" validate:"omitempty,oneof=strict parallel"` // (strict|parallel)
Hooks Hooks `yaml:"hooks,omitempty" json:"hooks,omitempty" validate:"omitempty,dive"`
Hooks *Hooks `yaml:"hooks,omitempty" json:"hooks,omitempty" validate:"omitempty,dive"`
}

type Hooks struct {
BeforeStart []Action `yaml:"beforeStart,omitempty" json:"beforeStart,omitempty" validate:"omitempty,dive"`
AfterFinish []Action `yaml:"afterFinish,omitempty" json:"afterFinish,omitempty" validate:"omitempty,dive"`
OnSuccess []Action `yaml:"onSuccess,omitempty" json:"onSuccess,omitempty" validate:"omitempty,dive"`
OnFailure []Action `yaml:"onFailure,omitempty" json:"onFailure,omitempty" validate:"omitempty,dive"`
}

type Action struct {
Type string `yaml:"type" json:"type" validate:"required,oneof=log save skip fail exec notify"` // eg log/save/skip/fail/exec/notify
Params map[string]any `yaml:"params" json:"params" validate:"required"`
BeforeRun []hooks.Action `yaml:"beforeRun,omitempty" json:"beforeRun,omitempty" validate:"omitempty,dive"`
AfterRun []hooks.Action `yaml:"afterRun,omitempty" json:"afterRun,omitempty" validate:"omitempty,dive"`
OnSuccess []hooks.Action `yaml:"onSuccess,omitempty" json:"onSuccess,omitempty" validate:"omitempty,dive"`
OnFailure []hooks.Action `yaml:"onFailure,omitempty" json:"onFailure,omitempty" validate:"omitempty,dive"`
}

func (p *Plan) GetID() string {
Expand Down
3 changes: 2 additions & 1 deletion internal/core/manifests/kinds/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type Server struct {
kinds.BaseManifest `yaml:",inline" json:",inline" validate:"required"`

Spec struct {
BaseUrl string `yaml:"baseUrl" json:"baseUrl" validate:"required,url"`
BaseURL string `yaml:"baseUrl" json:"baseUrl" validate:"required,url"`
Health string `yaml:"health" json:"health" validate:"omitempty,max=100"`
Headers map[string]string `yaml:"headers,omitempty" json:"headers"`
} `yaml:"spec" json:"spec" validate:"required"`

Expand Down
155 changes: 155 additions & 0 deletions internal/core/runner/accessor/accessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package accessor

import (
"fmt"
"strconv"
"strings"

"github.com/apiqube/cli/internal/core/runner/interfaces"
)

type DataAccessor interface {
Get(path string) (any, error)
GetString(path string) (string, error)
GetInt(path string) (int64, error)
GetFloat(path string) (float64, error)
GetBool(path string) (bool, error)
GetStringSlice(path string) ([]string, error)
GetMap(path string) (map[string]any, error)
}

var _ DataAccessor = (*Accessor)(nil)

type Accessor struct {
store interfaces.DataStore
}

func NewAccessor(store interfaces.DataStore) *Accessor {
return &Accessor{store: store}
}

func (a *Accessor) Get(path string) (any, error) {
key, subPath := splitKeyAndPath(path)
root, ok := a.store.Get(key)
if !ok {
return nil, fmt.Errorf("key not found: %s", key)
}
return walkPath(root, subPath)
}

func (a *Accessor) GetString(path string) (string, error) {
v, err := a.Get(path)
if err != nil {
return "", err
}

val, ok := v.(string)
if !ok {
return "", fmt.Errorf("not a string at path %s", path)
}

return val, nil
}

func (a *Accessor) GetInt(path string) (int64, error) {
v, err := a.Get(path)
if err != nil {
return -1, err
}

val, ok := v.(int64)
if !ok {
return -1, fmt.Errorf("not a int at path %s", path)
}

return val, nil
}

func (a *Accessor) GetFloat(path string) (float64, error) {
v, err := a.Get(path)
if err != nil {
return -1, err
}

val, ok := v.(float64)
if !ok {
return -1, fmt.Errorf("not a float at path %s", path)
}

return val, nil
}

func (a *Accessor) GetBool(path string) (bool, error) {
v, err := a.Get(path)
if err != nil {
return false, err
}

val, ok := v.(bool)
if !ok {
return false, fmt.Errorf("not a bool at path %s", path)
}

return val, nil
}

func (a *Accessor) GetStringSlice(path string) ([]string, error) {
v, err := a.Get(path)
if err != nil {
return []string{}, err
}

val, ok := v.([]string)
if !ok {
return []string{}, fmt.Errorf("not a string slice at path %s", path)
}

return val, nil
}

func (a *Accessor) GetMap(path string) (map[string]any, error) {
v, err := a.Get(path)
if err != nil {
return map[string]any{}, err
}

val, ok := v.(map[string]any)
if !ok {
return map[string]any{}, fmt.Errorf("not a map at path %s", path)
}

return val, nil
}

func splitKeyAndPath(full string) (string, string) {
if idx := strings.LastIndex(full, "."); idx != -1 {
return full[:idx], full[idx+1:]
}

return full, ""
}

func walkPath(v any, path string) (any, error) {
if path == "" {
return v, nil
}

parts := strings.Split(path, ".")
cur := v

for _, part := range parts {
switch val := cur.(type) {
case map[string]any:
cur = val[part]
case []any:
idx, err := strconv.Atoi(part)
if err != nil || idx >= len(val) {
return nil, fmt.Errorf("invalid index: %s", part)
}
cur = val[idx]
default:
return nil, fmt.Errorf("unsupported or unexpected type at %s", part)
}
}
return cur, nil
}
37 changes: 37 additions & 0 deletions internal/core/runner/assert/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package assert

import (
"fmt"
"net/http"
"reflect"
"strings"

"github.com/apiqube/cli/internal/core/manifests/kinds/tests"
"github.com/apiqube/cli/internal/core/runner/interfaces"
"github.com/tidwall/gjson"
)

type Runner struct{}

func NewRunner() *Runner {
return &Runner{}
}

func (a *Runner) Assert(_ interfaces.ExecutionContext, assert *tests.Assert, _ *http.Response, raw []byte, _ any) error {
for _, el := range assert.Assertions {
val := gjson.GetBytes(raw, el.Target).Value()

if el.Equals != nil && !reflect.DeepEqual(val, el.Equals) {
return fmt.Errorf("expected %v to equal %v", val, el.Equals)
}
if el.Contains != "" {
if s, ok := val.(string); !ok || !strings.Contains(s, el.Contains) {
return fmt.Errorf("expected %v to contain %q", val, el.Contains)
}
}
if el.Exists && val == nil {
return fmt.Errorf("expected %v to exist", el.Target)
}
}
return nil
}
8 changes: 1 addition & 7 deletions internal/core/runner/cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ func (o *Output) StartCase(manifest manifests.Manifest, caseName string) {

func (o *Output) EndCase(manifest manifests.Manifest, caseName string, result *interfaces.CaseResult) {
if result != nil {
cli.Println(fmt.Sprintf(
`Finish %s case from %s manifest with next reults
Result: %s
Success: %v
Status Code: %d
Duration: %s`,
cli.Infof("Finish %s case from %s manifest with next reults\nResult: %s\nSuccess: %v\nStatus Code: %d\nDuration: %s",
caseName,
manifest.GetName(),
result.Name,
result.Success,
result.StatusCode,
result.Duration.String(),
),
)
} else {
cli.Infof("Finish %s case from %s manifest", caseName, manifest.GetName())
Expand Down
3 changes: 3 additions & 0 deletions internal/core/runner/context/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"sync"

"github.com/apiqube/cli/internal/core/runner/cli"

"github.com/apiqube/cli/internal/core/manifests"
"github.com/apiqube/cli/internal/core/runner/interfaces"
)
Expand Down Expand Up @@ -35,6 +37,7 @@ func NewCtxBuilder() *CtxBuilder {
passChans: make(map[string]chan any),
passKinds: make(map[string]reflect.Kind),
passDone: make(map[string]bool),
output: cli.NewOutput(),
}
}

Expand Down
48 changes: 41 additions & 7 deletions internal/core/runner/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,37 @@ func (c *ctxBaseImpl) Value(key any) any {
return c.Context.Value(key)
}

func (c *ctxBaseImpl) GetManifest(id string) (manifests.Manifest, error) {
func (c *ctxBaseImpl) GetAllManifests() []manifests.Manifest {
c.manifestsMutex.RLock()
defer c.manifestsMutex.RUnlock()

ret := make([]manifests.Manifest, 0, len(c.manifests))
for _, m := range c.manifests {
ret = append(ret, m)
}

return ret
}

func (c *ctxBaseImpl) GetManifestsByKind(kind string) ([]manifests.Manifest, error) {
c.manifestsMutex.RLock()
defer c.manifestsMutex.RUnlock()

ret := make([]manifests.Manifest, 0, len(c.manifests))
for _, m := range c.manifests {
if m.GetKind() == kind {
ret = append(ret, m)
}
}

if len(ret) == 0 {
return nil, fmt.Errorf("no such manifest: %s", kind)
}

return ret, nil
}

func (c *ctxBaseImpl) GetManifestByID(id string) (manifests.Manifest, error) {
c.manifestsMutex.RLock()
defer c.manifestsMutex.RUnlock()

Expand All @@ -64,8 +94,12 @@ func (c *ctxBaseImpl) Set(key string, value any) {
func (c *ctxBaseImpl) Get(key string) (any, bool) {
c.storeMutex.RLock()
defer c.storeMutex.RUnlock()
v, ok := c.values[key]
return v, ok

if v, ok := c.values[key]; ok {
return v, true
}

return nil, false
}

func (c *ctxBaseImpl) Delete(key string) {
Expand All @@ -90,12 +124,12 @@ func (c *ctxBaseImpl) SetTyped(key string, value any, kind reflect.Kind) {
func (c *ctxBaseImpl) GetTyped(key string) (any, reflect.Kind, bool) {
c.storeMutex.RLock()
defer c.storeMutex.RUnlock()
v, ok := c.values[key]
if !ok {
return nil, reflect.Invalid, false

if v, ok := c.values[key]; ok {
return v, c.kinds[key], true
}

return v, c.kinds[key], true
return nil, reflect.Invalid, false
}

func (c *ctxBaseImpl) AsString(key string) (string, error) {
Expand Down
Loading
Loading