[augeas-devel] [PATCH] /etc/ethers: new lens and test

Satoru SATOH satoru.satoh at gmail.com
Tue Oct 6 09:53:12 UTC 2009


Thanks for your advice!

On Mon, Oct 05, 2009 at 11:19:06AM -0700, David Lutterkort wrote:
> On Mon, 2009-10-05 at 10:51 +0900, Satoru SATOH wrote:
> > The followings adds a new lens for /etc/ethers and associated test.
> Very nice. I have two little comments before committing:
(snip)
> > +  let address =
> > +    let chr = /[0-9a-fA-F][0-9a-fA-F]/ in
> > +      chr . ":" . chr . ":" . chr . ":" . chr . ":" . chr . ":" . chr
> 
> chr should match a single hexadecimal character, i.e. it should
> be /[0-9a-fA-F][0-9a-fA-F]?/
 
You're absolutely right. I missed that case.

> > +  let record = [ seq "ether" . indent .
> > +                              [ label "ethaddr" . store  address ] . sep_tab .
> > +                              [ label "ip" . store word ] . eol ]
> 
> How about renaming 'ethaddr' to 'mac' ?
NP.

Revised patch is here. The changes are summarized as follows:

 * Removed the line " let sep_spc ..." because it's not used.
 * re fix: /[0-9a-fA-F][0-9a-fA-F]/ -> /[0-9a-fA-F][0-9a-fA-F]?/
 * s/chr/hex/
 * s/ethaddr/mac/
 * Added a test pattern of mac address

---
 lenses/ethers.aug            |   25 +++++++++++++++++
 lenses/tests/test_ethers.aug |   61 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)
 create mode 100644 lenses/ethers.aug
 create mode 100644 lenses/tests/test_ethers.aug

diff --git a/lenses/ethers.aug b/lenses/ethers.aug
new file mode 100644
index 0000000..9cb633b
--- /dev/null
+++ b/lenses/ethers.aug
@@ -0,0 +1,25 @@
+(* Parsing /etc/ethers *)
+
+module Ethers =
+  autoload xfm
+
+  let sep_tab = Util.del_ws_tab
+
+  let eol = del /[ \t]*\n/ "\n"
+  let indent = del /[ \t]*/ ""
+
+  let comment = Util.comment
+  let empty   = [ del /[ \t]*#?[ \t]*\n/ "\n" ]
+
+  let word = /[^# \n\t]+/
+  let address =
+    let hex = /[0-9a-fA-F][0-9a-fA-F]?/ in
+      hex . ":" . hex . ":" . hex . ":" . hex . ":" . hex . ":" . hex
+
+  let record = [ seq "ether" . indent .
+                              [ label "mac" . store  address ] . sep_tab .
+                              [ label "ip" . store word ] . eol ]
+
+  let lns = ( empty | comment | record ) *
+
+  let xfm = transform lns (incl "/etc/ethers")
diff --git a/lenses/tests/test_ethers.aug b/lenses/tests/test_ethers.aug
new file mode 100644
index 0000000..7209c1b
--- /dev/null
+++ b/lenses/tests/test_ethers.aug
@@ -0,0 +1,61 @@
+(* Tests for the Ethers module *)
+
+module Test_ethers =
+
+(*
+  let empty_entries = "# see man ethers for syntax\n"
+
+  test Ethers.record get empty_entries =
+    { "#comment" = "see man ethers for syntax" }
+*)
+
+  let three_entries = "54:52:00:01:00:01 192.168.1.1
+# \tcomment\t
+54:52:00:01:00:02 foo.example.com
+00:16:3e:01:fe:03 bar
+"
+
+  test Ethers.lns get three_entries =
+   { "1" { "mac" = "54:52:00:01:00:01" }
+         { "ip" = "192.168.1.1" } }
+   { "#comment" = "comment" }
+   { "2" { "mac" = "54:52:00:01:00:02" }
+         { "ip" = "foo.example.com" } }
+   { "3" { "mac" = "00:16:3e:01:fe:03" }
+         { "ip" = "bar" } }
+
+  (* Deleting the 'ip' node violates the schema *)
+  test Ethers.lns put three_entries after
+      rm "/1/ip"
+    = *
+
+  test Ethers.lns put three_entries after
+    set "/2/ip" "192.168.1.2" ;
+    set "/3/ip" "baz"
+  = "54:52:00:01:00:01 192.168.1.1
+# \tcomment\t
+54:52:00:01:00:02 192.168.1.2
+00:16:3e:01:fe:03 baz
+"
+
+  test Ethers.lns put three_entries after
+    rm "/3"
+  = "54:52:00:01:00:01 192.168.1.1
+# \tcomment\t
+54:52:00:01:00:02 foo.example.com
+"
+
+  (* Make sure blank and indented lines get through *)
+  test Ethers.lns get "54:52:00:01:00:01\tfoo  \n \n\n
+54:52:00:01:00:02 bar\n" =
+    { "1" { "mac" = "54:52:00:01:00:01" }
+          { "ip" = "foo" } }
+    {} {} {}
+    { "2" { "mac" = "54:52:00:01:00:02" }
+          { "ip" = "bar" } }
+
+(* Local Variables: *)
+(* mode: caml *)
+(* End: *)
+
+
-- 
1.6.2.5




More information about the augeas-devel mailing list