Move logging, updating and testing utils into common directory
This commit is contained in:
1 parent
5da2219dc8
commit
82b54aabcd
8 files changed
+69
-52
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
package nameserver
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
@@ -32,6 +32,10 @@ func InitLogging(debugHandle io.Writer,
|
|||||||
Error = log.New(errorHandle, "ERROR: ", standardLogFlags)
|
Error = log.New(errorHandle, "ERROR: ", standardLogFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func InitDefaultLogging(debug bool) {
|
||||||
InitLogging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
|
debugOut := ioutil.Discard
|
||||||
|
if debug {
|
||||||
|
debugOut = os.Stderr
|
||||||
|
}
|
||||||
|
InitLogging(debugOut, os.Stdout, os.Stdout, os.Stderr)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CallSite() string {
|
||||||
|
_, file, line, ok := runtime.Caller(2)
|
||||||
|
if ok {
|
||||||
|
// Truncate file name at last file name separator.
|
||||||
|
if index := strings.LastIndex(file, "/"); index >= 0 {
|
||||||
|
file = file[index+1:]
|
||||||
|
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
||||||
|
file = file[index+1:]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
file = "???"
|
||||||
|
line = 1
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%d: ", file, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertNoErr(t *testing.T, err error) {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(CallSite(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertStatus(t *testing.T, got int, wanted int, desc string) {
|
||||||
|
if got != wanted {
|
||||||
|
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertErrorInterface(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||||
|
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
||||||
|
if !gotT.Implements(wantedT) {
|
||||||
|
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertErrorType(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||||
|
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
||||||
|
if gotT != wantedT {
|
||||||
|
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
. "github.com/zettio/weave/common"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package nameserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
. "github.com/zettio/weave/common"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package nameserver
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
. "github.com/zettio/weave/common"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
package nameserver
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func assertNoErr(t *testing.T, err error) {
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertStatus(t *testing.T, got int, wanted int, desc string) {
|
|
||||||
if got != wanted {
|
|
||||||
t.Fatalf("Expected %s %d but got %d", desc, wanted, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertErrorInterface(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
|
||||||
if got == nil {
|
|
||||||
t.Fatalf("Expected %s but got nil (%s)", reflect.TypeOf(wanted).Elem(), desc)
|
|
||||||
}
|
|
||||||
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
|
||||||
if !gotT.Implements(wantedT) {
|
|
||||||
t.Fatalf("Expected %s but got %s (%s)", wantedT, gotT, desc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertErrorType(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
|
||||||
if got == nil {
|
|
||||||
t.Fatalf("Expected %s but got nil (%s)", reflect.TypeOf(wanted).Elem(), desc)
|
|
||||||
}
|
|
||||||
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
|
||||||
if gotT != wantedT {
|
|
||||||
t.Fatalf("Expected %s but got %s (%s)", wantedT, gotT, desc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@ package nameserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
. "github.com/zettio/weave/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkError(err error, apiPath string) {
|
func checkError(err error, apiPath string) {
|
||||||
|
|||||||
+7
-11
@@ -3,10 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
. "github.com/zettio/weave/common"
|
||||||
weavedns "github.com/zettio/weave/nameserver"
|
weavedns "github.com/zettio/weave/nameserver"
|
||||||
weavenet "github.com/zettio/weave/net"
|
weavenet "github.com/zettio/weave/net"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -40,35 +40,31 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
debugOut := ioutil.Discard
|
InitDefaultLogging(debug)
|
||||||
if debug {
|
|
||||||
debugOut = os.Stderr
|
|
||||||
}
|
|
||||||
weavedns.InitLogging(debugOut, os.Stdout, os.Stdout, os.Stderr)
|
|
||||||
|
|
||||||
var zone = new(weavedns.ZoneDb)
|
var zone = new(weavedns.ZoneDb)
|
||||||
|
|
||||||
if watch {
|
if watch {
|
||||||
err := weavedns.StartUpdater(apiPath, zone)
|
err := weavedns.StartUpdater(apiPath, zone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
weavedns.Error.Fatal("Unable to start watcher", err)
|
Error.Fatal("Unable to start watcher", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var iface *net.Interface = nil
|
var iface *net.Interface = nil
|
||||||
if ifaceName != "" {
|
if ifaceName != "" {
|
||||||
var err error
|
var err error
|
||||||
weavedns.Info.Println("Waiting for interface", ifaceName, "to come up")
|
Info.Println("Waiting for interface", ifaceName, "to come up")
|
||||||
iface, err = weavenet.EnsureInterface(ifaceName, wait)
|
iface, err = weavenet.EnsureInterface(ifaceName, wait)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
weavedns.Error.Fatal(err)
|
Error.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
weavedns.Info.Println("Interface", ifaceName, "is up")
|
Info.Println("Interface", ifaceName, "is up")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := weavedns.StartServer(zone, iface, dnsPort, httpPort, wait)
|
err := weavedns.StartServer(zone, iface, dnsPort, httpPort, wait)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
weavedns.Error.Fatal("Failed to start server", err)
|
Error.Fatal("Failed to start server", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user