Skip to content

Commit

Permalink
Merge pull request #14 from CrowdStrike/chore/allow_yaml_config_loading
Browse files Browse the repository at this point in the history
chore: extend FS config loader to allow yaml configs
  • Loading branch information
jsteenb2 committed Jan 31, 2024
2 parents 1788bba + 90ffc14 commit 9889afc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
Expand Down
21 changes: 19 additions & 2 deletions config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package fdk

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -50,9 +54,22 @@ var configReaders = map[string]ConfigLoader{
type localCfgLoader struct{}

func (*localCfgLoader) LoadConfig(ctx context.Context) ([]byte, error) {
b, err := os.ReadFile(os.Getenv("CS_FN_CONFIG_PATH"))
file := os.Getenv("CS_FN_CONFIG_PATH")
b, err := os.ReadFile(file)
if os.IsNotExist(err) {
return nil, ErrCfgNotFound
}
return b, err
if err != nil {
return nil, err
}

if ext := filepath.Ext(file); ext == ".yaml" || ext == ".yml" {
var out map[string]any
if err := yaml.Unmarshal(b, &out); err != nil {
return nil, fmt.Errorf("failed to read yaml config: %w", err)
}
return json.Marshal(out)
}

return b, nil
}
95 changes: 92 additions & 3 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -28,6 +27,7 @@ func TestRun_httprunner(t *testing.T) {
accessToken string
body []byte
config string
configFile string
context []byte
headers http.Header
method string
Expand Down Expand Up @@ -84,6 +84,92 @@ func TestRun_httprunner(t *testing.T) {
containsHeaders(t, wantHeaders, echo.Req.Headers)
},
},
{
name: "simple DELETE request with yaml config should pass",
inputs: inputs{
config: `
string: val
integer: 1`,
configFile: "config.yaml",
headers: http.Header{
"X-Cs-Origin": []string{"fooorigin"},
"X-Cs-Executionid": []string{"exec_id"},
"X-Cs-Traceid": []string{"trace_id"},
},
method: "DELETE",
path: "/path",
queryParams: url.Values{"ids": []string{"id1"}},
},
newHandlerFn: func(ctx context.Context, cfg config) fdk.Handler {
m := fdk.NewMux()
m.Delete("/path", newSimpleHandler(cfg))
return m
},
want: func(t *testing.T, resp *http.Response, got respBody) {
equalVals(t, 201, resp.StatusCode)
equalVals(t, 201, got.Code)

if len(got.Errs) > 0 {
t.Errorf("received unexpected errors\n\t\tgot:\t%+v", got.Errs)
}

echo := got.Req
equalVals(t, config{Str: "val", Int: 1}, echo.Config)

equalVals(t, "/path", echo.Req.Path)
equalVals(t, "DELETE", echo.Req.Method)
equalVals(t, "id1", echo.Req.Queries.Get("ids"))

wantHeaders := make(http.Header)
wantHeaders.Set("X-Cs-Origin", "fooorigin")
wantHeaders.Set("X-Cs-Executionid", "exec_id")
wantHeaders.Set("X-Cs-Traceid", "trace_id")
containsHeaders(t, wantHeaders, echo.Req.Headers)
},
},
{
name: "simple DELETE request with yml config should pass",
inputs: inputs{
config: `
string: val
integer: 1`,
configFile: "config.yml",
headers: http.Header{
"X-Cs-Origin": []string{"fooorigin"},
"X-Cs-Executionid": []string{"exec_id"},
"X-Cs-Traceid": []string{"trace_id"},
},
method: "DELETE",
path: "/path",
queryParams: url.Values{"ids": []string{"id1"}},
},
newHandlerFn: func(ctx context.Context, cfg config) fdk.Handler {
m := fdk.NewMux()
m.Delete("/path", newSimpleHandler(cfg))
return m
},
want: func(t *testing.T, resp *http.Response, got respBody) {
equalVals(t, 201, resp.StatusCode)
equalVals(t, 201, got.Code)

if len(got.Errs) > 0 {
t.Errorf("received unexpected errors\n\t\tgot:\t%+v", got.Errs)
}

echo := got.Req
equalVals(t, config{Str: "val", Int: 1}, echo.Config)

equalVals(t, "/path", echo.Req.Path)
equalVals(t, "DELETE", echo.Req.Method)
equalVals(t, "id1", echo.Req.Queries.Get("ids"))

wantHeaders := make(http.Header)
wantHeaders.Set("X-Cs-Origin", "fooorigin")
wantHeaders.Set("X-Cs-Executionid", "exec_id")
wantHeaders.Set("X-Cs-Traceid", "trace_id")
containsHeaders(t, wantHeaders, echo.Req.Headers)
},
},
{
name: "simple GET request should pass",
inputs: inputs{
Expand Down Expand Up @@ -413,7 +499,11 @@ func TestRun_httprunner(t *testing.T) {
for _, tt := range tests {
fn := func(t *testing.T) {
if tt.inputs.config != "" {
tmp := filepath.Join(t.TempDir(), "config.json")
cfgFile := tt.inputs.configFile
if cfgFile == "" {
cfgFile = "config.json"
}
tmp := filepath.Join(t.TempDir(), cfgFile)
t.Setenv("CS_FN_CONFIG_PATH", tmp)

err := os.WriteFile(tmp, []byte(tt.inputs.config), 0666)
Expand Down Expand Up @@ -773,7 +863,6 @@ func decodeBody(t testing.TB, r io.Reader, v any) {
if err != nil {
t.Fatal("failed to read: " + err.Error())
}
fmt.Println("body: ", string(b))

err = json.Unmarshal(b, v)
if err != nil {
Expand Down

0 comments on commit 9889afc

Please sign in to comment.