[PATCH] Added firewall, partition, repo tests; Made minor changes to firewall, partition, repo commands.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Added checking of unexpected arguments, and raising a KickstartValueError, if they are given.
Also edited some output strings formatting, to ensure there are no double spaces, if some of the values are missing.
---
 pykickstart/commands/firewall.py  |   29 ++++++---
 pykickstart/commands/partition.py |    2 +-
 pykickstart/commands/repo.py      |    6 ++-
 tests/commands/firewall.py        |   98 +++++++++++++++++++++++++++++
 tests/commands/partition.py       |  124 +++++++++++++++++++++++++++++++++++++
 tests/commands/repo.py            |   88 ++++++++++++++++++++++++++
 6 files changed, 335 insertions(+), 12 deletions(-)
 create mode 100644 tests/commands/firewall.py
 create mode 100644 tests/commands/partition.py
 create mode 100644 tests/commands/repo.py

diff --git a/pykickstart/commands/firewall.py b/pykickstart/commands/firewall.py
index 8a968c6..9a26a8a 100644
--- a/pykickstart/commands/firewall.py
+++ b/pykickstart/commands/firewall.py
@@ -20,8 +20,12 @@
 import string
 
 from pykickstart.base import *
+from pykickstart.errors import *
 from pykickstart.options import *
 
+import gettext
+_ = lambda x: gettext.ldgettext("pykickstart", x)
+
 class FC3_Firewall(KickstartCommand):
     removedKeywords = KickstartCommand.removedKeywords
     removedAttrs = KickstartCommand.removedAttrs
@@ -38,7 +42,7 @@ class FC3_Firewall(KickstartCommand):
         extra = []
         filteredPorts = []
 
-        retval = BaseData.__str__(self)
+        retval = KickstartCommand.__str__(self)
 
         if self.enabled is None:
             return retval
@@ -49,35 +53,35 @@ class FC3_Firewall(KickstartCommand):
             # out into their own list leaving what we expect.
             for port in self.ports:
                 if port == "ssh":
-                    extra.append("--ssh")
+                    extra.append(" --ssh")
                 elif port == "telnet":
-                    extra.append("--telnet")
+                    extra.append(" --telnet")
                 elif port == "smtp":
-                    extra.append("--smtp")
+                    extra.append(" --smtp")
                 elif port == "http":
-                    extra.append("--http")
+                    extra.append(" --http")
                 elif port == "ftp":
-                    extra.append("--ftp")
+                    extra.append(" --ftp")
                 else:
                     filteredPorts.append(port)
 
             # All the port:proto strings go into a comma-separated list.
             portstr = string.join (filteredPorts, ",")
             if len(portstr) > 0:
-                portstr = "--port=" + portstr
+                portstr = " --port=" + portstr
             else:
                 portstr = ""
 
-            extrastr = string.join (extra, " ")
+            extrastr = string.join (extra, "")
 
             truststr = string.join (self.trusts, ",")
             if len(truststr) > 0:
-                truststr = "--trust=" + truststr
+                truststr = " --trust=" + truststr
 
             # The output port list consists only of port:proto for
             # everything that we don't recognize, and special options for
             # those that we do.
-            retval += "# Firewall configuration\nfirewall --enabled %s %s %s\n" % (extrastr, portstr, truststr)
+            retval += "# Firewall configuration\nfirewall --enabled%s%s%s\n" % (extrastr, portstr, truststr)
         else:
             retval += "# Firewall configuration\nfirewall --disabled\n"
 
@@ -110,6 +114,11 @@ class FC3_Firewall(KickstartCommand):
 
     def parse(self, args):
         (opts, extra) = self.op.parse_args(args=args)
+        
+        if len(extra) != 0:
+            mapping = {"command": "firewall", "options": extra}
+            raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("Unexpected arguments to %(command)s command: %(options)s") % mapping)
+            
         self._setToSelf(self.op, opts)
         return self
 
