Add a default constructor for EtcdConfig

This commit is contained in:
Hannes Hörl 2017-12-08 14:13:56 +00:00 committed by Gareth Smith
parent 08f495a8ee
commit 84e6727884
2 changed files with 83 additions and 25 deletions

View File

@ -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)

View File

@ -1,36 +1,68 @@
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() {
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())
})
Context("Constructor", func() {
var (
previousPortFinder PortFinder
)
BeforeEach(func() {
previousPortFinder = etcdPortFinder
})
AfterEach(func() {
etcdPortFinder = previousPortFinder
})
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("sets some sane default URLs", func() {
etcdPortFinder = func(host string) (port int, addr string, err error) {
return 42, "1.2.3.4", nil
}
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")))
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",
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")))
})
})
})