Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.

Commit

Permalink
Add gock based tests for wit service
Browse files Browse the repository at this point in the history
  • Loading branch information
chmouel committed Nov 29, 2018
1 parent 61558af commit 8063f0a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 17 deletions.
7 changes: 6 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ ignored = [
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "gopkg.in/h2non/gock.v1"
revision = "master"

[[constraint]]
name = "github.com/fabric8-services/fabric8-wit"
Expand Down
164 changes: 148 additions & 16 deletions controller/pipeline_environments_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/fabric8-services/fabric8-build/app"
Expand All @@ -21,8 +22,116 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
)

// TODO(chmouel): Templates externalize etc...
var defaultSpaceJson = `{
"data": {
"attributes": {
"created-at": "2018-11-29T15:50:57.981132Z",
"description": "",
"name": "%s",
"updated-at": "2018-11-29T15:50:57.981132Z",
"version": 0
},
"id": "%s",
"links": {
"backlog": {
"meta": {
"totalCount": 0
},
"self": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/backlog"
},
"filters": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/filters",
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b",
"self": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b",
"workitemlinktypes": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418/workitemlinktypes",
"workitemtypes": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418/workitemtypes"
},
"relationships": {
"areas": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/areas"
}
},
"backlog": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/backlog"
},
"meta": {
"totalCount": 0
}
},
"codebases": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/codebases"
}
},
"collaborators": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/collaborators"
}
},
"filters": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/filters"
}
},
"iterations": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/iterations"
}
},
"labels": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/labels"
}
},
"owned-by": {
"data": {
"id": "df61d335-a359-48eb-898d-fb4916c52937",
"type": "identities"
},
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/users/df61d335-a359-48eb-898d-fb4916c52937"
}
},
"space-template": {
"data": {
"id": "f405fa41-a8bb-46db-8800-2dbe13da1418",
"type": "spacetemplates"
},
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418",
"self": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418"
}
},
"workitemlinktypes": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418/workitemlinktypes"
}
},
"workitems": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spaces/fb0c49c4-7682-46cd-a29c-cb2bff83752b/workitems"
}
},
"workitemtypegroups": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418/workitemtypegroups"
}
},
"workitemtypes": {
"links": {
"related": "http://f8wit-fabric8-build.devtools-dev.ext.devshift.net/api/spacetemplates/f405fa41-a8bb-46db-8800-2dbe13da1418/workitemtypes"
}
}
},
"type": "spaces"
}
}`

