func (s *S) TestBindDoNotAddsAppToServiceInstanceIfCommunicationWithEndpointGoesWrong(c *C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, IsNil) defer db.Session.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp("somecoolapp", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, Not(IsNil)) n, err := db.Session.ServiceInstances().Find(bson.M{"app": []string{}}).Count() c.Assert(n, Equals, 0) }
func (s *S) TestBindAddsAppToTheServiceInstance(c *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, IsNil) defer db.Session.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "my-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} instance.Create() defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp("painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "10.10.10.10"}}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, IsNil) db.Session.ServiceInstances().Find(bson.M{"_id": instance.Name}).One(&instance) c.Assert(instance.Apps, DeepEquals, []string{a.Name}) }
func (s *S) TestBindCallTheServiceAPIAndSetsEnvironmentVariableReturnedFromTheCall(c *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, IsNil) defer db.Session.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, IsNil) defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp("painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, IsNil) newApp := app.App{Name: a.Name} err = newApp.Get() c.Assert(err, 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, DeepEquals, expectedEnv) }
func (s *S) TestBindUnbindsWhenDatabaseUpdateGoesWrong(c *C) { called := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = r.Method == "DELETE" && r.URL.Path == "/resources/their-mysql/hostname/127.0.0.1" if r.URL.String() == "/resources/their-mysql" { 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, IsNil) defer db.Session.Services().Remove(bson.M{"_id": "mysql"}) instance := service.ServiceInstance{Name: "their-mysql", ServiceName: "mysql", Teams: []string{s.team.Name}} a, err := createTestApp("somecoolapp", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, ErrorMatches, "^not found$") c.Assert(called, Equals, true) }
func (s *S) TestBindReturnsPreconditionFailedIfTheAppDoesNotHaveAnUnitAndServiceHasEndpoint(c *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, IsNil) defer db.Session.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, IsNil) defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp("painkiller", "", []string{s.team.Name}, []app.Unit{}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, NotNil) e, ok := err.(*errors.Http) c.Assert(ok, Equals, true) c.Assert(e.Code, Equals, http.StatusPreconditionFailed) c.Assert(e.Message, Equals, "This app does not have an IP yet.") }
func (s *S) TestBindReturnConflictIfTheAppIsAlreadyBinded(c *C) { srvc := service.Service{Name: "mysql"} err := srvc.Create() c.Assert(err, IsNil) defer db.Session.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, IsNil) defer db.Session.ServiceInstances().Remove(bson.M{"_id": "my-mysql"}) a, err := createTestApp("painkiller", "", []string{s.team.Name}, []app.Unit{{Ip: "127.0.0.1"}}) c.Assert(err, IsNil) defer db.Session.Apps().Remove(bson.M{"name": a.Name}) err = instance.Bind(&a) c.Assert(err, NotNil) e, ok := err.(*errors.Http) c.Assert(ok, Equals, true) c.Assert(e.Code, Equals, http.StatusConflict) c.Assert(e, ErrorMatches, "^This app is already binded to this service instance.$") }