diff --git a/Gopkg.lock b/Gopkg.lock index 2fd28cd..4d56463 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -469,6 +469,11 @@ revision = "947dcec5ba9c011838740e680966fd7087a71d0d" version = "v2.2.6" +[[projects]] + name = "gopkg.in/h2non/gock.v1" + packages = ["."] + revision = "master" + [[projects]] name = "gopkg.in/square/go-jose.v2" packages = [ @@ -488,6 +493,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "a40eabbe2a85347eff475ae29b4dde96eab5c8f704e421dd2c5f8ba0041db2d9" + inputs-digest = "243b6a93f8916459eea58c49d49e110faf1d215f25330727bc50828927f20df1" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index c9df145..b6e0a77 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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" diff --git a/controller/pipeline_environments_blackbox_test.go b/controller/pipeline_environments_blackbox_test.go index 0fca79c..4bd7c88 100644 --- a/controller/pipeline_environments_blackbox_test.go +++ b/controller/pipeline_environments_blackbox_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "os" "testing" "github.com/fabric8-services/fabric8-build/app" @@ -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 @@ -35,7 +144,7 @@ type PipelineEnvironmentControllerSuite struct { ctrl *controller.PipelineEnvironmentController ctrl2 *controller.PipelineEnvironmentController - svcfactory *application.ServiceFactory + svcFactory application.ServiceFactory } func TestPipelineEnvironmentController(t *testing.T) { @@ -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) @@ -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), @@ -88,34 +211,40 @@ 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) @@ -123,16 +252,19 @@ func (s *PipelineEnvironmentControllerSuite) TestCreate() { }) 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)