Browse Source

[1843] basic bindctl execute directives and lettuce tests

Jelte Jansen 13 years ago
parent
commit
64f69a1433

+ 46 - 1
src/bin/bindctl/bindcmd.py

@@ -737,7 +737,52 @@ class BindCmdInterpreter(Cmd):
         '''Handles the 'execute' command, which executes a number of
            (preset) statements. Currently only 'file' commands are supported,
            e.g. 'execute file <file>'.'''
-        pass
+        try:
+            command_file = open(command.params['filename'])
+            self.apply_commands_from_file(command_file)
+        except IOError as ioe:
+            print("Error: " + str(ioe))
+
+    def apply_commands_from_file(self, command_file):
+        '''Applies the configuration commands from the given opened file.
+           This is the method that catches, comments, echo statements, and
+           other directives. All commands not filtered by this method are
+           interpreted as if they are directly entered in an active session.
+           Lines starting with any of the following characters are not
+           passed directly:
+           # - These are comments
+           ! - These are directives
+               !echo: print the rest of the line
+               !verbose on/off: print the commands themselves too
+               Unknown directives are ignored (with a warning)
+           The execution is stopped if there are any errors.
+        '''
+        verbose = False
+        try:
+            for line in command_file:
+                line = line.strip()
+                if verbose:
+                    print(line)
+                if line.startswith('#'):
+                    continue
+                elif line.startswith('!'):
+                    if line.startswith('!echo ') and len(line) > 6:
+                        print(line[6:])
+                    elif line.startswith('!verbose on'):
+                        verbose = True
+                    elif line.startswith('!verbose off'):
+                        verbose = False
+                    else:
+                        print("Warning: ignoring unknown directive: " + line)
+                else:
+                    cmd = BindCmdParse(line)
+                    self._validate_cmd(cmd)
+                    self._handle_cmd(cmd)
+        except isc.config.ModuleCCSessionError as mcse:
+            print(str(mcse))
+        except (IOError, http.client.HTTPException,
+                BindCtlException, isc.cc.data.DataTypeError) as err:
+            print('Error: ', err)
 
     def apply_cmd(self, cmd):
         '''Handles a general module command'''

+ 3 - 0
tests/lettuce/data/commands/bad_command

@@ -0,0 +1,3 @@
+!echo shouldshow
+some bad command
+!echo shouldnotshow

+ 6 - 0
tests/lettuce/data/commands/directives

@@ -0,0 +1,6 @@
+# this is a comment: commentexample1
+!echo this is an echo: echoexample
+!verbose on
+# this is a comment with verbose on: verbosecommentexample
+!verbose off
+# this is a comment with verbose off again: commentexample2

+ 0 - 0
tests/lettuce/data/commands/empty


+ 2 - 0
tests/lettuce/data/commands/nested

@@ -0,0 +1,2 @@
+# include a different file
+execute file data/commands/nested1

+ 2 - 0
tests/lettuce/data/commands/nested1

@@ -0,0 +1,2 @@
+# this is included by nested
+!echo shouldshow

+ 29 - 0
tests/lettuce/features/bindctl_commands.feature

@@ -70,5 +70,34 @@ Feature: control with bindctl
     Given I have bind10 running with configuration bindctl_commands.config
     And wait for bind10 stderr message BIND10_STARTED_CC
     And wait for bind10 stderr message CMDCTL_STARTED
+
+    # first a few bad commands
+    When I send bind10 the command execute
+    last bindctl output should contain Error
+    When I send bind10 the command execute file
+    last bindctl output should contain Error
+    When I send bind10 the command execute file data/commands/nosuchfile
+    last bindctl output should contain Error
+
+    # empty list should be no-op
     When I send bind10 the command execute file data/commands/empty
     last bindctl output should not contain Error
+
+    # some tests of directives like !echo and !verbose
+    When I send bind10 the command execute file data/commands/directives
+    last bindctl output should not contain Error
+    last bindctl output should not contain commentexample1
+    last bindctl output should contain echoexample
+    last bindctl output should contain verbosecommentexample
+    last bindctl output should not contain commentexample2
+
+    # bad_command contains a bad command, at which point execution should stop
+    When I send bind10 the command execute file data/commands/bad_command
+    last bindctl output should contain shouldshow
+    last bindctl output should contain Error
+    last bindctl output should not contain shouldnotshow
+
+    # nested_command contains another execute script
+    When I send bind10 the command execute file data/commands/nested
+    last bindctl output should contain shouldshow
+    last bindctl output should not contain Error