func (s *ConsumptionSuite) TestServiceInstanceStatusHandler(c *check.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) w.Write([]byte(`Service instance "my_nosql" is up`)) })) defer ts.Close() srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}, Endpoint: map[string]string{"production": ts.URL}} err := srv.Create() c.Assert(err, check.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} err = si.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("mongodb", "my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, check.IsNil) b, err := ioutil.ReadAll(recorder.Body) c.Assert(string(b), check.Equals, "Service instance \"my_nosql\" is up") action := rectest.Action{ Action: "service-instance-status", User: s.user.Email, Extra: []interface{}{srv.Name, "my_nosql"}, } c.Assert(action, rectest.IsRecorded) }
func (s *ConsumptionSuite) TestServicePlansHandler(c *check.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { content := `[{"name": "ignite", "description": "some value"}, {"name": "small", "description": "not space left for you"}]` w.Write([]byte(content)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, check.IsNil) defer srvc.Delete() request, err := http.NewRequest("GET", "/services/mysql/plans?:name=mysql", nil) c.Assert(err, check.IsNil) recorder := httptest.NewRecorder() err = servicePlans(recorder, request, s.token) c.Assert(err, check.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, check.IsNil) var plans []service.Plan err = json.Unmarshal(body, &plans) c.Assert(err, check.IsNil) expected := []service.Plan{ {Name: "ignite", Description: "some value"}, {Name: "small", Description: "not space left for you"}, } c.Assert(plans, check.DeepEquals, expected) action := rectest.Action{ Action: "service-plans", User: s.user.Email, Extra: []interface{}{"mysql"}, } c.Assert(action, rectest.IsRecorded) }
func (s *ProvisionSuite) TestgetServiceByOwner(c *gocheck.C) { srv := service.Service{Name: "foo", OwnerTeams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() rSrv, err := getServiceByOwner("foo", s.user) c.Assert(err, gocheck.IsNil) c.Assert(rSrv.Name, gocheck.Equals, srv.Name) }
func (s *ConsumptionSuite) TestServiceInstanceStatusHandlerShouldReturnForbiddenWhenUserDontHaveAccess(c *check.C) { srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}} err := srv.Create() c.Assert(err, check.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name} err = si.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("mongodb", "my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, check.NotNil) e, ok := err.(*errors.HTTP) c.Assert(ok, check.Equals, true) c.Assert(e.Code, check.Equals, http.StatusForbidden) }
func (s *DeploySuite) TestDeployListByService(c *check.C) { user, _ := s.token.User() a := app.App{Name: "g1", Platform: "python", Teams: []string{s.team.Name}} err := app.CreateApp(&a, user) c.Assert(err, check.IsNil) defer app.Delete(&a, nil) var result []app.DeployData srv := service.Service{Name: "redis", Teams: []string{s.team.Name}} err = srv.Create() c.Assert(err, check.IsNil) instance := service.ServiceInstance{ Name: "redis-g1", ServiceName: "redis", Apps: []string{"g1", "qwerty"}, Teams: []string{s.team.Name}, } err = instance.Create() c.Assert(err, check.IsNil) defer func() { srv.Delete() service.DeleteInstance(&instance) }() request, err := http.NewRequest("GET", "/deploys?service=redis", nil) c.Assert(err, check.IsNil) recorder := httptest.NewRecorder() timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local) duration := time.Since(timestamp) err = s.conn.Deploys().Insert(app.DeployData{App: "g1", Timestamp: timestamp, Duration: duration}) c.Assert(err, check.IsNil) err = s.conn.Deploys().Insert(app.DeployData{App: "ge", Timestamp: timestamp, Duration: duration}) c.Assert(err, check.IsNil) defer s.conn.Deploys().RemoveAll(nil) request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusOK) err = json.Unmarshal(recorder.Body.Bytes(), &result) c.Assert(err, check.IsNil) c.Assert(result, check.HasLen, 1) c.Assert(result[0].App, check.Equals, "g1") c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.In(time.UTC)) c.Assert(result[0].Duration, check.DeepEquals, duration) }
func (s *ConsumptionSuite) TestServiceInfoHandler(c *check.C) { srv := service.Service{Name: "mongodb", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, check.IsNil) defer srv.Delete() si1 := service.ServiceInstance{ Name: "my_nosql", ServiceName: srv.Name, Apps: []string{}, Teams: []string{s.team.Name}, } err = si1.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si1) si2 := service.ServiceInstance{ Name: "your_nosql", ServiceName: srv.Name, Apps: []string{"wordpress"}, Teams: []string{s.team.Name}, } err = si2.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si2) request, err := http.NewRequest("GET", "/services/mongodb?:name=mongodb", nil) c.Assert(err, check.IsNil) recorder := httptest.NewRecorder() err = serviceInfo(recorder, request, s.token) c.Assert(err, check.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, check.IsNil) var instances []service.ServiceInstance err = json.Unmarshal(body, &instances) c.Assert(err, check.IsNil) expected := []service.ServiceInstance{si1, si2} c.Assert(instances, check.DeepEquals, expected) action := rectest.Action{ Action: "service-info", User: s.user.Email, Extra: []interface{}{"mongodb"}, } c.Assert(action, rectest.IsRecorded) }
func (s *DeploySuite) TestDeployListByAppAndService(c *check.C) { srv := service.Service{Name: "redis", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, check.IsNil) instance := service.ServiceInstance{ Name: "redis-myblog", ServiceName: "redis", Apps: []string{"yourblog"}, Teams: []string{s.team.Name}, } err = instance.Create() c.Assert(err, check.IsNil) defer func() { srv.Delete() service.DeleteInstance(&instance) }() user, _ := s.token.User() a := app.App{Name: "myblog", Platform: "python", Teams: []string{s.team.Name}} err = app.CreateApp(&a, user) c.Assert(err, check.IsNil) defer app.Delete(&a, nil) defer s.logConn.Logs(a.Name).DropCollection() timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local) duration := time.Since(timestamp) deploys := []app.DeployData{ {App: "myblog", Timestamp: timestamp, Duration: duration}, {App: "yourblog", Timestamp: timestamp, Duration: duration}, } for _, deploy := range deploys { err = s.conn.Deploys().Insert(deploy) c.Assert(err, check.IsNil) } defer s.conn.Deploys().RemoveAll(nil) recorder := httptest.NewRecorder() request, err := http.NewRequest("GET", "/deploys?app=myblog&service=redis", nil) request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusNoContent) }
func (s *ProvisionSuite) TestServicesAndInstancesByOwnerTeams(c *gocheck.C) { srvc := service.Service{Name: "mysql", OwnerTeams: []string{s.team.Name}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer srvc.Delete() srvc2 := service.Service{Name: "mongodb"} err = srvc2.Create() c.Assert(err, gocheck.IsNil) defer srvc2.Delete() sInstance := service.ServiceInstance{Name: "foo", ServiceName: "mysql"} err = sInstance.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&sInstance) sInstance2 := service.ServiceInstance{Name: "bar", ServiceName: "mongodb"} err = sInstance2.Create() defer service.DeleteInstance(&sInstance2) results := servicesAndInstancesByOwner(s.user) expected := []service.ServiceModel{ {Service: "mysql", Instances: []string{"foo"}}, } c.Assert(results, gocheck.DeepEquals, expected) }
func (s *ConsumptionSuite) TestServiceInfoHandlerShouldReturnOnlyInstancesOfTheSameTeamOfTheUser(c *check.C) { srv := service.Service{Name: "mongodb", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, check.IsNil) defer srv.Delete() si1 := service.ServiceInstance{ Name: "my_nosql", ServiceName: srv.Name, Apps: []string{}, Teams: []string{s.team.Name}, } err = si1.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si1) si2 := service.ServiceInstance{ Name: "your_nosql", ServiceName: srv.Name, Apps: []string{"wordpress"}, Teams: []string{}, } err = si2.Create() c.Assert(err, check.IsNil) defer service.DeleteInstance(&si2) request, err := http.NewRequest("GET", fmt.Sprintf("/services/%s?:name=%s", "mongodb", "mongodb"), nil) c.Assert(err, check.IsNil) recorder := httptest.NewRecorder() err = serviceInfo(recorder, request, s.token) c.Assert(err, check.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, check.IsNil) var instances []service.ServiceInstance err = json.Unmarshal(body, &instances) c.Assert(err, check.IsNil) expected := []service.ServiceInstance{si1} c.Assert(instances, check.DeepEquals, expected) }