Skip to content

Commit

Permalink
Introduce Ginkgo and Gomega
Browse files Browse the repository at this point in the history
Add the BDD testing frameworkg Ginko and its macher library Gomega.

Further add first REST tests for the RawDomainEndpoint.

Change-Id: I48b6b968f568c9bcf952fe3346bbd7ac2df98c86
Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Sep 28, 2016
1 parent 5149710 commit 2c7041f
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 12 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Hacking

### Setup

First make sure you have [govendor](https://github.com/kardianos/govendor)
installed.

Expand All @@ -17,6 +19,8 @@ If you don't have the `$GOPATH/bin` folder on your path, do
export PATH=$PATH:$GOPATH/bin
```

### Building

To build the whole project, type

```bash
Expand All @@ -34,3 +38,13 @@ It is also possible to target only specific modules. For instance to build only
```bash
make build WAHT=virt-launcher
```

### Testing

Type

```bash
make test
```

to run all tests.
6 changes: 3 additions & 3 deletions pkg/virt-controller/virt-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func (vmService) StartVmRaw(name string) error {
}

type Handlers struct {
rawDomainHandler http.Handler
RawDomainHandler http.Handler
}

func main() {
ctx := context.Background()
svc := vmService{}

endpoints := Handlers{
rawDomainHandler: makeRawDomainHandler(ctx, makeRawDomainEndpoint(svc)),
RawDomainHandler: makeRawDomainHandler(ctx, makeRawDomainEndpoint(svc)),
}

http.Handle("/", defineRoutes(&endpoints))
Expand All @@ -43,7 +43,7 @@ func main() {
func defineRoutes(endpoints *Handlers) http.Handler {
router := mux.NewRouter()
restV1Route := router.PathPrefix("/api/v1/").Subrouter()
restV1Route.Methods("POST").Path("/domain/raw").Headers("Content-Type", "application/xml").Handler(endpoints.rawDomainHandler)
restV1Route.Methods("POST").Path("/domain/raw").Headers("Content-Type", "application/xml").Handler(endpoints.RawDomainHandler)
return router
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/virt-controller/virt_controller_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestVirtController(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "VirtController Suite")
}
93 changes: 93 additions & 0 deletions pkg/virt-controller/virt_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"golang.org/x/net/context"
"net/http"

"io/ioutil"
"net/http/httptest"
"net/url"
"strings"
)

func newValidRequest() *http.Request {
request, _ := http.NewRequest("POST", "/api/v1/domain/raw", nil)
request.Body = ioutil.NopCloser(strings.NewReader("<domain><name>testvm</name></domain>"))
request.Header.Set("Content-Type", "application/xml")
return request

}

type mockVmService struct{}

func (mockVmService) StartVmRaw(name string) error {
return nil
}

var _ = Describe("VirtController", func() {
var recorder *httptest.ResponseRecorder
ctx := context.Background()
svc := mockVmService{}
endpoints := Handlers{
RawDomainHandler: makeRawDomainHandler(ctx, makeRawDomainEndpoint(svc)),
}
handler := http.Handler(defineRoutes(&endpoints))

BeforeEach(func() {
recorder = httptest.NewRecorder()
})

Describe("REST call", func() {
Context("with invalid URL", func() {
It("should return 404", func() {
request := newValidRequest()
request.URL, _ = url.Parse("/api/v1/wrong/url")
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(404))
})
})
Context("with missing Content-Type header", func() {
It("should return 404", func() {
request := newValidRequest()
request.Header.Del("Content-Type")
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(404))
})
})
Context("with invalid XML", func() {
It("should return 400", func() {
request := newValidRequest()
request.Body = ioutil.NopCloser(strings.NewReader("test"))
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(400))
})
})
Context("with unexpected root XML element", func() {
It("should return 400", func() {
request := newValidRequest()
request.Body = ioutil.NopCloser(strings.NewReader("<test><name>myvm</name></test>"))
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(400))
})
})
Context("with missing VM name", func() {
It("should return 400", func() {
request := newValidRequest()
request.Body = ioutil.NopCloser(strings.NewReader("<domain><name></name></domain>"))
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(400))
})
})
Context("with valid XMl", func() {
It("should return 200", func() {
request := newValidRequest()
handler.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(200))
})
})
})

})
9 changes: 0 additions & 9 deletions pkg/virt-launcher/run_test.go

This file was deleted.

File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/virt-launcher/virt_launcher_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestVirtLauncher(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "VirtLauncher Suite")
}
84 changes: 84 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,95 @@
"revision": "757bef944d0f21880861c2dd9c871ca543023cba",
"revisionTime": "2016-09-20T23:08:13Z"
},
{
"checksumSHA1": "qmlMYK6untubgQOaLZcnPin9BxM=",
"path": "github.com/onsi/ginkgo/ginkgo",
"revision": "45a5f6ffb2a14e4f29698c16ae4c0a34018a8951",
"revisionTime": "2016-09-26T21:18:03Z"
},
{
"checksumSHA1": "3g6SrQ0YmvEvx1MTqwKPqZd7B38=",
"path": "github.com/onsi/gomega",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "iKciSU4Btf8T1Fdux1CXEiGysOc=",
"path": "github.com/onsi/gomega/format",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "H4RBR9kcEWh2lECAzBaOTSvgUuA=",
"path": "github.com/onsi/gomega/internal/assertion",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "N7HhJzMN+STfzI315keM9SxFQAE=",
"path": "github.com/onsi/gomega/internal/asyncassertion",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "EsgeBqN2S5wj8aWvGsS162ZK7xI=",
"path": "github.com/onsi/gomega/internal/oraclematcher",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "G+KJsEQohAtPAHEbhHxRN0pffcA=",
"path": "github.com/onsi/gomega/internal/testingtsupport",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "cfZX1yRbg/mTs6Eu5Iu0WKykWpQ=",
"path": "github.com/onsi/gomega/matchers",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "gWJfsDHiZTga1esEChd+MfpftN0=",
"path": "github.com/onsi/gomega/matchers/support/goraph/bipartitegraph",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "zjTC6ady0bJUwzTFAKtv63T7Fmg=",
"path": "github.com/onsi/gomega/matchers/support/goraph/edge",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "o2+IscLOPKOiovl2g0/igkD1R4Q=",
"path": "github.com/onsi/gomega/matchers/support/goraph/node",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "yEF1BYQPwS3neYFKiqNQReqnadY=",
"path": "github.com/onsi/gomega/matchers/support/goraph/util",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "zEuQ0PUlbqojtNwvu01Mn3DQCJM=",
"path": "github.com/onsi/gomega/types",
"revision": "d59fa0ac68bb5dd932ee8d24eed631cdd519efc3",
"revisionTime": "2016-09-11T05:10:23Z"
},
{
"checksumSHA1": "DPHtow4r29lv9L6pbEv2Qp4QDiQ=",
"path": "github.com/rgbkrk/libvirt-go",
"revision": "26ca5c67cc596be31f449f59ae21844484028ed3",
"revisionTime": "2016-08-30T04:10:46Z"
},
{
"checksumSHA1": "SPMXWeoFQa5z0pLPmqpcFzyHqQQ=",
"path": "gopkg.in/yaml.v2",
"revision": "31c299268d302dd0aa9a0dcf765a3d58971ac83f",
"revisionTime": "2016-09-12T16:56:03Z"
}
],
"rootPath": "kubevirt/core"
Expand Down

0 comments on commit 2c7041f

Please sign in to comment.