Skip to content

Commit

Permalink
presubmit: check import path ordering (coredns#3636)
Browse files Browse the repository at this point in the history
Add a test for this as well as it's annoying to point out in every code
review.
Fix all the import paths that are flagged by this new test.

Fixes: coredns#3634

Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg authored Jan 30, 2020
1 parent 488464b commit 995179a
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 33 deletions.
4 changes: 1 addition & 3 deletions coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package main
//go:generate go run owners_generate.go

import (
_ "github.com/coredns/coredns/core/plugin" // Plug in CoreDNS.
"github.com/coredns/coredns/coremain"

// Plug in CoreDNS
_ "github.com/coredns/coredns/core/plugin"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion plugin/cache/prefech_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"

"github.com/coredns/coredns/plugin/test"

"github.com/miekg/dns"
)

Expand Down
2 changes: 1 addition & 1 deletion plugin/dnssec/dnskey.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/coredns/coredns/request"
"github.com/miekg/dns"

"github.com/miekg/dns"
"golang.org/x/crypto/ed25519"
)

Expand Down
2 changes: 1 addition & 1 deletion plugin/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request"

"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/miekg/dns"
etcdcv3 "go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/mvcc/mvccpb"
Expand Down
2 changes: 1 addition & 1 deletion plugin/kubernetes/ns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/miekg/dns"

"github.com/miekg/dns"
api "k8s.io/api/core/v1"
)

Expand Down
15 changes: 4 additions & 11 deletions plugin/kubernetes/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ import (
"github.com/caddyserver/caddy"
"github.com/miekg/dns"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

// Pull this in setting klog's output to stdout
"k8s.io/klog"

// Excluding azure because it is failing to compile
// pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)

var log = clog.NewWithPlugin("kubernetes")
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"context"
"fmt"

"github.com/miekg/dns"

"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/pkg/nonwriter"
"github.com/coredns/coredns/request"

"github.com/miekg/dns"
)

// Upstream is used to resolve CNAME or other external targets via CoreDNS itself.
Expand Down
3 changes: 1 addition & 2 deletions plugin/test/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import (
"strconv"

"github.com/matttproud/golang_protobuf_extensions/pbutil"
"github.com/prometheus/common/expfmt"

dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)

type (
Expand Down
8 changes: 3 additions & 5 deletions plugin/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import (
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/rcode"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

// Plugin the trace package.
_ "github.com/coredns/coredns/plugin/pkg/trace"
_ "github.com/coredns/coredns/plugin/pkg/trace" // Plugin the trace package.
"github.com/coredns/coredns/request"

"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin-contrib/zipkin-go-opentracing"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions test/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
"time"

"github.com/coredns/coredns/pb"

"github.com/miekg/dns"
"google.golang.org/grpc"

"github.com/coredns/coredns/pb"
)

func TestGrpc(t *testing.T) {
Expand Down
103 changes: 103 additions & 0 deletions test/presubmit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,106 @@ func hasImportTesting(path string, info os.FileInfo, _ error) error {
}
return nil
}

func TestImportOrdering(t *testing.T) {
err := filepath.Walk("..", hasImportOrdering)
if err != nil {
t.Fatal(err)
}
}

func hasImportOrdering(path string, info os.FileInfo, _ error) error {
if !info.Mode().IsRegular() {
return nil
}
if strings.HasPrefix(path, "../.") {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}

fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.AllErrors)
if err != nil {
return err
}
if len(f.Imports) == 0 {
return nil
}

// 3 blocks total, if
// 3 blocks: std + coredns + 3rd party
// 2 blocks: std + coredns, std + 3rd party, coredns + 3rd party
// 1 block: std, coredns, 3rd party
// first entry in a block specifies the type (std, coredns, 3rd party)
// we want: std, coredns, 3rd party
// more than 3 blocks as an error
blocks := [3][]*ast.ImportSpec{}
prevpos := 0
bl := 0
for _, im := range f.Imports {
line := fs.Position(im.Path.Pos()).Line
if line-prevpos > 1 && prevpos > 0 {
bl++
}
if bl > 2 {
return fmt.Errorf("more than %d import blocks in %q", bl, path)
}
blocks[bl] = append(blocks[bl], im)
prevpos = line
}
// if it:
// contains strings github.com/coredns/coredns -> coredns
// contains dots -> 3rd
// no dots -> std
ip := [3]string{} // type per block, just string, either std, coredns, 3rd
for i := 0; i <= bl; i++ {
ip[i] = importtype(blocks[i][0].Path.Value)
}

// Ok, now that we have the type, let's see if all members adhere to it.
// After that we check if the are in the right order.
for i := 0; i < bl; i++ {
for _, p := range blocks[i] {
t := importtype(p.Path.Value)
if t != ip[i] {
return fmt.Errorf("import path for %s is not of the same type %q in %q", p.Path.Value, ip[i], path)
}
}
}

// check order
switch bl {
case 0:
// we don't care
case 1:
if ip[0] == "std" && ip[1] == "coredns" {
break // OK
}
if ip[0] == "std" && ip[1] == "3rd" {
break // OK
}
if ip[0] == "coredns" && ip[1] == "3rd" {
break // OK
}
return fmt.Errorf("import path in %q are not in the right order (std -> coredns -> 3rd)", path)
case 2:
if ip[0] == "std" && ip[1] == "coredns" && ip[2] == "3rd" {
break // OK
}
return fmt.Errorf("import path in %q are not in the right order (std -> coredns -> 3rd)", path)
}

return nil
}

func importtype(s string) string {
if strings.Contains(s, "github.com/coredns/coredns") {
return "coredns"
}
if strings.Contains(s, ".") {
return "3rd"
}
return "std"
}
6 changes: 2 additions & 4 deletions test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package test
import (
"sync"

_ "github.com/coredns/coredns/core" // Hook in CoreDNS.
"github.com/coredns/coredns/core/dnsserver"
// Hook in CoreDNS.
_ "github.com/coredns/coredns/core"
// Load all managed plugins in github.com/coredns/coredns
_ "github.com/coredns/coredns/core/plugin"
_ "github.com/coredns/coredns/core/plugin" // Load all managed plugins in github.com/coredns/coredns.

"github.com/caddyserver/caddy"
)
Expand Down

0 comments on commit 995179a

Please sign in to comment.