Add a default constructor for EtcdConfig
This commit is contained in:
parent
08f495a8ee
commit
84e6727884
|
|
@ -1,6 +1,10 @@
|
||||||
package test
|
package test
|
||||||
|
|
||||||
import "github.com/asaskevich/govalidator"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/asaskevich/govalidator"
|
||||||
|
)
|
||||||
|
|
||||||
// EtcdConfig is a struct holding data to configure the Etcd process
|
// EtcdConfig is a struct holding data to configure the Etcd process
|
||||||
type EtcdConfig struct {
|
type EtcdConfig struct {
|
||||||
|
|
@ -8,6 +12,28 @@ type EtcdConfig struct {
|
||||||
PeerURL string `valid:"required,url"`
|
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
|
// Validate checks that the config contains only valid URLs
|
||||||
func (c *EtcdConfig) Validate() error {
|
func (c *EtcdConfig) Validate() error {
|
||||||
_, err := govalidator.ValidateStruct(c)
|
_, err := govalidator.ValidateStruct(c)
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,68 @@
|
||||||
package test_test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "k8s.io/kubectl/pkg/framework/test"
|
"fmt"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("EtcdConfig", func() {
|
var _ = Describe("EtcdConfig", func() {
|
||||||
It("does not error on valid config", func() {
|
Context("Constructor", func() {
|
||||||
conf := &EtcdConfig{
|
var (
|
||||||
PeerURL: "http://this.is.some.url:1234",
|
previousPortFinder PortFinder
|
||||||
ClientURL: "http://this.is.another.url/",
|
)
|
||||||
}
|
BeforeEach(func() {
|
||||||
err := conf.Validate()
|
previousPortFinder = etcdPortFinder
|
||||||
Expect(err).NotTo(HaveOccurred())
|
})
|
||||||
})
|
AfterEach(func() {
|
||||||
|
etcdPortFinder = previousPortFinder
|
||||||
|
})
|
||||||
|
|
||||||
It("errors on empty config", func() {
|
It("sets some sane default URLs", func() {
|
||||||
conf := &EtcdConfig{}
|
etcdPortFinder = func(host string) (port int, addr string, err error) {
|
||||||
err := conf.Validate()
|
return 42, "1.2.3.4", nil
|
||||||
Expect(err).To(MatchError(ContainSubstring("PeerURL: non zero value required")))
|
}
|
||||||
Expect(err).To(MatchError(ContainSubstring("ClientURL: non zero value required")))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("errors on malformed URLs", func() {
|
conf, err := NewEtcdConfig()
|
||||||
conf := &EtcdConfig{
|
Expect(err).NotTo(HaveOccurred())
|
||||||
PeerURL: "something not URLish",
|
Expect(conf.ClientURL).To(Equal("http://1.2.3.4:42"))
|
||||||
ClientURL: "something not URLesc",
|
})
|
||||||
}
|
|
||||||
err := conf.Validate()
|
It("prop[agates and error while trying to find a port", func() {
|
||||||
Expect(err).To(MatchError(ContainSubstring("PeerURL: something not URLish does not validate as url")))
|
etcdPortFinder = func(host string) (port int, addr string, err error) {
|
||||||
Expect(err).To(MatchError(ContainSubstring("ClientURL: something not URLesc does not validate as url")))
|
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",
|
||||||
|
ClientURL: "http://this.is.another.url/",
|
||||||
|
}
|
||||||
|
err := conf.Validate()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("errors on empty config", func() {
|
||||||
|
conf := &EtcdConfig{}
|
||||||
|
err := conf.Validate()
|
||||||
|
Expect(err).To(MatchError(ContainSubstring("PeerURL: non zero value required")))
|
||||||
|
Expect(err).To(MatchError(ContainSubstring("ClientURL: non zero value required")))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("errors on malformed URLs", func() {
|
||||||
|
conf := &EtcdConfig{
|
||||||
|
PeerURL: "something not URLish",
|
||||||
|
ClientURL: "something not URLesc",
|
||||||
|
}
|
||||||
|
err := conf.Validate()
|
||||||
|
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