Skip to content

Commit

Permalink
test: factor duplicated code into test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
blgm committed Mar 22, 2023
1 parent 3945048 commit e90ea00
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 deletions.
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)
}
}
}
26 changes: 26 additions & 0 deletions acceptance-tests/helpers/testpath/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package testpath provides path utilities for tests
package testpath

import (
"os"
"strings"

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

// Exists returns whether a path exists, failing the test on error
func Exists(path string) bool {
ginkgo.GinkgoHelper()

_, err := os.Stat(path)
switch {
case err == nil:
return true
case strings.Contains(err.Error(), "NotFound"):
return false
default:
ginkgo.Fail(err.Error())
}

return false // unreachable
}

0 comments on commit e90ea00

Please sign in to comment.