예제 #1
0
func (tag *TagWrapper) SetTag() error {
	c := DB.C("tags")
	var err error
	flag := false
	for _, v := range Tags {
		if tag.Name == v.Name {
			v.ArticleIds = append(v.ArticleIds, tag.ArticleIds...)
			removeDuplicate(&v.ArticleIds)
			v.Count = len(v.ArticleIds)
			v.ModifiedTime = bson.Now()
			err = c.UpdateId(v.Id_, v)
			flag = true
			break
		}
	}

	if !flag {
		tag.Id_ = bson.NewObjectId()
		tag.CreatedTime = bson.Now()
		tag.ModifiedTime = bson.Now()
		Tags = append(Tags, *tag)
		err = c.Insert(tag)
	}

	SetAppTags()
	return err
}
예제 #2
0
/* 校验密码是否正确 支持类型:邮箱 */
func (this *Member) CheckPass(account string, password string) (string, int) {
	err := memberC.Find(bson.M{"e": account}).One(&this)
	if err != nil {
		return "", -2 //账号不存在
	}

	if bson.Now().Before(this.StopTime) { //锁定时间还没过
		long := this.StopTime.Sub(bson.Now())
		return strconv.FormatFloat(long.Seconds(), 'f', 0, 64) + " 秒", -3
	}
	//密码加密
	m := md5.New()
	m.Write([]byte(password))
	n := md5.New()
	n.Write([]byte(hex.EncodeToString(m.Sum(nil)) + beego.AppConfig.String("md5salt") + bson.ObjectId.Hex(this.Id)))
	//对比
	if hex.EncodeToString(n.Sum(nil)) != this.Password { //验证出错
		if this.ErrorChance <= 1 { //用尽验证机会,账号锁定10分钟
			this.ErrorChance = 6
			minute := time.Duration(10) * time.Minute
			this.StopTime = bson.Now().Add(minute)
			this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance, "st": this.StopTime}})
			return "", -4 //进入锁定
		} else { //验证机会-1
			this.ErrorChance--
			this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance}})
			return strconv.Itoa(int(this.ErrorChance)), -1 //密码不匹配
		}
	} else { //通过验证,重置机会次数
		this.ErrorChance = 6
		this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance}})
		return this.Id.Hex(), 1
	}
}
예제 #3
0
/* 插入用户 */
func (this *Member) Insert() string {
	if this.Id == "" { //id没有设置则初始化新的
		this.Id = bson.NewObjectId()
	}
	this.LastTime = bson.Now()
	this.StopTime = bson.Now()
	this.SalayTime = bson.Now()
	this.Token = strconv.Itoa(int(rand.New(rand.NewSource(time.Now().UnixNano())).Uint32()))
	this.ErrorChance = 6
	//如果没有头像,随机设置一个头像
	if this.Image == "" {
		this.Image = strconv.Itoa(rand.Intn(9))
	}
	//密码md5加盐+用户ID+md5加密
	m := md5.New()
	m.Write([]byte(this.Password))
	n := md5.New()
	n.Write([]byte(hex.EncodeToString(m.Sum(nil)) + beego.AppConfig.String("md5salt") + bson.ObjectId.Hex(this.Id)))
	this.Password = hex.EncodeToString(n.Sum(nil))
	err := memberC.Insert(this)
	if err != nil {
		panic(err)
	}
	return bson.ObjectId.Hex(this.Id)
}
예제 #4
0
func (this *RootArticleRouter) Post() {
	id := this.GetString("id")
	if len(this.Input()) == 1 { //删除操作
		models.DeleteArticle(&bson.M{"_id": bson.ObjectIdHex(id)})
		this.Data["json"] = true
		this.ServeJson(true)
	} else {
		nodename := this.GetString("selectnode")
		name := this.GetString("name")
		title := this.GetString("title")
		content := this.GetString("content")
		isThumbnail, _ := this.GetBool("isThumbnail")
		featuredPicURL := this.GetString("featuredPicURL")
		tags := this.GetStrings("tags")
		author, _ := this.GetSession("username").(string)
		cat := models.GetCategoryNodeName(nodename)
		if name == "" {
			name = strconv.Itoa(int(bson.Now().UnixNano()))
		}
		if id != "" {
			//更新
			article, _ := models.GetArticle(&bson.M{"_id": bson.ObjectIdHex(id)})
			article.CName = cat.Name
			article.NName = nodename
			article.Name = name
			article.Author = author
			article.Title = title
			article.Tags = tags
			article.FeaturedPicURL = featuredPicURL
			article.ModifiedTime = bson.Now()
			article.Text = template.HTML(content)
			article.IsThumbnail = isThumbnail

			article.SetSummary()
			article.UpdateArticle()
			this.Redirect("/root/article", 302)
		} else {
			//创建
			article := models.Article{
				CName:          cat.Name,
				NName:          nodename,
				Name:           name,
				Author:         author,
				Title:          title,
				Tags:           tags,
				FeaturedPicURL: featuredPicURL,
				CreatedTime:    bson.Now(),
				ModifiedTime:   bson.Now(),
				Text:           template.HTML(content),
				IsThumbnail:    isThumbnail,
			}
			article.SetSummary()
			article.CreatArticle()
			go Publish(&article)
			this.Redirect("/root/article", 302)
		}
	}

}
예제 #5
0
func (this *RootNodeRouter) Post() {
	if len(this.Input()) == 2 { //删除操作
		cid := this.GetString("id")
		nname := this.GetString("nname")
		for _, v := range models.Categories {
			if v.Id_.Hex() == cid {
				for in, va := range v.Nodes {
					if va.Name == nname {
						v.Nodes = append(v.Nodes[:in], v.Nodes[(in+1):]...)
						break
					}
				}
				v.UpdateCategory()
				break
			}
		}
		this.Data["json"] = true
		this.ServeJson(true)
	} else {
		categoryid := this.GetString("selectcategory")
		name := this.GetString("name")
		title := this.GetString("title")
		content := this.GetString("content")
		if name == "" {
			name = strconv.Itoa(int(bson.Now().UnixNano()))
		}

		for _, v := range models.Categories {
			if v.Id_.Hex() == categoryid {
				flag := false
				for _, va := range v.Nodes {
					if va.Name == name { //更新
						va.Title = title
						va.Content = content
						va.UpdatedTime = bson.Now()
						flag = true
						break
					}
				}

				if !flag { //添加
					node := models.Node{
						Name:        name,
						Title:       title,
						Content:     content,
						CreatedTime: bson.Now(),
						UpdatedTime: bson.Now(),
					}
					v.Nodes = append(v.Nodes, node)
				}

				v.UpdateCategory()
				break
			}
		}
		this.Redirect("/root/node", 302)
	}
}
예제 #6
0
// Test basic database insertion, retrieval, and removal.
func TestDatabase(t *testing.T) {
	t.Log("Testing database connection...")
	initDb()

	t.Log("Testing database insertion...")

	collection := db.C("testUsers")

	testUsers := []interface{}{
		&User{
			Id:        bson.NewObjectId(),
			CreatedAt: bson.Now(),
			Username:  "******",
			Password:  "******",
			Fullname:  "Bob Sue",
			Stories:   []string{},
		},
		&User{
			Id:        bson.NewObjectId(),
			CreatedAt: bson.Now(),
			Username:  "******",
			Password:  "******",
			Fullname:  "Alice Dino",
			Stories:   []string{},
		},
	}
	err := collection.Insert(testUsers...)
	if err != nil {
		t.Errorf("Failed to insert test users into the database\n%v\n", err)
	}

	t.Log("Testing database retrieval...")

	result := []User{}
	err = collection.Find(bson.M{"username": "******"}).All(&result)
	if err != nil || len(result) == 0 {
		t.Errorf("Failed to find test user in the database\n%v\n", err)
	}

	err = collection.Find(nil).All(&result)
	if err != nil || len(result) == 0 {
		t.Errorf("Failed to find test users in the database\n%v\n", err)
	}

	info, err := collection.RemoveAll(nil)
	if err != nil {
		t.Errorf("Failed to remove test users from the database\n%v\n", err)
	}

	if info.Removed < 2 {
		t.Error("Failed to either add or remove test users from the database")
	}
}
예제 #7
0
func (s *StoreSuite) TestLockUpdatesExpires(c *gc.C) {
	urlA := charm.MustParseURL("cs:oneiric/wordpress-a")
	urlB := charm.MustParseURL("cs:oneiric/wordpress-b")
	urls := []*charm.URL{urlA, urlB}

	// Initiate an update of B only to force a partial conflict.
	lock1, err := s.store.LockUpdates(urls[1:])
	c.Assert(err, gc.IsNil)

	// Hack time to force an expiration.
	locks := s.Session.DB("juju").C("locks")
	selector := bson.M{"_id": urlB.String()}
	update := bson.M{"time": bson.Now().Add(-charmstore.UpdateTimeout - 10e9)}
	err = locks.Update(selector, update)
	c.Check(err, gc.IsNil)

	// Works due to expiration of previous lock.
	lock2, err := s.store.LockUpdates(urls)
	c.Assert(err, gc.IsNil)
	defer lock2.Unlock()

	// The expired lock was forcefully killed. Unlocking it must
	// not interfere with lock2 which is still alive.
	lock1.Unlock()

	// The above statement was a NOOP and lock2 is still in effect,
	// so attempting another lock must necessarily fail.
	lock3, err := s.store.LockUpdates(urls)
	c.Check(err, gc.Equals, charmstore.ErrUpdateConflict)
	c.Check(lock3, gc.IsNil)
}
예제 #8
0
// UpdateContract updates a Contract resource
func (cc ContractController) UpdateContract(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	// Stub a Contract to be populated from the body
	contract := models.Contract{}

	// Populate the Contract data
	json.NewDecoder(r.Body).Decode(&contract)

	// Add an Id and Created At timestamp
	contract.UpdatedAt = bson.Now()

	// Validate the contract
	if err := validator.Validate(&contract); err != nil {
		log.Println(err)
		w.WriteHeader(400)
		fmt.Fprintf(w, "%s", err)
		return
	}

	// Write the Contract to mongo
	cc.session.DB("orange").C("Contracts").UpdateId(contract.ID, contract)

	// Marshal provided interface into JSON structure
	contractJSON, _ := json.Marshal(contract)

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	fmt.Fprintf(w, "%s", contractJSON)
}
예제 #9
0
//move this into caling procedure so as to be able to respect state changes?
func LogMessage(collectionName string, key string, message []byte) {
	if Connected == true {

		var m map[string]interface{}
		err := json.Unmarshal([]byte(message), &m)
		m["createdAt"] = bson.Now()

		//info, err := collection.UpsertId(r.Product, r)
		collection := db.C(collectionName)
		info, err := collection.UpsertId(key, m)
		if err != nil {
			fmt.Printf("Unable to upsert document:%v\n", err)
			db.Session.Refresh()
			connectionChannel <- false
		} else {
			fmt.Sprintf("Upserted:", info.UpsertedId)
		}

		rates_log_collection := db.C("rateslog")
		err = rates_log_collection.Insert(m)
		if err != nil {
			fmt.Printf("Unable to insert document:%v\n", err)
			db.Session.Refresh()
			connectionChannel <- false
		}
		// else {
		// 	fmt.Sprintf("Upserted:", info.UpsertedId)
		// }

	} else {
		// fmt.Println("No connection")
	}
}
예제 #10
0
/* 更新用户信息 */
func (this *Member) UpdateFinish() {
	colQuerier := bson.M{"_id": this.Id}
	change := bson.M{"$set": bson.M{"la": bson.Now(), "l": this.LogTime}}
	err := memberC.Update(colQuerier, change)
	if err != nil {
		//处理错误
	}
}
예제 #11
0
/* 追加总浏览量历史 */
func (this *Category) AddViewHistory(views uint64) {
	if this.ViewsHistory == "" {
		this.ViewsHistory = strconv.Itoa(int(bson.Now().Unix())) + ":" + strconv.Itoa(int(views))
	} else {
		this.ViewsHistory += ";" + strconv.Itoa(int(bson.Now().Unix())) + ":" + strconv.Itoa(int(views))
	}
	historyArray := strings.Split(this.ViewsHistory, ";")
	if len(historyArray) > 30 { //如果历史记录超过30条
		//只取前30条
		subArray := historyArray[len(historyArray)-30 : len(historyArray)]
		this.ViewsHistory = strings.Join(subArray, ";")
	}
	err := categoryC.Update(bson.M{"_id": this.Id}, bson.M{"$set": bson.M{"vh": this.ViewsHistory}})
	if err != nil {
		return
	}
}
예제 #12
0
func (s *S) TestNow(c *C) {
	before := time.Now()
	time.Sleep(1e6)
	now := bson.Now()
	time.Sleep(1e6)
	after := time.Now()
	c.Assert(now.After(before) && now.Before(after), Equals, true, Commentf("now=%s, before=%s, after=%s", now, before, after))
}
예제 #13
0
파일: post.go 프로젝트: byrnedo/blogsvc
func (uU *UpdatePostDTO) MapToEntity() (*models.PostModel, error) {

	return &models.PostModel{
		ID:         bson.ObjectIdHex(uU.ID),
		Title:      uU.Title,
		Slug:       uU.Slug,
		Body:       uU.Body,
		AuthorID:   uU.AuthorID,
		UpdateTime: bson.Now(),
	}, nil
}
예제 #14
0
파일: order.go 프로젝트: treejames/woku_old
/* 创建一个待付款账单 */
func (this *Order) InsertOrder() {
	this.Id = bson.NewObjectId()
	this.Success = false
	this.Time = bson.Now()

	err := orderC.Insert(this)
	if err != nil {
		return
	} else {
		return
	}
}
예제 #15
0
func (this *RootCategoryRouter) Post() {
	id := this.GetString("id")
	if len(this.Input()) == 1 { //删除操作
		models.DeleteCategory(&bson.M{"_id": bson.ObjectIdHex(id)})
		this.Data["json"] = true
		this.ServeJson(true)
	} else {
		name := this.GetString("name")
		title := this.GetString("title")
		content := this.GetString("content")
		if name == "" {
			name = strconv.Itoa(int(bson.Now().UnixNano()))
		}
		if id != "" {
			for _, v := range models.Categories {
				if v.Id_.Hex() == id {
					v.Name = name
					v.Title = title
					v.Content = content
					v.UpdatedTime = bson.Now()
					v.UpdateCategory()
					break
				}
			}
		} else {
			cat := models.Category{
				Id_:         bson.NewObjectId(),
				Name:        name,
				Title:       title,
				Content:     content,
				CreatedTime: bson.Now(),
				UpdatedTime: bson.Now(),
				NodeTime:    bson.Now(),
			}
			cat.CreatCategory()
		}

		this.Redirect("/root/category", 302)
	}
}
예제 #16
0
// LockUpdates acquires a server-side lock for updating a single charm
// that is supposed to be made available in all of the provided urls.
// If the lock can't be acquired in any of the urls, an error will be
// immediately returned.
// In the usual case, any locking done is undone when an error happens,
// or when l.Unlock is called. If something else goes wrong, the locks
// will also expire after the period defined in UpdateTimeout.
func (s *Store) LockUpdates(urls []*charm.URL) (l *UpdateLock, err error) {
	session := s.session.Copy()
	keys := make([]string, len(urls))
	for i := range urls {
		keys[i] = urls[i].String()
	}
	sort.Strings(keys)
	l = &UpdateLock{keys, session.Locks(), bson.Now()}
	if err = l.tryLock(); err != nil {
		session.Close()
		return nil, err
	}
	return l, nil
}
예제 #17
0
파일: post.go 프로젝트: byrnedo/blogsvc
func (nU *NewPostDTO) MapToEntity() (*models.PostModel, error) {
	var (
		now = bson.Now()
	)

	return &models.PostModel{
		ID:           bson.NewObjectId(),
		Title:        nU.Title,
		Slug:         nU.Slug,
		Body:         nU.Body,
		AuthorID:     nU.AuthorID,
		CreationTime: now,
		UpdateTime:   now,
	}, nil
}
예제 #18
0
func saveAuthorization(storage *authserver.MongoStorage) (*osin.AuthorizeData, error) {
	client, err := setClient1234(storage)
	if err != nil {
		return &osin.AuthorizeData{}, err
	}
	data := &osin.AuthorizeData{
		Client:      client,
		Code:        "9999",
		ExpiresIn:   3600,
		CreatedAt:   bson.Now(),
		RedirectUri: "http://localhost:14000/appauth",
	}
	err = storage.SaveAuthorize(data)
	return data, err
}
예제 #19
0
func (file *GridFile) completeWrite() {
	for file.wpending > 0 {
		debugf("GridFile %p: waiting for %d pending chunks to complete file write", file, file.wpending)
		file.c.Wait()
	}
	if file.err != nil {
		file.gfs.Chunks.RemoveAll(bson.D{{"files_id", file.doc.Id}})
		return
	}
	hexsum := hex.EncodeToString(file.wsum.Sum(nil))
	file.doc.UploadDate = bson.Now()
	file.doc.MD5 = hexsum
	file.err = file.gfs.Files.Insert(file.doc)
	file.gfs.Chunks.EnsureIndexKey("files_id", "n")
}
예제 #20
0
func TestDateTimeBson(t *testing.T) {
	now := bson.Now()

	sut := testMarshalDateTimeType{
		MyDateTime: DateTime(now),
	}
	serialized, err := bson.Marshal(&sut)
	assert.NoError(t, err)

	deserialized := testMarshalDateTimeType{}
	err = bson.Unmarshal(serialized, &deserialized)
	assert.NoError(t, err)

	assert.Equal(t, sut, deserialized)
}
예제 #21
0
파일: mgo.go 프로젝트: wangroot/SCDht
// 保存搜索数据
func (this *SC_Search) Save() error {
	// 查询此条数据是否存在
	if Has(DbSearch, bson.M{"caption": this.Caption}) {
		// 存在则自增
		SetAdd(DbSearch, bson.M{"caption": this.Caption}, "views", true)
		// 更新查询时间
		return Update(DbSearch, bson.M{"caption": this.Caption}, bson.M{"$set": bson.M{"searchtime": bson.Now()}})
	} else {
		// 创建编号
		this.Id = bson.NewObjectId()
		// 设置查询时间
		this.SearchTime = bson.Now()
		// 添加数据
		return Insert(DbSearch, this)
	}
}
예제 #22
0
func (s *DriverService) New(form forms.DriverForm) (err error) {
	m := new(models.Driver)

	now := bson.Now()

	m.ID = bson.NewObjectId()
	m.Available = false
	m.Name = *form.Name
	m.CarPlate = *form.CarPlate
	m.Location = [2]float64{0.0, 0.0}
	m.Created = now
	m.Updated = now

	return s.database.Action(func(collection *mgo.Collection) error {
		return collection.Insert(m)
	})
}
예제 #23
0
func saveAccess(storage *authserver.MongoStorage) (*osin.AccessData, error) {
	authData, err := saveAuthorization(storage)
	if err != nil {
		return &osin.AccessData{}, err
	}

	data := &osin.AccessData{
		Client:        authData.Client,
		AuthorizeData: authData,
		AccessToken:   "9999",
		RefreshToken:  "r9999",
		ExpiresIn:     3600,
		CreatedAt:     bson.Now(),
	}

	err = storage.SaveAccess(data)
	return data, err
}
예제 #24
0
파일: user.go 프로젝트: byrnedo/usersvc
func (uU *UpdateUserDTO) MapToEntity() (*models.UserModel, error) {

	var err error
	if len(uU.Password) > 0 {
		if uU.Password, err = encryptPassword(uU.Password); err != nil {
			return nil, err
		}
	}
	return &models.UserModel{
		ID:         bson.ObjectIdHex(uU.ID),
		Alias:      uU.Alias,
		FirstName:  encBson.EncryptedString(uU.FirstName),
		LastName:   encBson.EncryptedString(uU.LastName),
		Email:      uU.Email,
		Password:   uU.Password,
		Role:       uU.Role,
		UpdateTime: bson.Now(),
	}, nil
}
예제 #25
0
파일: user.go 프로젝트: byrnedo/usersvc
func (nU *NewUserDTO) MapToEntity() (*models.UserModel, error) {
	var (
		now = bson.Now()
		err error
	)

	if nU.Password, err = encryptPassword(nU.Password); err != nil {
		return nil, err
	}

	return &models.UserModel{
		ID:           bson.NewObjectId(),
		Alias:        nU.Alias,
		FirstName:    encBson.EncryptedString(nU.FirstName),
		LastName:     encBson.EncryptedString(nU.LastName),
		Email:        nU.Email,
		Password:     nU.Password,
		Role:         nU.Role,
		CreationTime: now,
		UpdateTime:   now,
	}, nil
}
예제 #26
0
//Commit provides write to mongodb current results
//name - collection name
// If name is "", write to collection by default
func (mbackend *Mongobackend) Commit(dbname, name string) {
	/*if len(gs.currentrepos) == 0 {
		log.Fatal("Can't find current repositories for commit")
		return
	}*/

	if name == "" {
		name = mbackend.getWriteCollectionName()
	} else {

	}

	db := mbackend.mongosession.DB(dbname).C(name)
	db.DropCollection()
	mbackend.db = db
	mbackend.db.Insert(timeinfo{bson.Now()})
	for _, repo := range gs.currentrepos {
		gs.SetData(*repo.FullName, *repo.StargazersCount)
	}

	log.Printf(fmt.Sprintf("Commit new data to db %s", gs.dbname))

}
예제 #27
0
//Commit provides write to mongodb current results
//name - collection name
// If name is "", write to collection by default
func (gs *githubstars) Commit(name string) {
	if len(gs.currentrepos) == 0 {
		log.Fatal("Can't find current repositories for commit")
		return
	}

	if name == "" {
		name = gs.getWriteCollectionName()
	} else {

	}

	db := gs.mongosession.DB(gs.dbname).C(name)
	db.DropCollection()
	gs.db = db
	gs.db.Insert(timeinfo{bson.Now()})
	for _, repo := range gs.currentrepos {
		gs.setData(*repo.FullName, *repo.StargazersCount)
	}

	log.Printf(fmt.Sprintf("Commit new data to db %s", gs.dbname))

}
예제 #28
0
파일: gridfs.go 프로젝트: hgGeorg/mongo
func (file *GridFile) completeWrite() {
	for file.wpending > 0 {
		debugf("GridFile %p: waiting for %d pending chunks to complete file write", file, file.wpending)
		file.c.Wait()
	}
	if file.err == nil {
		hexsum := hex.EncodeToString(file.wsum.Sum(nil))
		if file.doc.UploadDate.IsZero() {
			file.doc.UploadDate = bson.Now()
		}
		file.doc.MD5 = hexsum
		file.err = file.gfs.Files.Insert(file.doc)
	}
	if file.err != nil {
		file.gfs.Chunks.RemoveAll(bson.D{{"files_id", file.doc.Id}})
	}
	if file.err == nil {
		index := Index{
			Key:    []string{"files_id", "n"},
			Unique: true,
		}
		file.err = file.gfs.Chunks.EnsureIndex(index)
	}
}
예제 #29
0
파일: gridfs_test.go 프로젝트: rajjan/qstn
func (s *S) TestGridFSCreate(c *C) {
	session, err := mgo.Dial("localhost:40011")
	c.Assert(err, IsNil)
	defer session.Close()

	db := session.DB("mydb")

	before := bson.Now()

	gfs := db.GridFS("fs")
	file, err := gfs.Create("")
	c.Assert(err, IsNil)

	n, err := file.Write([]byte("some data"))
	c.Assert(err, IsNil)
	c.Assert(n, Equals, 9)

	err = file.Close()
	c.Assert(err, IsNil)

	after := bson.Now()

	// Check the file information.
	result := M{}
	err = db.C("fs.files").Find(nil).One(result)
	c.Assert(err, IsNil)

	fileId, ok := result["_id"].(bson.ObjectId)
	c.Assert(ok, Equals, true)
	c.Assert(fileId.Valid(), Equals, true)
	result["_id"] = "<id>"

	ud, ok := result["uploadDate"].(time.Time)
	c.Assert(ok, Equals, true)
	c.Assert(ud.After(before) && ud.Before(after), Equals, true)
	result["uploadDate"] = "<timestamp>"

	expected := M{
		"_id":        "<id>",
		"length":     9,
		"chunkSize":  255 * 1024,
		"uploadDate": "<timestamp>",
		"md5":        "1e50210a0202497fb79bc38b6ade6c34",
	}
	c.Assert(result, DeepEquals, expected)

	// Check the chunk.
	result = M{}
	err = db.C("fs.chunks").Find(nil).One(result)
	c.Assert(err, IsNil)

	chunkId, ok := result["_id"].(bson.ObjectId)
	c.Assert(ok, Equals, true)
	c.Assert(chunkId.Valid(), Equals, true)
	result["_id"] = "<id>"

	expected = M{
		"_id":      "<id>",
		"files_id": fileId,
		"n":        0,
		"data":     []byte("some data"),
	}
	c.Assert(result, DeepEquals, expected)

	// Check that an index was created.
	indexes, err := db.C("fs.chunks").Indexes()
	c.Assert(err, IsNil)
	c.Assert(len(indexes), Equals, 2)
	c.Assert(indexes[1].Key, DeepEquals, []string{"files_id", "n"})
}
예제 #30
0
func (s *StoreSuite) TestLogCharmEvent(c *gc.C) {
	url1 := charm.MustParseURL("cs:oneiric/wordpress")
	url2 := charm.MustParseURL("cs:oneiric/mysql")
	urls := []*charm.URL{url1, url2}

	event1 := &charmstore.CharmEvent{
		Kind:     charmstore.EventPublished,
		Revision: 42,
		Digest:   "revKey1",
		URLs:     urls,
		Warnings: []string{"A warning."},
		Time:     time.Unix(1, 0),
	}
	event2 := &charmstore.CharmEvent{
		Kind:     charmstore.EventPublished,
		Revision: 42,
		Digest:   "revKey2",
		URLs:     urls,
		Time:     time.Unix(1, 0),
	}
	event3 := &charmstore.CharmEvent{
		Kind:   charmstore.EventPublishError,
		Digest: "revKey2",
		Errors: []string{"An error."},
		URLs:   urls[:1],
	}

	for _, event := range []*charmstore.CharmEvent{event1, event2, event3} {
		err := s.store.LogCharmEvent(event)
		c.Assert(err, gc.IsNil)
	}

	events := s.Session.DB("juju").C("events")
	var s1, s2 map[string]interface{}

	err := events.Find(bson.M{"digest": "revKey1"}).One(&s1)
	c.Assert(err, gc.IsNil)
	c.Assert(s1["kind"], gc.Equals, int(charmstore.EventPublished))
	c.Assert(s1["urls"], gc.DeepEquals, []interface{}{"cs:oneiric/wordpress", "cs:oneiric/mysql"})
	c.Assert(s1["warnings"], gc.DeepEquals, []interface{}{"A warning."})
	c.Assert(s1["errors"], gc.IsNil)
	c.Assert(s1["time"], gc.DeepEquals, time.Unix(1, 0))

	err = events.Find(bson.M{"digest": "revKey2", "kind": charmstore.EventPublishError}).One(&s2)
	c.Assert(err, gc.IsNil)
	c.Assert(s2["urls"], gc.DeepEquals, []interface{}{"cs:oneiric/wordpress"})
	c.Assert(s2["warnings"], gc.IsNil)
	c.Assert(s2["errors"], gc.DeepEquals, []interface{}{"An error."})
	c.Assert(s2["time"].(time.Time).After(bson.Now().Add(-10e9)), gc.Equals, true)

	// Mongo stores timestamps in milliseconds, so chop
	// off the extra bits for comparison.
	event3.Time = time.Unix(0, event3.Time.UnixNano()/1e6*1e6)

	event, err := s.store.CharmEvent(urls[0], "revKey2")
	c.Assert(err, gc.IsNil)
	c.Assert(event, gc.DeepEquals, event3)

	event, err = s.store.CharmEvent(urls[1], "revKey1")
	c.Assert(err, gc.IsNil)
	c.Assert(event, gc.DeepEquals, event1)

	event, err = s.store.CharmEvent(urls[1], "revKeyX")
	c.Assert(err, gc.Equals, charmstore.ErrNotFound)
	c.Assert(event, gc.IsNil)
}