Add apiserver launcher to test framework

To start an apiserver:

```
apiServer := APIServer{Path: "/path/to/my/apiserver/binary"}
session, err := apiServer.Start("tcp://whereever.is.my.etcd:port")
Expect(err).NotTo(HaveOccurred())
```

When you're done testing against that apiserver:

```
session.Terminate().Wait()
```

...or if you prefer:

```
gexec.Terminate()
```

...which will terminate not only this apiserver, but also all other
command sessions you started in this test.
This commit is contained in:
Gareth Smith 2017-11-22 12:19:02 +00:00
parent 782bee8a6b
commit 9d271bf497
4 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package test
import (
"os/exec"
"fmt"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega/gexec"
)
// APIServer knows how to run a kubernetes apiserver. Set it up with the path to a precompiled binary.
type APIServer struct {
// The path to the apiserver binary
Path string
}
// Start starts the apiserver, and returns a gexec.Session. To stop it again, call Terminate and Wait on that session.
func (s APIServer) Start(etcdURL string) (*gexec.Session, error) {
args := []string{
"--authorization-mode=Node,RBAC",
"--runtime-config=admissionregistration.k8s.io/v1alpha1",
"--v=3", "--vmodule=",
"--admission-control=Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,SecurityContextDeny,DefaultStorageClass,DefaultTolerationSeconds,GenericAdmissionWebhook,ResourceQuota",
"--admission-control-config-file=",
"--bind-address=0.0.0.0",
"--insecure-bind-address=127.0.0.1",
"--insecure-port=8080",
"--storage-backend=etcd3",
fmt.Sprintf("--etcd-servers=%s", etcdURL),
}
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("Apiserver", func() {
Context("when given a path to a binary that runs for a long time", func() {
It("can start and stop that binary", func() {
pathToFakeAPIServer, err := gexec.Build("k8s.io/kubectl/pkg/framework/test/assets/fakeapiserver")
Expect(err).NotTo(HaveOccurred())
apiServer := APIServer{Path: pathToFakeAPIServer}
By("Starting the API Server")
session, err := apiServer.Start("the etcd url")
Expect(err).NotTo(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Everything is fine"))
Expect(session).NotTo(gexec.Exit())
By("Stopping the API Server")
session.Terminate()
Eventually(session).Should(gexec.Exit(143))
})
})
Context("when no path is given", func() {
It("fails with a helpful error", func() {
apiServer := APIServer{}
_, err := apiServer.Start("the 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 := APIServer{
Path: "./apiserver.go",
}
_, err := apiServer.Start("the etcd url")
Expect(err).To(MatchError(ContainSubstring("./apiserver.go: permission denied")))
})
})
})

View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"os"
"time"
)
func main() {
expectedArgs := []string{
"--authorization-mode=Node,RBAC",
"--runtime-config=admissionregistration.k8s.io/v1alpha1",
"--v=3", "--vmodule=",
"--admission-control=Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,SecurityContextDeny,DefaultStorageClass,DefaultTolerationSeconds,GenericAdmissionWebhook,ResourceQuota",
"--admission-control-config-file=",
"--bind-address=0.0.0.0",
"--insecure-bind-address=127.0.0.1",
"--insecure-port=8080",
"--storage-backend=etcd3",
"--etcd-servers=the etcd url",
}
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 fine")
time.Sleep(10 * time.Minute)
}

View File

@ -0,0 +1,13 @@
package test_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestTest(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Test Suite")
}