Browse Source

test(vpn): add test case for emitToFile & improve

Mustafa Arici 8 years ago
parent
commit
fb0dc4ed1f
2 changed files with 36 additions and 2 deletions
  1. 6 2
      vpn.go
  2. 30 0
      vpn_test.go

+ 6 - 2
vpn.go

@@ -12,6 +12,8 @@ import (
 	"strings"
 	"text/template"
 
+	"time"
+
 	"github.com/Sirupsen/logrus"
 	"github.com/asaskevich/govalidator"
 	"github.com/cad/ovpm/bindata"
@@ -19,7 +21,6 @@ import (
 	"github.com/cad/ovpm/supervisor"
 	"github.com/google/uuid"
 	"github.com/jinzhu/gorm"
-	"time"
 )
 
 // DBNetwork is database model for external networks on the VPN server.
@@ -74,7 +75,7 @@ func Init(hostname string, port string) error {
 	}
 
 	if !govalidator.IsNumeric(port) {
-		return fmt.Errorf("validation error: port:`%s` should be numeric", hostname)
+		return fmt.Errorf("validation error: port:`%s` should be numeric", port)
 	}
 
 	serverName := "default"
@@ -591,6 +592,9 @@ func checkIptablesExecutable() bool {
 }
 
 func ensureBaseDir() {
+	if Testing {
+		return
+	}
 	os.Mkdir(varBasePath, 0755)
 }
 

+ 30 - 0
vpn_test.go

@@ -32,6 +32,12 @@ func TestVPNInit(t *testing.T) {
 		t.Fatalf("server is expected to be empty struct(new record) but it isn't %+v", server)
 	}
 
+	// Wrongfully initialize server.
+	err := Init("localhost", "asdf")
+	if err == nil {
+		t.Fatalf("error is expected to be not nil but it's nil instead")
+	}
+
 	// Initialize the server.
 	Init("localhost", "")
 
@@ -354,6 +360,30 @@ func TestVPNEmit(t *testing.T) {
 	// TODO(cad): Write test cases for ccd/ files as well.
 }
 
+func TestVPNemitToFile(t *testing.T) {
+	// Initialize:
+	// Prepare:
+	path := "/test/file"
+	content := "blah blah blah"
+
+	// Test:
+	// Is path exist?
+	if _, ok := fs[path]; ok {
+		t.Fatalf("key '%s' expected to be non-existent on fs, but it is instead", path)
+	}
+
+	// Emit the contents.
+	err := emitToFile(path, content, 0)
+	if err != nil {
+		t.Fatalf("expected  to be able to emit to the filesystem but we got this error instead: %v", err)
+	}
+
+	// Is the content on the filesystem correct?
+	if fs[path] != content {
+		t.Fatalf("content on the filesytem is expected to be same with '%s' but it's '%s' instead", content, fs[path])
+	}
+}
+
 type fakeProcess struct {
 	state supervisor.State
 }