func cleanupDomain(r53 *route53.Route53, id string) { // delete all non-default SOA/NS records rrsets, err := cli53.ListAllRecordSets(r53, id) fatalIfErr(err) changes := []*route53.Change{} for _, rrset := range rrsets { if *rrset.Type != "NS" && *rrset.Type != "SOA" { change := &route53.Change{ Action: aws.String("DELETE"), ResourceRecordSet: rrset, } changes = append(changes, change) } } if len(changes) > 0 { req2 := route53.ChangeResourceRecordSetsInput{ HostedZoneId: &id, ChangeBatch: &route53.ChangeBatch{ Changes: changes, }, } _, err = r53.ChangeResourceRecordSets(&req2) if err != nil { fmt.Printf("Warning: cleanup failed - %s\n", err) } } req3 := route53.DeleteHostedZoneInput{Id: &id} _, err = r53.DeleteHostedZone(&req3) if err != nil { fmt.Printf("Warning: cleanup failed - %s\n", err) } }
func hasRecord(name, record string) bool { r53 := getService() zone := domainZone(name) rrsets, err := cli53.ListAllRecordSets(r53, *zone.Id) fatalIfErr(err) for _, rrset := range rrsets { rrs := cli53.ConvertRRSetToBind(rrset) cli53.UnexpandSelfAliases(rrs, zone, false) for _, rr := range rrs { line := rr.String() line = strings.Replace(line, "\t", " ", -1) if record == line { return true } } } return false }
func init() { Before("", func() { // randomize temporary test domain name World["$domain"] = fmt.Sprintf("example%s.com", uniqueReference()) }) After("", func() { delete(World, "$domain") delete(World, "$delegationSet") if len(cleanupIds) > 0 { // cleanup r53 := getService() for _, id := range cleanupIds { cleanupDomain(r53, id) } cleanupIds = []string{} } if len(cleanupDSIds) > 0 { // cleanup r53 := getService() for _, id := range cleanupDSIds { cleanupReusableDelegationSet(r53, id) } cleanupDSIds = []string{} } }) Given(`^I have a domain "(.+?)"$`, func(name string) { name = domain(name) // create a test domain r53 := getService() callerReference := uniqueReference() req := route53.CreateHostedZoneInput{ CallerReference: &callerReference, Name: &name, } resp, err := r53.CreateHostedZone(&req) fatalIfErr(err) cleanupIds = append(cleanupIds, *resp.HostedZone.Id) }) Given(`^I have a delegation set$`, func() { r53 := getService() callerReference := uniqueReference() req := route53.CreateReusableDelegationSetInput{ CallerReference: &callerReference, } resp, err := r53.CreateReusableDelegationSet(&req) fatalIfErr(err) id := *resp.DelegationSet.Id World["$delegationSet"] = id cleanupDSIds = append(cleanupDSIds, id) }) When(`^I run "(.+?)"$`, func(cmd string) { cmd = replaceMagics(cmd) args := safeSplit(cmd) if os.Getenv("COVERAGE") != "" { args = coverageArgs(args) } ps := exec.Command("./"+args[0], args[1:]...) out, err := ps.CombinedOutput() if err != nil { T.Errorf("Error: %s Output: %s", err, out) } else { runOutput = string(out) } }) When(`^I execute "(.+?)"$`, func(cmd string) { cmd = domain(cmd) args := safeSplit(cmd) ps := exec.Command("./"+args[0], args[1:]...) out, err := ps.CombinedOutput() runOutput = string(out) if err, ok := err.(*exec.ExitError); ok { waitStatus := err.Sys().(syscall.WaitStatus) retCode = waitStatus.ExitStatus() } else if err != nil { T.Errorf("Error: %s Output: %s", err, out) } else { runOutput = string(out) } }) Then(`^the domain "(.+?)" is created$`, func(name string) { name = domain(name) id := domainId(name) if id == "" { T.Errorf("Domain %s was not created", name) } else { cleanupIds = append(cleanupIds, id) } }) Then(`^the domain "(.+?)" is deleted$`, func(name string) { name = domain(name) id := domainId(name) if id == "" { cleanupIds = []string{} // drop from cleanupIds } else { T.Errorf("Domain %s was not deleted", name) cleanupIds = append(cleanupIds, id) } }) Then(`^the domain "(.+?)" has (\d+) records$`, func(name string, expected int) { name = domain(name) r53 := getService() id := domainId(name) rrsets, err := cli53.ListAllRecordSets(r53, id) fatalIfErr(err) actual := len(rrsets) if expected != actual { T.Errorf("Domain %s: Expected %d records, actually %d records ", name, expected, actual) } }) Then(`^the domain "(.+?)" has record "(.+)"$`, func(name, record string) { name = domain(name) record = domain(record) if !hasRecord(name, record) { T.Errorf("Domain %s: missing record %s", name, record) } }) Then(`^the domain "(.+?)" doesn't have record "(.+)"$`, func(name, record string) { name = domain(name) record = domain(record) if hasRecord(name, record) { T.Errorf("Domain %s: present record %s", name, record) } }) Then(`^the domain "(.+?)" export matches file "(.+?)"( including auth)?$`, func(name, filename, auth string) { name = domain(name) r53 := getService() zone := domainZone(name) out := new(bytes.Buffer) cli53.ExportBindToWriter(r53, zone, false, out) actual := out.Bytes() rfile, err := os.Open(filename) fatalIfErr(err) defer rfile.Close() expected, err := ioutil.ReadAll(rfile) fatalIfErr(err) errors := compareDomains(expected, actual, auth != "") if len(errors) > 0 { T.Errorf(errors) } }) Then(`^the output contains "(.+?)"$`, func(s string) { s = unquote(domain(s)) if !strings.Contains(runOutput, s) { T.Errorf("Output did not contain \"%s\"\nactual: %s", s, runOutput) } }) Then(`^the output matches "(.+?)"$`, func(s string) { re, err := regexp.Compile(s) fatalIfErr(err) match := re.FindStringSubmatch(runOutput) if match == nil { T.Errorf("Output did not match \"%s\"", s) } backReferences = match }) Then(`^the exit code was (\d+)$`, func(code int) { if code != retCode { T.Errorf("Exit code expected: %d != actual: %d", code, retCode) } }) Then(`^the delegation set "(.+?)" is created$`, func(id string) { id = replaceMagics(id) ds := reusableDelegationSet(id) if ds == nil { T.Errorf("Reusable delegation set %s was not created", id) } else { cleanupDSIds = append(cleanupDSIds, id) } }) Then(`^the delegation set "(.+?)" is deleted$`, func(id string) { id = replaceMagics(id) ds := reusableDelegationSet(id) if ds == nil { cleanupDSIds = []string{} } else { T.Errorf("Reusable delegation set %s was not deleted", id) cleanupDSIds = append(cleanupDSIds, id) } }) }