diff --git a/.github/scripts/examples_compile.sh b/.github/scripts/examples_compile.sh new file mode 100755 index 0000000..b4c8f99 --- /dev/null +++ b/.github/scripts/examples_compile.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +examplesDir="$1" +exitCode=0 + +for dir in ${examplesDir}/*; do + echo "Entering $dir" + cd $dir + + if ! go mod tidy; then + echo "Error: failed to 'go mod tidy' $dir" + exitCode=1 + continue + fi + + if ! go build -buildvcs=false; then + echo "Error: failed to 'go build' $dir" + exitCode=1 + continue + else + echo "::notice::Compiled successfully: $dir" + fi + +done + +exit $exitCode diff --git a/.github/scripts/examples_test.sh b/.github/scripts/examples_test.sh new file mode 100755 index 0000000..d8f17f9 --- /dev/null +++ b/.github/scripts/examples_test.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +examplesDir="$1" +testdataDir="testdata" +testFile="main_test.go" +exitCode=0 + +for dir in ${examplesDir}/*; do + echo "Entering $dir" + cd $dir + + # examples usually only have a 'main.go', hence the test 'main_test.go' must exist + if [[ ! -f $testFile ]]; then + echo "::error::Missing test file '$dir/$testFile'" + exitCode=1 + continue + fi + + # testdata directory must exist + if [[ ! -d $testdataDir ]]; then + echo "::error::Missing '$dir/$testdataDir' directory" + exitCode=1 + continue + fi + + # at least one golden file fixture needs to be present + fileCount=$(ls $testdataDir | wc -l) + if [[ ! $fileCount -gt 0 ]]; then + echo "::error::No golden files in '$testdataDir'" + continue + fi + + # run actual test + if go test -v; then + echo "::notice::Tests ran successfully in $dir" + else + echo "::error::Tests failed in $dir" + exitCode=1 + fi + +done + +exit $exitCode diff --git a/.github/workflows/examples.yaml b/.github/workflows/examples.yaml new file mode 100644 index 0000000..b878cd9 --- /dev/null +++ b/.github/workflows/examples.yaml @@ -0,0 +1,21 @@ +--- +name: Example Tests +on: [push, pull_request] +jobs: + compile_examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: 1.19.x + - run: ./.github/scripts/examples_compile.sh $(pwd)/examples + + run_example_tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: 1.19.x + - run: ./.github/scripts/examples_test.sh $(pwd)/examples diff --git a/examples/secrets/go.mod b/examples/secrets/go.mod index 1f52f86..2e205cb 100644 --- a/examples/secrets/go.mod +++ b/examples/secrets/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/lukasjarosch/skipper v0.0.0-20220823200445-07585978767f + github.com/onsi/gomega v1.22.1 github.com/spf13/afero v1.9.2 ) @@ -20,6 +21,7 @@ require ( github.com/Masterminds/semver/v3 v3.1.1 // indirect github.com/Masterminds/sprig/v3 v3.2.2 // indirect github.com/golang-jwt/jwt v3.2.1+incompatible // indirect + github.com/google/go-cmp v0.5.8 // indirect github.com/google/uuid v1.1.2 // indirect github.com/huandu/xstrings v1.3.1 // indirect github.com/imdario/mergo v0.3.11 // indirect @@ -31,8 +33,8 @@ require ( github.com/shopspring/decimal v1.2.0 // indirect github.com/spf13/cast v1.3.1 // indirect golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88 // indirect - golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect - golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/examples/secrets/go.sum b/examples/secrets/go.sum index a082559..93a6f8c 100644 --- a/examples/secrets/go.sum +++ b/examples/secrets/go.sum @@ -116,6 +116,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -162,6 +164,9 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/onsi/ginkgo/v2 v2.3.0 h1:kUMoxMoQG3ogk/QWyKh3zibV7BKZ+xBpWil1cTylVqc= +github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI= +github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM= github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI= github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -266,8 +271,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -322,8 +327,8 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/examples/secrets/main_test.go b/examples/secrets/main_test.go new file mode 100644 index 0000000..91bb53a --- /dev/null +++ b/examples/secrets/main_test.go @@ -0,0 +1,23 @@ +package main_test + +import ( + "io/ioutil" + "path" + "testing" + + "github.com/onsi/gomega" +) + +func TestInventory(t *testing.T) { + g := gomega.NewGomegaWithT(t) + goldenFile := "azure_keyvault_inventory.golden.json" + inventortyFile := path.Join("compiled", "azure_keyvault", "inventory.json") + + goldenData, err := ioutil.ReadFile(path.Join("testdata", goldenFile)) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + inventoryData, err := ioutil.ReadFile(inventortyFile) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + g.Expect(goldenData).To(gomega.MatchJSON(inventoryData)) +} diff --git a/examples/secrets/secrets b/examples/secrets/secrets new file mode 100755 index 0000000..197f970 Binary files /dev/null and b/examples/secrets/secrets differ diff --git a/examples/secrets/testdata/azure_keyvault_inventory.golden.json b/examples/secrets/testdata/azure_keyvault_inventory.golden.json new file mode 100644 index 0000000..94ca862 --- /dev/null +++ b/examples/secrets/testdata/azure_keyvault_inventory.golden.json @@ -0,0 +1,20 @@ +{ + "azure": { + "common": { + "skipper": { + "copy": [ + { + "source": "inventory.json", + "target": "something_else/foobar.json" + } + ] + }, + "subscription_id": "INVALID DEFAULT VALUE" + } + }, + "secrets": { + "test1": "?{azurekv:targets/azure_keyvault/test1||randomstring:32}", + "test2": "?{azurekv:targets/azure_keyvault/test2||randomstring:64}" + }, + "test": "azure_keyvault" +}