diff --git a/pykickstart/commands/partition.py b/pykickstart/commands/partition.py
index 1a162fe..434f210 100644
--- a/pykickstart/commands/partition.py
+++ b/pykickstart/commands/partition.py
@@ -79,7 +79,7 @@ class FC3_PartData(BaseData):
 
     def __str__(self):
         retval = BaseData.__str__(self)
-        retval += "part %s %s\n" % (self.mountpoint, self._getArgsAsStr())
+        retval += "part %s%s\n" % (self.mountpoint, self._getArgsAsStr())
         return retval
 
 class FC4_PartData(FC3_PartData):
diff --git a/pykickstart/commands/repo.py b/pykickstart/commands/repo.py
index 071838c..4eefdc2 100644
--- a/pykickstart/commands/repo.py
+++ b/pykickstart/commands/repo.py
@@ -86,7 +86,7 @@ class F11_RepoData(F8_RepoData):
         retval = F8_RepoData._getArgsAsStr(self)
 
         if self.ignoregroups:
-            retval += " --ignoregroups true"
+            retval += " --ignoregroups=true"
         return retval
 
 class FC6_Repo(KickstartCommand):
@@ -115,6 +115,10 @@ class FC6_Repo(KickstartCommand):
 
     def parse(self, args):
         (opts, extra) = self.op.parse_args(args=args)
+        
+        if len(extra) != 0:
+            mapping = {"command": "repo", "options": extra}
+            raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("Unexpected arguments to %(command)s command: %(options)s") % mapping)
 
         # This is lame, but I can't think of a better way to make sure only
         # one of these two is specified.
diff --git a/tests/commands/firewall.py b/tests/commands/firewall.py
new file mode 100644
index 0000000..2da14e8
--- /dev/null
+++ b/tests/commands/firewall.py
@@ -0,0 +1,98 @@
+#
+# Martin Gracik <mgracik@xxxxxxxxxx>
+#
+# Copyright 2009 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use, modify,
+# copy, or redistribute it subject to the terms and conditions of the GNU
+# General Public License v.2.  This program is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
+# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
+# trademarks that are incorporated in the source code or documentation are not
+# subject to the GNU General Public License and may only be used or replicated
+# with the express permission of Red Hat, Inc.
+#
+
+import unittest
+from tests.baseclass import *
+
+class FC3_TestCase(CommandTest):
+    def runTest(self):
+        # pass
+        # enable firewall
+        self.assert_parse("firewall --enabled --trust=eth0 --ssh --port=imap:tcp",
+                          "firewall --enabled --port=22:tcp,imap:tcp --trust=eth0\n")
+        self.assert_parse("firewall --enable --trust=eth0,eth1 --ssh --telnet --http --smtp --ftp --port=1234:udp"
+                          "firewall --enabled --port=22:tcp,23:tcp,80:tcp,443:tcp,25:tcp,21:tcp,1234:udp --trust=eth0,eth1\n")
+        self.assert_parse("firewall --enabled --ssh --ftp", "firewall --enabled --port=22:tcp,21:tcp\n")
+        self.assert_parse("firewall --enable --port=1234:udp,4321:tcp", "firewall --enabled --port=1234:udp,4321:tcp\n")
+        
+        # disable firewall                  
+        self.assert_parse("firewall --disabled", "firewall --disabled\n")
+        self.assert_parse("firewall --disable", "firewall --disabled\n")
+        
+        # enable by default
+        self.assert_parse("firewall --trust=eth0", "firewall --enabled --trust=eth0\n")
+        self.assert_parse("firewall", "firewall --enabled\n")
+        
+        # deprecated
+        self.assert_deprecated("firewall", "--high")
+        self.assert_deprecated("firewall", "--medium")
+        
+        # fail
+        # unknown option
+        self.assert_parse_error("firewall --bad-flag", KickstartParseError)
+        # unexpected argument
+        self.assert_parse_error("firewall arg", KickstartValueError)
+        
+        
+class F9_TestCase(FC3_TestCase):
+    def runTest(self):
+        # run FC3 test case
+        FC3_TestCase.runTest(self)
+        
+        # removed
+        self.assert_removed("firewall", "--high")
+        self.assert_removed("firewall", "--medium")
+        
+        
+class F10_TestCase(F9_TestCase):
+    def runTest(self):
+        # run F9 test case
+        #F9_TestCase.runTest(self)
+        
+        # pass
+        # enable firewall
+        self.assert_parse("firewall --enabled --trust=eth0 --ssh --port=imap:tcp",
+                          "firewall --enabled --port=imap:tcp --trust=eth0 --service=ssh\n")
+        self.assert_parse("firewall --enable --trust=eth0,eth1 --ssh --http --smtp --ftp --port=1234:udp"
+                          "firewall --enabled --port=1234:udp --trust=eth0,eth1 --service=ssh,http,smtp,ftp\n")
+        self.assert_parse("firewall --enabled --ssh --ftp", "firewall --enabled --service=ssh,ftp\n")
+        self.assert_parse("firewall --enable --port=1234:udp,4321:tcp", "firewall --enabled --port=1234:udp,4321:tcp\n")
+        
+        # disable firewall                  
+        self.assert_parse("firewall --disabled", "firewall --disabled\n")
+        self.assert_parse("firewall --disable", "firewall --disabled\n")
+        
+        # enable by default
+        self.assert_parse("firewall --trust=eth0", "firewall --enabled --trust=eth0\n")
+        self.assert_parse("firewall", "firewall --enabled\n")
+                          
+        # deprecated
+        self.assert_deprecated("firewall", "--telnet")
+        
+        # fail
+        # unknown option
+        self.assert_parse_error("firewall --bad-flag", KickstartParseError)
+        # unexpected argument
+        self.assert_parse_error("firewall arg", KickstartValueError)
+        
+        
+if __name__ == "__main__":
+    unittest.main()
+    
diff --git a/tests/commands/partition.py b/tests/commands/partition.py
new file mode 100644
index 0000000..a0151dd
--- /dev/null
+++ b/tests/commands/partition.py
@@ -0,0 +1,124 @@
+#
+# Martin Gracik <mgracik@xxxxxxxxxx>
+#
+# Copyright 2009 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use, modify,
+# copy, or redistribute it subject to the terms and conditions of the GNU
+# General Public License v.2.  This program is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
+# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
+# trademarks that are incorporated in the source code or documentation are not
+# subject to the GNU General Public License and may only be used or replicated
+# with the express permission of Red Hat, Inc.
+#
+
+import unittest
+from tests.baseclass import *
+
+class FC3_TestCase(CommandTest):
+    def runTest(self, bytes_per_inode=False):
+        bpi = ""
+        if bytes_per_inode:
+            bpi = " --bytes-per-inode=4096"
+            
+        # pass
+        self.assert_parse("part /home", "part /home%s\n" % bpi)
+        self.assert_parse("partition raid.1 --active --asprimary --start=0 --end=10 --fstype=ext3 --noformat",
+                          "part raid.1 --active --asprimary --end=10 --fstype=\"ext3\" --noformat%s\n" % bpi)
+        self.assert_parse("part pv.1 --ondisk=sda --onpart=sda1 --recommended",
+                          "part pv.1 --ondisk=sda --onpart=sda1 --recommended%s\n" % bpi)
+        self.assert_parse("part pv.1 --ondrive=sda --usepart=sda1 --recommended",
+                          "part pv.1 --ondisk=sda --onpart=sda1 --recommended%s\n" % bpi)
+        self.assert_parse("part / --onbiosdisk=hda --size=100", "part / --onbiosdisk=hda --size=100%s\n" % bpi)
+        self.assert_parse("part swap --grow --maxsize=100", "part swap --grow --maxsize=100%s\n" % bpi)
+        
+        # does not remove the /dev/ part
+        self.assert_parse("part /usr --ondisk=/dev/sda --recommended --noformat --active",
+                          "part /usr --active --noformat --ondisk=/dev/sda --recommended%s\n" % bpi)
+                          
+        # fail
+        # missing mountpoint
+        self.assert_parse_error("part", KickstartValueError)
+        self.assert_parse_error("part --ondisk=sda --size=100", KickstartValueError)
+        
+        for opt in ("start", "end", "size", "maxsize"):
+            # integer argument required
+            self.assert_parse_error("part / --%s=string" % opt, KickstartParseError)
+            # value required
+            self.assert_parse_error("part / --%s" % opt, KickstartParseError)
+            
+        for opt in ("fstype", "onbiosdisk", "ondisk", "ondrive", "onpart", "usepart"):
+            # value required
+            self.assert_parse_error("part / --%s" % opt, KickstartParseError)
+            
+        # only one argument allowed
+        self.assert_parse_error("part / /home /usr", KickstartValueError)
+        # unknown option
+        self.assert_parse_error("part /home --unknown=value", KickstartParseError)
+
+        
+        
+class FC4_TestCase(FC3_TestCase):
+    def runTest(self):
+        # run FC3 test case
+        FC3_TestCase.runTest(self, bytes_per_inode=True)
+        
+        # pass
+        self.assert_parse("part /home --bytes-per-inode=2048 --fsoptions=blah --label=home",
+                          "part /home --bytes-per-inode=2048 --fsoptions=\"blah\" --label=home\n")
+                          
+        # fail
+        # --bytes-per-inode requires int argument
+        self.assert_parse_error("part /home --bytes-per-inode=string", KickstartParseError)
+        self.assert_parse_error("part /home --bytes-per-inode", KickstartParseError)
+        # missing required arguments
+        for opt in ("fsoptions", "label"):
+            self.assert_parse_error("part /home --%s" % opt, KickstartParseError)
+        
+        
+        
+class RHEL5_TestCase(FC4_TestCase):
+    def runTest(self):
+        # run FC4 test case
+        FC4_TestCase.runTest(self)
+        
+        # pass
+        self.assert_parse("part / --encrypted --passphrase=blahblah",
+                          "part / --bytes-per-inode=4096 --encrypted --passphrase=\"blahblah\"\n")
+                          
+        # fail
+        # missing required --passphrase argument
+        self.assert_parse_error("part / --encrypted --passphrase", KickstartParseError)
+
+        
+        
+class F9_TestCase(FC3_TestCase):
+    def runTest(self):
+        # run FC3 test case
+        FC3_TestCase.runTest(self, bytes_per_inode=False)
+        
+        # pass
+        self.assert_parse("part / --encrypted --passphrase=blahblah",
+                          "part / --encrypted --passphrase=\"blahblah\"\n")
+        self.assert_parse("part /home --fsprofile=blah", "part /home --fsprofile=\"blah\"\n")
+        
+        # deprecated
+        self.assert_deprecated("part", "--bytes-per-inode")
+        
+        # fail
+        # missing required --passphrase argument
+        self.assert_parse_error("part / --encrypted --passphrase", KickstartParseError)
+        # missing required --fsprofile argument
+        self.assert_parse_error("part / --fsprofile", KickstartParseError)
+        
+        
+        
+if __name__ == "__main__":
+    unittest.main()
+    
diff --git a/tests/commands/repo.py b/tests/commands/repo.py
new file mode 100644
index 0000000..fbaf589
--- /dev/null
+++ b/tests/commands/repo.py
@@ -0,0 +1,88 @@
+#
+# Martin Gracik <mgracik@xxxxxxxxxx>
+#
+# Copyright 2009 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use, modify,
+# copy, or redistribute it subject to the terms and conditions of the GNU
+# General Public License v.2.  This program is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
+# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
+# trademarks that are incorporated in the source code or documentation are not
+# subject to the GNU General Public License and may only be used or replicated
+# with the express permission of Red Hat, Inc.
+#
+
+import unittest
+from tests.baseclass import *
+
+class FC6_TestCase(CommandTest):
+    def runTest(self):
+        # pass
+        self.assert_parse("repo --name=blah --baseurl=http://www.domain.com";,
+                          "repo --name=blah --baseurl=http://www.domain.com\n";)
+        self.assert_parse("repo --name=blah --mirrorlist=http://www.domain.com";,
+                          "repo --name=blah --mirrorlist=http://www.domain.com\n";)
+                          
+        # fail
+        # missing required option --name
+        self.assert_parse_error("repo --baseurl=www.domain.com", KickstartValueError)
+        self.assert_parse_error("repo --name --baseurl=www.domain.com", KickstartParseError)
+        # missing one of required options --baseurl or --mirrorlist
+        self.assert_parse_error("repo --name=blah", KickstartValueError)
+        self.assert_parse_error("repo --name=blah --baseurl", KickstartParseError)
+        self.assert_parse_error("repo --name=blah --mirrorlist", KickstartParseError)
+        # only one of --baseurl or --mirrorlist must be specified
+        self.assert_parse_error("repo --name=blah --baseurl=www.domain.com --mirrorlist=www.domain.com",
+                                KickstartValueError)
+        # unknown option
+        self.assert_parse_error("repo --name=blah --baseurl=www.domain.com --unknown", KickstartParseError)
+        # not expected argument
+        self.assert_parse_error("repo --name=blah --baseurl=www.domain.com blah", KickstartValueError)
+        
+        
+class F8_TestCase(FC6_TestCase):
+    def runTest(self):
+        # run FC6 test case
+        FC6_TestCase.runTest(self)
+        
+        # pass
+        self.assert_parse("repo --name=blah --baseurl=www.domain.com --cost=10 --excludepkgs=pkg1,pkg2 --includepkgs=pkg3,pkg4",
+                          "repo --name=blah --baseurl=www.domain.com --cost=10 --includepkgs=\"pkg3,pkg4\" --excludepkgs=\"pkg1,pkg2\"\n")
+        self.assert_parse("repo --name=blah --baseurl=123xyz --cost=10 --excludepkgs=pkg1,pkg2 --includepkgs=pkg3,pkg4",
+                          "repo --name=blah --baseurl=123xyz --cost=10 --includepkgs=\"pkg3,pkg4\" --excludepkgs=\"pkg1,pkg2\"\n")
+                          
+        # fail
+        # missing required arguments
+        for opt in ("--cost", "--includepkgs", "--excludepkgs"):
+            self.assert_parse_error("repo --name=blah --baseurl=www.domain.com %s" % opt, KickstartParseError)
+        # --cost argument not integer
+        self.assert_parse_error("repo --name=blah --baseurl=www.domain.com --cost=high", KickstartParseError)
+        
+        
+class F11_TestCase(F8_TestCase):
+    def runTest(self):
+        # run F8 test case
+        F8_TestCase.runTest(self)
+        
+        # pass
+        for val in ("1", "true", "on"):
+            self.assert_parse("repo --name=blah --baseurl=www.domain.com --cost=10 --excludepkgs=pkg1,pkg2 --includepkgs=pkg3,pkg4 --ignoregroups=%s" % val,
+                              "repo --name=blah --baseurl=www.domain.com --cost=10 --includepkgs=\"pkg3,pkg4\" --excludepkgs=\"pkg1,pkg2\" --ignoregroups=true\n")
+        for val in ("0", "false", "off"):
+            self.assert_parse("repo --name=blah --baseurl=www.domain.com --cost=10 --excludepkgs=pkg1,pkg2 --includepkgs=pkg3,pkg4 --ignoregroups=%s" % val,
+                              "repo --name=blah --baseurl=www.domain.com --cost=10 --includepkgs=\"pkg3,pkg4\" --excludepkgs=\"pkg1,pkg2\"\n")
+                              
+        # fail
+        # missing --ignoregroups argument
+        self.assert_parse_error("repo --name=blah --baseurl=www.domain.com --ignoregroups", KickstartParseError)
+        
+        
+if __name__ == "__main__":
+    unittest.main()
+    
-- 
1.6.0.6

_______________________________________________
Kickstart-list mailing list
Kickstart-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/kickstart-list

[Index of Archives]     [Red Hat General]     [CentOS Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux