Ejemplo n.º 1
1
func TestParseCommand_Use(t *testing.T) {
	t.Parallel()
	ts := emptyTestServer()
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := cli.CommandLine{Client: c}

	tests := []struct {
		cmd string
	}{
		{cmd: "use db"},
		{cmd: " use db"},
		{cmd: "use db "},
		{cmd: "use db;"},
		{cmd: "use db; "},
		{cmd: "Use db"},
	}

	for _, test := range tests {
		if err := m.ParseCommand(test.cmd); err != nil {
			t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
		}

		if m.Database != "db" {
			t.Fatalf(`Command "use" changed database to %q. Expected db`, m.Database)
		}
	}
}
Ejemplo n.º 2
0
func TestClient_Timeout(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1 * time.Second)
		var data client.Response
		w.WriteHeader(http.StatusOK)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u, Timeout: 500 * time.Millisecond}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	query := client.Query{}
	_, err = c.Query(query)
	if err == nil {
		t.Fatalf("unexpected success.  expected timeout error")
	} else if !strings.Contains(err.Error(), "use of closed network connection") {
		t.Fatalf("unexpected error.  expected 'use of closed network connection' error, got %v", err)
	}

	confignotimeout := client.Config{URL: *u}
	cnotimeout, err := client.NewClient(confignotimeout)
	_, err = cnotimeout.Query(query)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}
