lot of refactoring to create integration tests for cli
parent
fa103d9400
commit
33f976a340
|
@ -92,7 +92,7 @@ func main() {
|
|||
}
|
||||
|
||||
if c.Execute != "" {
|
||||
if err := c.executeQuery(c.Execute); err != nil {
|
||||
if err := c.ExecuteQuery(c.Execute); err != nil {
|
||||
os.Exit(1)
|
||||
} else {
|
||||
os.Exit(0)
|
||||
|
@ -140,13 +140,13 @@ func (c *CommandLine) ParseCommand(cmd string) bool {
|
|||
// signal the program to exit
|
||||
return false
|
||||
case strings.HasPrefix(lcmd, "gopher"):
|
||||
gopher()
|
||||
c.gopher()
|
||||
case strings.HasPrefix(lcmd, "connect"):
|
||||
c.connect(cmd)
|
||||
case strings.HasPrefix(lcmd, "auth"):
|
||||
c.SetAuth()
|
||||
c.SetAuth(cmd)
|
||||
case strings.HasPrefix(lcmd, "help"):
|
||||
help()
|
||||
c.help()
|
||||
case strings.HasPrefix(lcmd, "format"):
|
||||
c.SetFormat(cmd)
|
||||
case strings.HasPrefix(lcmd, "settings"):
|
||||
|
@ -163,7 +163,7 @@ func (c *CommandLine) ParseCommand(cmd string) bool {
|
|||
case lcmd == "":
|
||||
break
|
||||
default:
|
||||
c.executeQuery(cmd)
|
||||
c.ExecuteQuery(cmd)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -227,19 +227,33 @@ func (c *CommandLine) connect(cmd string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *CommandLine) SetAuth() {
|
||||
u, e := c.Line.Prompt("username: ")
|
||||
if e != nil {
|
||||
fmt.Printf("Unable to process input: %s", e)
|
||||
return
|
||||
func (c *CommandLine) SetAuth(cmd string) {
|
||||
// If they pass in the entire command, we should parse it
|
||||
// auth <username> <password>
|
||||
args := strings.Fields(cmd)
|
||||
if len(args) == 3 {
|
||||
args = args[1:]
|
||||
} else {
|
||||
args = []string{}
|
||||
}
|
||||
c.Username = strings.TrimSpace(u)
|
||||
p, e := c.Line.PasswordPrompt("password: ")
|
||||
if e != nil {
|
||||
fmt.Printf("Unable to process input: %s", e)
|
||||
return
|
||||
|
||||
if len(args) == 2 {
|
||||
c.Username = args[0]
|
||||
c.Password = args[1]
|
||||
} else {
|
||||
u, e := c.Line.Prompt("username: ")
|
||||
if e != nil {
|
||||
fmt.Printf("Unable to process input: %s", e)
|
||||
return
|
||||
}
|
||||
c.Username = strings.TrimSpace(u)
|
||||
p, e := c.Line.PasswordPrompt("password: ")
|
||||
if e != nil {
|
||||
fmt.Printf("Unable to process input: %s", e)
|
||||
return
|
||||
}
|
||||
c.Password = p
|
||||
}
|
||||
c.Password = p
|
||||
|
||||
// Update the client as well
|
||||
c.Client.SetAuth(c.Username, c.Password)
|
||||
|
@ -286,7 +300,7 @@ func (c *CommandLine) dump() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandLine) executeQuery(query string) error {
|
||||
func (c *CommandLine) ExecuteQuery(query string) error {
|
||||
response, err := c.Client.Query(client.Query{Command: query, Database: c.Database})
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %s\n", err)
|
||||
|
@ -473,7 +487,7 @@ func (c *CommandLine) Settings() {
|
|||
w.Flush()
|
||||
}
|
||||
|
||||
func help() {
|
||||
func (c *CommandLine) help() {
|
||||
fmt.Println(`Usage:
|
||||
connect <host:port> connect to another node
|
||||
auth prompt for username and password
|
||||
|
@ -494,7 +508,7 @@ func help() {
|
|||
`)
|
||||
}
|
||||
|
||||
func gopher() {
|
||||
func (c *CommandLine) gopher() {
|
||||
fmt.Println(`
|
||||
.-::-::://:-::- .:/++/'
|
||||
'://:-''/oo+//++o+/.://o- ./+:
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
package main_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdb/influxdb/client"
|
||||
main "github.com/influxdb/influxdb/cmd/influx"
|
||||
influxd "github.com/influxdb/influxdb/cmd/influxd"
|
||||
)
|
||||
|
||||
func TestParseCommand_CommandsExist(t *testing.T) {
|
||||
|
@ -75,3 +82,161 @@ func TestParseCommand_Use(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuery_NoAuth(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping TestQuery_NoAuth")
|
||||
}
|
||||
|
||||
// Create root path to server.
|
||||
// Remove it to clean up past failed panics
|
||||
// Defer it to clean up for successful tests
|
||||
path := tempfile()
|
||||
os.Remove(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
config, _ := influxd.NewTestConfig()
|
||||
|
||||
// Start server.
|
||||
node, err := newNode(config, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
c := main.CommandLine{
|
||||
Format: "column",
|
||||
}
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: fmt.Sprintf("%s:%d", "localhost", 8086),
|
||||
}
|
||||
cl, err := client.NewClient(
|
||||
client.Config{
|
||||
URL: u,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
c.Client = cl
|
||||
|
||||
ok := c.ParseCommand("CREATE USER admin WITH PASSWORD 'password'")
|
||||
if !ok {
|
||||
t.Fatal("Failed to create user")
|
||||
}
|
||||
node.Close()
|
||||
}
|
||||
|
||||
func TestQuery_Auth(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping TestQuery_Auth")
|
||||
}
|
||||
|
||||
// Create root path to server.
|
||||
// Remove it to clean up past failed panics
|
||||
// Defer it to clean up for successful tests
|
||||
path := tempfile()
|
||||
os.Remove(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
// Create the cli
|
||||
c := main.CommandLine{Format: "column"}
|
||||
cl, err := client.NewClient(
|
||||
client.Config{
|
||||
URL: url.URL{
|
||||
Scheme: "http",
|
||||
Host: fmt.Sprintf("%s:%d", "localhost", 8086),
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
c.Client = cl
|
||||
|
||||
// spin up a server
|
||||
config, _ := influxd.NewTestConfig()
|
||||
config.Authentication.Enabled = true
|
||||
node, err := newNode(config, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Check to make sure we can't do anything
|
||||
err = c.ExecuteQuery("CREATE USER admin WITH PASSWORD 'password'")
|
||||
if err == nil {
|
||||
t.Fatal("Should have failed to create user")
|
||||
}
|
||||
|
||||
// spin down server
|
||||
node.Close()
|
||||
|
||||
// disable auth and spin back up
|
||||
config.Authentication.Enabled = false
|
||||
node, err = newNode(config, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// create the user
|
||||
err = c.ExecuteQuery("CREATE USER admin WITH PASSWORD 'password'")
|
||||
if err != nil {
|
||||
t.Fatalf("Should have created user: %s\n", err)
|
||||
}
|
||||
// Make cluster admin
|
||||
err = c.ExecuteQuery("GRANT ALL PRIVILEGES TO admin")
|
||||
if err != nil {
|
||||
t.Fatalf("Should have made cluster admin: %s\n", err)
|
||||
}
|
||||
|
||||
// spin down again
|
||||
node.Close()
|
||||
|
||||
// enable auth, spin back up
|
||||
config.Authentication.Enabled = true
|
||||
node, err = newNode(config, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Check to make sure we still can't do anything
|
||||
err = c.ExecuteQuery("CREATE USER admin WITH PASSWORD 'password'")
|
||||
if err == nil {
|
||||
t.Fatal("Should have failed to create user")
|
||||
}
|
||||
|
||||
c.SetAuth("auth admin password")
|
||||
|
||||
// Check to make sure we can't do anything
|
||||
err = c.ExecuteQuery("CREATE DATABASE foo")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %s\n", err)
|
||||
}
|
||||
node.Close()
|
||||
}
|
||||
|
||||
// tempfile returns a temporary path.
|
||||
func tempfile() string {
|
||||
f, _ := ioutil.TempFile("", "influxdb-")
|
||||
path := f.Name()
|
||||
f.Close()
|
||||
os.Remove(path)
|
||||
return path
|
||||
}
|
||||
|
||||
func newNode(config *influxd.Config, path string) (*influxd.Node, error) {
|
||||
config.Broker.Dir = filepath.Join(path, "broker")
|
||||
config.Data.Dir = filepath.Join(path, "data")
|
||||
|
||||
// Start server.
|
||||
cmd := influxd.NewRunCommand()
|
||||
|
||||
node := cmd.Open(config, "")
|
||||
if node.Broker == nil {
|
||||
return nil, fmt.Errorf("cannot run broker")
|
||||
} else if node.DataNode == nil {
|
||||
return nil, fmt.Errorf("cannot run server")
|
||||
}
|
||||
return node, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue