From e83f65b7f2cfc84f00131afbbffe271361816816 Mon Sep 17 00:00:00 2001 From: mcorbin Date: Thu, 28 Apr 2016 17:03:20 +0200 Subject: [PATCH 1/2] add os.path.expanduser to params --- goss.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/goss.py b/goss.py index c7df8c9..c3f017b 100644 --- a/goss.py +++ b/goss.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import os + DOCUMENTATION = ''' --- module: goss @@ -15,19 +17,19 @@ format: required: false description: - - change the output goss format. - - Goss format list : goss v --format => [documentation json junit nagios rspecish tap]. + - change the output goss format. + - Goss format list : goss v --format => [documentation json junit nagios rspecish tap]. - Default: None output_file: required: false description: - save the result of the goss command in a file whose path is output_file examples: - - name: test goss file + - name: test goss file goss: path: "/path/to/file.yml" - - name: test goss files + - name: test goss files goss: path: "{{ item }}" format: json @@ -60,9 +62,9 @@ def main(): supports_check_mode=False ) - file_path = module.params['path'] + file_path = os.path.expanduser(module.params['path']) output_format = module.params['format'] - output_file_path = module.params['output_file'] + output_file_path = os.path.expanduser(module.params['output_file']) (rc, out, err) = check(module, file_path, output_format) From b3fe48ed93742fba7ca1c499648629c576fe1a5a Mon Sep 17 00:00:00 2001 From: mcorbin Date: Thu, 28 Apr 2016 18:36:09 +0200 Subject: [PATCH 2/2] add tests in goss.py and examples --- examples/ansible.cfg | 2 ++ examples/playbook.yml | 48 +++++++++++++++++++++++++++++++++++++++++++ goss.py | 48 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 examples/ansible.cfg create mode 100644 examples/playbook.yml diff --git a/examples/ansible.cfg b/examples/ansible.cfg new file mode 100644 index 0000000..d1ff671 --- /dev/null +++ b/examples/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +library=./.. \ No newline at end of file diff --git a/examples/playbook.yml b/examples/playbook.yml new file mode 100644 index 0000000..0fe8b62 --- /dev/null +++ b/examples/playbook.yml @@ -0,0 +1,48 @@ +--- + +# success +- hosts: all + connection: local + tasks: + - name: execute goss test file. + goss: + path: "~/goss/goss.yaml" + - name: Execute goss test file and write result on output_file + goss: + path: "~/goss/goss.yaml" + output_file: "~/goss/result.yaml" + - name: Execute test file with json output + goss: + path: "~/goss/goss.yaml" + format: "json" +# errors +- hosts: all + connection: local + tasks: + - name: Error because path is a directory + goss: + path: "~/goss/" + ignore_errors: True + - name: Error because path does not exists + goss: + path: "~/goss/toto.yaml" + ignore_errors: True + - name: Error because output_file is a directory + goss: + path: "~/goss/goss.yaml" + output_file: "~/goss/" + ignore_errors: True + - name: Error because goss_root not readable + goss: + path: "~/goss/goss_root.yaml" + ignore_errors: True + - name: Error because output_file not writable + goss: + path: "~/goss/goss.yaml" + output_file: "/root/result" + ignore_errors: True + - name: Error because output_file directory not exists + goss: + path: "~/goss/goss.yaml" + output_file: "~/goss/notexists/result" + ignore_errors: True diff --git a/goss.py b/goss.py index c3f017b..53ebdc2 100644 --- a/goss.py +++ b/goss.py @@ -19,7 +19,7 @@ description: - change the output goss format. - Goss format list : goss v --format => [documentation json junit nagios rspecish tap]. - - Default: None + - Default: rspecish output_file: required: false description: @@ -38,12 +38,12 @@ ''' # launch goss validate command on the file -def check(module, file_path, output_format): +def check(module, test_file_path, output_format): cmd = "" if output_format != None: - cmd = "goss -g {0} v --format {1}".format(file_path, output_format) + cmd = "goss -g {0} v --format {1}".format(test_file_path, output_format) else: - cmd = "goss -g {0} v".format(file_path) + cmd = "goss -g {0} v".format(test_file_path) return module.run_command(cmd) #write goss result to output_file_path @@ -62,13 +62,43 @@ def main(): supports_check_mode=False ) - file_path = os.path.expanduser(module.params['path']) - output_format = module.params['format'] - output_file_path = os.path.expanduser(module.params['output_file']) + test_file_path = module.params['path'] # test file path + output_format = module.params['format'] # goss output format + output_file_path = module.params['output_file'] - (rc, out, err) = check(module, file_path, output_format) + if test_file_path == None: + module.fail_json(msg="test file path is null") - output_file(output_file_path, out) + test_file_path = os.path.expanduser(test_file_path) + + # test if access to test file is ok + + if not os.access(test_file_path, os.R_OK): + module.fail_json(msg="Test file %s not readable" % (test_file_path)) + + # test if test file is not a dir + if os.path.isdir(test_file_path): + module.fail_json(msg="Test file must be a file ! : %s" % (test_file_path)) + + (rc, out, err) = check(module, test_file_path, output_format) + + if output_file_path != None: + output_file_path = os.path.expanduser(output_file_path) + # check if output_file is a file + if output_file_path.endswith(os.sep): + module.fail_json(msg="output_file must be a file. Actually : %s " % (output_file_path)) + + output_dirname = os.path.dirname(output_file_path) + + # check if output directory exists + if not os.path.exists(output_dirname): + module.fail_json(msg="directory %s does not exists" % (output_dirname)) + + # check if writable + if not os.access(os.path.dirname(output_file_path), os.W_OK): + module.fail_json(msg="Destination %s not writable" % (os.path.dirname(output_file_path))) + # write goss result on the output file + output_file(output_file_path, out) if rc is not None and rc != 0: error_msg = "err : {0} ; out : {1}".format(err, out)