func influxConnect(config *StartupConfig, runningConfig *RunningConfig) (*influx.Client, error) {
	//Connect to InfluxDb
	activeServers := len(runningConfig.InfluxDbProps)
	rand.Seed(4200)
	//if there is only 1 active, use it
	if activeServers == 1 {
		u, err := url.Parse(fmt.Sprintf("http://%s:%d", runningConfig.InfluxDbProps[0].Fqdn, runningConfig.InfluxDbProps[0].Port))
		if err != nil {
			return nil, err
		}
		conf := influx.Config{
			URL:      *u,
			Username: config.InfluxUser,
			Password: config.InfluxPassword,
		}
		con, err := influx.NewClient(conf)
		if err != nil {
			return nil, err
		}
		_, _, err = con.Ping()
		if err != nil {
			return nil, err
		}
		return con, nil
	} else if activeServers > 1 {
		//try to connect to a random server until we find one that works.  if we dont find one in 20 tries, bail.
		for i := 0; i < 20; i++ {
			index := rand.Intn(activeServers)
			u, err := url.Parse(fmt.Sprintf("http://%s:%d", runningConfig.InfluxDbProps[index].Fqdn, runningConfig.InfluxDbProps[index].Port))
			if err != nil {
				errHndlr(err, ERROR)
				continue
			} else {
				conf := influx.Config{
					URL:      *u,
					Username: config.InfluxUser,
					Password: config.InfluxPassword,
				}
				con, err := influx.NewClient(conf)
				if err != nil {
					errHndlr(err, ERROR)
					continue
				} else {
					_, _, err = con.Ping()
					if err != nil {
						errHndlr(err, ERROR)
						continue
					} else {
						return con, nil
					}
				}
			}
		}
		err := errors.New("Could not connect to any of the InfluxDb servers that are ONLINE in traffic ops.")
		return nil, err
	} else {
		err := errors.New("No online InfluxDb servers could be found!")
		return nil, err
	}
}
Ejemplo n.º 4
0
func influxConnect(config *StartupConfig, trafOps TrafOpsData) (*influx.Client, error) {
	//Connect to InfluxDb
	activeServers := len(trafOps.InfluxDbProps)
	rand.Seed(42)
	//if there is only 1 active, use it
	if activeServers == 1 {
		u, err := url.Parse(fmt.Sprintf("http://%s:%d", trafOps.InfluxDbProps[0].Fqdn, trafOps.InfluxDbProps[0].Port))
		if err != nil {
			return nil, err
		}
		conf := influx.Config{
			URL:      *u,
			Username: config.InfluxUser,
			Password: config.InfluxPassword,
		}
		con, err := influx.NewClient(conf)
		if err != nil {
			return nil, err
		}
		_, _, err = con.Ping()
		if err != nil {
			return nil, err
		}
		return con, nil
	} else if activeServers > 1 {
		//try to connect to all ONLINE servers until we find one that works
		for i := 0; i < activeServers; i++ {
			u, err := url.Parse(fmt.Sprintf("http://%s:%d", trafOps.InfluxDbProps[i].Fqdn, trafOps.InfluxDbProps[i].Port))
			if err != nil {
				errHndlr(err, ERROR)
			} else {
				conf := influx.Config{
					URL:      *u,
					Username: config.InfluxUser,
					Password: config.InfluxPassword,
				}
				con, err := influx.NewClient(conf)
				if err != nil {
					errHndlr(err, ERROR)
					continue
				} else {
					_, _, err = con.Ping()
					if err != nil {
						errHndlr(err, ERROR)
						continue
					} else {
						return con, nil
					}
				}
			}
		}
		err := errors.New("Could not connect to any of the InfluxDb servers that are ONLINE in traffic ops.")
		return nil, err
	} else {
		err := errors.New("No online InfluxDb servers could be found!")
		return nil, err
	}
}
Ejemplo n.º 5
0
func main() {
	numberOfSeries := 50000
	if len(os.Args) > 1 {
		numberOfSeries, _ = strconv.Atoi(os.Args[1])
	}
	fmt.Printf("Benchmarking writing %d series\n", numberOfSeries)

	c, err := client.NewClient(&client.ClientConfig{})
	if err != nil {
		panic(err)
	}

	before := time.Now()
	c.DeleteDatabase("performance")
	fmt.Printf("Deleting took %s\n", time.Now().Sub(before))
	os.Exit(0)
	if err := c.CreateDatabase("performance"); err != nil {
		panic(err)
	}

	c, err = client.NewClient(&client.ClientConfig{
		Database: "performance",
	})
	if err != nil {
		panic(err)
	}

	before = time.Now()

	for i := 0; i < 10; i++ {
		series := []*client.Series{}
		for i := 0; i < numberOfSeries; i++ {
			name := fmt.Sprintf("series_%d", i+1)
			series = append(series, &client.Series{
				Name:    name,
				Columns: []string{"value"},
				Points: [][]interface{}{
					{rand.Float64()},
				},
			})
		}
		if err := c.WriteSeries(series); err != nil {
			panic(err)
		}
	}

	fmt.Printf("Writing took %s\n", time.Now().Sub(before))
}
Ejemplo n.º 6
0
func BenchmarkWrite(b *testing.B) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var data client.Response
		w.WriteHeader(http.StatusNoContent)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		b.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	bp := client.BatchPoints{
		Points: []client.Point{
			{Fields: map[string]interface{}{"value": 101}}},
	}
	for i := 0; i < b.N; i++ {
		r, err := c.Write(bp)
		if err != nil {
			b.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
		}
		if r != nil {
			b.Fatalf("unexpected response. expected %v, actual %v", nil, r)
		}
	}
}
Ejemplo n.º 7
0
func TestClient_BasicAuth(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		u, p, ok := r.BasicAuth()

		if !ok {
			t.Errorf("basic auth error")
		}
		if u != "username" {
			t.Errorf("unexpected username, expected %q, actual %q", "username", u)
		}
		if p != "password" {
			t.Errorf("unexpected password, expected %q, actual %q", "password", p)
		}
		w.WriteHeader(http.StatusNoContent)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	u.User = url.UserPassword("username", "password")
	config := client.Config{URL: *u, Username: "******", Password: "******"}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	_, _, err = c.Ping()
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}
Ejemplo n.º 8
0
func TestSetWriteConsistency(t *testing.T) {
	t.Parallel()
	c := cli.New(CLIENT_VERSION)
	config := client.NewConfig()
	client, _ := client.NewClient(config)
	c.Client = client

	// set valid write consistency
	consistency := "all"
	c.SetWriteConsistency("consistency " + consistency)
	if c.WriteConsistency != consistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}

	// set different valid write consistency and validate change
	consistency = "quorum"
	c.SetWriteConsistency("consistency " + consistency)
	if c.WriteConsistency != consistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}

	// set invalid write consistency and verify there was no change
	invalidConsistency := "invalid_consistency"
	c.SetWriteConsistency("consistency " + invalidConsistency)
	if c.WriteConsistency == invalidConsistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}
}
Ejemplo n.º 9
0
func setupInflux() {
	influx_port, _ := strconv.ParseInt(os.Getenv("INFLUX_PORT"), 10, 0)

	u, err := url.Parse(fmt.Sprintf("http://%s:%d", os.Getenv("INFLUX_HOST"), influx_port))
	if err != nil {
		log.Fatal(err)
	}

	conf := client.Config{
		URL:      *u,
		Username: os.Getenv("INFLUX_USER"),
		Password: os.Getenv("INFLUX_PASS"),
	}

	con, err = client.NewClient(conf)
	if err != nil {
		log.Fatal(err)
	}

	dur, ver, err := con.Ping()
	if err != nil {
		log.Fatal(err)
	}
	if os.Getenv("DEBUG") == "true" {
		log.Printf("Connected in %v | Version: %s", dur, ver)
	}
}
Ejemplo n.º 10
0
func (s *influxdbSource) query(query string, precision ...influxdb.TimePrecision) ([]*influxdb.Series, error) {
	client, err := influxdb.NewClient(s.conf)
	if err != nil {
		return nil, err
	}
	return client.Query(query, precision...)
}
Ejemplo n.º 11
0
func NewInfluxDBSink(rawUrl, username, password, database string) (*InfluxDBSink, error) {
	url, err := url.Parse(fmt.Sprintf(rawUrl))
	if err != nil {
		return nil, err
	}

	config := influxdb.Config{
		URL:      *url,
		Username: username,
		Password: password,
	}

	client, err := influxdb.NewClient(config)
	if err != nil {
		return nil, err
	}

	_, _, err = client.Ping()
	if err != nil {
		return nil, err
	}

	return &InfluxDBSink{
		Client:         client,
		Database:       database,
		MetricsToFlush: []influxdb.Point{},
		LastFlush:      time.Now(),
	}, nil
}
func main() {
	// Listen for incoming connections.
	l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
	if err != nil {
		fmt.Println("Error listening:", err.Error())
		os.Exit(1)
	}
	c, err := client.NewClient(&Global.Influx)
	if err != nil {
		panic(err)
	}

	// Close the listener when the application closes.
	defer l.Close()
	fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
	for {
		// Listen for an incoming connection.
		conn, err := l.Accept()
		if err != nil {
			fmt.Println("Error accepting: ", err.Error())
		} else {
			// Handle connections in a new goroutine.
			go handleRequest(conn, c)
		}
	}
}
Ejemplo n.º 13
0
func (i *InfluxDB) Connect() error {
	u, err := url.Parse(i.URL)
	if err != nil {
		return err
	}

	c, err := client.NewClient(client.Config{
		URL:       *u,
		Username:  i.Username,
		Password:  i.Password,
		UserAgent: i.UserAgent,
		Timeout:   i.Timeout.Duration,
	})

	if err != nil {
		return err
	}

	_, err = c.Query(client.Query{
		Command: fmt.Sprintf("CREATE DATABASE telegraf"),
	})

	if err != nil && !strings.Contains(err.Error(), "database already exists") {
		log.Fatal(err)
	}

	i.conn = c
	return nil
}
Ejemplo n.º 14
0
func NewInfluxSinker(conf InfluxConfig) (Sinker, error) {

	u, err := url.Parse("http://" + conf.Address)
	if err != nil {
		return nil, err
	}
	cli, err := influx.NewClient(influx.Config{
		URL:      *u,
		Username: conf.Username,
		Password: conf.Password,
	})

	if err != nil {
		return nil, err
	}

	host, err := os.Hostname()
	if err != nil {
		return nil, err
	}

	return &influxSinker{
		influx:   cli,
		hostName: host,
		database: conf.Database,
	}, nil
}
Ejemplo n.º 15
0
func init_fluxdb(conf InfluxdbConf) (pcon *client.Client, perr error) {
	u, err := url.Parse(fmt.Sprintf("http://%s:%d", conf.Host, conf.Port))
	if err != nil {
		log.Fatal(err)
		return nil, err
	}

	influxconf := client.Config{
		URL:      *u,
		Username: os.Getenv(conf.User),
		Password: os.Getenv(conf.Pwd),
	}

	con, err := client.NewClient(influxconf)
	if err != nil {
		log.Fatal(err)
		return nil, err
	}

	dur, ver, err := con.Ping()
	if err != nil {
		log.Fatal(err)
		return nil, err
	}
	log.Printf("Happy as a Hippo! %v, %s", dur, ver)
	return con, nil
}
Ejemplo n.º 16
0
func NewClient(c Config) (*MonitorInfo, error) {
	monClient := MonitorInfo{
		host:      c.Host,
		port:      c.Port,
		db:        c.DB,
		metricMap: make(map[string]time.Time),
	}

	u, err := url.Parse(fmt.Sprintf("http://%s:%d", monClient.host, monClient.port))
	if err != nil {
		log.Fatal(err)
	}

	conf := client.Config{
		URL:      *u,
		Username: os.Getenv("INFLUX_USER"),
		Password: os.Getenv("INFLUX_PWD"),
	}

	con, err := client.NewClient(conf)
	if err != nil {
		log.Fatal(err)
	}

	monClient.influxClient = con

	return &monClient, nil
}
Ejemplo n.º 17
0
func TestParseCommand_Insert(t *testing.T) {
	t.Parallel()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var data client.Response
		w.WriteHeader(http.StatusNoContent)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := main.CommandLine{Client: c}

	tests := []struct {
		cmd string
	}{
		{cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: " INSERT test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "INSERT   test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "Insert test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "insert test.test cpu,host=serverA,region=us-west value=1.0"},
	}

	for _, test := range tests {
		if !m.ParseCommand(test.cmd) {
			t.Fatalf(`Command "insert" failed for %q.`, test.cmd)
		}
	}
}
Ejemplo n.º 18
0
func TestClient_NoTimeout(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1 * time.Second)
		var data client.Response
		w.WriteHeader(http.StatusOK)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	query := client.Query{}
	_, err = c.Query(query)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}
