func (s *ConsumptionSuite) TestServicesInstancesHandlerReturnsOnlyServicesThatTheUserHasAccess(c *gocheck.C) { u := &auth.User{Email: "*****@*****.**", Password: "******"} err := u.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Users().Remove(bson.M{"email": u.Email}) srv := service.Service{Name: "redis", IsRestricted: true} err = s.conn.Services().Insert(srv) c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "redis"}) instance := service.ServiceInstance{ Name: "redis-globo", ServiceName: "redis", Apps: []string{"globo"}, } err = instance.Create() c.Assert(err, gocheck.IsNil) request, err := http.NewRequest("GET", "/services/instances", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = ServicesInstancesHandler(recorder, request, u) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceModel err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) c.Assert(instances, gocheck.DeepEquals, []service.ServiceModel{}) }
func (s *S) TestUnbindRemovesAppFromServiceInstance(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{"painkiller"}, } instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindApp(&a) c.Assert(err, gocheck.IsNil) s.conn.ServiceInstances().Find(bson.M{"_id": instance.Name}).One(&instance) c.Assert(instance.Apps, gocheck.DeepEquals, []string{}) }
func (s *S) TestHandleBindServiceMessage(c *gocheck.C) { called := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true w.Write([]byte(`{"DATABASE_USER":"******","DATABASE_PASSWORD":"******"}`)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a := App{ Name: "nemesis", Units: []Unit{ { Name: "i-00800", State: "started", Machine: 19, }, }, } err = s.conn.Apps().Insert(a) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.AddApp(a.Name) c.Assert(err, gocheck.IsNil) err = s.conn.ServiceInstances().Update(bson.M{"name": instance.Name}, instance) c.Assert(err, gocheck.IsNil) message := queue.Message{Action: BindService, Args: []string{a.Name, a.Units[0].Name}} handle(&message) c.Assert(called, gocheck.Equals, true) }
func (s *S) TestUnbindUnit(c *gocheck.C) { called := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true w.WriteHeader(http.StatusNoContent) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{"painkiller"}, } instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindUnit(a.GetUnits()[0]) c.Assert(err, gocheck.IsNil) c.Assert(called, gocheck.Equals, true) }
func (s *ConsumptionSuite) TestRemoveServiceInstanceHandler(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) defer ts.Close() se := service.Service{Name: "foo", Endpoint: map[string]string{"production": ts.URL}} err := se.Create() defer s.conn.Services().Remove(bson.M{"_id": se.Name}) c.Assert(err, gocheck.IsNil) si := service.ServiceInstance{Name: "foo-instance", ServiceName: "foo", Teams: []string{s.team.Name}} err = si.Create() c.Assert(err, gocheck.IsNil) recorder, request := makeRequestToRemoveInstanceHandler("foo-instance", c) err = removeServiceInstance(recorder, request, s.token) c.Assert(err, gocheck.IsNil) b, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) c.Assert(string(b), gocheck.Equals, "service instance successfuly removed") n, err := s.conn.ServiceInstances().Find(bson.M{"name": "foo-instance"}).Count() c.Assert(err, gocheck.IsNil) c.Assert(n, gocheck.Equals, 0) action := testing.Action{ Action: "remove-service-instance", User: s.user.Email, Extra: []interface{}{"foo-instance"}, } c.Assert(action, testing.IsRecorded) }
func (s *ConsumptionSuite) TestServicesInstancesHandler(c *gocheck.C) { srv := service.Service{Name: "redis", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) instance := service.ServiceInstance{ Name: "redis-globo", ServiceName: "redis", Apps: []string{"globo"}, Teams: []string{s.team.Name}, } err = instance.Create() c.Assert(err, gocheck.IsNil) request, err := http.NewRequest("GET", "/services/instances", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = ServicesInstancesHandler(recorder, request, s.user) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceModel err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) expected := []service.ServiceModel{ {Service: "redis", Instances: []string{"redis-globo"}}, } c.Assert(instances, gocheck.DeepEquals, expected) }
func (s *S) TestBindUnit(c *gocheck.C) { called := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true w.Write([]byte(`{"DATABASE_USER":"******","DATABASE_PASSWORD":"******"}`)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) envs, err := instance.BindUnit(&a, a.GetUnits()[0]) c.Assert(err, gocheck.IsNil) c.Assert(called, gocheck.Equals, true) expectedEnvs := map[string]string{ "DATABASE_USER": "******", "DATABASE_PASSWORD": "******", } c.Assert(envs, gocheck.DeepEquals, expectedEnvs) }
func (s *ConsumptionSuite) TestServiceInstanceStatusHandler(c *gocheck.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, gocheck.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} err = si.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, gocheck.IsNil) b, err := ioutil.ReadAll(recorder.Body) c.Assert(string(b), gocheck.Equals, "Service instance \"my_nosql\" is up") action := testing.Action{ Action: "service-instance-status", User: s.user.Email, Extra: []interface{}{"my_nosql"}, } c.Assert(action, testing.IsRecorded) }
func (s *S) TestRemoveUnits(c *gocheck.C) { var calls int32 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v := atomic.LoadInt32(&calls) atomic.StoreInt32(&calls, v+1) w.WriteHeader(http.StatusNoContent) })) srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) app := App{ Name: "chemistry", Framework: "python", } instance := service.ServiceInstance{ Name: "my-inst", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{app.Name}, } instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"name": "my-inst"}) err = s.conn.Apps().Insert(app) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": app.Name}) c.Assert(err, gocheck.IsNil) s.provisioner.Provision(&app) defer s.provisioner.Destroy(&app) app.AddUnits(4) otherApp := App{Name: app.Name, Units: app.Units} err = otherApp.RemoveUnits(2) c.Assert(err, gocheck.IsNil) ts.Close() units := s.provisioner.GetUnits(&app) c.Assert(units, gocheck.HasLen, 3) // when you provision you already have one, so it's 4+1-2 (in provisioner, in app struct we have 2) c.Assert(units[0].Name, gocheck.Equals, "chemistry/0") c.Assert(units[1].Name, gocheck.Equals, "chemistry/3") c.Assert(units[2].Name, gocheck.Equals, "chemistry/4") err = app.Get() c.Assert(err, gocheck.IsNil) c.Assert(app.Framework, gocheck.Equals, "python") c.Assert(app.Units, gocheck.HasLen, 2) c.Assert(app.Units[0].Name, gocheck.Equals, "chemistry/3") c.Assert(app.Units[1].Name, gocheck.Equals, "chemistry/4") ok := make(chan int8) go func() { for _ = range time.Tick(1e3) { if atomic.LoadInt32(&calls) == 2 { ok <- 1 return } } }() select { case <-ok: case <-time.After(2e9): c.Fatal("Did not call service endpoint twice.") } }
func (s *ConsumptionSuite) TestGetServiceInstanceOrError(c *gocheck.C) { si := service.ServiceInstance{Name: "foo", Teams: []string{s.team.Name}} err := si.Create() c.Assert(err, gocheck.IsNil) rSi, err := getServiceInstanceOrError("foo", s.user) c.Assert(err, gocheck.IsNil) c.Assert(rSi.Name, gocheck.Equals, si.Name) }
func (s *S) TestUnbindRemovesEnvironmentVariableFromApp(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{"painkiller"}, } err = instance.Create() c.Assert(err, gocheck.IsNil) defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a := app.App{ Name: "painkiller", Teams: []string{s.team.Name}, Env: map[string]bind.EnvVar{ "DATABASE_HOST": { Name: "DATABASE_HOST", Value: "arrea", Public: false, InstanceName: instance.Name, }, "MY_VAR": { Name: "MY_VAR", Value: "123", }, }, Units: []app.Unit{ { Ip: "10.10.10.10", }, }, } err = s.conn.Apps().Insert(&a) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindApp(&a) c.Assert(err, gocheck.IsNil) newApp := app.App{Name: a.Name} err = newApp.Get() c.Assert(err, gocheck.IsNil) expected := map[string]bind.EnvVar{ "MY_VAR": { Name: "MY_VAR", Value: "123", }, } c.Assert(a.Env, gocheck.DeepEquals, expected) }
func (s *S) TestBindDoesNotFailsAndStopsWhenAppDoesNotHaveAnUnit(c *gocheck.C) { instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} err := instance.Create() c.Assert(err, gocheck.IsNil) defer s.conn.ServiceInstances().Remove(bson.M{"name": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.BindApp(&a) c.Assert(err, gocheck.IsNil) }
func (s *ConsumptionSuite) TestRemoveServiceHandlerWithoutPermissionShouldReturn401(c *gocheck.C) { se := service.Service{Name: "foo"} err := se.Create() defer s.conn.Services().Remove(bson.M{"_id": se.Name}) c.Assert(err, gocheck.IsNil) si := service.ServiceInstance{Name: "foo-instance", ServiceName: "foo"} err = si.Create() defer s.conn.ServiceInstances().Remove(bson.M{"name": si.Name}) c.Assert(err, gocheck.IsNil) recorder, request := makeRequestToRemoveInstanceHandler("foo-instance", c) err = removeServiceInstance(recorder, request, s.token) c.Assert(err.Error(), gocheck.Equals, service.ErrAccessNotAllowed.Error()) }
func (s *ConsumptionSuite) TestRemoveServiceHandlerWithoutPermissionShouldReturn401(c *gocheck.C) { se := service.Service{Name: "foo"} err := se.Create() defer s.conn.Services().Remove(bson.M{"_id": se.Name}) c.Assert(err, gocheck.IsNil) si := service.ServiceInstance{Name: "foo-instance", ServiceName: "foo"} err = si.Create() defer s.conn.ServiceInstances().Remove(bson.M{"name": si.Name}) c.Assert(err, gocheck.IsNil) recorder, request := makeRequestToRemoveInstanceHandler("foo-instance", c) err = RemoveServiceInstanceHandler(recorder, request, s.user) c.Assert(err, gocheck.ErrorMatches, "^This user does not have access to this service instance$") }
func (s *ConsumptionSuite) TestRemoveServiceHandlerWIthAssociatedAppsShouldFailAndReturnError(c *gocheck.C) { se := service.Service{Name: "foo"} err := se.Create() defer s.conn.Services().Remove(bson.M{"_id": se.Name}) c.Assert(err, gocheck.IsNil) si := service.ServiceInstance{Name: "foo-instance", ServiceName: "foo", Apps: []string{"foo-bar"}, Teams: []string{s.team.Name}} err = si.Create() defer s.conn.ServiceInstances().Remove(bson.M{"name": si.Name}) c.Assert(err, gocheck.IsNil) recorder, request := makeRequestToRemoveInstanceHandler("foo-instance", c) err = RemoveServiceInstanceHandler(recorder, request, s.user) c.Assert(err, gocheck.ErrorMatches, "^This service instance is bound to at least one app. Unbind them before removing it$") }
func (s *S) TestBindAppFailsWhenEndpointIsDown(c *gocheck.C) { srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ""}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"name": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.BindApp(&a) c.Assert(err, gocheck.NotNil) }
func (s *ConsumptionSuite) TestServicesInstancesHandlerFilterInstancesPerServiceIncludingServicesThatDoesNotHaveInstances(c *gocheck.C) { u := &auth.User{Email: "*****@*****.**", Password: "******"} err := u.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Users().Remove(bson.M{"email": u.Email}) serviceNames := []string{"redis", "mysql", "pgsql", "memcached"} defer s.conn.Services().RemoveAll(bson.M{"name": bson.M{"$in": serviceNames}}) defer s.conn.ServiceInstances().RemoveAll(bson.M{"service_name": bson.M{"$in": serviceNames}}) for _, name := range serviceNames { srv := service.Service{Name: name, Teams: []string{s.team.Name}} err = srv.Create() c.Assert(err, gocheck.IsNil) instance := service.ServiceInstance{ Name: srv.Name + "1", ServiceName: srv.Name, Teams: []string{s.team.Name}, } err = instance.Create() c.Assert(err, gocheck.IsNil) instance = service.ServiceInstance{ Name: srv.Name + "2", ServiceName: srv.Name, Teams: []string{s.team.Name}, } err = instance.Create() } srv := service.Service{Name: "oracle", Teams: []string{s.team.Name}} err = srv.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"name": "oracle"}) request, err := http.NewRequest("GET", "/services/instances", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = ServicesInstancesHandler(recorder, request, s.user) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceModel err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) expected := []service.ServiceModel{ {Service: "redis", Instances: []string{"redis1", "redis2"}}, {Service: "mysql", Instances: []string{"mysql1", "mysql2"}}, {Service: "pgsql", Instances: []string{"pgsql1", "pgsql2"}}, {Service: "memcached", Instances: []string{"memcached1", "memcached2"}}, {Service: "oracle", Instances: []string(nil)}, } c.Assert(instances, gocheck.DeepEquals, expected) }
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request, u *auth.User) error { log.Print("Receiving request to create a service instance") b, err := ioutil.ReadAll(r.Body) if err != nil { log.Print("Got error while reading request body:") log.Print(err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: err.Error()} } var sJson map[string]string err = json.Unmarshal(b, &sJson) if err != nil { log.Print("Got a problem while unmarshalling request's json:") log.Print(err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: err.Error()} } var s service.Service err = validateInstanceForCreation(&s, sJson, u) if err != nil { log.Print("Got error while validation:") log.Print(err.Error()) return err } var teamNames []string teams, err := u.Teams() if err != nil { return err } for _, t := range teams { if s.HasTeam(&t) || !s.IsRestricted { teamNames = append(teamNames, t.Name) } } si := service.ServiceInstance{ Name: sJson["name"], ServiceName: sJson["service_name"], Teams: teamNames, } if err = s.ProductionEndpoint().Create(&si); err != nil { log.Print("Error while calling create action from service api.") log.Print(err.Error()) return err } err = si.Create() if err != nil { return err } fmt.Fprint(w, "success") return nil }
func (s *ConsumptionSuite) TestServiceInstanceStatusHandlerShouldReturnForbiddenWhenUserDontHaveAccess(c *gocheck.C) { srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name} err = si.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, gocheck.NotNil) e, ok := err.(*errors.Http) c.Assert(ok, gocheck.Equals, true) c.Assert(e.Code, gocheck.Equals, http.StatusForbidden) }
func (s *ConsumptionSuite) TestServiceAndServiceInstancesByTeamsShouldReturnServiceInstancesByTeam(c *gocheck.C) { srv := service.Service{Name: "mongodb"} srv.Create() defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} si.Create() defer service.DeleteInstance(&si) si2 := service.ServiceInstance{Name: "some_nosql", ServiceName: srv.Name} si2.Create() defer service.DeleteInstance(&si2) obtained := serviceAndServiceInstancesByTeams(s.user) expected := []service.ServiceModel{ {Service: "mongodb", Instances: []string{"my_nosql"}}, } c.Assert(obtained, gocheck.DeepEquals, expected) }
func (s *ConsumptionSuite) TestRemoveServiceShouldCallTheServiceAPI(c *gocheck.C) { var called bool ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = r.Method == "DELETE" && r.URL.Path == "/resources/purity-instance" })) defer ts.Close() se := service.Service{Name: "purity", Endpoint: map[string]string{"production": ts.URL}} err := se.Create() defer s.conn.Services().Remove(bson.M{"_id": se.Name}) c.Assert(err, gocheck.IsNil) si := service.ServiceInstance{Name: "purity-instance", ServiceName: "purity", Teams: []string{s.team.Name}} err = si.Create() c.Assert(err, gocheck.IsNil) defer s.conn.ServiceInstances().Remove(bson.M{"name": si.Name}) recorder, request := makeRequestToRemoveInstanceHandler("purity-instance", c) err = RemoveServiceInstanceHandler(recorder, request, s.user) c.Assert(err, gocheck.IsNil) c.Assert(called, gocheck.Equals, true) }
func (s *ProvisionSuite) TestDeleteHandlerReturns403WhenTheServiceHasInstance(c *gocheck.C) { se := service.Service{Name: "mysql", OwnerTeams: []string{s.team.Name}} err := se.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": se.Name}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: se.Name} err = instance.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&instance) request, err := http.NewRequest("DELETE", fmt.Sprintf("/services/%s?:name=%s", se.Name, se.Name), nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = DeleteHandler(recorder, request, s.token) c.Assert(err, gocheck.NotNil) e, ok := err.(*errors.Http) c.Assert(ok, gocheck.Equals, true) c.Assert(e.Code, gocheck.Equals, http.StatusForbidden) c.Assert(e, gocheck.ErrorMatches, "^This service cannot be removed because it has instances.\nPlease remove these instances before removing the service.$") }
func (s *ProvisionSuite) TestServicesHandlerShoudGetAllServicesFromUsersTeam(c *gocheck.C) { srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}} srv.Create() defer s.conn.Services().Remove(bson.M{"_id": srv.Name}) si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} si.Create() defer service.DeleteInstance(&si) recorder, request := s.makeRequestToServicesHandler(c) err := ServicesHandler(recorder, request, s.token) c.Assert(err, gocheck.IsNil) b, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) services := make([]service.ServiceModel, 1) err = json.Unmarshal(b, &services) expected := []service.ServiceModel{ {Service: "mongodb", Instances: []string{"my_nosql"}}, } c.Assert(services, gocheck.DeepEquals, expected) }
func (s *ConsumptionSuite) TestServiceInfoHandler(c *gocheck.C) { srv := service.Service{Name: "mongodb", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.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, gocheck.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, gocheck.IsNil) defer service.DeleteInstance(&si2) request, err := http.NewRequest("GET", "/services/mongodb?:name=mongodb", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = serviceInfo(recorder, request, s.token) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceInstance err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) expected := []service.ServiceInstance{si1, si2} c.Assert(instances, gocheck.DeepEquals, expected) action := testing.Action{ Action: "service-info", User: s.user.Email, Extra: []interface{}{"mongodb"}, } c.Assert(action, testing.IsRecorded) }
func (s *S) TestBindAddsAppToTheServiceInstance(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"DATABASE_USER":"******","DATABASE_PASSWORD":"******"}`)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.BindApp(&a) c.Assert(err, gocheck.IsNil) s.conn.ServiceInstances().Find(bson.M{"_id": instance.Name}).One(&instance) c.Assert(instance.Apps, gocheck.DeepEquals, []string{a.Name}) }
func (s *S) TestBindCallTheServiceAPIAndSetsEnvironmentVariableReturnedFromTheCall(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"DATABASE_USER":"******","DATABASE_PASSWORD":"******"}`)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, } err = instance.Create() c.Assert(err, gocheck.IsNil) defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.BindApp(&a) c.Assert(err, gocheck.IsNil) newApp := app.App{Name: a.Name} err = newApp.Get() c.Assert(err, gocheck.IsNil) expectedEnv := map[string]bind.EnvVar{ "DATABASE_USER": { Name: "DATABASE_USER", Value: "root", Public: false, InstanceName: instance.Name, }, "DATABASE_PASSWORD": { Name: "DATABASE_PASSWORD", Value: "s3cr3t", Public: false, InstanceName: instance.Name, }, } c.Assert(a.Env, gocheck.DeepEquals, expectedEnv) }
func (s *S) TestUnbindMultiUnits(c *gocheck.C) { var calls int32 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { i := atomic.LoadInt32(&calls) i++ atomic.StoreInt32(&calls, i) w.WriteHeader(http.StatusNoContent) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{"painkiller"}, } instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}, {Ip: "9.9.9.9"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindApp(&a) c.Assert(err, gocheck.IsNil) s.conn.ServiceInstances().Find(bson.M{"_id": instance.Name}).One(&instance) ok := make(chan bool, 1) go func() { t := time.Tick(1) for _ = <-t; atomic.LoadInt32(&calls) < 2; _ = <-t { } ok <- true }() select { case <-ok: c.SucceedNow() case <-time.After(1 * time.Second): c.Error("endpoint not called") } }
func (s *S) TestUnbindReturnsPreconditionFailedIfTheAppIsNotBoundToTheInstance(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindApp(&a) c.Assert(err, gocheck.NotNil) e, ok := err.(*errors.Http) c.Assert(ok, gocheck.Equals, true) c.Assert(e, gocheck.ErrorMatches, "^This app is not bound to this service instance.$") }
func (s *S) TestUnbindCallsTheUnbindMethodFromAPI(c *gocheck.C) { var called int32 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == "DELETE" && r.URL.Path == "/resources/my-mysql/hostname/127.0.0.1" { atomic.StoreInt32(&called, 1) } })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer s.conn.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{ Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}, Apps: []string{"painkiller"}, } err = instance.Create() c.Assert(err, gocheck.IsNil) defer s.conn.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp(s.conn, "painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, gocheck.IsNil) defer s.conn.Apps().Remove(bson.M{"name": a.Name}) err = instance.UnbindApp(&a) c.Assert(err, gocheck.IsNil) ch := make(chan bool) go func() { t := time.Tick(1) for _ = <-t; atomic.LoadInt32(&called) == 0; _ = <-t { } ch <- true }() select { case <-ch: c.SucceedNow() case <-time.After(1e9): c.Errorf("Failed to call API after 1 second.") } }
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) }