coredns/plugin/geoip/geoip_test.go
Sven Nebel 21f1207afe
Create geoip plugin (#4688)
* Create geoip plugin

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/README.md

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/README.md

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/README.md

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Move DBFILE bullet below example

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/README.md

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove plugin name test case

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove languages option

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update free database link

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove last language bits

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Use 127.0.0.1 as probing IP

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/geoip.go

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Update plugin/geoip/geoip.go

Co-authored-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Use relative path for fixtures dir

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Set names with default string zero value

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove unused db types

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove non city databases in testdata

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Remove create databases main

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Fix metadata label format test case

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Fix import path block

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* go fmt after changes

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Tidy up go.mod and go.sum

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

* Add plugin to CODEOWNERS

Signed-off-by: Sven Nebel <nebel.sven@gmail.com>

Co-authored-by: Miek Gieben <miek@miek.nl>
2021-07-14 09:25:30 +02:00

61 lines
1.8 KiB
Go

package geoip
import (
"context"
"fmt"
"testing"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
)
func TestMetadata(t *testing.T) {
tests := []struct {
dbPath string
label string
expectedValue string
}{
{cityDBPath, "geoip/city/name", "Cambridge"},
{cityDBPath, "geoip/country/code", "GB"},
{cityDBPath, "geoip/country/name", "United Kingdom"},
// is_in_european_union is set to true only to work around bool zero value, and test is really being set.
{cityDBPath, "geoip/country/is_in_european_union", "true"},
{cityDBPath, "geoip/continent/code", "EU"},
{cityDBPath, "geoip/continent/name", "Europe"},
{cityDBPath, "geoip/latitude", "52.2242"},
{cityDBPath, "geoip/longitude", "0.1315"},
{cityDBPath, "geoip/timezone", "Europe/London"},
{cityDBPath, "geoip/postalcode", "CB4"},
}
for i, _test := range tests {
geoIP, err := newGeoIP(_test.dbPath)
if err != nil {
t.Fatalf("Test %d: unable to create geoIP plugin: %v", i, err)
}
state := request.Request{
W: &test.ResponseWriter{RemoteIP: "81.2.69.142"}, // This IP should be be part of the CDIR address range used to create the database fixtures.
}
ctx := metadata.ContextWithMetadata(context.Background())
rCtx := geoIP.Metadata(ctx, state)
if fmt.Sprintf("%p", ctx) != fmt.Sprintf("%p", rCtx) {
t.Errorf("Test %d: returned context is expected to be the same one passed in the Metadata function", i)
}
fn := metadata.ValueFunc(ctx, _test.label)
if fn == nil {
t.Errorf("Test %d: label %q not set in metadata plugin context", i, _test.label)
continue
}
value := fn()
if value != _test.expectedValue {
t.Errorf("Test %d: expected value for label %q should be %q, got %q instead",
i, _test.label, _test.expectedValue, value)
}
}
}