From c2281b8aa48008ef2483757ddb3d1d65db5a34cc Mon Sep 17 00:00:00 2001
From: Miha Purg <miha.purg@canonical.com>
Date: Wed, 21 Feb 2024 16:13:26 +0100
Subject: [PATCH 1/2] Fix ubuntu tests in template grub2_bootloader_argument

---
 .../tests/arg_not_there_etcdefaultgrub.fail.sh              | 6 +++++-
 .../grub2_bootloader_argument/tests/correct_value.pass.sh   | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
index 93907c79405..b594abe6de4 100644
--- a/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
+++ b/shared/templates/grub2_bootloader_argument/tests/arg_not_there_etcdefaultgrub.fail.sh
@@ -1,7 +1,11 @@
 #!/bin/bash
 
-# platform = multi_platform_fedora,multi_platform_ol,multi_platform_rhel
+# platform = multi_platform_fedora,multi_platform_ol,multi_platform_rhel,multi_platform_ubuntu
+{{%- if 'ubuntu' in product %}}
+# packages = grub2
+{{%- else %}}
 # packages = grub2,grubby
+{{%- endif %}}
 
 source common.sh
 
diff --git a/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh b/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh
index 2a0ea70caeb..25f7d6a8f62 100644
--- a/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh
+++ b/shared/templates/grub2_bootloader_argument/tests/correct_value.pass.sh
@@ -1,7 +1,12 @@
 #!/bin/bash
 
 # platform = multi_platform_all
+{{%- if 'ubuntu' in product %}}
+# packages = grub2
+{{%- else %}}
 # packages = grub2,grubby
+{{%- endif %}}
+
 {{%- if ARG_VARIABLE %}}
 # variables = {{{ ARG_VARIABLE }}}=correct_value
 {{%- set ARG_NAME_VALUE= ARG_NAME ~ "=correct_value" %}}

From b0e18855dde4ae5aa71a3773f4014733d71e26de Mon Sep 17 00:00:00 2001
From: Miha Purg <miha.purg@canonical.com>
Date: Wed, 21 Feb 2024 16:14:43 +0100
Subject: [PATCH 2/2] Fix grub2_bootloader_argument to check
 /etc/default/grub.d on Ubuntu

---
 ...ootloader_disable_recovery_set_to_true.xml |  4 ++++
 .../grub2_bootloader_argument/oval.template   |  8 ++++++++
 .../tests/correct_value_configdir.pass.sh     | 19 +++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 shared/templates/grub2_bootloader_argument/tests/correct_value_configdir.pass.sh

diff --git a/shared/checks/oval/bootloader_disable_recovery_set_to_true.xml b/shared/checks/oval/bootloader_disable_recovery_set_to_true.xml
index ff64177d6df..e58cc95c228 100644
--- a/shared/checks/oval/bootloader_disable_recovery_set_to_true.xml
+++ b/shared/checks/oval/bootloader_disable_recovery_set_to_true.xml
@@ -24,7 +24,11 @@
 
   <ind:textfilecontent54_object id="object_bootloader_disable_recovery_argument"
   version="1">
+    {{%- if 'ubuntu' in product %}}
+    <ind:filepath operation="pattern match">^/etc/default/grub(\.d/[^/]+\.cfg)?$</ind:filepath>
+    {{%- else %}}
     <ind:filepath>/etc/default/grub</ind:filepath>
+    {{%- endif %}}
     <ind:pattern operation="pattern match">^\s*GRUB_DISABLE_RECOVERY=(.*)$</ind:pattern>
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
   </ind:textfilecontent54_object>
diff --git a/shared/templates/grub2_bootloader_argument/oval.template b/shared/templates/grub2_bootloader_argument/oval.template
index b8ef28297a8..63478873a01 100644
--- a/shared/templates/grub2_bootloader_argument/oval.template
+++ b/shared/templates/grub2_bootloader_argument/oval.template
@@ -88,7 +88,11 @@
   </ind:textfilecontent54_test>
 
   <ind:textfilecontent54_object id="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument" version="1">
+    {{%- if 'ubuntu' in product %}}
+    <ind:filepath operation="pattern match">^/etc/default/grub(\.d/[^/]+\.cfg)?$</ind:filepath>
+    {{%- else %}}
     <ind:filepath>/etc/default/grub</ind:filepath>
+    {{%- endif %}}
     <ind:pattern operation="pattern match">^\s*GRUB_CMDLINE_LINUX="(.*)"$</ind:pattern>
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
   </ind:textfilecontent54_object>
@@ -102,7 +106,11 @@
 
   <ind:textfilecontent54_object id="object_grub2_{{{ SANITIZED_ARG_NAME }}}_argument_default"
   version="1">
+    {{%- if 'ubuntu' in product %}}
+    <ind:filepath operation="pattern match">^/etc/default/grub(\.d/[^/]+\.cfg)?$</ind:filepath>
+    {{%- else %}}
     <ind:filepath>/etc/default/grub</ind:filepath>
+    {{%- endif %}}
     <ind:pattern operation="pattern match">^\s*GRUB_CMDLINE_LINUX_DEFAULT="(.*)"$</ind:pattern>
     <ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
   </ind:textfilecontent54_object>
diff --git a/shared/templates/grub2_bootloader_argument/tests/correct_value_configdir.pass.sh b/shared/templates/grub2_bootloader_argument/tests/correct_value_configdir.pass.sh
new file mode 100644
index 00000000000..734142a4e3a
--- /dev/null
+++ b/shared/templates/grub2_bootloader_argument/tests/correct_value_configdir.pass.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+# platform = multi_platform_ubuntu
+# packages = grub2
+
+{{%- if ARG_VARIABLE %}}
+# variables = {{{ ARG_VARIABLE }}}=correct_value
+{{%- set ARG_NAME_VALUE= ARG_NAME ~ "=correct_value" %}}
+{{%- endif %}}
+
+source common.sh
+
+echo > /etc/default/grub
+rm -f /etc/default/grub.d/*
+
+echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX {{{ ARG_NAME_VALUE }}}\"" > /etc/default/grub.d/custom.cfg
+
+{{{ grub_command("update") }}}
+