Skip to content

Commit

Permalink
fix test scenarios for file_(groupownership|owner|permissions) template
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vojtapolasek committed Jun 13, 2023
1 parent 742d63c commit 6b0b757
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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 }}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}}
Expand Down
11 changes: 9 additions & 2 deletions shared/templates/file_groupowner/tests/missing_file_test.pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}}')"
Expand Down
11 changes: 9 additions & 2 deletions shared/templates/file_owner/tests/correct_owner.pass.sh
Original file line number Diff line number Diff line change
@@ -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 }}}
Expand Down
11 changes: 9 additions & 2 deletions shared/templates/file_owner/tests/incorrect_owner.fail.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}}
Expand Down
9 changes: 8 additions & 1 deletion shared/templates/file_owner/tests/missing_file_test.pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}}')"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }}}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }}}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }}}
Expand Down

0 comments on commit 6b0b757

Please sign in to comment.