2022-03-18 14:11:14 +00:00
|
|
|
//go:build etcd
|
2016-04-10 18:50:11 +01:00
|
|
|
|
|
|
|
package test
|
|
|
|
|
2016-08-08 19:18:55 -07:00
|
|
|
import (
|
2018-06-01 15:12:49 +01:00
|
|
|
"context"
|
2016-08-08 19:18:55 -07:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin/etcd"
|
|
|
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
2016-08-08 19:18:55 -07:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2021-05-14 23:55:01 -07:00
|
|
|
etcdcv3 "go.etcd.io/etcd/client/v3"
|
2016-08-08 19:18:55 -07:00
|
|
|
)
|
|
|
|
|
2017-09-16 14:13:28 +01:00
|
|
|
func etcdPlugin() *etcd.Etcd {
|
2018-06-30 20:49:13 +05:30
|
|
|
etcdCfg := etcdcv3.Config{
|
2016-08-08 19:18:55 -07:00
|
|
|
Endpoints: []string{"http://localhost:2379"},
|
|
|
|
}
|
2018-06-30 20:49:13 +05:30
|
|
|
cli, _ := etcdcv3.New(etcdCfg)
|
|
|
|
return &etcd.Etcd{Client: cli, PathPrefix: "/skydns"}
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
2016-04-10 18:50:11 +01:00
|
|
|
|
2019-02-01 16:30:53 +01:00
|
|
|
func etcdPluginWithCredentials(username, password string) *etcd.Etcd {
|
|
|
|
etcdCfg := etcdcv3.Config{
|
|
|
|
Endpoints: []string{"http://localhost:2379"},
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
cli, _ := etcdcv3.New(etcdCfg)
|
|
|
|
return &etcd.Etcd{Client: cli, PathPrefix: "/skydns"}
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:50:11 +01:00
|
|
|
// This test starts two coredns servers (and needs etcd). Configure a stubzones in both (that will loop) and
|
|
|
|
// will then test if we detect this loop.
|
2017-01-12 08:13:50 +00:00
|
|
|
func TestEtcdStubLoop(t *testing.T) {
|
2016-04-10 18:50:11 +01:00
|
|
|
// TODO(miek)
|
|
|
|
}
|
2016-08-08 19:18:55 -07:00
|
|
|
|
|
|
|
func TestEtcdStubAndProxyLookup(t *testing.T) {
|
|
|
|
corefile := `.:0 {
|
2020-04-25 14:08:36 +08:00
|
|
|
etcd skydns.local {
|
|
|
|
stubzones
|
|
|
|
path /skydns
|
|
|
|
endpoint http://localhost:2379
|
|
|
|
upstream
|
|
|
|
fallthrough
|
|
|
|
}
|
|
|
|
forward . 8.8.8.8:53
|
|
|
|
}`
|
2016-08-08 19:18:55 -07:00
|
|
|
|
2017-08-24 11:35:14 +01:00
|
|
|
ex, udp, _, err := CoreDNSServerAndPorts(corefile)
|
2016-08-08 19:18:55 -07:00
|
|
|
if err != nil {
|
2016-10-02 08:31:44 +01:00
|
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
2016-08-19 17:14:17 -07:00
|
|
|
}
|
2016-08-08 19:18:55 -07:00
|
|
|
defer ex.Stop()
|
|
|
|
|
2017-09-16 14:13:28 +01:00
|
|
|
etc := etcdPlugin()
|
2016-08-08 19:18:55 -07:00
|
|
|
|
|
|
|
var ctx = context.TODO()
|
|
|
|
for _, serv := range servicesStub { // adds example.{net,org} as stubs
|
|
|
|
set(ctx, t, etc, serv.Key, 0, serv)
|
|
|
|
defer delete(ctx, t, etc, serv.Key)
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:54:49 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion("example.com.", dns.TypeA)
|
|
|
|
resp, err := dns.Exchange(m, udp)
|
2016-08-08 19:18:55 -07:00
|
|
|
if err != nil {
|
2017-01-29 12:06:26 -08:00
|
|
|
t.Fatalf("Expected to receive reply, but didn't: %v", err)
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
|
|
|
if len(resp.Answer) == 0 {
|
2017-01-15 08:12:58 +00:00
|
|
|
t.Fatalf("Expected to at least one RR in the answer section, got none")
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
|
|
|
if resp.Answer[0].Header().Rrtype != dns.TypeA {
|
2016-08-20 23:03:36 +01:00
|
|
|
t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
|
|
|
if resp.Answer[0].(*dns.A).A.String() != "93.184.216.34" {
|
2017-01-29 12:06:26 -08:00
|
|
|
t.Errorf("Expected 93.184.216.34, got: %s", resp.Answer[0].(*dns.A).A.String())
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var servicesStub = []*msg.Service{
|
|
|
|
// Two tests, ask a question that should return servfail because remote it no accessible
|
|
|
|
// and one with edns0 option added, that should return refused.
|
|
|
|
{Host: "127.0.0.1", Port: 666, Key: "b.example.org.stub.dns.skydns.test."},
|
|
|
|
// Actual test that goes out to the internet.
|
|
|
|
{Host: "199.43.132.53", Key: "a.example.net.stub.dns.skydns.test."},
|
|
|
|
}
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// Copied from plugin/etcd/setup_test.go
|
2016-08-08 19:18:55 -07:00
|
|
|
func set(ctx context.Context, t *testing.T, e *etcd.Etcd, k string, ttl time.Duration, m *msg.Service) {
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
path, _ := msg.PathWithWildcard(k, e.PathPrefix)
|
2018-06-30 20:49:13 +05:30
|
|
|
e.Client.KV.Put(ctx, path, string(b))
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// Copied from plugin/etcd/setup_test.go
|
2016-08-08 19:18:55 -07:00
|
|
|
func delete(ctx context.Context, t *testing.T, e *etcd.Etcd, k string) {
|
|
|
|
path, _ := msg.PathWithWildcard(k, e.PathPrefix)
|
2018-06-30 20:49:13 +05:30
|
|
|
e.Client.Delete(ctx, path)
|
2016-08-08 19:18:55 -07:00
|
|
|
}
|