浏览代码

test(cli): add initial cli tests

Mustafa Arici 8 年之前
父节点
当前提交
380b6c98de
共有 4 个文件被更改,包括 277 次插入0 次删除
  1. 54 0
      cmd/ovpm/main_test.go
  2. 145 0
      cmd/ovpm/net_test.go
  3. 43 0
      cmd/ovpm/user_test.go
  4. 35 0
      cmd/ovpm/vpn_test.go

+ 54 - 0
cmd/ovpm/main_test.go

@@ -0,0 +1,54 @@
+package main
+
+import (
+	"bytes"
+	"strings"
+	"testing"
+
+	"github.com/cad/ovpm"
+)
+
+func TestMainCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	err := app.Run([]string{"ovpm"})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if !strings.Contains(output.String(), ovpm.Version) {
+		t.Fatal("version is missing")
+	}
+
+	if !strings.Contains(output.String(), "user, u") {
+		t.Fatal("subcommand missing 'user'")
+	}
+
+	if !strings.Contains(output.String(), "vpn, v") {
+		t.Fatal("subcommand missing 'vpn'")
+	}
+
+	if !strings.Contains(output.String(), "net, n") {
+		t.Fatal("subcommand missing 'net'")
+	}
+
+	if !strings.Contains(output.String(), "help, h") {
+		t.Fatal("subcommand missing 'help'")
+	}
+
+	if !strings.Contains(output.String(), "--daemon-port") {
+		t.Fatal("flag missing '--daemon-port'")
+	}
+
+	if !strings.Contains(output.String(), "--verbose") {
+		t.Fatal("flag missing '--verbose'")
+	}
+
+	if !strings.Contains(output.String(), "--version") {
+		t.Fatal("flag missing '--version'")
+	}
+
+}

+ 145 - 0
cmd/ovpm/net_test.go

@@ -0,0 +1,145 @@
+package main
+
+import (
+	"bytes"
+	"strings"
+	"testing"
+)
+
+func TestNetCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	err := app.Run([]string{"ovpm", "net"})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if !strings.Contains(output.String(), "list, l") {
+		t.Fatal("subcommand missing 'list, l'")
+	}
+
+	if !strings.Contains(output.String(), "types, t") {
+		t.Fatal("subcommand missing 'types, t'")
+	}
+
+	if !strings.Contains(output.String(), "def, d") {
+		t.Fatal("subcommand missing 'undef, u'")
+	}
+
+	if !strings.Contains(output.String(), "assoc, a") {
+		t.Fatal("subcommand missing 'assoc, a'")
+	}
+
+	if !strings.Contains(output.String(), "dissoc, di") {
+		t.Fatal("subcommand missing 'dissoc, di'")
+	}
+}
+
+func TestNetDefineCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	var err error
+
+	// Empty call
+	err = app.Run([]string{"ovpm", "net", "def"})
+	if err == nil {
+		t.Fatal("error is expected about missing fields, but we didn't got error")
+	}
+
+	// Missing type
+	err = app.Run([]string{"ovpm", "net", "def", "--cidr", "192.168.1.1/24"})
+	if err == nil {
+		t.Fatal("error is expected about missing network type, but we didn't got error")
+	}
+	// Missing name
+	err = app.Run([]string{"ovpm", "net", "def", "--type", "SERVERNET", "--cidr", "192.168.1.1/24"})
+	if err == nil {
+		t.Fatal("error is expected about missing network name, but we didn't got error")
+	}
+
+	// Incorrect type
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNUT", "--cidr", "192.168.1.1/24"})
+	if err == nil {
+		t.Fatal("error is expected about incorrect server type, but we didn't got error")
+	}
+
+	// Incorrect use of via
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1/24", "--via", "8.8.8.8"})
+	if err == nil {
+		t.Fatal("error is expected about incorrect use of via, but we didn't got error")
+	}
+
+	// Incorrect cidr format
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1"})
+	if err == nil {
+		t.Fatal("error is expected about incorrect cidr format, but we didn't got error")
+	}
+
+	// Ensure ROUTE type use without --via
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "ROUTE", "--cidr", "192.168.1.1/24"})
+	if err != nil && !strings.Contains(err.Error(), "grpc") {
+		t.Fatalf("error is not expected: %v", err)
+	}
+
+	// Incorrect use of via
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd", "--type", "SERVERNET", "--cidr", "192.168.1.1/24", "--via", "8.8.8.8/24"})
+	if err == nil {
+		t.Fatal("error is expected about incorrect via format, but we didn't got error")
+	}
+
+	// Ensure network name alphanumeric and dot, dash, underscore chars are allowed
+	err = app.Run([]string{"ovpm", "net", "def", "--name", "asd.asd-d5sa_fasA32", "--type", "ROUTE", "--cidr", "192.168.1.1/24"})
+	if err != nil && !strings.Contains(err.Error(), "grpc") {
+		t.Fatalf("error is not expected: %v", err)
+	}
+
+}
+
+func TestNetUnDefineCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	var err error
+
+	// Empty call
+	err = app.Run([]string{"ovpm", "net", "undef"})
+	if err == nil {
+		t.Fatal("error is expected about missing fields, but we didn't got error")
+	}
+
+}
+
+func TestAssocCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	var err error
+
+	// Empty call
+	err = app.Run([]string{"ovpm", "net", "assoc"})
+	if err == nil {
+		t.Fatal("error is expected about missing fields, but we didn't got error")
+	}
+
+	// Missing network name
+	err = app.Run([]string{"ovpm", "net", "def", "--user", "asd"})
+	if err == nil {
+		t.Fatal("error is expected about missing network name, but we didn't got error")
+	}
+
+	// Missing username
+	err = app.Run([]string{"ovpm", "net", "def", "--network", "asddsa"})
+	if err == nil {
+		t.Fatal("error is expected about missing username, but we didn't got error")
+	}
+}

