Skip to content

Commit

Permalink
adapt test
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Dec 5, 2024
1 parent 45b3907 commit 926c6c3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
6 changes: 4 additions & 2 deletions tooling/templatize/internal/end2end/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"context"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -134,14 +135,15 @@ func TestE2EShell(t *testing.T) {
t.Skip("Skipping end-to-end tests")
}

tmpDir := t.TempDir()
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
assert.NilError(t, err)

e2eImpl := newE2E(tmpDir)

e2eImpl.AddStep(pipeline.Step{
Name: "readInput",
Action: "Shell",
Command: "/usr/bin/echo ${PWD} > env.txt",
Command: "/bin/echo ${PWD} > env.txt",
})

persistAndRun(t, &e2eImpl)
Expand Down
2 changes: 1 addition & 1 deletion tooling/templatize/pkg/pipeline/pipeline.schema.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"command": {
"type": "string"
},
"envVars": {
"variables": {
"type": "array",
"items": {
"$ref": "#/definitions/variable"
Expand Down
16 changes: 8 additions & 8 deletions tooling/templatize/pkg/pipeline/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ func TestCreateCommand(t *testing.T) {
{
name: "basic",
step: &Step{
Command: "/usr/bin/echo hello",
Command: "/bin/echo hello",
},
expectedScript: buildBashScript("/usr/bin/echo hello"),
expectedScript: buildBashScript("/bin/echo hello"),
},
{
name: "dry-run",
step: &Step{
Command: "/usr/bin/echo hello",
Command: "/bin/echo hello",
DryRun: DryRun{
Command: "/usr/bin/echo dry-run",
Command: "/bin/echo dry-run",
},
},
dryRun: true,
expectedScript: buildBashScript("/usr/bin/echo dry-run"),
expectedScript: buildBashScript("/bin/echo dry-run"),
},
{
name: "dry-run-env",
step: &Step{
Command: "/usr/bin/echo",
Command: "/bin/echo",
DryRun: DryRun{
Variables: []Variable{
{
Expand All @@ -54,14 +54,14 @@ func TestCreateCommand(t *testing.T) {
},
},
dryRun: true,
expectedScript: buildBashScript("/usr/bin/echo"),
expectedScript: buildBashScript("/bin/echo"),
envVars: map[string]string{},
expectedEnv: []string{"DRY_RUN=true"},
},
{
name: "dry-run fail",
step: &Step{
Command: "/usr/bin/echo",
Command: "/bin/echo",
},
dryRun: true,
skipCommand: true,
Expand Down
33 changes: 16 additions & 17 deletions tooling/templatize/pkg/pipeline/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

//go:embed pipeline.schema.v1.json
var pipelineSchemaV1Content []byte
var pipelineSchemaV1Ref = "pipeline.schema.v1"
var defaultSchemaRef = pipelineSchemaV1Ref

func ValidatePipelineSchema(pipelineContent []byte) error {
// unmarshal pipeline content
Expand All @@ -21,57 +23,54 @@ func ValidatePipelineSchema(pipelineContent []byte) error {
}

// load pipeline schema
pipelineSchema, schemaUrl, err := getSchemaForPipeline(pipelineMap)
pipelineSchema, schemaRef, err := getSchemaForPipeline(pipelineMap)
if err != nil {
return fmt.Errorf("failed to load pipeline schema: %v", err)
}

// validate pipeline schema
err = pipelineSchema.Validate(pipelineMap)
if err != nil {
return fmt.Errorf("pipeline is not compliant with schema %s: %v", schemaUrl, err)
return fmt.Errorf("pipeline is not compliant with schema %s: %v", schemaRef, err)
}
return nil
}

func getSchemaForPipeline(pipelineMap map[string]interface{}) (*jsonschema.Schema, string, error) {
func getSchemaForPipeline(pipelineMap map[string]interface{}) (pipelineSchema *jsonschema.Schema, schemaRef string, err error) {
schemaRef, ok := pipelineMap["$schema"].(string)
if !ok {
return nil, "", fmt.Errorf("pipeline $schema reference is missing - add $schema: pipeline.schema.v1")
schemaRef = defaultSchemaRef
}

switch schemaRef {
case "pipeline.schema.v1":
return compileSchema(pipelineSchemaV1Content)
case pipelineSchemaV1Ref:
pipelineSchema, err = compileSchema(schemaRef, pipelineSchemaV1Content)
default:
return nil, "", fmt.Errorf("unsupported schema reference: %s", schemaRef)
}
return
}

func compileSchema(schemaBytes []byte) (*jsonschema.Schema, string, error) {
func compileSchema(schemaRef string, schemaBytes []byte) (*jsonschema.Schema, error) {
// parse schema content
schemaMap := make(map[string]interface{})
err := json.Unmarshal(schemaBytes, &schemaMap)
if err != nil {
return nil, "", fmt.Errorf("failed to unmarshal schema content: %v", err)
}
schemaUrl, ok := schemaMap["title"].(string)
if !ok {
return nil, "", fmt.Errorf("failed to get schema title")
return nil, fmt.Errorf("failed to unmarshal schema content: %v", err)
}

// compile schema
c := jsonschema.NewCompiler()
err = c.AddResource(schemaUrl, schemaMap)
err = c.AddResource(schemaRef, schemaMap)
if err != nil {
return nil, "", fmt.Errorf("failed to add schema resource %s: %v", schemaUrl, err)
return nil, fmt.Errorf("failed to add schema resource %s: %v", schemaRef, err)
}
pipelineSchema, err := c.Compile(schemaUrl)
pipelineSchema, err := c.Compile(schemaRef)
if err != nil {
return nil, "", fmt.Errorf("failed to compile schema %s: %v", schemaUrl, err)
return nil, fmt.Errorf("failed to compile schema %s: %v", schemaRef, err)
}

return pipelineSchema, schemaUrl, nil
return pipelineSchema, nil
}

func (p *Pipeline) Validate() error {
Expand Down
1 change: 1 addition & 0 deletions tooling/templatize/testdata/pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$schema: pipeline.schema.v1
serviceGroup: Microsoft.Azure.ARO.Test
rolloutName: Test Rollout
resourceGroups:
Expand Down
1 change: 1 addition & 0 deletions tooling/templatize/testdata/zz_fixture_TestRawOptions.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$schema: pipeline.schema.v1
serviceGroup: Microsoft.Azure.ARO.Test
rolloutName: Test Rollout
resourceGroups:
Expand Down

0 comments on commit 926c6c3

Please sign in to comment.