type PipelineEnvironmentControllerSuite struct {
testsuite.DBTestSuite
db *gormapp.GormDB
Expand All @@ -35,7 +144,7 @@ type PipelineEnvironmentControllerSuite struct {
ctrl *controller.PipelineEnvironmentController
ctrl2 *controller.PipelineEnvironmentController

svcfactory *application.ServiceFactory
svcFactory application.ServiceFactory
}

func TestPipelineEnvironmentController(t *testing.T) {
Expand All @@ -46,6 +155,7 @@ func TestPipelineEnvironmentController(t *testing.T) {

func (s *PipelineEnvironmentControllerSuite) SetupSuite() {
s.DBTestSuite.SetupSuite()
config, err := configuration.New("")

s.db = gormapp.NewGormDB(s.DB)

Expand All @@ -59,16 +169,29 @@ func (s *PipelineEnvironmentControllerSuite) SetupSuite() {
s.ctx = s.svc.Context
s.ctx2 = s.svc2.Context

s.svcFactory = application.ServiceFactory
s.svcFactory = application.NewServiceFactory(config)

s.ctrl = controller.NewPipelineEnvironmentController(s.svc, s.db, s.svcFactory)
s.ctrl2 = controller.NewPipelineEnvironmentController(s.svc2, s.db, s.svcFactory)

os.Setenv("F8_WIT_URL", "http://witservice")
// gock.Observe(gock.DumpRequest)

defer func() {
gock.OffAll()
}()
}

s.ctrl = controller.NewPipelineEnvironmentController(s.svc, s.db)
s.ctrl2 = controller.NewPipelineEnvironmentController(s.svc2, s.db)
func (s *PipelineEnvironmentControllerSuite) createGockONSpace(spaceID uuid.UUID, spaceName string) {
gock.New("http://witservice").
Get("/api/spaces/" + spaceID.String()).
Reply(200).
JSON(fmt.Sprintf(defaultSpaceJson, "space1", spaceID))
}

// createPipelineEnvironmentCtrlNoErroring we do this one manually cause the one from
// goatest one exit on errro without being able to catch
func (s *PipelineEnvironmentControllerSuite) createPipelineEnvironmentCtrlNoErroring() (*app.CreatePipelineEnvironmentsContext, *httptest.ResponseRecorder) {
spaceID := uuid.NewV4()
func (s *PipelineEnvironmentControllerSuite) createPipelineEnvironmentCtrlNoErroring(spaceID uuid.UUID) (*app.CreatePipelineEnvironmentsContext, *httptest.ResponseRecorder) {
rw := httptest.NewRecorder()
u := &url.URL{
Path: fmt.Sprintf("/api/pipelines/environments/%v", spaceID),
Expand All @@ -88,51 +211,60 @@ func (s *PipelineEnvironmentControllerSuite) createPipelineEnvironmentCtrlNoErro
}

func (s *PipelineEnvironmentControllerSuite) TestCreate() {
s.T().Run("ok", func(t *testing.T) {
defer s.T().Run("ok", func(t *testing.T) {
space1ID := uuid.NewV4()
s.createGockONSpace(space1ID, "space1")
payload := newPipelineEnvironmentPayload("osio-stage-create", uuid.NewV4())
_, newEnv := test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, uuid.NewV4(), payload)
_, newEnv := test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, space1ID, payload)
assert.NotNil(t, newEnv)
assert.NotNil(t, newEnv.Data.ID)
assert.NotNil(t, newEnv.Data.Environments[0].EnvUUID)

// Same pipeline_name but different spaceID is OK
space2ID := uuid.NewV4()
s.createGockONSpace(space2ID, "space2")
payload = newPipelineEnvironmentPayload("osio-stage-create", uuid.NewV4())
_, newEnv = test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, uuid.NewV4(), payload)
_, newEnv = test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, space2ID, payload)
assert.NotNil(t, newEnv)
assert.NotNil(t, newEnv.Data.ID)
assert.NotNil(t, newEnv.Data.Environments[0].EnvUUID)
})

s.T().Run("fail", func(t *testing.T) {
conflict_space_id := uuid.NewV4()
payload := newPipelineEnvironmentPayload("osio-stage-create-conflict", uuid.NewV4())
space1ID := uuid.NewV4()

_, newEnv := test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, conflict_space_id, payload)
s.createGockONSpace(space1ID, "space1")
payload := newPipelineEnvironmentPayload("osio-stage-create-conflict", uuid.NewV4())
_, newEnv := test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, space1ID, payload)
assert.NotNil(t, newEnv)

response, err := test.CreatePipelineEnvironmentsConflict(t, s.ctx2, s.svc2, s.ctrl2, conflict_space_id, payload)
s.createGockONSpace(space1ID, "space1")
response, err := test.CreatePipelineEnvironmentsConflict(t, s.ctx2, s.svc2, s.ctrl2, space1ID, payload)
require.NotNil(t, response.Header().Get("Location"))
assert.Regexp(s.T(), ".*data_conflict_error.*", err.Errors)

emptyPayload := &app.CreatePipelineEnvironmentsPayload{}
createCtxerr, rw := s.createPipelineEnvironmentCtrlNoErroring()
createCtxerr, rw := s.createPipelineEnvironmentCtrlNoErroring(space1ID)
createCtxerr.Payload = emptyPayload
jerr := s.ctrl2.Create(createCtxerr)
require.Nil(t, jerr)
require.Equal(t, 400, rw.Code)
})

s.T().Run("unauthorized", func(t *testing.T) {
space1ID := uuid.NewV4()
s.createGockONSpace(space1ID, "space1")

payload := newPipelineEnvironmentPayload("osio-stage", uuid.NewV4())
_, err := test.CreatePipelineEnvironmentsUnauthorized(t, s.ctx, s.svc, s.ctrl, uuid.NewV4(), payload)
_, err := test.CreatePipelineEnvironmentsUnauthorized(t, s.ctx, s.svc, s.ctrl, space1ID, payload)
assert.NotNil(t, err)
})

}

func (s *PipelineEnvironmentControllerSuite) TestShow() {
s.T().Run("ok", func(t *testing.T) {
spaceID := uuid.NewV4()
s.createGockONSpace(spaceID, "space1")
payload := newPipelineEnvironmentPayload("osio-stage-show", uuid.NewV4())
_, newEnv := test.CreatePipelineEnvironmentsCreated(t, s.ctx2, s.svc2, s.ctrl2, spaceID, payload)
require.NotNil(t, newEnv)
Expand Down

0 comments on commit 8063f0a

Please sign in to comment.