+ 43 - 0
cmd/ovpm/user_test.go

@@ -0,0 +1,43 @@
+package main
+
+import (
+	"bytes"
+	"strings"
+	"testing"
+)
+
+func TestUserCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	err := app.Run([]string{"ovpm", "user"})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if !strings.Contains(output.String(), "list, l") {
+		t.Fatal("subcommand missing 'list, l'")
+	}
+
+	if !strings.Contains(output.String(), "create, c") {
+		t.Fatal("subcommand missing 'create, c'")
+	}
+
+	if !strings.Contains(output.String(), "update, u") {
+		t.Fatal("subcommand missing 'update, u'")
+	}
+
+	if !strings.Contains(output.String(), "delete, d") {
+		t.Fatal("subcommand missing 'delete, d'")
+	}
+
+	if !strings.Contains(output.String(), "renew, r") {
+		t.Fatal("subcommand missing 'renew, r'")
+	}
+
+	if !strings.Contains(output.String(), "genconfig, g") {
+		t.Fatal("subcommand missing 'update, u'")
+	}
+}

+ 35 - 0
cmd/ovpm/vpn_test.go

@@ -0,0 +1,35 @@
+package main
+
+import (
+	"bytes"
+	"strings"
+	"testing"
+)
+
+func TestVPNCmd(t *testing.T) {
+	app := NewApp()
+
+	output := new(bytes.Buffer)
+	app.Writer = output
+
+	err := app.Run([]string{"ovpm", "vpn"})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if !strings.Contains(output.String(), "status, s") {
+		t.Fatal("subcommand missing 'status, s'")
+	}
+
+	if !strings.Contains(output.String(), "init, i") {
+		t.Fatal("subcommand missing 'init, i'")
+	}
+
+	if !strings.Contains(output.String(), "update, u") {
+		t.Fatal("subcommand missing 'update, u'")
+	}
+
+	if !strings.Contains(output.String(), "restart, r") {
+		t.Fatal("subcommand missing 'restart, r'")
+	}
+}