From 9e5fe255a5c555070fda4e71f445e5eb42eed10c Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Tue, 28 Apr 2015 11:47:09 +0200 Subject: [PATCH] Improved mocks --- nameserver/mocks_test.go | 87 ++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/nameserver/mocks_test.go b/nameserver/mocks_test.go index abe84d54..b5fe7393 100644 --- a/nameserver/mocks_test.go +++ b/nameserver/mocks_test.go @@ -3,51 +3,86 @@ package nameserver import ( . "github.com/weaveworks/weave/common" "net" + "sync" +) + +const ( + testSocketTimeout = 100 // in millisecs ) // Warn about some methods that some day should be implemented... func notImplWarn() { Warning.Printf("Mocked method. Not implemented.") } -// A mocked Zone that always returns the same record -type mockedZone struct { - record ZoneRecord +// A mocked Zone that always returns the same records +// * it does not send/receive any mDNS query +type mockedZoneWithRecords struct { + sync.RWMutex + + records []ZoneRecord // Statistics NumLookupsName int NumLookupsInaddr int } -func NewMockedZone(zr ZoneRecord) *mockedZone { return &mockedZone{record: zr} } -func (mz mockedZone) Domain() string { return DefaultLocalDomain } -func (mz mockedZone) LookupName(name string) ([]ZoneRecord, error) { - Debug.Printf("[mocked zone]: LookupName: returning record %s", mz.record) - mz.NumLookupsName += 1 - return []ZoneRecord{mz.record}, nil +func newMockedZoneWithRecords(zr []ZoneRecord) *mockedZoneWithRecords { + return &mockedZoneWithRecords{records: zr} } -func (mz mockedZone) LookupInaddr(inaddr string) ([]ZoneRecord, error) { - Debug.Printf("[mocked zone]: LookupInaddr: returning record %s", mz.record) +func (mz mockedZoneWithRecords) Domain() string { return DefaultLocalDomain } +func (mz *mockedZoneWithRecords) LookupName(name string) ([]ZoneRecord, error) { + Debug.Printf("[mocked zone]: LookupName: returning records %s", mz.records) + mz.Lock() + defer mz.Unlock() + + mz.NumLookupsName += 1 + res := make([]ZoneRecord, 0) + for _, r := range mz.records { + if r.Name() == name { + res = append(res, r) + } + } + return res, nil +} + +func (mz *mockedZoneWithRecords) LookupInaddr(inaddr string) ([]ZoneRecord, error) { + Debug.Printf("[mocked zone]: LookupInaddr: returning records %s", mz.records) + mz.Lock() + defer mz.Unlock() + mz.NumLookupsInaddr += 1 - return []ZoneRecord{mz.record}, nil + res := make([]ZoneRecord, 0) + for _, r := range mz.records { + revIP, err := raddrToIP(inaddr) + if err != nil { + return nil, newParseError("lookup address", inaddr) + } + if r.IP().Equal(revIP) { + res = append(res, r) + } + } + return res, nil +} + +func (mz mockedZoneWithRecords) DomainLookupName(name string) ([]ZoneRecord, error) { + return mz.LookupName(name) +} +func (mz mockedZoneWithRecords) DomainLookupInaddr(inaddr string) ([]ZoneRecord, error) { + return mz.LookupInaddr(inaddr) } // the following methods are not currently needed... -func (mz mockedZone) Status() string { notImplWarn(); return "nothing" } -func (mz mockedZone) AddRecord(ident string, name string, ip net.IP) error { notImplWarn(); return nil } -func (mz mockedZone) DeleteRecord(ident string, ip net.IP) error { notImplWarn(); return nil } -func (mz mockedZone) DeleteRecordsFor(ident string) error { notImplWarn(); return nil } -func (mz mockedZone) DomainLookupName(name string) ([]ZoneRecord, error) { - notImplWarn() - return nil, nil -} -func (mz mockedZone) DomainLookupInaddr(inaddr string) ([]ZoneRecord, error) { - notImplWarn() - return nil, nil -} -func (mz mockedZone) ObserveName(name string, observer ZoneRecordObserver) error { +func (mz mockedZoneWithRecords) AddRecord(ident string, name string, ip net.IP) error { notImplWarn() return nil } -func (mz mockedZone) ObserveInaddr(inaddr string, observer ZoneRecordObserver) error { +func (mz mockedZoneWithRecords) DeleteRecord(ident string, ip net.IP) error { notImplWarn(); return nil } +func (mz mockedZoneWithRecords) DeleteRecordsFor(ident string) error { notImplWarn(); return nil } +func (mz mockedZoneWithRecords) Status() string { notImplWarn(); return "nothing" } +func (mz mockedZoneWithRecords) ObserveName(name string, observer ZoneRecordObserver) error { + notImplWarn() + return nil +} +func (mz mockedZoneWithRecords) ObserveInaddr(inaddr string, observer ZoneRecordObserver) error { notImplWarn() return nil }