Add etcd launcher to test framework

This can be started and stopped the same way as the apiserver.
This commit is contained in:
Hannes Hoerl 2017-11-22 12:19:10 +00:00 committed by Gareth Smith
parent 9d271bf497
commit 677652447e
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package main
import (
"fmt"
"os"
"time"
)
func main() {
expectedArgs := []string{
"--advertise-client-urls",
"our etcd url",
"--data-dir",
"our data directory",
"--listen-client-urls",
"our etcd url",
"--debug",
}
for i, arg := range os.Args[1:] {
if arg != expectedArgs[i] {
fmt.Printf("Expected arg %s, got arg %s", expectedArgs[i], arg)
os.Exit(1)
}
}
fmt.Println("Everything is dandy")
time.Sleep(10 * time.Minute)
}

View File

@ -0,0 +1,30 @@
package test
import (
"os/exec"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega/gexec"
)
// Etcd knows how to run an etcd server. Set it up with the path to a precompiled binary.
type Etcd struct {
// The path to the etcd binary
Path string
}
// Start starts the etcd, and returns a gexec.Session. To stop it again, call Terminate and Wait on that session.
func (s Etcd) Start(etcdURL string, datadir string) (*gexec.Session, error) {
args := []string{
"--advertise-client-urls",
etcdURL,
"--data-dir",
datadir,
"--listen-client-urls",
etcdURL,
"--debug",
}
command := exec.Command(s.Path, args...)
return gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
}

View File

@ -0,0 +1,51 @@
package test_test
import (
. "k8s.io/kubectl/pkg/framework/test"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Etcd", func() {
Context("when given a path to a binary that runs for a long time", func() {
It("can start and stop that binary", func() {
pathToFakeEtcd, err := gexec.Build("k8s.io/kubectl/pkg/framework/test/assets/fakeetcd")
Expect(err).NotTo(HaveOccurred())
etcd := Etcd{Path: pathToFakeEtcd}
By("Starting the Etcd Server")
session, err := etcd.Start("our etcd url", "our data directory")
Expect(err).NotTo(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Everything is dandy"))
Expect(session).NotTo(gexec.Exit())
By("Stopping the Etcd Server")
session.Terminate()
Eventually(session).Should(gexec.Exit(143))
})
})
Context("when no path is given", func() {
It("fails with a helpful error", func() {
etcd := Etcd{}
_, err := etcd.Start("our etcd url", "")
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
Context("when given a path to a non-executable", func() {
It("fails with a helpful error", func() {
apiServer := Etcd{
Path: "./etcd.go",
}
_, err := apiServer.Start("our etcd url", "")
Expect(err).To(MatchError(ContainSubstring("./etcd.go: permission denied")))
})
})
})