Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: factor duplicated code into test helper #745

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions acceptance-tests/helpers/apps/testappmanifests.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package apps

import (
"fmt"
"os"
"path/filepath"
"csbbrokerpakgcp/acceptance-tests/helpers/testpath"
)

type ManifestCode string
Expand All @@ -14,18 +12,7 @@ const (
)

func (a ManifestCode) Path() string {
for _, d := range []string{"apps", "../apps"} {
p, err := filepath.Abs(filepath.Join(d, string(a)))
if err != nil {
panic(fmt.Sprintf("error resolving absolute path: %s", err))
}

if _, err := os.Stat(p); err == nil {
return p
}
}

panic(fmt.Sprintf("could not find source for app manifest: %s", a))
return testpath.BrokerpakFile("acceptance-tests", "apps", string(a))
}

func WithTestAppManifest(manifest ManifestCode) Option {
Expand Down
14 changes: 2 additions & 12 deletions acceptance-tests/helpers/apps/testapps.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package apps

import (
"fmt"
"os"
"path/filepath"
"csbbrokerpakgcp/acceptance-tests/helpers/testpath"
)

type AppCode string
Expand All @@ -20,15 +18,7 @@ const (
)

func (a AppCode) Dir() string {
for _, d := range []string{"apps", "../apps"} {
p := filepath.Join(d, string(a))
_, err := os.Stat(p)
if err == nil {
return p
}
}

panic(fmt.Sprintf("could not find source for app: %s", a))
return testpath.BrokerpakFile("acceptance-tests", "apps", string(a))
}

func WithApp(app AppCode) Option {
Expand Down
16 changes: 2 additions & 14 deletions acceptance-tests/helpers/brokers/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package brokers

import (
"csbbrokerpakgcp/acceptance-tests/helpers/testpath"
"fmt"
"os"
"strings"

"csbbrokerpakgcp/acceptance-tests/helpers/apps"
Expand Down Expand Up @@ -94,23 +94,11 @@ func WithPassword(password string) Option {
func defaultConfig(opts ...Option) (broker Broker) {
defaults := []Option{
WithName(random.Name(random.WithPrefix("broker"))),
WithSourceDir(defaultSourceDir()),
WithSourceDir(testpath.BrokerpakRoot()),
WithUsername(random.Name()),
WithPassword(random.Password()),
WithEncryptionSecret(random.Password()),
}
WithOptions(append(defaults, opts...)...)(&broker)
return broker
}

func defaultSourceDir() string {
for _, d := range []string{"..", "../.."} {
p := fmt.Sprintf("%s/%s", d, "cf-manifest.yml")
_, err := os.Stat(p)
if err == nil {
return d
}
}

panic("could not find source for broker app")
}
22 changes: 22 additions & 0 deletions acceptance-tests/helpers/testpath/brokerpak_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package testpath

import (
"fmt"
"path"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func BrokerpakFile(parts ...string) string {
GinkgoHelper()

r := BrokerpakRoot()
p := filepath.Join(append([]string{r}, parts...)...)
Expect(p).To(BeAnExistingFile(), func() string {
return fmt.Sprintf("could not find file %q in brokerpak %q", path.Join(parts...), r)
})

return p
}
36 changes: 36 additions & 0 deletions acceptance-tests/helpers/testpath/brokerpak_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testpath

import (
"fmt"
"os"
"path/filepath"

"github.com/onsi/ginkgo/v2"
)

// BrokerpakRoot searches upwards from the current working directory to find the root path of the brokerpak
// Fails the test if not found
func BrokerpakRoot() string {
ginkgo.GinkgoHelper()

cwd, err := os.Getwd()
if err != nil {
panic(err)
}

d, err := filepath.Abs(cwd)
if err != nil {
panic(err)
}

for {
switch {
case Exists(filepath.Join(d, "manifest.yml")) && Exists(filepath.Join(d, "acceptance-tests")):
return d
case d == "/":
ginkgo.Fail(fmt.Sprintf("could not determine brokerpak root from %q", cwd))
default:
d = filepath.Dir(d)
}
}
}
12 changes: 12 additions & 0 deletions acceptance-tests/helpers/testpath/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package testpath provides path utilities for tests
package testpath

import (
"os"
)

// Exists returns whether a path exists
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}