Skip to content

Commit

Permalink
Merge pull request #196 from totherme/hide_internals
Browse files Browse the repository at this point in the history
Hide test framework internals
  • Loading branch information
k8s-ci-robot authored Jan 8, 2018
2 parents 6d8a680 + 10ba636 commit 143e500
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 161 deletions.
63 changes: 0 additions & 63 deletions pkg/framework/test/address_manager.go

This file was deleted.

25 changes: 8 additions & 17 deletions pkg/framework/test/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ 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.
type APIServer struct {
// Address is the address, a host and a port, the ApiServer should listen on for client connections.
// If this is not specified, the DefaultAddressManager is used to determine this address.
// If this is not specified, we default to a random free port on localhost.
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
Expand All @@ -32,7 +33,7 @@ type APIServer struct {
ProcessStarter SimpleSessionStarter

// CertDir is a struct holding a path to a certificate directory and a function to cleanup that directory.
CertDir *Directory
CertDir *CleanableDirectory

// Etcd is an implementation of a ControlPlaneProcess and is responsible to run Etcd and provide its coordinates.
// If not specified, a brand new instance of Etcd is brought up.
Expand Down Expand Up @@ -112,10 +113,10 @@ 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{}
am := &internal.AddressManager{}
port, host, err := am.Initialize()
if err != nil {
return err
Expand Down Expand Up @@ -179,13 +180,3 @@ func (s *APIServer) Stop() error {
}
return s.CertDir.Cleanup()
}

// ExitCode returns the exit code of the process, if it has exited. If it hasn't exited yet, ExitCode returns -1.
func (s *APIServer) ExitCode() int {
return s.session.ExitCode()
}

// Buffer implements the gbytes.BufferProvider interface and returns the stdout of the process
func (s *APIServer) Buffer() *gbytes.Buffer {
return s.session.Buffer()
}
14 changes: 1 addition & 13 deletions pkg/framework/test/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"k8s.io/kubectl/pkg/framework/test/testfakes"
)
Expand All @@ -41,7 +40,7 @@ var _ = Describe("Apiserver", func() {
apiServer = &APIServer{
Address: &url.URL{Scheme: "http", Host: "the.host.for.api.server:5678"},
Path: "/some/path/to/apiserver",
CertDir: &Directory{
CertDir: &CleanableDirectory{
Path: "/some/path/to/certdir",
Cleanup: func() error {
cleanupCallCount += 1
Expand All @@ -56,10 +55,6 @@ var _ = Describe("Apiserver", func() {
Describe("starting and stopping the server", func() {
Context("when given a path to a binary that runs for a long time", func() {
It("can start and stop that binary", func() {
sessionBuffer := gbytes.NewBuffer()
fmt.Fprint(sessionBuffer, "Everything is fine")
fakeSession.BufferReturns(sessionBuffer)

fakeSession.ExitCodeReturnsOnCall(0, -1)
fakeSession.ExitCodeReturnsOnCall(1, 143)

Expand Down Expand Up @@ -87,19 +82,12 @@ var _ = Describe("Apiserver", func() {
By("...getting the URL of Etcd")
Expect(fakeEtcdProcess.URLCallCount()).To(Equal(1))

Eventually(apiServer).Should(gbytes.Say("Everything is fine"))
Expect(fakeSession.ExitCodeCallCount()).To(Equal(0))
Expect(apiServer).NotTo(gexec.Exit())
Expect(fakeSession.ExitCodeCallCount()).To(Equal(1))

By("Stopping the API Server")
Expect(apiServer.Stop()).To(Succeed())

Expect(cleanupCallCount).To(Equal(1))
Expect(fakeEtcdProcess.StopCallCount()).To(Equal(1))
Expect(apiServer).To(gexec.Exit(143))
Expect(fakeSession.TerminateCallCount()).To(Equal(1))
Expect(fakeSession.ExitCodeCallCount()).To(Equal(2))
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"os"
)

// Directory holds a path to a directory and knows how to tear down / cleanup that directory
type Directory struct {
// CleanableDirectory holds a path to a directory and knows how to tear down / cleanup that directory
type CleanableDirectory struct {
Path string
Cleanup func() error
}

func newDirectory() (*Directory, error) {
func newDirectory() (*CleanableDirectory, error) {
path, err := ioutil.TempDir("", "k8s_test_framework_")
if err != nil {
return nil, err
}

return &Directory{
return &CleanableDirectory{
Path: path,
Cleanup: func() error {
return os.RemoveAll(path)
Expand Down
File renamed without changes.
17 changes: 4 additions & 13 deletions pkg/framework/test/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,7 +21,7 @@ type Etcd struct {
Address *url.URL
Path string
ProcessStarter SimpleSessionStarter
DataDir *Directory
DataDir *CleanableDirectory
StopTimeout time.Duration
StartTimeout time.Duration
session SimpleSession
Expand Down Expand Up @@ -86,10 +87,10 @@ 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{}
am := &internal.AddressManager{}
port, host, err := am.Initialize()
if err != nil {
return err
Expand Down Expand Up @@ -147,13 +148,3 @@ func (e *Etcd) Stop() error {
}
return e.DataDir.Cleanup()
}

// ExitCode returns the exit code of the process, if it has exited. If it hasn't exited yet, ExitCode returns -1.
func (e *Etcd) ExitCode() int {
return e.session.ExitCode()
}

// Buffer implements the gbytes.BufferProvider interface and returns the stdout of the process
func (e *Etcd) Buffer() *gbytes.Buffer {
return e.session.Buffer()
}
21 changes: 5 additions & 16 deletions pkg/framework/test/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
. "k8s.io/kubectl/pkg/framework/test"
"k8s.io/kubectl/pkg/framework/test/testfakes"
Expand All @@ -36,8 +35,8 @@ var _ = Describe("Etcd", func() {

etcd = &Etcd{
Path: "/path/to/some/etcd",
DataDir: &Directory{
Path: "/path/to/some/etcd",
DataDir: &CleanableDirectory{
Path: "/path/to/some/data",
Cleanup: func() error {
dataDirCleanupCount += 1
return nil
Expand All @@ -50,18 +49,15 @@ var _ = Describe("Etcd", func() {
Describe("starting and stopping 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() {
sessionBuffer := gbytes.NewBuffer()
fmt.Fprintf(sessionBuffer, "Everything is dandy")
fakeSession.BufferReturns(sessionBuffer)

fakeSession.ExitCodeReturnsOnCall(0, -1)
fakeSession.ExitCodeReturnsOnCall(1, 143)

etcd.Address = &url.URL{Scheme: "http", Host: "this.is.etcd.listening.for.clients:1234"}

etcd.ProcessStarter = func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error) {
Expect(command.Args).To(ContainElement(fmt.Sprintf("--advertise-client-urls=http://%s:%d", "this.is.etcd.listening.for.clients", 1234)))
Expect(command.Args).To(ContainElement(fmt.Sprintf("--listen-client-urls=http://%s:%d", "this.is.etcd.listening.for.clients", 1234)))
Expect(command.Args).To(ContainElement("--advertise-client-urls=http://this.is.etcd.listening.for.clients:1234"))
Expect(command.Args).To(ContainElement("--listen-client-urls=http://this.is.etcd.listening.for.clients:1234"))
Expect(command.Args).To(ContainElement("--data-dir=/path/to/some/data"))
Expect(command.Path).To(Equal("/path/to/some/etcd"))
fmt.Fprint(err, "serving insecure client requests on this.is.etcd.listening.for.clients:1234")
return fakeSession, nil
Expand All @@ -71,18 +67,11 @@ var _ = Describe("Etcd", func() {
err := etcd.Start()
Expect(err).NotTo(HaveOccurred())

Eventually(etcd).Should(gbytes.Say("Everything is dandy"))
Expect(fakeSession.ExitCodeCallCount()).To(Equal(0))
Expect(etcd).NotTo(gexec.Exit())
Expect(fakeSession.ExitCodeCallCount()).To(Equal(1))

By("Stopping the Etcd Server")
Expect(etcd.Stop()).To(Succeed())

Expect(dataDirCleanupCount).To(Equal(1))
Expect(etcd).To(gexec.Exit(143))
Expect(fakeSession.TerminateCallCount()).To(Equal(1))
Expect(fakeSession.ExitCodeCallCount()).To(Equal(2))
})
})

Expand Down
53 changes: 53 additions & 0 deletions pkg/framework/test/internal/address_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package internal

import (
"fmt"
"net"
)

// AddressManager allocates a new address (interface & port) a process
// can bind and keeps track of that.
type AddressManager struct {
port int
host string
}

// Initialize returns a address a process can listen on. It returns
// a tuple consisting of a free port and the hostname resolved to its IP.
func (d *AddressManager) Initialize() (port int, resolvedHost string, err error) {
if d.port != 0 {
return 0, "", fmt.Errorf("this AddressManager is already initialized")
}
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return
}
d.port = l.Addr().(*net.TCPAddr).Port
defer func() {
err = l.Close()
}()
d.host = addr.IP.String()
return d.port, d.host, nil
}

// Port returns the port that this AddressManager is managing. Port returns an
// error if this AddressManager has not yet been initialized.
func (d *AddressManager) Port() (int, error) {
if d.port == 0 {
return 0, fmt.Errorf("this AdressManager is not initialized yet")
}
return d.port, nil
}

// Host returns the host that this AddressManager is managing. Host returns an
// error if this AddressManager has not yet been initialized.
func (d *AddressManager) Host() (string, error) {
if d.host == "" {
return "", fmt.Errorf("this AdressManager is not initialized yet")
}
return d.host, nil
}
Loading

0 comments on commit 143e500

Please sign in to comment.