Hide the BinPathFinder in our internal package

This commit is contained in:
Hannes Hörl 2018-01-08 10:58:34 +00:00 committed by Gareth Smith
parent 58961be445
commit 40566a76e1
5 changed files with 33 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"k8s.io/kubectl/pkg/framework/test/internal"
)
// APIServer knows how to run a kubernetes apiserver.
@ -19,9 +20,9 @@ type APIServer struct {
Address *url.URL
// Path is the path to the apiserver binary. If this is left as the empty
// string, we will use DefaultBinPathFinder to attempt to locate a binary, by
// checking for the TEST_ASSET_KUBE_APISERVER environment variable, and the
// default test assets directory.
// string, we will attempt to locate a binary, by checking for the
// TEST_ASSET_KUBE_APISERVER environment variable, and the default test
// assets directory.
Path string
// ProcessStarter is a way to hook into how a the APIServer process is started. By default `gexec.Start(...)` is
@ -112,7 +113,7 @@ func (s *APIServer) Start() error {
func (s *APIServer) ensureInitialized() error {
if s.Path == "" {
s.Path = DefaultBinPathFinder("kube-apiserver")
s.Path = internal.BinPathFinder("kube-apiserver")
}
if s.Address == nil {
am := &DefaultAddressManager{}

View File

@ -10,6 +10,7 @@ import (
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"k8s.io/kubectl/pkg/framework/test/internal"
)
// Etcd knows how to run an etcd server.
@ -86,7 +87,7 @@ func (e *Etcd) Start() error {
func (e *Etcd) ensureInitialized() error {
if e.Path == "" {
e.Path = DefaultBinPathFinder("etcd")
e.Path = internal.BinPathFinder("etcd")
}
if e.Address == nil {
am := &DefaultAddressManager{}

View File

@ -1,4 +1,4 @@
package test
package internal
import (
"os"
@ -15,19 +15,12 @@ func init() {
if !ok {
panic("Could not determine the path of the BinPathFinder")
}
assetsPath = filepath.Join(filepath.Dir(thisFile), "assets", "bin")
assetsPath = filepath.Join(filepath.Dir(thisFile), "..", "assets", "bin")
}
// BinPathFinder is the signature of a function translating a symbolic name of a binary to
// a resolvable path to the binary in question
type BinPathFinder func(symbolicName string) (binPath string)
//go:generate counterfeiter . BinPathFinder
// DefaultBinPathFinder is an implementation of BinPathFinder which checks the an environment
// variable, derived from the symbolic name, and falls back to a default assets location when
// this variable is not set
func DefaultBinPathFinder(symbolicName string) (binPath string) {
// BinPathFinder checks the an environment variable, derived from the symbolic name,
// and falls back to a default assets location when this variable is not set
func BinPathFinder(symbolicName string) (binPath string) {
punctuationPattern := regexp.MustCompile("[^A-Z0-9]+")
sanitizedName := punctuationPattern.ReplaceAllString(strings.ToUpper(symbolicName), "_")
leadingNumberPattern := regexp.MustCompile("^[0-9]+")

View File

@ -1,4 +1,4 @@
package test
package internal
import (
"os"
@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("DefaultBinPathFinder", func() {
var _ = Describe("BinPathFinder", func() {
Context("when relying on the default assets path", func() {
var (
previousAssetsPath string
@ -20,7 +20,7 @@ var _ = Describe("DefaultBinPathFinder", func() {
assetsPath = previousAssetsPath
})
It("returns the default path when no env var is configured", func() {
binPath := DefaultBinPathFinder("some_bin")
binPath := BinPathFinder("some_bin")
Expect(binPath).To(Equal("/some/path/assets/bin/some_bin"))
})
})
@ -46,20 +46,20 @@ var _ = Describe("DefaultBinPathFinder", func() {
}
})
It("returns the path from the env", func() {
binPath := DefaultBinPathFinder("another_symbolic_name")
binPath := BinPathFinder("another_symbolic_name")
Expect(binPath).To(Equal("/path/to/some_bin.exe"))
})
It("sanitizes the environment variable name", func() {
By("cleaning all non-underscore punctuation")
binPath := DefaultBinPathFinder("another-symbolic name")
binPath := BinPathFinder("another-symbolic name")
Expect(binPath).To(Equal("/path/to/some_bin.exe"))
binPath = DefaultBinPathFinder("another+symbolic\\name")
binPath = BinPathFinder("another+symbolic\\name")
Expect(binPath).To(Equal("/path/to/some_bin.exe"))
binPath = DefaultBinPathFinder("another=symbolic.name")
binPath = BinPathFinder("another=symbolic.name")
Expect(binPath).To(Equal("/path/to/some_bin.exe"))
By("removing numbers from the beginning of the name")
binPath = DefaultBinPathFinder("12another_symbolic_name")
binPath = BinPathFinder("12another_symbolic_name")
Expect(binPath).To(Equal("/path/to/some_bin.exe"))
})
})

View File

@ -0,0 +1,13 @@
package internal_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestInternal(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Internal Suite")
}