func TestGetMachinesByUsernameAndProvider(t *testing.T) { db := modeltesthelper.NewMongoDB(t) defer db.Close() user := &models.User{Name: "testuser", ObjectId: bson.NewObjectId()} defer modelhelper.RemoveUser(user.Name) if err := modelhelper.CreateUser(user); err != nil { t.Error(err) } // koding provider machine m1 := &models.Machine{ ObjectId: bson.NewObjectId(), Uid: bson.NewObjectId().Hex(), Provider: "koding", Users: []models.MachineUser{ {Id: user.ObjectId, Owner: true}, }, } if err := modelhelper.CreateMachine(m1); err != nil { t.Errorf(err.Error()) } defer modelhelper.DeleteMachine(m1.ObjectId) // non koding provider machine m2 := &models.Machine{ ObjectId: bson.NewObjectId(), Uid: bson.NewObjectId().Hex(), Provider: "amazon", Users: []models.MachineUser{ {Id: user.ObjectId, Owner: true}, }, } if err := modelhelper.CreateMachine(m2); err != nil { t.Errorf(err.Error()) } defer modelhelper.DeleteMachine(m2.ObjectId) // should only get koding provider machine machines, err := modelhelper.GetMachinesByUsernameAndProvider(user.Name, m1.Provider) if err != nil { t.Error(err.Error()) } if len(machines) != 1 { t.Errorf("machine count should be 2, got: %d", len(machines)) } }
func (c *cleaner) add(v interface{}) { var fn func() switch model := v.(type) { case *models.User: fn = func() { modelhelper.RemoveUser(model.Name) } case *models.Account: fn = func() { modelhelper.RemoveAccount(model.Id) } case *models.Group: fn = func() { modelhelper.RemoveGroup(model.Id) } case *models.Machine: fn = func() { modelhelper.DeleteMachine(model.ObjectId) } case *models.ComputeStack: fn = func() { modelhelper.DeleteComputeStack(model.Id.Hex()) } default: panic(fmt.Errorf("cleaner for %T not implemented", v)) } *c = append(*c, fn) }
func deleteMachines(res interface{}) error { m := res.(*models.Machine) if m.Provider != "koding" { return nil } if len(m.Groups) > 1 { return nil } fmt.Printf("deleting jMachine %q\n", m.ObjectId.Hex()) if !*flagDry { return helper.DeleteMachine(m.ObjectId) } return nil }
// Destroy implements the Database interface. func (db *mongoDatabase) Destroy(opts *DestroyOptions) error { if err := opts.Valid(); err != nil { return err } err := new(multierror.Error) for _, id := range opts.Stack.Machines { if e := modelhelper.DeleteMachine(id); e != nil { err = multierror.Append(err, e) } } if e := modelhelper.DeleteComputeStack(opts.Stack.Id.Hex()); e != nil { err = multierror.Append(err, e) } return err.ErrorOrNil() }
func TestUnshareMachine(t *testing.T) { db := modeltesthelper.NewMongoDB(t) defer db.Close() m := createMachine(t) defer modelhelper.DeleteMachine(m.ObjectId) if err := modelhelper.UnshareMachineByUid(m.Uid); err != nil { t.Error(err) } m2, err := modelhelper.GetMachineByUid(m.Uid) if err != nil { t.Error(err.Error()) } if len(m2.Users) != 1 { t.Errorf("user count should be 1, got: %d", len(m2.Users)) } if !(m2.Users[0].Sudo && m2.Users[0].Owner) { t.Errorf("only owner should have sudo and owner priv.") } }