Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware/chaos: fix version #669

Merged
merged 2 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
middleware/chaos: fix version
Move the version setting into a init function so it is done early. Then
tweak the setup code for chaos a bit to correctly pick this version up.
Add an integration test to pick this up in the toplevel test/ directory.

Fixes #667
  • Loading branch information
miekg committed May 22, 2017
commit e6d9af929773b1bb7d0a46815318e0ff91e53474
6 changes: 3 additions & 3 deletions coremain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ func init() {

caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader))
caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))

caddy.AppName = coreName
caddy.AppVersion = coreVersion
}

// Run is CoreDNS's main() function.
func Run() {

flag.Parse()

caddy.AppName = coreName
caddy.AppVersion = coreVersion

// Set up process log before anything bad happens
switch logfile {
case "stdout":
Expand Down
8 changes: 6 additions & 2 deletions middleware/chaos/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func init() {
ServerType: "dns",
Action: setup,
})

}

func setup(c *caddy.Controller) error {
Expand All @@ -28,13 +29,16 @@ func setup(c *caddy.Controller) error {
}

func chaosParse(c *caddy.Controller) (string, map[string]bool, error) {
// Set here so we pick up AppName and AppVersion that get set in coremain's init().
chaosVersion = caddy.AppName + "-" + caddy.AppVersion

version := ""
authors := make(map[string]bool)

for c.Next() {
args := c.RemainingArgs()
if len(args) == 0 {
return defaultVersion, nil, nil
return chaosVersion, nil, nil
}
if len(args) == 1 {
return args[0], nil, nil
Expand All @@ -48,4 +52,4 @@ func chaosParse(c *caddy.Controller) (string, map[string]bool, error) {
return version, authors, nil
}

var defaultVersion = caddy.AppName + "-" + caddy.AppVersion
var chaosVersion string
48 changes: 48 additions & 0 deletions test/chaos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package test

import (
"io/ioutil"
"log"
"testing"

// Plug in CoreDNS, needed for AppVersion and AppName in this test.
_ "github.com/coredns/coredns/coremain"

"github.com/mholt/caddy"
"github.com/miekg/dns"
)

func TestChaos(t *testing.T) {
corefile := `.:0 {
chaos
}
`

i, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
// Stop the server.
defer i.Stop()

udp, _ := CoreDNSServerPorts(i, 0)
if udp == "" {
t.Fatalf("Could not get UDP listening port")
}

log.SetOutput(ioutil.Discard)

m := new(dns.Msg)
m.SetQuestion("version.bind.", dns.TypeTXT)
m.Question[0].Qclass = dns.ClassCHAOS

resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %v", err)
}
chTxt := resp.Answer[0].(*dns.TXT).Txt[0]
version := caddy.AppName + "-" + caddy.AppVersion
if chTxt != version {
t.Fatalf("Expected version to bo %s, got %s", version, chTxt)
}
}