Skip to content

Commit

Permalink
testing file for internal yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Nov 5, 2024
1 parent 8ba7055 commit 23205cd
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.8.0
golang.org/x/time v0.6.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.12
Expand Down Expand Up @@ -148,7 +149,6 @@ require (
google.golang.org/grpc v1.66.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
)
18 changes: 18 additions & 0 deletions internal/testdata/buildkite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "legacy"

aliases:
images:
alpine: &alpine-image
image: alpine:latest

env:
dev-env: &dev-environment
environment:
REGION: dev

steps:
- name: example
<<: *alpine-image
<<: *dev-environment
commands:
- echo $REGION
19 changes: 19 additions & 0 deletions internal/testdata/go-yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "1"

aliases:
images:
alpine: &alpine-image
image: alpine:latest

env:
dev-env: &dev-environment
environment:
REGION: dev

steps:
- name: example
<<:
- *alpine-image
- *dev-environment
commands:
- echo $REGION
2 changes: 2 additions & 0 deletions internal/testdata/invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- sliceNodeA
- sliceNodeB
17 changes: 17 additions & 0 deletions internal/testdata/no_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
aliases:
images:
alpine: &alpine-image
image: alpine:latest

env:
dev-env: &dev-environment
environment:
REGION: dev

steps:
- name: example
<<:
- *alpine-image
- *dev-environment
commands:
- echo $REGION
88 changes: 88 additions & 0 deletions internal/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: Apache-2.0

package internal

import (
"os"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/go-vela/server/compiler/types/yaml/yaml"
)

func TestInternal_ParseYAML(t *testing.T) {
// wantBuild
wantBuild := &yaml.Build{
Version: "1",
Metadata: yaml.Metadata{
Environment: []string{"steps", "services", "secrets"},
},
Steps: yaml.StepSlice{
&yaml.Step{
Name: "example",
Image: "alpine:latest",
Environment: map[string]string{
"REGION": "dev",
},
Pull: "not_present",
Commands: []string{
"echo $REGION",
},
},
},
}

// set up tests
tests := []struct {
file string
want *yaml.Build
wantErr bool
}{
{
file: "testdata/go-yaml.yml",
want: wantBuild,
},
{
file: "testdata/buildkite.yml",
want: wantBuild,
},
{
file: "testdata/no_version.yml",
want: wantBuild,
},
{
file: "testdata/invalid.yml",
want: nil,
wantErr: true,
},
}

// run tests
for _, test := range tests {
bytes, err := os.ReadFile(test.file)
if err != nil {
t.Errorf("unable to read file: %v", err)
}

gotBuild, err := ParseYAML(bytes)
if err != nil && !test.wantErr {
t.Errorf("ParseYAML returned err: %v", err)
}

if err == nil && test.wantErr {
t.Errorf("ParseYAML returned nil error")
}

if err != nil && test.wantErr {
continue
}

// different versions expected
wantBuild.Version = gotBuild.Version

if diff := cmp.Diff(gotBuild, test.want); diff != "" {
t.Errorf("ParseYAML returned diff (-got +want):\n%s", diff)
}
}
}

0 comments on commit 23205cd

Please sign in to comment.