Add a default constructor for EtcdConfig
This commit is contained in:
parent
08f495a8ee
commit
84e6727884
|
|
@ -1,6 +1,10 @@
|
|||
package test
|
||||
|
||||
import "github.com/asaskevich/govalidator"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
)
|
||||
|
||||
// EtcdConfig is a struct holding data to configure the Etcd process
|
||||
type EtcdConfig struct {
|
||||
|
|
@ -8,6 +12,28 @@ type EtcdConfig struct {
|
|||
PeerURL string `valid:"required,url"`
|
||||
}
|
||||
|
||||
var etcdPortFinder = DefaultPortFinder
|
||||
|
||||
// NewEtcdConfig returns a simple config for Etcd with sane default values
|
||||
func NewEtcdConfig() (etcdConfig *EtcdConfig, err error) {
|
||||
conf := &EtcdConfig{}
|
||||
host := "localhost"
|
||||
|
||||
if port, addr, err := etcdPortFinder(host); err == nil {
|
||||
conf.ClientURL = fmt.Sprintf("http://%s:%d", addr, port)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if port, addr, err := etcdPortFinder(host); err == nil {
|
||||
conf.PeerURL = fmt.Sprintf("http://%s:%d", addr, port)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// Validate checks that the config contains only valid URLs
|
||||
func (c *EtcdConfig) Validate() error {
|
||||
_, err := govalidator.ValidateStruct(c)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,44 @@
|
|||
package test_test
|
||||
package test
|
||||
|
||||
import (
|
||||
. "k8s.io/kubectl/pkg/framework/test"
|
||||
"fmt"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("EtcdConfig", func() {
|
||||
Context("Constructor", func() {
|
||||
var (
|
||||
previousPortFinder PortFinder
|
||||
)
|
||||
BeforeEach(func() {
|
||||
previousPortFinder = etcdPortFinder
|
||||
})
|
||||
AfterEach(func() {
|
||||
etcdPortFinder = previousPortFinder
|
||||
})
|
||||
|
||||
It("sets some sane default URLs", func() {
|
||||
etcdPortFinder = func(host string) (port int, addr string, err error) {
|
||||
return 42, "1.2.3.4", nil
|
||||
}
|
||||
|
||||
conf, err := NewEtcdConfig()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(conf.ClientURL).To(Equal("http://1.2.3.4:42"))
|
||||
})
|
||||
|
||||
It("prop[agates and error while trying to find a port", func() {
|
||||
etcdPortFinder = func(host string) (port int, addr string, err error) {
|
||||
return 43, "5.6.7.8", fmt.Errorf("oh nooos, no port")
|
||||
}
|
||||
|
||||
_, err := NewEtcdConfig()
|
||||
Expect(err).To(MatchError(ContainSubstring("oh nooos, no port")))
|
||||
})
|
||||
})
|
||||
Context("Validate()", func() {
|
||||
It("does not error on valid config", func() {
|
||||
conf := &EtcdConfig{
|
||||
PeerURL: "http://this.is.some.url:1234",
|
||||
|
|
@ -33,4 +64,5 @@ var _ = Describe("EtcdConfig", func() {
|
|||
Expect(err).To(MatchError(ContainSubstring("PeerURL: something not URLish does not validate as url")))
|
||||
Expect(err).To(MatchError(ContainSubstring("ClientURL: something not URLesc does not validate as url")))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue