Skip to content

Commit

Permalink
Add a separate pipeline for integration tests if integration/*.go exists
Browse files Browse the repository at this point in the history
  • Loading branch information
garethjevans committed Mar 8, 2022
1 parent b20fb4a commit 8d4b08c
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 4 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/pelletier/go-toml v1.9.4
github.com/rakyll/statik v0.1.7
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
google.golang.org/api v0.70.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand All @@ -31,6 +32,7 @@ require (
github.com/antchfx/xmlquery v1.3.9 // indirect
github.com/antchfx/xpath v1.2.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.11.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.12+incompatible // indirect
github.com/docker/distribution v2.8.0+incompatible // indirect
github.com/docker/docker v20.10.12+incompatible // indirect
Expand All @@ -49,6 +51,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
Expand Down
5 changes: 5 additions & 0 deletions octo/run-integration-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -euo pipefail

richgo test ./integration/...
5 changes: 5 additions & 0 deletions octo/run-short-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -euo pipefail

richgo test -short ./...
2 changes: 1 addition & 1 deletion octo/statik/statik.go

Large diffs are not rendered by default.

78 changes: 75 additions & 3 deletions octo/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package octo

import (
"fmt"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -46,9 +47,17 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) {
Jobs: map[string]actions.Job{},
}

if f, err := Find(descriptor.Path, regexp.MustCompile(`.+\.go`).MatchString); err != nil {
goFiles, err := Find(descriptor.Path, regexp.MustCompile(`.+\.go`).MatchString)
if err != nil {
return nil, fmt.Errorf("unable to Find .go files in %s\n%w", descriptor.Path, err)
} else if len(f) > 0 {
}

integrationTestFiles, err := Find(descriptor.Path, regexp.MustCompile(`integration/.+\.go`).MatchString)
if err != nil {
return nil, fmt.Errorf("unable to Find .go files in %s\n%w", filepath.Join(descriptor.Path, "integration"), err)
}

if len(goFiles) > 0 {
j := actions.Job{
Name: "Unit Test",
RunsOn: []actions.VirtualEnvironment{actions.UbuntuLatest},
Expand All @@ -71,11 +80,74 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) {
},
}

j.Steps = append(j.Steps, descriptor.Test.Steps...)
if len(integrationTestFiles) == 0 {
j.Steps = append(j.Steps, descriptor.Test.Steps...)
} else {
j.Steps = append(j.Steps, actions.Step{
Name: "Install richgo",
Run: StatikString("/install-richgo.sh"),
Env: map[string]string{"RICHGO_VERSION": RichGoVersion},
},
actions.Step{
Name: "Run Tests",
Run: StatikString("/run-short-tests.sh"),
Env: map[string]string{"RICHGO_FORCE_COLOR": "1"},
})
}

w.Jobs["unit"] = j
}

if len(integrationTestFiles) > 0 {
j := actions.Job{
Name: "Integration Test",
RunsOn: []actions.VirtualEnvironment{actions.UbuntuLatest},
Steps: []actions.Step{
{
Uses: "actions/checkout@v2",
},
{
Uses: "actions/cache@v2",
With: map[string]interface{}{
"path": "${{ env.HOME }}/go/pkg/mod",
"key": "${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}",
"restore-keys": "${{ runner.os }}-go-",
},
},
{
Uses: "actions/setup-go@v2",
With: map[string]interface{}{"go-version": GoVersion},
},
{
Name: "Install create-package",
Run: StatikString("/install-create-package.sh"),
},
{
Name: "Install pack",
Run: StatikString("/install-pack.sh"),
Env: map[string]string{"PACK_VERSION": PackVersion},
},
{
Name: "Enable pack Experimental",
If: fmt.Sprintf("${{ %t }}", descriptor.Package.Platform.OS == PlatformWindows),
Run: StatikString("/enable-pack-experimental.sh"),
},
{
Name: "Install richgo",
Run: StatikString("/install-richgo.sh"),
Env: map[string]string{"RICHGO_VERSION": RichGoVersion},
},
{
Name: "Run Tests",
Run: StatikString("/run-integration-tests.sh"),
Env: map[string]string{"RICHGO_FORCE_COLOR": "1"},
},
},
}

w.Jobs["integration"] = j
}

if descriptor.Builder != nil {
j := actions.Job{
Name: "Create Builder Test",
Expand Down
107 changes: 107 additions & 0 deletions octo/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package octo_test

import (
"github.com/paketo-buildpacks/pipeline-builder/octo"
"github.com/paketo-buildpacks/pipeline-builder/octo/actions"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// use our own type as the events cannot be unmarshalled
type jobs struct {
Jobs map[string]*actions.Job `yaml:jobs`
}

func TestContributeTest_Default(t *testing.T) {
dir, descriptor := setUp(t)
defer os.RemoveAll(dir)

err := ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte{}, 0644)
assert.NoError(t, err)

contribution, err := octo.ContributeTest(descriptor)
assert.NoError(t, err)

assert.Equal(t, ".github/workflows/tests.yml", contribution.Path)
t.Log(string(contribution.Content))

var workflow jobs
err = yaml.Unmarshal(contribution.Content, &workflow)
assert.NoError(t, err)

assert.Equal(t, 2, len(workflow.Jobs))
assert.NotNil(t, workflow.Jobs["unit"])
assert.Nil(t, workflow.Jobs["integration"])

steps := workflow.Jobs["unit"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test ./...")
}


func TestContributeTest_IntegrationTests(t *testing.T) {
dir, descriptor := setUp(t)
defer os.RemoveAll(dir)

err := ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte{}, 0644)
assert.NoError(t, err)

err = os.Mkdir(filepath.Join(dir, "integration"), 0755)
assert.NoError(t, err)

err = ioutil.WriteFile(filepath.Join(dir, "integration", "main.go"), []byte{}, 0644)
assert.NoError(t, err)

contribution, err := octo.ContributeTest(descriptor)
assert.NoError(t, err)

assert.Equal(t, ".github/workflows/tests.yml", contribution.Path)
t.Log(string(contribution.Content))

var workflow jobs
err = yaml.Unmarshal(contribution.Content, &workflow)
assert.NoError(t, err)

assert.Equal(t, 3, len(workflow.Jobs))
assert.NotNil(t, workflow.Jobs["unit"])
assert.NotNil(t, workflow.Jobs["integration"])

steps := workflow.Jobs["unit"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test -short ./...")

steps = workflow.Jobs["integration"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test ./integration/...")
}

func setUp(t *testing.T) (string, octo.Descriptor ){
dir, err := ioutil.TempDir("", "main-package")
assert.NoError(t, err)

err = os.Mkdir(filepath.Join(dir, ".github"), 0755)
assert.NoError(t, err)

err = ioutil.WriteFile(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"), []byte(`---
github:
username: ${{ secrets.JAVA_GITHUB_USERNAME }}
token: ${{ secrets.JAVA_GITHUB_TOKEN }}
package:
repository: gcr.io/paketo-buildpacks/dummy
register: true
registry_token: ${{ secrets.JAVA_GITHUB_TOKEN }}
`), 0644)
assert.NoError(t, err)

descriptor, err := octo.NewDescriptor(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"))
assert.NoError(t, err)

t.Logf("%+v", descriptor)


assert.NotNil(t, descriptor.Package)

return dir, descriptor
}

0 comments on commit 8d4b08c

Please sign in to comment.