|
4 | 4 | package krusty_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
@@ -1766,3 +1767,108 @@ metadata: |
1766 | 1767 | name: fluentd-sa-abc |
1767 | 1768 | `) |
1768 | 1769 | } |
| 1770 | + |
| 1771 | +// TestEmptyPatchFilesShouldBeIgnored verifies that empty patch files are ignored. |
| 1772 | +// Tests three cases: |
| 1773 | +// 1. Completely empty file |
| 1774 | +// 2. File with only comments |
| 1775 | +// 3. File with whitespace and comments |
| 1776 | +func TestEmptyPatchFilesShouldBeIgnored(t *testing.T) { |
| 1777 | + th := kusttest_test.MakeHarness(t) |
| 1778 | + |
| 1779 | + // Write a basic resource |
| 1780 | + th.WriteF("deployment.yaml", ` |
| 1781 | +apiVersion: apps/v1 |
| 1782 | +kind: Deployment |
| 1783 | +metadata: |
| 1784 | + name: nginx |
| 1785 | +spec: |
| 1786 | + template: |
| 1787 | + spec: |
| 1788 | + containers: |
| 1789 | + - name: nginx |
| 1790 | + image: nginx |
| 1791 | +`) |
| 1792 | + |
| 1793 | + // Create empty patch files of different types |
| 1794 | + th.WriteF("empty.yaml", ``) |
| 1795 | + th.WriteF("comments-only.yaml", ` |
| 1796 | +# This is a comment |
| 1797 | +# Another comment |
| 1798 | +`) |
| 1799 | + th.WriteF("whitespace.yaml", ` |
| 1800 | +
|
| 1801 | +# Comments with whitespace |
| 1802 | +
|
| 1803 | + # Indented comment |
| 1804 | +
|
| 1805 | +`) |
| 1806 | + |
| 1807 | + // Reference empty patches in kustomization |
| 1808 | + th.WriteK(".", ` |
| 1809 | +resources: |
| 1810 | +- deployment.yaml |
| 1811 | +patches: |
| 1812 | +- path: empty.yaml |
| 1813 | +- path: comments-only.yaml |
| 1814 | +- path: whitespace.yaml |
| 1815 | +`) |
| 1816 | + |
| 1817 | + // Empty patches should be ignored, output should be unchanged |
| 1818 | + m := th.Run(".", th.MakeDefaultOptions()) |
| 1819 | + th.AssertActualEqualsExpected(m, ` |
| 1820 | +apiVersion: apps/v1 |
| 1821 | +kind: Deployment |
| 1822 | +metadata: |
| 1823 | + name: nginx |
| 1824 | +spec: |
| 1825 | + template: |
| 1826 | + spec: |
| 1827 | + containers: |
| 1828 | + - image: nginx |
| 1829 | + name: nginx |
| 1830 | +`) |
| 1831 | +} |
| 1832 | + |
| 1833 | +// TestEmptyPatchesStrategicMergeFails verifies that empty patch files are |
| 1834 | +// handled correctly with the deprecated patchesStrategicMerge field |
| 1835 | +func TestEmptyPatchesStrategicMergeFails(t *testing.T) { |
| 1836 | + th := kusttest_test.MakeHarness(t) |
| 1837 | + |
| 1838 | + // Create a basic resource |
| 1839 | + th.WriteF("resource.yaml", ` |
| 1840 | +apiVersion: v1 |
| 1841 | +kind: ConfigMap |
| 1842 | +metadata: |
| 1843 | + name: dummy |
| 1844 | +data: |
| 1845 | + dummy: value |
| 1846 | +`) |
| 1847 | + |
| 1848 | + // Create an empty patch file |
| 1849 | + th.WriteF("empty-patch.yaml", ``) |
| 1850 | + |
| 1851 | + // Create a patch file with only comments |
| 1852 | + th.WriteF("comments-patch.yaml", ` |
| 1853 | +# This is just a comment |
| 1854 | +# Another comment |
| 1855 | +`) |
| 1856 | + |
| 1857 | + // Create kustomization using patchesStrategicMerge |
| 1858 | + th.WriteK(".", ` |
| 1859 | +resources: |
| 1860 | +- resource.yaml |
| 1861 | +patchesStrategicMerge: |
| 1862 | +- empty-patch.yaml |
| 1863 | +- comments-patch.yaml |
| 1864 | +`) |
| 1865 | + |
| 1866 | + // This fails with message |
| 1867 | + err := th.RunWithErr(".", th.MakeDefaultOptions()) |
| 1868 | + if err == nil { |
| 1869 | + t.Fatalf("expected error for empty patchesStrategicMerge files but got none") |
| 1870 | + } |
| 1871 | + if !strings.Contains(err.Error(), "patch appears to be empty") { |
| 1872 | + t.Fatalf("unexpected error: %v", err) |
| 1873 | + } |
| 1874 | +} |
0 commit comments