Browse Source

Set serial number from file modtime if not explicitly set in json

Closes #36
Ask Bjørn Hansen 12 years ago
parent
commit
f7ab6b01ed
3 changed files with 13 additions and 3 deletions
  1. 1 2
      dns/test.example.org.json
  2. 8 1
      zones.go
  3. 4 0
      zones_test.go

+ 1 - 2
dns/test.example.org.json

@@ -1,8 +1,7 @@
 {
 {
-    "serial": 1,
     "data" : {
     "data" : {
         "bad-example-there-really-should-be-an-ns-record-at-the-apex-here": {},
         "bad-example-there-really-should-be-an-ns-record-at-the-apex-here": {},
-        "bar": { 
+        "bar": {
             "a": [ [ "192.168.1.2" ] ]
             "a": [ [ "192.168.1.2" ] ]
         }
         }
     }
     }

+ 8 - 1
zones.go

@@ -117,12 +117,19 @@ func readZoneFile(zoneName, fileName string) (zone *Zone, zerr error) {
 
 
 	fh, err := os.Open(fileName)
 	fh, err := os.Open(fileName)
 	if err != nil {
 	if err != nil {
-		log.Println("Could not read ", fileName, ": ", err)
+		log.Printf("Could not read '%s': %s", fileName, err)
 		panic(err)
 		panic(err)
 	}
 	}
 
 
 	zone = NewZone(zoneName)
 	zone = NewZone(zoneName)
 
 
+	fileInfo, err := fh.Stat()
+	if err != nil {
+		log.Printf("Could not stat '%s': %s", fileName, err)
+	} else {
+		zone.Options.Serial = int(fileInfo.ModTime().Unix())
+	}
+
 	var objmap map[string]interface{}
 	var objmap map[string]interface{}
 	decoder := json.NewDecoder(fh)
 	decoder := json.NewDecoder(fh)
 	if err = decoder.Decode(&objmap); err != nil {
 	if err = decoder.Decode(&objmap); err != nil {

+ 4 - 0
zones_test.go

@@ -27,6 +27,10 @@ func (s *ConfigSuite) TestReadConfigs(c *C) {
 	// Just check that example.com and test.example.org loaded, too.
 	// Just check that example.com and test.example.org loaded, too.
 	c.Check(s.zones["example.com"].Origin, Equals, "example.com")
 	c.Check(s.zones["example.com"].Origin, Equals, "example.com")
 	c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
 	c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
+	if s.zones["test.example.org"].Options.Serial == 0 {
+		c.Log("Serial number is 0, should be set by file timestamp")
+		c.Fail()
+	}
 
 
 	// The real tests are in test.example.com so we have a place
 	// The real tests are in test.example.com so we have a place
 	// to make nutty configuration entries
 	// to make nutty configuration entries