Skip to content

Commit

Permalink
better tests and100% fewer debugging files
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey committed Oct 30, 2024
1 parent 0751c8f commit 687ac14
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 221 deletions.
79 changes: 55 additions & 24 deletions src/test/e2e/api_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -211,42 +212,72 @@ func TestApiValidation(t *testing.T) {
testEnv.Test(t, featureTrueValidation, featureFalseValidation)
}

// TestApiValidation_templated uses a URL parameter to control the return response from the API.
func TestApiValidation_templated(t *testing.T) {
message.NoProgress = true
dev.RunInteractively = false
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"pass": true}`))
wantResp := r.URL.Query().Get("response")
require.NotEmpty(t, wantResp)
passRsp := false
if wantResp == "true" {
passRsp = true
}
resp := struct {
Pass bool `json:"pass"`
}{
passRsp,
}
err := json.NewEncoder(w).Encode(resp)
require.NoError(t, err)
}))
defer svr.Close()

tmpl := "scenarios/api-validations/component-definition.yaml.tmpl"
composer, err := composition.New(
composition.WithModelFromLocalPath(tmpl),
composition.WithRenderSettings("all", true),
composition.WithTemplateRenderer("all", nil, []template.VariableConfig{
{
Key: "reqUrl",
Default: svr.URL,
},
}, []string{}),
)
require.NoError(t, err)

validator, err := validation.New(validation.WithComposition(composer, tmpl))
require.NoError(t, err)
// since it's just the two tests I'm using the name to check the assessment result.
tests := map[string]struct {
response string
}{
"satisfied": {"true"},
"not-satisfied": {"false"},
}

assessment, err := validator.ValidateOnPath(context.Background(), tmpl, "")
require.NoError(t, err)
require.GreaterOrEqual(t, len(assessment.Results), 1)
for name, test := range tests {
t.Run(name, func(t *testing.T) {

composer, err := composition.New(
composition.WithModelFromLocalPath(tmpl),
composition.WithRenderSettings("all", true),
composition.WithTemplateRenderer("all", nil, []template.VariableConfig{
{
Key: "reqUrl",
Default: svr.URL,
},
{
Key: "response",
Default: test.response,
},
}, []string{}),
)
require.NoError(t, err)

validator, err := validation.New(validation.WithComposition(composer, tmpl))
require.NoError(t, err)

assessment, err := validator.ValidateOnPath(context.Background(), tmpl, "")
require.NoError(t, err)
require.GreaterOrEqual(t, len(assessment.Results), 1)

result := assessment.Results[0]
require.NotNil(t, result.Findings)
for _, finding := range *result.Findings {
state := finding.Target.Status.State
if state != "satisfied" {
t.Fatal("State should be satisfied, but got :", state)
}
result := assessment.Results[0]
require.NotNil(t, result.Findings)
for _, finding := range *result.Findings {
state := finding.Target.Status.State
if state != name {
t.Fatalf("State should be %s, but got :%s", name, state)
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ component-definition:
- name: healthcheck
url: {{ .var.reqUrl }}
parameters:
key: value
response: {{ .var.response }}
provider:
type: opa
opa-spec:
Expand Down
196 changes: 0 additions & 196 deletions src/test/e2e/scenarios/api-validations/out.yaml

This file was deleted.

0 comments on commit 687ac14

Please sign in to comment.