func (s *S) TestListPoolsToUserHandler(c *check.C) { team := auth.Team{Name: "angra"} err := s.conn.Teams().Insert(team) c.Assert(err, check.IsNil) token := userWithPermission(c, permission.Permission{ Scheme: permission.PermAppCreate, Context: permission.Context(permission.CtxTeam, "angra"), }) pool := provision.Pool{Name: "pool1", Teams: []string{"angra"}} opts := provision.AddPoolOptions{Name: pool.Name} err = provision.AddPool(opts) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(pool.Name, pool.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) opts = provision.AddPoolOptions{Name: "nopool"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("nopool") poolsExpected := map[string]interface{}{ "pools_by_team": []interface{}{map[string]interface{}{"Team": "angra", "Pools": []interface{}{"pool1"}}}, "public_pools": []interface{}{}, "default_pool": []interface{}{}, } req, err := http.NewRequest("GET", "/pool", nil) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = listPoolsToUser(rec, req, token) c.Assert(err, check.IsNil) var pools map[string]interface{} err = json.NewDecoder(rec.Body).Decode(&pools) c.Assert(err, check.IsNil) c.Assert(pools, check.DeepEquals, poolsExpected) }
func (s *S) TestSchedulerNoNodesWithDefaultPool(c *check.C) { provision.RemovePool("test-default") app := app.App{Name: "bill", Teams: []string{"jean"}} err := s.storage.Apps().Insert(app) c.Assert(err, check.IsNil) defer s.storage.Apps().Remove(bson.M{"name": app.Name}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) c.Assert(err, check.IsNil) o := provision.AddPoolOptions{Name: "mypool"} err = provision.AddPool(o) c.Assert(err, check.IsNil) o = provision.AddPoolOptions{Name: "mypool2"} err = provision.AddPool(o) c.Assert(err, check.IsNil) defer provision.RemovePool("mypool") defer provision.RemovePool("mypool2") provision.AddTeamsToPool("mypool", []string{"jean"}) provision.AddTeamsToPool("mypool2", []string{"jean"}) opts := docker.CreateContainerOptions{} schedOpts := []string{app.Name, "web"} node, err := scheduler.Schedule(clusterInstance, opts, schedOpts) c.Assert(node.Address, check.Equals, "") c.Assert(err, check.NotNil) c.Assert(err.Error(), check.Matches, "No nodes found with one of the following metadata: pool=mypool, pool=mypool2") }
func (s *S) TestPoolListHandler(c *check.C) { team := auth.Team{Name: "angra"} err := s.conn.Teams().Insert(team) c.Assert(err, check.IsNil) token := userWithPermission(c, permission.Permission{ Scheme: permission.PermAppCreate, Context: permission.Context(permission.CtxTeam, "angra"), }) pool := provision.Pool{Name: "pool1", Teams: []string{"angra"}} opts := provision.AddPoolOptions{Name: pool.Name} err = provision.AddPool(opts) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(pool.Name, pool.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) opts = provision.AddPoolOptions{Name: "nopool"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("nopool") defaultPools, err := provision.ListPools(bson.M{"default": true}) c.Assert(err, check.IsNil) expected := []provision.Pool{ defaultPools[0], {Name: "pool1", Teams: []string{"angra"}}, } req, err := http.NewRequest("GET", "/pools", nil) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = poolList(rec, req, token) c.Assert(err, check.IsNil) var pools []provision.Pool err = json.NewDecoder(rec.Body).Decode(&pools) c.Assert(err, check.IsNil) c.Assert(pools, check.DeepEquals, expected) }
func (s *S) TestPoolUpdateProvisioner(c *check.C) { provision.RemovePool("test1") opts := provision.AddPoolOptions{Name: "pool1", Public: true, Default: true} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") b := bytes.NewBufferString("provisioner=myprov&default=false") req, err := http.NewRequest("PUT", "/pools/pool1", b) c.Assert(err, check.IsNil) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Authorization", "bearer "+s.token.GetValue()) rec := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusOK) c.Assert(err, check.IsNil) p, err := provision.GetPoolByName("pool1") c.Assert(err, check.IsNil) c.Assert(p.Provisioner, check.Equals, "myprov") c.Assert(p.Public, check.Equals, true) c.Assert(p.Default, check.Equals, false) c.Assert(eventtest.EventDesc{ Target: event.Target{Type: event.TargetTypePool, Value: "pool1"}, Owner: s.token.GetUserName(), Kind: "pool.update", StartCustomData: []map[string]interface{}{ {"name": ":name", "value": "pool1"}, {"name": "default", "value": "false"}, {"name": "provisioner", "value": "myprov"}, }, }, eventtest.HasEvent) }
func (s *S) TestListPoolsToUserHandler(c *check.C) { u := auth.User{Email: "*****@*****.**", Password: "******"} _, err := nativeScheme.Create(&u) c.Assert(err, check.IsNil) defer s.conn.Users().Remove(bson.M{"email": u.Email}) token, err := nativeScheme.Login(map[string]string{"email": u.Email, "password": "******"}) c.Assert(err, check.IsNil) defer s.conn.Tokens().Remove(bson.M{"token": token.GetValue()}) team := auth.Team{Name: "angra", Users: []string{s.user.Email, u.Email}} err = s.conn.Teams().Insert(team) c.Assert(err, check.IsNil) defer s.conn.Teams().Remove(bson.M{"_id": team.Name}) pool := provision.Pool{Name: "pool1", Teams: []string{"angra"}} err = provision.AddPool(pool.Name, false) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(pool.Name, pool.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) err = provision.AddPool("nopool", false) c.Assert(err, check.IsNil) defer provision.RemovePool("nopool") poolsExpected := map[string]interface{}{ "pools_by_team": []interface{}{map[string]interface{}{"Team": "angra", "Pools": []interface{}{"pool1"}}}, "public_pools": interface{}(nil), } req, err := http.NewRequest("GET", "/pool", nil) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = listPoolsToUser(rec, req, token) c.Assert(err, check.IsNil) var pools map[string]interface{} err = json.NewDecoder(rec.Body).Decode(&pools) c.Assert(err, check.IsNil) c.Assert(pools, check.DeepEquals, poolsExpected) }
func (s *S) TestAddPool(c *check.C) { b := bytes.NewBufferString("name=pool1") req, err := http.NewRequest("POST", "/pools", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() defer provision.RemovePool("pool1") m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusCreated) c.Assert(err, check.IsNil) pools, err := provision.ListPools(bson.M{"_id": "pool1"}) c.Assert(err, check.IsNil) c.Assert(pools, check.HasLen, 1) b = bytes.NewBufferString("name=pool2&public=true") req, err = http.NewRequest("POST", "/pools", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec = httptest.NewRecorder() defer provision.RemovePool("pool2") m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusCreated) pools, err = provision.ListPools(bson.M{"_id": "pool2"}) c.Assert(err, check.IsNil) c.Assert(pools[0].Public, check.Equals, true) }
func (s *S) TestAddNodeHandlerExisting(c *check.C) { opts := provision.AddPoolOptions{Name: "pool1"} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") serverAddr := "http://mysrv1" params := provision.AddNodeOptions{ Register: true, Metadata: map[string]string{ "address": serverAddr, "pool": "pool1", }, } v, err := form.EncodeToValues(¶ms) c.Assert(err, check.IsNil) req, err := http.NewRequest("POST", "/1.2/node", strings.NewReader(v.Encode())) c.Assert(err, check.IsNil) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Authorization", s.token.GetValue()) rec := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusCreated) req, err = http.NewRequest("POST", "/1.2/node", strings.NewReader(v.Encode())) c.Assert(err, check.IsNil) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Authorization", s.token.GetValue()) rec = httptest.NewRecorder() m.ServeHTTP(rec, req) var result map[string]string err = json.NewDecoder(rec.Body).Decode(&result) c.Assert(err, check.IsNil) c.Assert(result["Error"], check.Equals, "node with address \"http://mysrv1\" already exists in provisioner \"fake\"") c.Assert(rec.Code, check.Equals, http.StatusCreated) }
func (s *S) TestUpdateNodeEnableAndDisableCantBeDone(c *check.C) { err := s.provisioner.AddNode(provision.AddNodeOptions{ Address: "localhost:1999", }) c.Assert(err, check.IsNil) opts := provision.AddPoolOptions{Name: "pool1"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") params := provision.UpdateNodeOptions{ Address: "localhost:1999", Enable: true, Disable: true, } v, err := form.EncodeToValues(¶ms) c.Assert(err, check.IsNil) b := strings.NewReader(v.Encode()) recorder := httptest.NewRecorder() request, err := http.NewRequest("PUT", "/node", b) c.Assert(err, check.IsNil) request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusBadRequest) c.Assert(recorder.Body.String(), check.Equals, "A node can't be enabled and disabled simultaneously.\n") }
func (s *S) TestUpdateNodeEnableNodeHandler(c *check.C) { err := s.provisioner.AddNode(provision.AddNodeOptions{ Address: "localhost:1999", }) c.Assert(err, check.IsNil) opts := provision.AddPoolOptions{Name: "pool1"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") params := provision.UpdateNodeOptions{ Address: "localhost:1999", Enable: true, } v, err := form.EncodeToValues(¶ms) c.Assert(err, check.IsNil) b := strings.NewReader(v.Encode()) recorder := httptest.NewRecorder() request, err := http.NewRequest("PUT", "/node", b) c.Assert(err, check.IsNil) request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusOK) nodes, err := s.provisioner.ListNodes(nil) c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(nodes[0].Status(), check.Equals, "enabled") }
func (s *S) TestRemoveTeamsToPoolHandler(c *check.C) { pool := provision.Pool{Name: "pool1", Teams: []string{"test"}} opts := provision.AddPoolOptions{Name: pool.Name} err := provision.AddPool(opts) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(pool.Name, pool.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) req, err := http.NewRequest("DELETE", "/pools/pool1/team?team=test", nil) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) rec := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusOK) var p provision.Pool err = s.conn.Pools().FindId(pool.Name).One(&p) c.Assert(err, check.IsNil) c.Assert(p.Teams, check.DeepEquals, []string{}) c.Assert(eventtest.EventDesc{ Target: event.Target{Type: event.TargetTypePool, Value: "pool1"}, Owner: s.token.GetUserName(), Kind: "pool.update.team.remove", StartCustomData: []map[string]interface{}{ {"name": ":name", "value": "pool1"}, {"name": "team", "value": "test"}, }, }, eventtest.HasEvent) }
func (s *S) TestSchedulerScheduleFallback(c *check.C) { a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}} cont1 := container{ID: "1", Name: "impius1", AppName: a1.Name} err := s.storage.Apps().Insert(a1) c.Assert(err, check.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": a1.Name}) p := provision.Pool{Name: "pool1", Teams: []string{}} o := provision.AddPoolOptions{Name: p.Name} err = provision.AddPool(o) c.Assert(err, check.IsNil) defer provision.RemovePool(p.Name) contColl := s.p.collection() defer contColl.Close() err = contColl.Insert(cont1) c.Assert(err, check.IsNil) defer contColl.RemoveAll(bson.M{"name": cont1.Name}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) s.p.cluster = clusterInstance c.Assert(err, check.IsNil) _, err = clusterInstance.Register("http://url0:1234", map[string]string{"pool": "pool1"}) c.Assert(err, check.IsNil) opts := docker.CreateContainerOptions{Name: cont1.Name} node, err := scheduler.Schedule(clusterInstance, opts, []string{a1.Name, "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, "http://url0:1234") }
func (s *DeploySuite) TearDownSuite(c *check.C) { provision.RemovePool("pool1") s.conn.Apps().Database.DropDatabase() s.logConn.Logs("myapp").Database.DropDatabase() s.conn.Close() s.logConn.Close() }
func (s *S) TestSchedulerScheduleByTeamOwner(c *check.C) { a1 := app.App{Name: "impius", Teams: []string{}, TeamOwner: "tsuruteam"} cont1 := container.Container{ID: "1", Name: "impius1", AppName: a1.Name} err := s.storage.Apps().Insert(a1) c.Assert(err, check.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": a1.Name}) p := provision.Pool{Name: "pool1", Teams: []string{"tsuruteam"}} o := provision.AddPoolOptions{Name: p.Name} err = provision.AddPool(o) c.Assert(err, check.IsNil) defer provision.RemovePool(p.Name) err = provision.AddTeamsToPool(p.Name, p.Teams) c.Assert(err, check.IsNil) contColl := s.p.Collection() defer contColl.Close() err = contColl.Insert(cont1) c.Assert(err, check.IsNil) defer contColl.RemoveAll(bson.M{"name": cont1.Name}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) s.p.cluster = clusterInstance c.Assert(err, check.IsNil) err = clusterInstance.Register(cluster.Node{ Address: s.server.URL(), Metadata: map[string]string{"pool": "pool1"}, }) c.Assert(err, check.IsNil) opts := docker.CreateContainerOptions{Name: cont1.Name} node, err := scheduler.Schedule(clusterInstance, opts, []string{a1.Name, "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, s.server.URL()) }
func (s *S) TestPoolListHandlerWithPermissionToDefault(c *check.C) { team := auth.Team{Name: "angra"} err := s.conn.Teams().Insert(team) c.Assert(err, check.IsNil) perms := []permission.Permission{ { Scheme: permission.PermAppCreate, Context: permission.Context(permission.CtxGlobal, ""), }, { Scheme: permission.PermPoolUpdate, Context: permission.Context(permission.CtxGlobal, ""), }, } token := userWithPermission(c, perms...) pool := provision.Pool{Name: "pool1", Teams: []string{team.Name}} opts := provision.AddPoolOptions{Name: pool.Name, Default: pool.Default} err = provision.AddPool(opts) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(pool.Name, pool.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) req, err := http.NewRequest("GET", "/pools", nil) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = poolList(rec, req, token) c.Assert(err, check.IsNil) var pools []provision.Pool err = json.NewDecoder(rec.Body).Decode(&pools) c.Assert(err, check.IsNil) c.Assert(pools, check.HasLen, 2) c.Assert(pools[0].Name, check.Equals, "test1") c.Assert(pools[1].Name, check.Equals, "pool1") }
func (s *S) TestPoolListPublicPool(c *check.C) { pool := provision.Pool{Name: "pool1", Public: true} opts := provision.AddPoolOptions{Name: pool.Name, Public: pool.Public} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) defaultPools, err := provision.ListPools(bson.M{"default": true}) c.Assert(err, check.IsNil) expected := []provision.Pool{ defaultPools[0], {Name: "pool1", Public: true, Teams: []string{}}, } token := userWithPermission(c, permission.Permission{ Scheme: permission.PermTeamCreate, Context: permission.Context(permission.CtxGlobal, ""), }) req, err := http.NewRequest("GET", "/pools", nil) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = poolList(rec, req, token) c.Assert(err, check.IsNil) var pools []provision.Pool err = json.NewDecoder(rec.Body).Decode(&pools) c.Assert(err, check.IsNil) c.Assert(pools, check.DeepEquals, expected) }
func (s *S) TestAddTeamsToPool(c *check.C) { pool := provision.Pool{Name: "pool1"} opts := provision.AddPoolOptions{Name: pool.Name} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool(pool.Name) b := strings.NewReader("team=test") req, err := http.NewRequest("POST", "/pools/pool1/team", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusOK) p, err := provision.GetPoolByName("pool1") c.Assert(err, check.IsNil) c.Assert(p.Teams, check.DeepEquals, []string{"test"}) c.Assert(eventtest.EventDesc{ Target: event.Target{Type: event.TargetTypePool, Value: "pool1"}, Owner: s.token.GetUserName(), Kind: "pool.update.team.add", StartCustomData: []map[string]interface{}{ {"name": ":name", "value": "pool1"}, {"name": "team", "value": "test"}, }, }, eventtest.HasEvent) }
func (s *S) TestPoolUpdateToDefaultPoolHandler(c *check.C) { provision.RemovePool("test1") opts := provision.AddPoolOptions{Name: "pool1"} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") b := bytes.NewBufferString(`{"default": true}`) req, err := http.NewRequest("POST", "/pool/pool1?:name=pool1", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = poolUpdateHandler(rec, req, s.token) c.Assert(err, check.IsNil) p, err := provision.ListPools(bson.M{"_id": "pool1"}) c.Assert(err, check.IsNil) c.Assert(p[0].Default, check.Equals, true) }
func (s *HandlersSuite) TestUpdateNodeHandler(c *check.C) { mainDockerProvisioner.cluster, _ = cluster.New(&segregatedScheduler{}, &cluster.MapStorage{}, cluster.Node{Address: "localhost:1999", Metadata: map[string]string{ "m1": "v1", "m2": "v2", }}, ) opts := provision.AddPoolOptions{Name: "pool1"} err := provision.AddPool(opts) defer provision.RemovePool("pool1") json := `{"address": "localhost:1999", "m1": "", "m2": "v9", "m3": "v8"}` b := bytes.NewBufferString(json) recorder := httptest.NewRecorder() request, err := http.NewRequest("PUT", "/docker/node", b) c.Assert(err, check.IsNil) request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := api.RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusOK) nodes, err := mainDockerProvisioner.getCluster().Nodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(nodes[0].Metadata, check.DeepEquals, map[string]string{ "m2": "v9", "m3": "v8", }) }
func (s *HandlersSuite) TestAddNodeHandlerCreatingAnIaasMachineExplicit(c *check.C) { server, waitQueue := s.startFakeDockerNode(c) defer server.Stop() iaas.RegisterIaasProvider("test-iaas", newTestIaaS) iaas.RegisterIaasProvider("another-test-iaas", newTestIaaS) mainDockerProvisioner.cluster, _ = cluster.New(&segregatedScheduler{}, &cluster.MapStorage{}) opts := provision.AddPoolOptions{Name: "pool1"} err := provision.AddPool(opts) defer provision.RemovePool("pool1") b := bytes.NewBufferString(`{"pool": "pool1", "id": "test1", "iaas": "another-test-iaas"}`) req, err := http.NewRequest("POST", "/docker/node?register=false", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = addNodeHandler(rec, req, nil) c.Assert(err, check.IsNil) waitQueue() nodes, err := mainDockerProvisioner.getCluster().Nodes() c.Assert(err, check.IsNil) c.Assert(nodes, check.HasLen, 1) c.Assert(nodes[0].Address, check.Equals, strings.TrimRight(server.URL(), "/")) c.Assert(nodes[0].Metadata, check.DeepEquals, map[string]string{ "id": "test1", "pool": "pool1", "iaas": "another-test-iaas", "iaas-id": "test1", }) }
func (s *DeploySuite) TearDownSuite(c *check.C) { config.Unset("docker:router") provision.RemovePool("pool1") s.conn.Apps().Database.DropDatabase() s.logConn.Logs("myapp").Database.DropDatabase() s.conn.Close() s.logConn.Close() }
func (s *S) TestGetRemovableContainer(c *check.C) { a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}, Pool: "pool1"} cont1 := container{ID: "1", Name: "impius1", AppName: a1.Name, ProcessName: "web"} cont2 := container{ID: "2", Name: "mirror1", AppName: a1.Name, ProcessName: "worker"} a2 := app.App{Name: "notimpius", Teams: []string{"tsuruteam", "nodockerforme"}, Pool: "pool1"} cont3 := container{ID: "3", Name: "dedication1", AppName: a2.Name, ProcessName: "web"} cont4 := container{ID: "4", Name: "dedication2", AppName: a2.Name, ProcessName: "worker"} err := s.storage.Apps().Insert(a1) c.Assert(err, check.IsNil) err = s.storage.Apps().Insert(a2) c.Assert(err, check.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": a1.Name}) defer s.storage.Apps().RemoveAll(bson.M{"name": a2.Name}) p := provision.Pool{Name: "pool1", Teams: []string{ "tsuruteam", "nodockerforme", }} o := provision.AddPoolOptions{Name: p.Name} err = provision.AddPool(o) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(p.Name, p.Teams) defer provision.RemovePool(p.Name) contColl := s.p.collection() defer contColl.Close() err = contColl.Insert( cont1, cont2, cont3, cont4, ) c.Assert(err, check.IsNil) defer contColl.RemoveAll(bson.M{"name": bson.M{"$in": []string{cont1.Name, cont2.Name, cont3.Name, cont4.Name}}}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) s.p.cluster = clusterInstance c.Assert(err, check.IsNil) _, err = clusterInstance.Register("http://url0:1234", map[string]string{"pool": "pool1"}) c.Assert(err, check.IsNil) _, err = clusterInstance.Register("http://url1:1234", map[string]string{"pool": "pool1"}) c.Assert(err, check.IsNil) opts := docker.CreateContainerOptions{Name: cont1.Name} _, err = scheduler.Schedule(clusterInstance, opts, []string{a1.Name, cont1.ProcessName}) c.Assert(err, check.IsNil) opts = docker.CreateContainerOptions{Name: cont2.Name} _, err = scheduler.Schedule(clusterInstance, opts, []string{a1.Name, cont2.ProcessName}) c.Assert(err, check.IsNil) opts = docker.CreateContainerOptions{Name: cont3.Name} _, err = scheduler.Schedule(clusterInstance, opts, []string{a2.Name, cont3.ProcessName}) c.Assert(err, check.IsNil) opts = docker.CreateContainerOptions{Name: cont4.Name} _, err = scheduler.Schedule(clusterInstance, opts, []string{a2.Name, cont4.ProcessName}) c.Assert(err, check.IsNil) cont, err := scheduler.GetRemovableContainer(a1.Name, "web") c.Assert(err, check.IsNil) c.Assert(cont, check.Equals, cont1.ID) err = cont1.remove(s.p) c.Assert(err, check.IsNil) _, err = scheduler.GetRemovableContainer(a1.Name, "web") c.Assert(err, check.NotNil) }
func (s *S) TestSchedulerSchedule(c *check.C) { a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}, Pool: "pool1"} a2 := app.App{Name: "mirror", Teams: []string{"tsuruteam"}, Pool: "pool1"} a3 := app.App{Name: "dedication", Teams: []string{"nodockerforme"}, Pool: "pool1"} cont1 := container.Container{ID: "1", Name: "impius1", AppName: a1.Name} cont2 := container.Container{ID: "2", Name: "mirror1", AppName: a2.Name} cont3 := container.Container{ID: "3", Name: "dedication1", AppName: a3.Name} err := s.storage.Apps().Insert(a1, a2, a3) c.Assert(err, check.IsNil) defer s.storage.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{a1.Name, a2.Name, a3.Name}}}) p := provision.Pool{Name: "pool1", Teams: []string{ "tsuruteam", "nodockerforme", }} o := provision.AddPoolOptions{Name: p.Name} err = provision.AddPool(o) c.Assert(err, check.IsNil) err = provision.AddTeamsToPool(p.Name, p.Teams) c.Assert(err, check.IsNil) defer provision.RemovePool(p.Name) contColl := s.p.Collection() defer contColl.Close() err = contColl.Insert( cont1, cont2, cont3, ) c.Assert(err, check.IsNil) defer contColl.RemoveAll(bson.M{"name": bson.M{"$in": []string{cont1.Name, cont2.Name, cont3.Name}}}) scheduler := segregatedScheduler{provisioner: s.p} clusterInstance, err := cluster.New(&scheduler, &cluster.MapStorage{}) s.p.cluster = clusterInstance c.Assert(err, check.IsNil) server1, err := testing.NewServer("127.0.0.1:0", nil, nil) c.Assert(err, check.IsNil) defer server1.Stop() server2, err := testing.NewServer("localhost:0", nil, nil) c.Assert(err, check.IsNil) defer server2.Stop() err = clusterInstance.Register(cluster.Node{ Address: server1.URL(), Metadata: map[string]string{"pool": "pool1"}, }) c.Assert(err, check.IsNil) localURL := strings.Replace(server2.URL(), "127.0.0.1", "localhost", -1) err = clusterInstance.Register(cluster.Node{ Address: localURL, Metadata: map[string]string{"pool": "pool1"}, }) c.Assert(err, check.IsNil) opts := docker.CreateContainerOptions{Name: cont1.Name} node, err := scheduler.Schedule(clusterInstance, opts, &container.SchedulerOpts{AppName: a1.Name, ProcessName: "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, server1.URL()) opts = docker.CreateContainerOptions{Name: cont2.Name} node, err = scheduler.Schedule(clusterInstance, opts, &container.SchedulerOpts{AppName: a2.Name, ProcessName: "web"}) c.Assert(err, check.IsNil) c.Check(node.Address, check.Equals, localURL) }
func (s *S) TestAddPool(c *check.C) { b := bytes.NewBufferString("name=pool1") req, err := http.NewRequest("POST", "/pools", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() defer provision.RemovePool("pool1") m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusCreated) c.Assert(err, check.IsNil) _, err = provision.GetPoolByName("pool1") c.Assert(err, check.IsNil) b = bytes.NewBufferString("name=pool2&public=true") req, err = http.NewRequest("POST", "/pools", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec = httptest.NewRecorder() defer provision.RemovePool("pool2") m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusCreated) pool, err := provision.GetPoolByName("pool2") c.Assert(err, check.IsNil) c.Assert(pool.Public, check.Equals, true) c.Assert(eventtest.EventDesc{ Target: event.Target{Type: event.TargetTypePool, Value: "pool1"}, Owner: s.token.GetUserName(), Kind: "pool.create", StartCustomData: []map[string]interface{}{ {"name": "name", "value": "pool1"}, }, }, eventtest.HasEvent) c.Assert(eventtest.EventDesc{ Target: event.Target{Type: event.TargetTypePool, Value: "pool2"}, Owner: s.token.GetUserName(), Kind: "pool.create", StartCustomData: []map[string]interface{}{ {"name": "name", "value": "pool2"}, {"name": "public", "value": "true"}, }, }, eventtest.HasEvent) }
func (s *S) TestPoolUpdateToDefaultPoolHandler(c *check.C) { provision.RemovePool("test1") opts := provision.AddPoolOptions{Name: "pool1"} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") b := bytes.NewBufferString("default=true") req, err := http.NewRequest("PUT", "/pools/pool1", b) c.Assert(err, check.IsNil) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Authorization", "bearer "+s.token.GetValue()) rec := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusOK) c.Assert(err, check.IsNil) p, err := provision.ListPools(bson.M{"_id": "pool1"}) c.Assert(err, check.IsNil) c.Assert(p[0].Default, check.Equals, true) }
func (s *S) TestPoolUpdateNotOverwriteDefaultPoolHandler(c *check.C) { provision.RemovePool("test1") opts := provision.AddPoolOptions{Name: "pool1", Default: true} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") opts = provision.AddPoolOptions{Name: "pool2"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool2") b := bytes.NewBufferString(`{"default": true}`) req, err := http.NewRequest("POST", "/pool/pool2?:name=pool2", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() err = poolUpdateHandler(rec, req, 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.StatusPreconditionFailed) c.Assert(e.Message, check.Equals, "Default pool already exists.") }
func (s *S) TestPoolUpdateNotOverwriteDefaultPoolHandler(c *check.C) { provision.RemovePool("test1") opts := provision.AddPoolOptions{Name: "pool1", Default: true} err := provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool1") opts = provision.AddPoolOptions{Name: "pool2"} err = provision.AddPool(opts) c.Assert(err, check.IsNil) defer provision.RemovePool("pool2") b := bytes.NewBufferString("default=true") request, err := http.NewRequest("PUT", "/pools/pool2", b) c.Assert(err, check.IsNil) request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Authorization", "bearer "+s.token.GetValue()) recorder := httptest.NewRecorder() m := RunServer(true) m.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusConflict) c.Assert(recorder.Body.String(), check.Equals, provision.ErrDefaultPoolAlreadyExists.Error()+"\n") }
func removePoolHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error { b, err := ioutil.ReadAll(r.Body) if err != nil { return err } var params map[string]string err = json.Unmarshal(b, ¶ms) if err != nil { return err } return provision.RemovePool(params["pool"]) }
func (s *S) TestAddPoolHandler(c *check.C) { b := bytes.NewBufferString(`{"name": "pool1"}`) req, err := http.NewRequest("POST", "/pool", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() defer provision.RemovePool("pool1") err = addPoolHandler(rec, req, nil) c.Assert(err, check.IsNil) pools, err := provision.ListPools(bson.M{"_id": "pool1"}) c.Assert(err, check.IsNil) c.Assert(len(pools), check.Equals, 1) }
func (s *S) TestAddPoolDefaultPoolAlreadyExists(c *check.C) { b := bytes.NewBufferString("name=pool1&default=true") req, err := http.NewRequest("POST", "/pools", b) c.Assert(err, check.IsNil) req.Header.Set("Authorization", "bearer "+s.token.GetValue()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() defer provision.RemovePool("pool1") m := RunServer(true) m.ServeHTTP(rec, req) c.Assert(rec.Code, check.Equals, http.StatusConflict) c.Assert(rec.Body.String(), check.Equals, provision.ErrDefaultPoolAlreadyExists.Error()+"\n") }
func (s *S) TestAddPoolHandler(c *check.C) { b := bytes.NewBufferString(`{"name": "pool1"}`) req, err := http.NewRequest("POST", "/pool", b) c.Assert(err, check.IsNil) rec := httptest.NewRecorder() defer provision.RemovePool("pool1") err = addPoolHandler(rec, req, s.token) c.Assert(err, check.IsNil) pools, err := provision.ListPools(bson.M{"_id": "pool1"}) c.Assert(err, check.IsNil) c.Assert(pools, check.HasLen, 1) b = bytes.NewBufferString(`{"name": "pool2", "public": true}`) req, err = http.NewRequest("POST", "/pool", b) c.Assert(err, check.IsNil) rec = httptest.NewRecorder() defer provision.RemovePool("pool2") err = addPoolHandler(rec, req, s.token) c.Assert(err, check.IsNil) pools, err = provision.ListPools(bson.M{"_id": "pool2"}) c.Assert(err, check.IsNil) c.Assert(pools[0].Public, check.Equals, true) }