mirror of
https://github.com/Oxalide/vsphere-influxdb-go.git
synced 2023-10-10 11:36:51 +00:00
add vendoring with go dep
This commit is contained in:
1077
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go
generated
vendored
Normal file
1077
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
58
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go
generated
vendored
Normal file
58
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
package cli
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseCommand_InsertInto(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
c := CommandLine{}
|
||||
|
||||
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: "",
|
||||
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 {
|
||||
t.Logf("command: %s", test.cmd)
|
||||
bp, err := c.parseInsert(test.cmd)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bp.Database != test.db {
|
||||
t.Fatalf(`Command "insert into" db parsing failed, expected: %q, actual: %q`, test.db, bp.Database)
|
||||
}
|
||||
if bp.RetentionPolicy != test.rp {
|
||||
t.Fatalf(`Command "insert into" rp parsing failed, expected: %q, actual: %q`, test.rp, bp.RetentionPolicy)
|
||||
}
|
||||
}
|
||||
}
|
594
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go
generated
vendored
Normal file
594
vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go
generated
vendored
Normal file
@@ -0,0 +1,594 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/influxdb/client"
|
||||
"github.com/influxdata/influxdb/cmd/influx/cli"
|
||||
"github.com/influxdata/influxdb/influxql"
|
||||
"github.com/peterh/liner"
|
||||
)
|
||||
|
||||
const (
|
||||
CLIENT_VERSION = "y.y"
|
||||
SERVER_VERSION = "x.x"
|
||||
)
|
||||
|
||||
func TestNewCLI(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
|
||||
if c == nil {
|
||||
t.Fatal("CommandLine shouldn't be nil.")
|
||||
}
|
||||
|
||||
if c.ClientVersion != CLIENT_VERSION {
|
||||
t.Fatalf("CommandLine version is %s but should be %s", c.ClientVersion, CLIENT_VERSION)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCLI(t *testing.T) {
|
||||
t.Parallel()
|
||||
ts := emptyTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
u, _ := url.Parse(ts.URL)
|
||||
h, p, _ := net.SplitHostPort(u.Host)
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
c.Host = h
|
||||
c.Port, _ = strconv.Atoi(p)
|
||||
c.IgnoreSignals = true
|
||||
c.ForceTTY = true
|
||||
go func() {
|
||||
close(c.Quit)
|
||||
}()
|
||||
if err := c.Run(); err != nil {
|
||||
t.Fatalf("Run failed with error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCLI_ExecuteInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
ts := emptyTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
u, _ := url.Parse(ts.URL)
|
||||
h, p, _ := net.SplitHostPort(u.Host)
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
c.Host = h
|
||||
c.Port, _ = strconv.Atoi(p)
|
||||
c.ClientConfig.Precision = "ms"
|
||||
c.Execute = "INSERT sensor,floor=1 value=2"
|
||||
c.IgnoreSignals = true
|
||||
c.ForceTTY = true
|
||||
if err := c.Run(); err != nil {
|
||||
t.Fatalf("Run failed with error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAuth(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
config := client.NewConfig()
|
||||
client, _ := client.NewClient(config)
|
||||
c.Client = client
|
||||
u := "userx"
|
||||
p := "pwdy"
|
||||
c.SetAuth("auth " + u + " " + p)
|
||||
|
||||
// validate CLI configuration
|
||||
if c.ClientConfig.Username != u {
|
||||
t.Fatalf("Username is %s but should be %s", c.ClientConfig.Username, u)
|
||||
}
|
||||
if c.ClientConfig.Password != p {
|
||||
t.Fatalf("Password is %s but should be %s", c.ClientConfig.Password, p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPrecision(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
config := client.NewConfig()
|
||||
client, _ := client.NewClient(config)
|
||||
c.Client = client
|
||||
|
||||
// validate set non-default precision
|
||||
p := "ns"
|
||||
c.SetPrecision("precision " + p)
|
||||
if c.ClientConfig.Precision != p {
|
||||
t.Fatalf("Precision is %s but should be %s", c.ClientConfig.Precision, p)
|
||||
}
|
||||
|
||||
// validate set default precision which equals empty string
|
||||
p = "rfc3339"
|
||||
c.SetPrecision("precision " + p)
|
||||
if c.ClientConfig.Precision != "" {
|
||||
t.Fatalf("Precision is %s but should be empty", c.ClientConfig.Precision)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
config := client.NewConfig()
|
||||
client, _ := client.NewClient(config)
|
||||
c.Client = client
|
||||
|
||||
// validate set non-default format
|
||||
f := "json"
|
||||
c.SetFormat("format " + f)
|
||||
if c.Format != f {
|
||||
t.Fatalf("Format is %s but should be %s", c.Format, f)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetChunked(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
config := client.NewConfig()
|
||||
client, _ := client.NewClient(config)
|
||||
c.Client = client
|
||||
|
||||
// make sure chunked is on by default
|
||||
if got, exp := c.Chunked, true; got != exp {
|
||||
t.Fatalf("chunked should be on by default. got %v, exp %v", got, exp)
|
||||
}
|
||||
|
||||
// turn chunked off
|
||||
if err := c.ParseCommand("Chunked"); err != nil {
|
||||
t.Fatalf("setting chunked failed: err: %s", err)
|
||||
}
|
||||
|
||||
if got, exp := c.Chunked, false; got != exp {
|
||||
t.Fatalf("setting chunked failed. got %v, exp %v", got, exp)
|
||||
}
|
||||
|
||||
// turn chunked back on
|
||||
if err := c.ParseCommand("Chunked"); err != nil {
|
||||
t.Fatalf("setting chunked failed: err: %s", err)
|
||||
}
|
||||
|
||||
if got, exp := c.Chunked, true; got != exp {
|
||||
t.Fatalf("setting chunked failed. got %v, exp %v", got, exp)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetChunkSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
config := client.NewConfig()
|
||||
client, _ := client.NewClient(config)
|
||||
c.Client = client
|
||||
|
||||
// check default chunk size
|
||||
if got, exp := c.ChunkSize, 0; got != exp {
|
||||
t.Fatalf("unexpected chunk size. got %d, exp %d", got, exp)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
command string
|
||||
exp int
|
||||
}{
|
||||
{"chunk size 20", 20},
|
||||
{" CHunk siZE 55 ", 55},
|
||||
{"chunk 10", 10},
|
||||
{" chuNK 15", 15},
|
||||
{"chunk size -60", 0},
|
||||
{"chunk size 10", 10},
|
||||
{"chunk size 0", 0},
|
||||
{"chunk size 10", 10},
|
||||
{"chunk size junk", 10},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if err := c.ParseCommand(test.command); err != nil {
|
||||
t.Logf("command: %q", test.command)
|
||||
t.Fatalf("setting chunked failed: err: %s", err)
|
||||
}
|
||||
|
||||
if got, exp := c.ChunkSize, test.exp; got != exp {
|
||||
t.Logf("command: %q", test.command)
|
||||
t.Fatalf("unexpected chunk size. got %d, exp %d", got, exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.ClientConfig.WriteConsistency != consistency {
|
||||
t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency)
|
||||
}
|
||||
|
||||
// set different valid write consistency and validate change
|
||||
consistency = "quorum"
|
||||
c.SetWriteConsistency("consistency " + consistency)
|
||||
if c.ClientConfig.WriteConsistency != consistency {
|
||||
t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency)
|
||||
}
|
||||
|
||||
// set invalid write consistency and verify there was no change
|
||||
invalidConsistency := "invalid_consistency"
|
||||
c.SetWriteConsistency("consistency " + invalidConsistency)
|
||||
if c.ClientConfig.WriteConsistency == invalidConsistency {
|
||||
t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_CommandsExist(t *testing.T) {
|
||||
t.Parallel()
|
||||
c, err := client.NewClient(client.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error %v", err)
|
||||
}
|
||||
|
||||
m := cli.CommandLine{Client: c, Line: liner.NewLiner()}
|
||||
tests := []struct {
|
||||
cmd string
|
||||
}{
|
||||
{cmd: "gopher"},
|
||||
{cmd: "auth"},
|
||||
{cmd: "help"},
|
||||
{cmd: "format"},
|
||||
{cmd: "precision"},
|
||||
{cmd: "settings"},
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_Connect(t *testing.T) {
|
||||
t.Parallel()
|
||||
ts := emptyTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
u, _ := url.Parse(ts.URL)
|
||||
cmd := "connect " + u.Host
|
||||
c := cli.CommandLine{}
|
||||
|
||||
// assert connection is established
|
||||
if err := c.ParseCommand(cmd); err != nil {
|
||||
t.Fatalf("There was an error while connecting to %v: %v", u.Path, err)
|
||||
}
|
||||
|
||||
// assert server version is populated
|
||||
if c.ServerVersion != SERVER_VERSION {
|
||||
t.Fatalf("Server version is %s but should be %s.", c.ServerVersion, SERVER_VERSION)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_TogglePretty(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.CommandLine{}
|
||||
if c.Pretty {
|
||||
t.Fatalf(`Pretty should be false.`)
|
||||
}
|
||||
c.ParseCommand("pretty")
|
||||
if !c.Pretty {
|
||||
t.Fatalf(`Pretty should be true.`)
|
||||
}
|
||||
c.ParseCommand("pretty")
|
||||
if c.Pretty {
|
||||
t.Fatalf(`Pretty should be false.`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_Exit(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
cmd string
|
||||
}{
|
||||
{cmd: "exit"},
|
||||
{cmd: " exit"},
|
||||
{cmd: "exit "},
|
||||
{cmd: "Exit "},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
c := cli.CommandLine{Quit: make(chan struct{}, 1)}
|
||||
c.ParseCommand(test.cmd)
|
||||
// channel should be closed
|
||||
if _, ok := <-c.Quit; ok {
|
||||
t.Fatalf(`Command "exit" failed for %q.`, test.cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_Quit(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
cmd string
|
||||
}{
|
||||
{cmd: "quit"},
|
||||
{cmd: " quit"},
|
||||
{cmd: "quit "},
|
||||
{cmd: "Quit "},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
c := cli.CommandLine{Quit: make(chan struct{}, 1)}
|
||||
c.ParseCommand(test.cmd)
|
||||
// channel should be closed
|
||||
if _, ok := <-c.Quit; ok {
|
||||
t.Fatalf(`Command "quit" failed for %q.`, test.cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
m := cli.CommandLine{Client: c}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_UseAuth(t *testing.T) {
|
||||
t.Parallel()
|
||||
ts := emptyTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
u, _ := url.Parse(ts.URL)
|
||||
tests := []struct {
|
||||
cmd string
|
||||
user string
|
||||
database string
|
||||
}{
|
||||
{
|
||||
cmd: "use db",
|
||||
user: "admin",
|
||||
database: "db",
|
||||
},
|
||||
{
|
||||
cmd: "use blank",
|
||||
user: "admin",
|
||||
database: "",
|
||||
},
|
||||
{
|
||||
cmd: "use db",
|
||||
user: "anonymous",
|
||||
database: "db",
|
||||
},
|
||||
{
|
||||
cmd: "use blank",
|
||||
user: "anonymous",
|
||||
database: "blank",
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
config := client.Config{URL: *u, Username: tt.user}
|
||||
fmt.Println("using auth:", tt.user)
|
||||
c, err := client.NewClient(config)
|
||||
if err != nil {
|
||||
t.Errorf("%d. unexpected error. expected %v, actual %v", i, nil, err)
|
||||
continue
|
||||
}
|
||||
m := cli.CommandLine{Client: c}
|
||||
m.ClientConfig.Username = tt.user
|
||||
|
||||
if err := m.ParseCommand(tt.cmd); err != nil {
|
||||
t.Fatalf(`%d. Got error %v for command %q, expected nil.`, i, err, tt.cmd)
|
||||
}
|
||||
|
||||
if m.Database != tt.database {
|
||||
t.Fatalf(`%d. Command "use" changed database to %q. Expected %q`, i, m.Database, tt.database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_Consistency(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.CommandLine{}
|
||||
tests := []struct {
|
||||
cmd string
|
||||
}{
|
||||
{cmd: "consistency one"},
|
||||
{cmd: " consistency one"},
|
||||
{cmd: "consistency one "},
|
||||
{cmd: "consistency one;"},
|
||||
{cmd: "consistency one; "},
|
||||
{cmd: "Consistency one"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if err := c.ParseCommand(test.cmd); err != nil {
|
||||
t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
|
||||
}
|
||||
|
||||
if c.ClientConfig.WriteConsistency != "one" {
|
||||
t.Fatalf(`Command "consistency" changed consistency to %q. Expected one`, c.ClientConfig.WriteConsistency)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_History(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.CommandLine{Line: liner.NewLiner()}
|
||||
defer c.Line.Close()
|
||||
|
||||
// append one entry to history
|
||||
c.Line.AppendHistory("abc")
|
||||
|
||||
tests := []struct {
|
||||
cmd string
|
||||
}{
|
||||
{cmd: "history"},
|
||||
{cmd: " history"},
|
||||
{cmd: "history "},
|
||||
{cmd: "History "},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if err := c.ParseCommand(test.cmd); err != nil {
|
||||
t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// buf size should be at least 1
|
||||
var buf bytes.Buffer
|
||||
c.Line.WriteHistory(&buf)
|
||||
if buf.Len() < 1 {
|
||||
t.Fatal("History is borked")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommand_HistoryWithBlankCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := cli.CommandLine{Line: liner.NewLiner()}
|
||||
defer c.Line.Close()
|
||||
|
||||
// append one entry to history
|
||||
c.Line.AppendHistory("x")
|
||||
|
||||
tests := []struct {
|
||||
cmd string
|
||||
err error
|
||||
}{
|
||||
{cmd: "history"},
|
||||
{cmd: " history"},
|
||||
{cmd: "history "},
|
||||
{cmd: "", err: cli.ErrBlankCommand}, // shouldn't be persisted in history
|
||||
{cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history
|
||||
{cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history
|
||||
}
|
||||
|
||||
// a blank command will return cli.ErrBlankCommand.
|
||||
for _, test := range tests {
|
||||
if err := c.ParseCommand(test.cmd); err != test.err {
|
||||
t.Errorf(`Got error %v for command %q, expected %v`, err, test.cmd, test.err)
|
||||
}
|
||||
}
|
||||
|
||||
// buf shall not contain empty commands
|
||||
var buf bytes.Buffer
|
||||
c.Line.WriteHistory(&buf)
|
||||
scanner := bufio.NewScanner(&buf)
|
||||
for scanner.Scan() {
|
||||
if strings.TrimSpace(scanner.Text()) == "" {
|
||||
t.Fatal("Empty commands should not be persisted in history.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// helper methods
|
||||
|
||||
func emptyTestServer() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Influxdb-Version", SERVER_VERSION)
|
||||
|
||||
// Fake authorization entirely based on the username.
|
||||
authorized := false
|
||||
user, _, _ := r.BasicAuth()
|
||||
switch user {
|
||||
case "", "admin":
|
||||
authorized = true
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/query":
|
||||
values := r.URL.Query()
|
||||
parser := influxql.NewParser(bytes.NewBufferString(values.Get("q")))
|
||||
q, err := parser.ParseQuery()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
stmt := q.Statements[0]
|
||||
|
||||
switch stmt.(type) {
|
||||
case *influxql.ShowDatabasesStatement:
|
||||
if authorized {
|
||||
io.WriteString(w, `{"results":[{"series":[{"name":"databases","columns":["name"],"values":[["db"]]}]}]}`)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, fmt.Sprintf(`{"error":"error authorizing query: %s not authorized to execute statement 'SHOW DATABASES', requires admin privilege"}`, user))
|
||||
}
|
||||
case *influxql.ShowDiagnosticsStatement:
|
||||
io.WriteString(w, `{"results":[{}]}`)
|
||||
}
|
||||
case "/write":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}))
|
||||
}
|
34
vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go
generated
vendored
Normal file
34
vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func parseDatabaseAndRetentionPolicy(stmt []byte) (string, string, error) {
|
||||
var db, rp []byte
|
||||
var quoted bool
|
||||
var seperatorCount int
|
||||
|
||||
stmt = bytes.TrimSpace(stmt)
|
||||
|
||||
for _, b := range stmt {
|
||||
if b == '"' {
|
||||
quoted = !quoted
|
||||
continue
|
||||
}
|
||||
if b == '.' && !quoted {
|
||||
seperatorCount++
|
||||
if seperatorCount > 1 {
|
||||
return "", "", fmt.Errorf("unable to parse database and retention policy from %s", string(stmt))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if seperatorCount == 1 {
|
||||
rp = append(rp, b)
|
||||
continue
|
||||
}
|
||||
db = append(db, b)
|
||||
}
|
||||
return string(db), string(rp), nil
|
||||
}
|
90
vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go
generated
vendored
Normal file
90
vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_parseDatabaseAndretentionPolicy(t *testing.T) {
|
||||
tests := []struct {
|
||||
stmt string
|
||||
db string
|
||||
rp string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
stmt: `foo`,
|
||||
db: "foo",
|
||||
},
|
||||
{
|
||||
stmt: `"foo.bar"`,
|
||||
db: "foo.bar",
|
||||
},
|
||||
{
|
||||
stmt: `"foo.bar".`,
|
||||
db: "foo.bar",
|
||||
},
|
||||
{
|
||||
stmt: `."foo.bar"`,
|
||||
rp: "foo.bar",
|
||||
},
|
||||
{
|
||||
stmt: `foo.bar`,
|
||||
db: "foo",
|
||||
rp: "bar",
|
||||
},
|
||||
{
|
||||
stmt: `"foo".bar`,
|
||||
db: "foo",
|
||||
rp: "bar",
|
||||
},
|
||||
{
|
||||
stmt: `"foo"."bar"`,
|
||||
db: "foo",
|
||||
rp: "bar",
|
||||
},
|
||||
{
|
||||
stmt: `"foo.bin"."bar"`,
|
||||
db: "foo.bin",
|
||||
rp: "bar",
|
||||
},
|
||||
{
|
||||
stmt: `"foo.bin"."bar.baz...."`,
|
||||
db: "foo.bin",
|
||||
rp: "bar.baz....",
|
||||
},
|
||||
{
|
||||
stmt: ` "foo.bin"."bar.baz...." `,
|
||||
db: "foo.bin",
|
||||
rp: "bar.baz....",
|
||||
},
|
||||
|
||||
{
|
||||
stmt: `"foo.bin"."bar".boom`,
|
||||
err: errors.New("foo"),
|
||||
},
|
||||
{
|
||||
stmt: "foo.bar.",
|
||||
err: errors.New("foo"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
db, rp, err := parseDatabaseAndRetentionPolicy([]byte(test.stmt))
|
||||
if err != nil && test.err == nil {
|
||||
t.Errorf("unexpected error: got %s", err)
|
||||
continue
|
||||
}
|
||||
if test.err != nil && err == nil {
|
||||
t.Errorf("expected err: got: nil, exp: %s", test.err)
|
||||
continue
|
||||
}
|
||||
if db != test.db {
|
||||
t.Errorf("unexpected database: got: %s, exp: %s", db, test.db)
|
||||
}
|
||||
if rp != test.rp {
|
||||
t.Errorf("unexpected retention policy: got: %s, exp: %s", rp, test.rp)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
120
vendor/github.com/influxdata/influxdb/cmd/influx/main.go
generated
vendored
Normal file
120
vendor/github.com/influxdata/influxdb/cmd/influx/main.go
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
// The influx command is a CLI client to InfluxDB.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/influxdata/influxdb/client"
|
||||
"github.com/influxdata/influxdb/cmd/influx/cli"
|
||||
)
|
||||
|
||||
// These variables are populated via the Go linker.
|
||||
var (
|
||||
version string
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultFormat is the default format of the results when issuing queries
|
||||
defaultFormat = "column"
|
||||
|
||||
// defaultPrecision is the default timestamp format of the results when issuing queries
|
||||
defaultPrecision = "ns"
|
||||
|
||||
// defaultPPS is the default points per second that the import will throttle at
|
||||
// by default it's 0, which means it will not throttle
|
||||
defaultPPS = 0
|
||||
)
|
||||
|
||||
func init() {
|
||||
// If version is not set, make that clear.
|
||||
if version == "" {
|
||||
version = "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := cli.New(version)
|
||||
|
||||
fs := flag.NewFlagSet("InfluxDB shell version "+version, flag.ExitOnError)
|
||||
fs.StringVar(&c.Host, "host", client.DefaultHost, "Influxdb host to connect to.")
|
||||
fs.IntVar(&c.Port, "port", client.DefaultPort, "Influxdb port to connect to.")
|
||||
fs.StringVar(&c.ClientConfig.UnixSocket, "socket", "", "Influxdb unix socket to connect to.")
|
||||
fs.StringVar(&c.ClientConfig.Username, "username", "", "Username to connect to the server.")
|
||||
fs.StringVar(&c.ClientConfig.Password, "password", "", `Password to connect to the server. Leaving blank will prompt for password (--password="").`)
|
||||
fs.StringVar(&c.Database, "database", c.Database, "Database to connect to the server.")
|
||||
fs.BoolVar(&c.Ssl, "ssl", false, "Use https for connecting to cluster.")
|
||||
fs.BoolVar(&c.ClientConfig.UnsafeSsl, "unsafeSsl", false, "Set this when connecting to the cluster using https and not use SSL verification.")
|
||||
fs.StringVar(&c.Format, "format", defaultFormat, "Format specifies the format of the server responses: json, csv, or column.")
|
||||
fs.StringVar(&c.ClientConfig.Precision, "precision", defaultPrecision, "Precision specifies the format of the timestamp: rfc3339,h,m,s,ms,u or ns.")
|
||||
fs.StringVar(&c.ClientConfig.WriteConsistency, "consistency", "all", "Set write consistency level: any, one, quorum, or all.")
|
||||
fs.BoolVar(&c.Pretty, "pretty", false, "Turns on pretty print for the json format.")
|
||||
fs.StringVar(&c.Execute, "execute", c.Execute, "Execute command and quit.")
|
||||
fs.BoolVar(&c.ShowVersion, "version", false, "Displays the InfluxDB version.")
|
||||
fs.BoolVar(&c.Import, "import", false, "Import a previous database.")
|
||||
fs.IntVar(&c.ImporterConfig.PPS, "pps", defaultPPS, "How many points per second the import will allow. By default it is zero and will not throttle importing.")
|
||||
fs.StringVar(&c.ImporterConfig.Path, "path", "", "path to the file to import")
|
||||
fs.BoolVar(&c.ImporterConfig.Compressed, "compressed", false, "set to true if the import file is compressed")
|
||||
|
||||
// Define our own custom usage to print
|
||||
fs.Usage = func() {
|
||||
fmt.Println(`Usage of influx:
|
||||
-version
|
||||
Display the version and exit.
|
||||
-host 'host name'
|
||||
Host to connect to.
|
||||
-port 'port #'
|
||||
Port to connect to.
|
||||
-socket 'unix domain socket'
|
||||
Unix socket to connect to.
|
||||
-database 'database name'
|
||||
Database to connect to the server.
|
||||
-password 'password'
|
||||
Password to connect to the server. Leaving blank will prompt for password (--password '').
|
||||
-username 'username'
|
||||
Username to connect to the server.
|
||||
-ssl
|
||||
Use https for requests.
|
||||
-unsafeSsl
|
||||
Set this when connecting to the cluster using https and not use SSL verification.
|
||||
-execute 'command'
|
||||
Execute command and quit.
|
||||
-format 'json|csv|column'
|
||||
Format specifies the format of the server responses: json, csv, or column.
|
||||
-precision 'rfc3339|h|m|s|ms|u|ns'
|
||||
Precision specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns.
|
||||
-consistency 'any|one|quorum|all'
|
||||
Set write consistency level: any, one, quorum, or all
|
||||
-pretty
|
||||
Turns on pretty print for the json format.
|
||||
-import
|
||||
Import a previous database export from file
|
||||
-pps
|
||||
How many points per second the import will allow. By default it is zero and will not throttle importing.
|
||||
-path
|
||||
Path to file to import
|
||||
-compressed
|
||||
Set to true if the import file is compressed
|
||||
|
||||
Examples:
|
||||
|
||||
# Use influx in a non-interactive mode to query the database "metrics" and pretty print json:
|
||||
$ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty
|
||||
|
||||
# Connect to a specific database on startup and set database context:
|
||||
$ influx -database 'metrics' -host 'localhost' -port '8086'
|
||||
`)
|
||||
}
|
||||
fs.Parse(os.Args[1:])
|
||||
|
||||
if c.ShowVersion {
|
||||
c.Version()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err := c.Run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user