예제 #1
0
func dropDb(session *r.Session, dbName string) {
	resp, err := r.DBDrop(dbName).RunWrite(session)
	if err != nil {
		log.Fatalln(err)
		return
	}

	fmt.Printf("%v: %d DB dropped, %d tables dropped.\n", dbName, resp.DBsDropped, resp.TablesDropped)
}
func setupRethinkdb(s *r.Session) error {
	_, err := r.DBDrop("alerts").RunWrite(s)
	r.DBCreate("alerts").RunWrite(s)
	return err
}
예제 #3
0
파일: adapter.go 프로젝트: ycaihua/chat
// CreateDb initializes the storage. If reset is true, the database is first deleted losing all the data.
func (a *RethinkDbAdapter) CreateDb(reset bool) error {

	// Drop database if exists, ignore error if it does not.
	if reset {
		rdb.DBDrop("tinode").RunWrite(a.conn)
	}

	if _, err := rdb.DBCreate("tinode").RunWrite(a.conn); err != nil {
		return err
	}

	// Users
	if _, err := rdb.DB("tinode").TableCreate("users", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil {
		return err
	}
	// Create secondary index on User.Tags array so user can be found by tags
	if _, err := rdb.DB("tinode").Table("users").IndexCreate("Tags", rdb.IndexCreateOpts{Multi: true}).RunWrite(a.conn); err != nil {
		return err
	}
	// User authentication records {unique, userid, secret}
	if _, err := rdb.DB("tinode").TableCreate("auth", rdb.TableCreateOpts{PrimaryKey: "unique"}).RunWrite(a.conn); err != nil {
		return err
	}
	// Should be able to access user's auth records by user id
	if _, err := rdb.DB("tinode").Table("auth").IndexCreate("userid").RunWrite(a.conn); err != nil {
		return err
	}

	// Subscription to a topic. The primary key is a Topic:User string
	if _, err := rdb.DB("tinode").TableCreate("subscriptions", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil {
		return err
	}
	if _, err := rdb.DB("tinode").Table("subscriptions").IndexCreate("User").RunWrite(a.conn); err != nil {
		return err
	}
	if _, err := rdb.DB("tinode").Table("subscriptions").IndexCreate("Topic").RunWrite(a.conn); err != nil {
		return err
	}

	// Topic stored in database
	if _, err := rdb.DB("tinode").TableCreate("topics", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil {
		return err
	}

	// Stored message
	if _, err := rdb.DB("tinode").TableCreate("messages", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil {
		return err
	}
	if _, err := rdb.DB("tinode").Table("messages").IndexCreateFunc("Topic_SeqId",
		func(row rdb.Term) interface{} {
			return []interface{}{row.Field("Topic"), row.Field("SeqId")}
		}).RunWrite(a.conn); err != nil {
		return err
	}

	// Index of unique user contact information as strings, such as "email:[email protected]" or "tel:18003287448":
	// {Id: <tag>, Source: <uid>} to ensure uniqueness of tags.
	if _, err := rdb.DB("tinode").TableCreate("tagunique", rdb.TableCreateOpts{PrimaryKey: "Id"}).RunWrite(a.conn); err != nil {
		return err
	}

	// Index for unique fields
	//if _, err := rdb.DB("tinode").TableCreate("_uniques").RunWrite(a.conn); err != nil {
	//	return err
	//}

	return nil
}
func TestMetrics(t *testing.T) {

	sess, dbName, err := setupDB(t)
	if err != nil {
		t.Errorf("DB setup borked")
		return
	}
	defer r.DBDrop(dbName).Run(sess)

	e := NewRethinkDBExporter("localhost:28015", "", "test", "")
	e.metrics = map[string]*prometheus.GaugeVec{}

	chM := make(chan prometheus.Metric)
	chD := make(chan *prometheus.Desc)

	go func() {
		e.Collect(chM)
		close(chM)
	}()

	countMetrics := 0
	countMetricsForDB := 0
	for m := range chM {

		// descString := m.Desc().String()
		// log.Printf("descString: %s", descString)
		countMetrics++

		switch m.(type) {
		case prometheus.Gauge:

			g := &dto.Metric{}
			m.Write(g)
			if g.GetGauge() == nil {
				continue
			}

			if len(g.Label) == 4 && *g.Label[1].Name == "db" && *g.Label[1].Value == dbName {
				countMetricsForDB++
			}

		default:
			log.Printf("default: m: %#v", m)
		}

	}
	if countMetrics < 10 {
		t.Errorf("Expected at least 10 metrics, found only %d", countMetrics)
	}

	if countMetricsForDB < 4 {
		t.Errorf("Expected at least 4 metrics, found only %d", countMetricsForDB)
	}

	// descriptions
	go func() {
		e.Describe(chD)
		close(chD)
	}()

	allDescr := []*prometheus.Desc{}
	for d := range chD {
		allDescr = append(allDescr, d)
	}

	wants := []string{"server_client_connections", "cluster_servers_total", "cluster_client_connections", "table_server_disk_read_bytes_per_sec", "table_server_disk_garbage_bytes"}
	for _, w := range wants {
		found := false
		for _, d := range allDescr {
			if strings.Contains(d.String(), w) {
				found = true
				break
			}
		}

		if !found {
			t.Errorf("didn't find %s in descriptions", w)
		}
	}

	if len(allDescr) < 10 {
		t.Errorf("Expected at least 10 descriptions, found only %d", len(allDescr))
	}
}
func (s *RethinkDBStorage) Cleanup() {
	r.DBDrop(s.dbName).RunWrite(s.session)
	s.Close()
}
	testConn         *r.Session
	tst              *testing.T
	loginCredentials = map[string]string{"username": "******", "password": "******"}
)

func TestTravelPlanning(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "TravelPlanning Suite")
}

var _ = BeforeSuite(func() {
	testConn = NewDBSession(database)
})

var _ = AfterSuite(func() {
	_, err := r.DBDrop(database).RunWrite(testConn)
	if err != nil {
		log.Print(err)
	}
	testConn.Close()
})

type resultToken struct {
	TokenString string `json:"token"`
}

func Login() string {
	APIRequest("/login", "POST", loginCredentials, "")

	token := resultToken{}
	json.Unmarshal(Response.Body.Bytes(), &token)