Ejemplo n.º 19
0
func TestClient_WriteUint64(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var data client.Response
		w.WriteHeader(http.StatusNoContent)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	bp := client.BatchPoints{
		Points: []client.Point{
			{
				Fields: map[string]interface{}{"value": uint64(10)},
			},
		},
	}
	r, err := c.Write(bp)
	if err == nil {
		t.Fatalf("unexpected error. expected err, actual %v", err)
	}
	if r != nil {
		t.Fatalf("unexpected response. expected %v, actual %v", nil, r)
	}
}
Ejemplo n.º 20
0
func (self *SingleServerSuite) createUser(c *C) {
	client, err := influxdb.NewClient(&influxdb.ClientConfig{})
	c.Assert(err, IsNil)
	c.Assert(client.CreateDatabase("db1"), IsNil)
	c.Assert(client.CreateDatabaseUser("db1", "user", "pass"), IsNil)
	c.Assert(client.AlterDatabasePrivilege("db1", "user", true), IsNil)
}
Ejemplo n.º 21
0
// machineName: A unique identifier to identify the host that current cAdvisor
// instance is running on.
// influxdbHost: The host which runs influxdb.
// percentilesDuration: Time window which will be considered when calls Percentiles()
func New(machineName,
	tablename,
	database,
	username,
	password,
	influxdbHost string,
	isSecure bool,
	percentilesDuration time.Duration,
) (storage.StorageDriver, error) {
	config := &influxdb.ClientConfig{
		Host:     influxdbHost,
		Username: username,
		Password: password,
		Database: database,
		IsSecure: isSecure,
	}
	client, err := influxdb.NewClient(config)
	if err != nil {
		return nil, err
	}
	// TODO(monnand): With go 1.3, we cannot compress data now.
	client.DisableCompression()
	if percentilesDuration.Seconds() < 1.0 {
		percentilesDuration = 5 * time.Minute
	}

	ret := &influxdbStorage{
		client:      client,
		windowLen:   percentilesDuration,
		machineName: machineName,
		tableName:   tablename,
	}
	return ret, nil
}
Ejemplo n.º 22
0
func (self *SingleServerSuite) TestSslOnly(c *C) {
	self.server.Stop()
	// TODO: don't hard code the path here
	c.Assert(os.RemoveAll("/tmp/influxdb/development"), IsNil)
	server := NewSslServer("integration/test_ssl_only.toml", c)
	server.WaitForServerToStart()

	defer func() {
		server.Stop()
		self.server = NewServer("integration/test_config_single.toml", c)
	}()

	client, err := influxdb.NewClient(&influxdb.ClientConfig{
		IsSecure: true,
		HttpClient: &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{
					InsecureSkipVerify: true,
				},
			},
		},
	})

	c.Assert(err, IsNil)
	c.Assert(client.Ping(), IsNil)

}
Ejemplo n.º 23
0
func InitInfluxdb(ch chan *influxdb.Series, conf *InfluxDBConfig) error {
	config := &influxdb.ClientConfig{
		Host:       conf.Host,
		Username:   conf.User,
		Password:   conf.Password,
		Database:   conf.Database,
		IsUDP:      conf.Udp,
		HttpClient: http.DefaultClient,
	}
	client, err := influxdb.NewClient(config)
	if err != nil {
		Conf.Logger.Fatalln(err)
	}
	client.DisableCompression()

	go func() {
		Conf.Logger.Println("Influxdb queue is starts")
		buf := make([]*influxdb.Series, 0)
		for series := range ch {
			debug("go func", *series)
			buf = append(buf, series)
			if len(buf) >= conf.SendBuffer {
				if conf.Udp {
					go client.WriteSeriesOverUDP(buf)
				} else {
					go client.WriteSeries(buf)
				}
				// clean buffer
				buf = make([]*influxdb.Series, 0)
			}
		}
	}()
	return nil
}
Ejemplo n.º 24
0
// Returns a thread-compatible implementation of influxdb interactions.
func NewSink(hostname, username, password, databaseName string, avoidColumns bool) (sink_api.ExternalSink, error) {
	var err error
	config := &influxdb.ClientConfig{
		Host:     hostname,
		Username: username,
		Password: password,
		Database: databaseName,
		IsSecure: false,
	}
	glog.Infof("Using influxdb on host %q with database %q", hostname, databaseName)
	client, err := influxdb.NewClient(config)
	if err != nil {
		return nil, err
	}
	client.DisableCompression()
	for i := 0; i < maxRetries; i++ {
		err = createDatabase(databaseName, client)
		if err == nil {
			break
		}
		glog.Errorf("%s. Retrying after 30 seconds", err)
		time.Sleep(waitDuration)
	}
	if err != nil {
		return nil, err
	}
	return &influxdbSink{
		hostname:     hostname,
		database:     databaseName,
		client:       client,
		dbName:       databaseName,
		avoidColumns: avoidColumns,
		seqNum:       newMetricSequenceNum(),
	}, nil
}
Ejemplo n.º 25
0
// Returns a thread-compatible implementation of influxdb interactions.
func new(c config) (sink_api.ExternalSink, error) {
	var err error
	iConfig := &influxdb.ClientConfig{
		Host:     c.host,
		Username: c.user,
		Password: c.password,
		Database: c.dbName,
		IsSecure: false,
	}
	client, err := influxdb.NewClient(iConfig)
	if err != nil {
		return nil, err
	}
	client.DisableCompression()
	for i := 0; i < maxRetries; i++ {
		err = createDatabase(c.dbName, client)
		if err == nil {
			break
		}
		glog.Errorf("%s. Retrying after 30 seconds", err)
		time.Sleep(waitDuration)
	}
	if err != nil {
		return nil, err
	}
	return &influxdbSink{
		client: client,
		seqNum: newMetricSequenceNum(),
		c:      c,
	}, nil
}
Ejemplo n.º 26
0
func TestParseCommand_Insert(t *testing.T) {
	t.Parallel()
	ts := emptyTestServer()
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := cli.CommandLine{Client: c}

	tests := []struct {
		cmd string
	}{
		{cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: " INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "INSERT   cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "insert cpu,host=serverA,region=us-west    value=1.0    "},
		{cmd: "insert"},
		{cmd: "Insert "},
		{cmd: "insert c"},
		{cmd: "insert int"},
	}

	for _, test := range tests {
		if err := m.ParseCommand(test.cmd); err != nil {
			t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
		}
	}
}
Ejemplo n.º 27
0
func (c *CommandLine) connect(cmd string) error {
	var cl *client.Client
	var u url.URL

	// Remove the "connect" keyword if it exists
	path := strings.TrimSpace(strings.Replace(cmd, "connect", "", -1))

	// If they didn't provide a connection string, use the current settings
	if path == "" {
		path = net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
	}

	var e error
	u, e = client.ParseConnectionString(path, c.Ssl)
	if e != nil {
		return e
	}

	config := client.NewConfig()
	config.URL = u
	config.Username = c.Username
	config.Password = c.Password
	config.UserAgent = "InfluxDBShell/" + version
	cl, err := client.NewClient(config)
	if err != nil {
		return fmt.Errorf("Could not create client %s", err)
	}
	c.Client = cl
	if _, v, e := c.Client.Ping(); e != nil {
		return fmt.Errorf("Failed to connect to %s\n", c.Client.Addr())
	} else {
		c.Version = v
	}
	return nil
}
Ejemplo n.º 28
0
func (self *DataTestClient) RunInvalidQuery(query string, c *C, timePrecision ...influxdb.TimePrecision) []*influxdb.Series {
	client, err := influxdb.NewClient(&influxdb.ClientConfig{Database: self.db})
	c.Assert(err, IsNil)
	_, err = client.Query(query, timePrecision...)
	c.Assert(err, NotNil)
	return nil
}
Ejemplo n.º 29
0
func TestParseCommand_InsertInto(t *testing.T) {
	t.Parallel()
	ts := emptyTestServer()
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := cli.CommandLine{Client: c}

	tests := []struct {
		cmd, db, rp string
	}{
		{
			cmd: `INSERT INTO test cpu,host=serverA,region=us-west value=1.0`,
			db:  "",
			rp:  "test",
		},
		{
			cmd: ` INSERT INTO .test cpu,host=serverA,region=us-west value=1.0`,
			db:  "",
			rp:  "test",
		},
		{
			cmd: `INSERT INTO   "test test" cpu,host=serverA,region=us-west value=1.0`,
			db:  "",
			rp:  "test test",
		},
		{
			cmd: `Insert iNTO test.test cpu,host=serverA,region=us-west value=1.0`,
			db:  "test",
			rp:  "test",
		},
		{
			cmd: `insert into "test test" cpu,host=serverA,region=us-west value=1.0`,
			db:  "test",
			rp:  "test test",
		},
		{
			cmd: `insert into "d b"."test test" cpu,host=serverA,region=us-west value=1.0`,
			db:  "d b",
			rp:  "test test",
		},
	}

	for _, test := range tests {
		if err := m.ParseCommand(test.cmd); err != nil {
			t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
		}
		if m.Database != test.db {
			t.Fatalf(`Command "insert into" db parsing failed, expected: %q, actual: %q`, test.db, m.Database)
		}
		if m.RetentionPolicy != test.rp {
			t.Fatalf(`Command "insert into" rp parsing failed, expected: %q, actual: %q`, test.rp, m.RetentionPolicy)
		}
	}
}
Ejemplo n.º 30
0
func TestNewClient(t *testing.T) {
	config := client.Config{}
	_, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}