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 (
|
||||
"io"
|
||||
@@ -32,6 +32,10 @@ func InitLogging(debugHandle io.Writer,
|
||||
Error = log.New(errorHandle, "ERROR: ", standardLogFlags)
|
||||
}
|
||||
|
||||
func init() {
|
||||
InitLogging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
|
||||
func InitDefaultLogging(debug bool) {
|
||||
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"
|
||||
"fmt"
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/zettio/weave/common"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
@@ -2,6 +2,7 @@ package nameserver
|
||||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/zettio/weave/common"
|
||||
"net"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package nameserver
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/zettio/weave/common"
|
||||
"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 (
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
. "github.com/zettio/weave/common"
|
||||
)
|
||||
|
||||
func checkError(err error, apiPath string) {
|
||||
|
||||
+7
-11
@@ -3,10 +3,10 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
. "github.com/zettio/weave/common"
|
||||
weavedns "github.com/zettio/weave/nameserver"
|
||||
weavenet "github.com/zettio/weave/net"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
@@ -40,35 +40,31 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
debugOut := ioutil.Discard
|
||||
if debug {
|
||||
debugOut = os.Stderr
|
||||
}
|
||||
weavedns.InitLogging(debugOut, os.Stdout, os.Stdout, os.Stderr)
|
||||
InitDefaultLogging(debug)
|
||||
|
||||
var zone = new(weavedns.ZoneDb)
|
||||
|
||||
if watch {
|
||||
err := weavedns.StartUpdater(apiPath, zone)
|
||||
if err != nil {
|
||||
weavedns.Error.Fatal("Unable to start watcher", err)
|
||||
Error.Fatal("Unable to start watcher", err)
|
||||
}
|
||||
}
|
||||
|
||||
var iface *net.Interface = nil
|
||||
if ifaceName != "" {
|
||||
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)
|
||||
if err != nil {
|
||||
weavedns.Error.Fatal(err)
|
||||
Error.Fatal(err)
|
||||
} else {
|
||||
weavedns.Info.Println("Interface", ifaceName, "is up")
|
||||
Info.Println("Interface", ifaceName, "is up")
|
||||
}
|
||||
}
|
||||
|
||||
err := weavedns.StartServer(zone, iface, dnsPort, httpPort, wait)
|
||||
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