feat(influxdb): Add test around CLI proxy path
parent
6a621bf28b
commit
c2c864d611
|
@ -65,13 +65,13 @@ type Query struct {
|
|||
NodeID int
|
||||
}
|
||||
|
||||
func splitPath(v string) (string, string) {
|
||||
parts := strings.Split(v, "/")
|
||||
|
||||
first := parts[0]
|
||||
last := strings.Join(parts[1:], "/")
|
||||
|
||||
return first, last
|
||||
// SplitPath gets the path of a url
|
||||
func SplitPath(v string) (string, string) {
|
||||
i := strings.Index(v, "/")
|
||||
if i == -1 {
|
||||
return v, ""
|
||||
}
|
||||
return v[:i] /* first */, v[i+1:] /* rest */
|
||||
}
|
||||
|
||||
// ParseConnectionString will parse a string to create a valid connection URL
|
||||
|
@ -85,13 +85,13 @@ func ParseConnectionString(path string, ssl bool) (url.URL, error) {
|
|||
if path == "" {
|
||||
host = DefaultHost
|
||||
} else {
|
||||
host, pth = splitPath(path)
|
||||
host, pth = SplitPath(path)
|
||||
}
|
||||
// If they didn't specify a port, always use the default port
|
||||
port = DefaultPort
|
||||
} else {
|
||||
host = h
|
||||
prt, pt := splitPath(p)
|
||||
prt, pt := SplitPath(p)
|
||||
pth = pt
|
||||
|
||||
port, err = strconv.Atoi(prt)
|
||||
|
|
|
@ -819,6 +819,10 @@ func TestClient_ParseConnectionString(t *testing.T) {
|
|||
addr: "192.168.2.13:8086/boom",
|
||||
exp: "http://192.168.2.13:8086/boom",
|
||||
},
|
||||
{
|
||||
addr: "",
|
||||
exp: "http://localhost:8086",
|
||||
},
|
||||
} {
|
||||
name := tt.addr
|
||||
if tt.ssl {
|
||||
|
@ -1024,3 +1028,49 @@ func TestClient_Proxy(t *testing.T) {
|
|||
t.Fatalf("no http request was received")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPath(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
input string
|
||||
expectedfirst string
|
||||
expectedrest string
|
||||
}{
|
||||
"empty": {
|
||||
input: "",
|
||||
expectedfirst: "",
|
||||
expectedrest: "",
|
||||
},
|
||||
"noslash": {
|
||||
input: "foo",
|
||||
expectedfirst: "foo",
|
||||
expectedrest: "",
|
||||
},
|
||||
"ideal": {
|
||||
input: "foo/bar/baz/cuux:8080",
|
||||
expectedfirst: "foo",
|
||||
expectedrest: "bar/baz/cuux:8080",
|
||||
},
|
||||
"trailingslash": {
|
||||
input: "foo/",
|
||||
expectedfirst: "foo",
|
||||
expectedrest: "",
|
||||
},
|
||||
"onlyslash": {
|
||||
input: "/",
|
||||
expectedfirst: "",
|
||||
expectedrest: "",
|
||||
},
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
for name, test := range tests {
|
||||
name, test := name, test
|
||||
t.Run(name, func(t *testing.T) {
|
||||
first, rest := client.SplitPath(test.input)
|
||||
|
||||
if gotfirst, gotrest, expectedfirst, expectedrest := first, rest, test.expectedfirst, test.expectedrest; gotfirst != expectedfirst || gotrest != expectedrest {
|
||||
t.Fatalf("splitPath(%q) returned (%q, %q); expected (%q, %q)", test.input, gotfirst, gotrest, expectedfirst, expectedrest)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ type CommandLine struct {
|
|||
URL url.URL
|
||||
Host string
|
||||
Port int
|
||||
Path string
|
||||
PathPrefix string
|
||||
Database string
|
||||
Type QueryLanguage
|
||||
Ssl bool
|
||||
|
@ -116,7 +116,7 @@ func (c *CommandLine) Run() error {
|
|||
c.ClientConfig.Password = os.Getenv("INFLUX_PASSWORD")
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d/%s", c.Host, c.Port, c.Path)
|
||||
addr := fmt.Sprintf("%s:%d/%s", c.Host, c.Port, c.PathPrefix)
|
||||
url, err := client.ParseConnectionString(addr, c.Ssl)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -56,6 +56,27 @@ func TestRunCLI_ExecuteInsert(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRunCLI_ExecuteInsertWithPath(t *testing.T) {
|
||||
path := "boom"
|
||||
t.Parallel()
|
||||
ts := emptyTestServerWithPath(path)
|
||||
defer ts.Close()
|
||||
|
||||
u, _ := url.Parse(ts.URL)
|
||||
h, p, _ := net.SplitHostPort(u.Host)
|
||||
c := cli.New(CLIENT_VERSION)
|
||||
c.Host = h
|
||||
c.PathPrefix = path
|
||||
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 TestRunCLI_ExecuteInsert_WithSignals(t *testing.T) {
|
||||
t.Parallel()
|
||||
ts := emptyTestServer()
|
||||
|
@ -591,9 +612,7 @@ func TestParseCommand_HistoryWithBlankCommand(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// helper methods
|
||||
|
||||
func emptyTestServer() *httptest.Server {
|
||||
func emptyTestServerWithPath(path string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Influxdb-Version", SERVER_VERSION)
|
||||
|
||||
|
@ -605,13 +624,15 @@ func emptyTestServer() *httptest.Server {
|
|||
authorized = true
|
||||
}
|
||||
|
||||
queryPath := path + "/query"
|
||||
writePath := path + "/write"
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/query":
|
||||
case queryPath:
|
||||
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]
|
||||
|
@ -627,8 +648,14 @@ func emptyTestServer() *httptest.Server {
|
|||
case *influxql.ShowDiagnosticsStatement:
|
||||
io.WriteString(w, `{"results":[{}]}`)
|
||||
}
|
||||
case "/write":
|
||||
case writePath:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// helper methods
|
||||
|
||||
func emptyTestServer() *httptest.Server {
|
||||
return emptyTestServerWithPath("")
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ func main() {
|
|||
|
||||
fs := flag.NewFlagSet("InfluxDB shell version "+version, flag.ExitOnError)
|
||||
fs.StringVar(&c.Host, "host", client.DefaultHost, "Influxdb host to connect to.")
|
||||
fs.StringVar(&c.Path, "proxy-path", client.DefaultPath, "Influxdb url path (for running behind proxies)")
|
||||
fs.StringVar(&c.PathPrefix, "path-prefix", client.DefaultPath, "Influxdb url path prefix (for running behind proxies)")
|
||||
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.")
|
||||
|
@ -66,7 +66,7 @@ func main() {
|
|||
fmt.Println(`Usage of influx:
|
||||
-version
|
||||
Display the version and exit.
|
||||
-proxy-path 'url path'
|
||||
-path-prefix 'url path'
|
||||
Path that follows the host and port
|
||||
-host 'host name'
|
||||
Host to connect to.
|
||||
|
|
Loading…
Reference in New Issue