diff --git a/pkg/framework/test/apiserver.go b/pkg/framework/test/apiserver.go new file mode 100644 index 000000000..a32daf311 --- /dev/null +++ b/pkg/framework/test/apiserver.go @@ -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) +} diff --git a/pkg/framework/test/apiserver_test.go b/pkg/framework/test/apiserver_test.go new file mode 100644 index 000000000..71dfacd7a --- /dev/null +++ b/pkg/framework/test/apiserver_test.go @@ -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"))) + }) + }) +}) diff --git a/pkg/framework/test/assets/fakeapiserver/apiserver.go b/pkg/framework/test/assets/fakeapiserver/apiserver.go new file mode 100644 index 000000000..a2dd6585b --- /dev/null +++ b/pkg/framework/test/assets/fakeapiserver/apiserver.go @@ -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) +} diff --git a/pkg/framework/test/test_suite_test.go b/pkg/framework/test/test_suite_test.go new file mode 100644 index 000000000..76e998ced --- /dev/null +++ b/pkg/framework/test/test_suite_test.go @@ -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") +}