From 23205cde77a973bed98692dfe1cb69d145769c30 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Tue, 5 Nov 2024 16:05:31 -0600 Subject: [PATCH] testing file for internal yaml --- go.mod | 2 +- internal/testdata/buildkite.yml | 18 +++++++ internal/testdata/go-yaml.yml | 19 +++++++ internal/testdata/invalid.yml | 2 + internal/testdata/no_version.yml | 17 ++++++ internal/yaml_test.go | 88 ++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 internal/testdata/buildkite.yml create mode 100644 internal/testdata/go-yaml.yml create mode 100644 internal/testdata/invalid.yml create mode 100644 internal/testdata/no_version.yml create mode 100644 internal/yaml_test.go diff --git a/go.mod b/go.mod index 290078d95..02d450edb 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/internal/testdata/buildkite.yml b/internal/testdata/buildkite.yml new file mode 100644 index 000000000..1a74a344b --- /dev/null +++ b/internal/testdata/buildkite.yml @@ -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 \ No newline at end of file diff --git a/internal/testdata/go-yaml.yml b/internal/testdata/go-yaml.yml new file mode 100644 index 000000000..7a9d284f5 --- /dev/null +++ b/internal/testdata/go-yaml.yml @@ -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 diff --git a/internal/testdata/invalid.yml b/internal/testdata/invalid.yml new file mode 100644 index 000000000..a1cde504a --- /dev/null +++ b/internal/testdata/invalid.yml @@ -0,0 +1,2 @@ +- sliceNodeA +- sliceNodeB \ No newline at end of file diff --git a/internal/testdata/no_version.yml b/internal/testdata/no_version.yml new file mode 100644 index 000000000..5b59dea2a --- /dev/null +++ b/internal/testdata/no_version.yml @@ -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 \ No newline at end of file diff --git a/internal/yaml_test.go b/internal/yaml_test.go new file mode 100644 index 000000000..627139229 --- /dev/null +++ b/internal/yaml_test.go @@ -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) + } + } +}