From 6b0b75798130821b2197eafd266e6b9e1d1ad04a Mon Sep 17 00:00:00 2001 From: Vojtech Polasek Date: Tue, 13 Jun 2023 10:47:02 +0200 Subject: [PATCH] fix test scenarios for file_(groupownership|owner|permissions) template The test did not account for a case when a filepath is a directory and it does not exist on the system. Also the test made use of is_directory parameter which is only available at runtime when building the template and it is not saved anywhere. Instead of this parameter, scenarios now check if the path ends with a slash. This signifies the filepath is a directory as mentioned in the tempate documentation. --- .../file_groupowner/tests/correct_groupowner.pass.sh | 11 +++++++++-- .../tests/incorrect_groupowner.fail.sh | 11 +++++++++-- .../file_groupowner/tests/missing_file_test.pass.sh | 11 +++++++++-- .../templates/file_owner/tests/correct_owner.pass.sh | 11 +++++++++-- .../file_owner/tests/incorrect_owner.fail.sh | 11 +++++++++-- .../file_owner/tests/missing_file_test.pass.sh | 9 ++++++++- .../tests/correct_permissions.pass.sh | 11 +++++++++-- .../tests/lenient_permissions.fail.sh | 11 +++++++++-- .../tests/stricter_permisions.pass.sh | 11 +++++++++-- 9 files changed, 80 insertions(+), 17 deletions(-) diff --git a/shared/templates/file_groupowner/tests/correct_groupowner.pass.sh b/shared/templates/file_groupowner/tests/correct_groupowner.pass.sh index f7951ea64ade..b08fe1eacde2 100644 --- a/shared/templates/file_groupowner/tests/correct_groupowner.pass.sh +++ b/shared/templates/file_groupowner/tests/correct_groupowner.pass.sh @@ -1,11 +1,18 @@ #!/bin/bash {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chgrp {{{ GID_OR_NAME }}} {} \; {{% else %}} +chgrp {{{ GID_OR_NAME }}} {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_groupowner/tests/incorrect_groupowner.fail.sh b/shared/templates/file_groupowner/tests/incorrect_groupowner.fail.sh index dce6c878376f..522eba12bf4d 100644 --- a/shared/templates/file_groupowner/tests/incorrect_groupowner.fail.sh +++ b/shared/templates/file_groupowner/tests/incorrect_groupowner.fail.sh @@ -3,11 +3,18 @@ groupadd group_test {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chgrp group_test {} \; {{% else %}} +chgrp group_test {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_groupowner/tests/missing_file_test.pass.sh b/shared/templates/file_groupowner/tests/missing_file_test.pass.sh index 970c54567c71..c1fd4536b3d7 100644 --- a/shared/templates/file_groupowner/tests/missing_file_test.pass.sh +++ b/shared/templates/file_groupowner/tests/missing_file_test.pass.sh @@ -4,10 +4,17 @@ {{% if MISSING_FILE_PASS %}} rm -f {{{ path }}} {{% else %}} - {{% if IS_DIRECTORY and FILE_REGEX %}} + {{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" - {{% elif IS_DIRECTORY and RECURSIVE %}} + {{% elif path.endswith("/") and RECURSIVE %}} find -L {{{ path }}} -type d -exec chgrp {{{ GID_OR_NAME }}} {} \; +{{% else %}} + chgrp {{{ GID_OR_NAME }}} {{{ path }}} +{{% endif %}} {{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" diff --git a/shared/templates/file_owner/tests/correct_owner.pass.sh b/shared/templates/file_owner/tests/correct_owner.pass.sh index b7dfcb8865cb..f05418c94f22 100644 --- a/shared/templates/file_owner/tests/correct_owner.pass.sh +++ b/shared/templates/file_owner/tests/correct_owner.pass.sh @@ -1,11 +1,18 @@ #!/bin/bash {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chown {{{ FILEUID }}} {} \; {{% else %}} +chown {{{ FILEUID }}} {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_owner/tests/incorrect_owner.fail.sh b/shared/templates/file_owner/tests/incorrect_owner.fail.sh index 04012bfd5fc4..487da32753c0 100644 --- a/shared/templates/file_owner/tests/incorrect_owner.fail.sh +++ b/shared/templates/file_owner/tests/incorrect_owner.fail.sh @@ -3,11 +3,18 @@ useradd testuser_123 {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chown testuser_123 {} \; {{% else %}} +chown testuser_123 {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_owner/tests/missing_file_test.pass.sh b/shared/templates/file_owner/tests/missing_file_test.pass.sh index 45f176226d94..83473718096a 100644 --- a/shared/templates/file_owner/tests/missing_file_test.pass.sh +++ b/shared/templates/file_owner/tests/missing_file_test.pass.sh @@ -4,8 +4,15 @@ {{% if MISSING_FILE_PASS %}} rm -f {{{ path }}} {{% else %}} - {{% if IS_DIRECTORY and RECURSIVE %}} + {{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if RECURSIVE %}} find -L {{{ path }}} -type d -exec chown {{{ FILEUID }}} {} \; +{{% else %}} + chown {{{ FILEUID }}} {{{ path }}} +{{%endif %}} {{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" diff --git a/shared/templates/file_permissions/tests/correct_permissions.pass.sh b/shared/templates/file_permissions/tests/correct_permissions.pass.sh index db3417dcc760..aa9ad4434d44 100644 --- a/shared/templates/file_permissions/tests/correct_permissions.pass.sh +++ b/shared/templates/file_permissions/tests/correct_permissions.pass.sh @@ -1,11 +1,18 @@ #!/bin/bash {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chmod {{{ FILEMODE }}} {} \; {{% else %}} +chmod {{{ FILEMODE }}} {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_permissions/tests/lenient_permissions.fail.sh b/shared/templates/file_permissions/tests/lenient_permissions.fail.sh index 91413fca4ed7..03ba4b45f711 100644 --- a/shared/templates/file_permissions/tests/lenient_permissions.fail.sh +++ b/shared/templates/file_permissions/tests/lenient_permissions.fail.sh @@ -1,11 +1,18 @@ #!/bin/bash {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -maxdepth 1 -exec chmod 777 {} \; {{% else %}} +chmod 777 {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}} diff --git a/shared/templates/file_permissions/tests/stricter_permisions.pass.sh b/shared/templates/file_permissions/tests/stricter_permisions.pass.sh index d26853d436bf..dba17743b408 100644 --- a/shared/templates/file_permissions/tests/stricter_permisions.pass.sh +++ b/shared/templates/file_permissions/tests/stricter_permisions.pass.sh @@ -1,11 +1,18 @@ #!/bin/bash {{% for path in FILEPATH %}} -{{% if IS_DIRECTORY and FILE_REGEX %}} +{{% if path.endswith("/") %}} +if [ ! -d {{{ path }}} ]; then + mkdir -p {{{ path }}} +fi +{{% if FILE_REGEX %}} echo "Create specific tests for this rule because of regex" -{{% elif IS_DIRECTORY and RECURSIVE %}} +{{% elif RECURSIVE %}} find -L {{{ path }}} -type d -exec chmod {{{ FILEMODE }}} {} \; {{% else %}} +chmod 000 {{{ path }}} +{{% endif %}} +{{% else %}} if [ ! -f {{{ path }}} ]; then mkdir -p "$(dirname '{{{ path }}}')" touch {{{ path }}}