|
1 | 1 | // Copyright 2021 ArgoCD Operator Developers |
| 2 | + |
2 | 3 | // |
3 | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 5 | // you may not use this file except in compliance with the License. |
@@ -1642,6 +1643,97 @@ func TestReconcileApplicationSetSourceNamespacesResources_NonClusterConfigNamesp |
1642 | 1643 | } |
1643 | 1644 | } |
1644 | 1645 |
|
| 1646 | +func TestReconcileApplicationSet_LogFormatFallback(t *testing.T) { |
| 1647 | + logf.SetLogger(ZapLogger(true)) |
| 1648 | + |
| 1649 | + tests := []struct { |
| 1650 | + name string |
| 1651 | + // logFormat has priority over logformat |
| 1652 | + logFormat string |
| 1653 | + logformat string |
| 1654 | + wantFormat string |
| 1655 | + }{ |
| 1656 | + { |
| 1657 | + // Production code path: logFormat != "" so the if-fallback is never triggered |
| 1658 | + name: "new LogFormat field is used when set", |
| 1659 | + logFormat: "json", |
| 1660 | + logformat: "", |
| 1661 | + wantFormat: "json", |
| 1662 | + }, |
| 1663 | + { |
| 1664 | + // Backward compatibility path: logFormat == "" so code enters the if block and reads the deprecated logformat field instead |
| 1665 | + name: "deprecated logformat field used as fallback when LogFormat is empty", |
| 1666 | + logFormat: "", |
| 1667 | + logformat: "json", |
| 1668 | + wantFormat: "json", |
| 1669 | + }, |
| 1670 | + { |
| 1671 | + // Priority check - both fields contain some value |
| 1672 | + name: "new LogFormat takes precedence when both fields are set", |
| 1673 | + logFormat: "json", |
| 1674 | + logformat: "text", |
| 1675 | + wantFormat: "json", |
| 1676 | + }, |
| 1677 | + { |
| 1678 | + // Both fields are empty |
| 1679 | + name: "default format text is used when neither field is set", |
| 1680 | + logFormat: "", |
| 1681 | + logformat: "", |
| 1682 | + wantFormat: common.ArgoCDDefaultLogFormat, |
| 1683 | + }, |
| 1684 | + } |
| 1685 | + |
| 1686 | + for _, tt := range tests { |
| 1687 | + t.Run(tt.name, func(t *testing.T) { |
| 1688 | + // Provides base ArgoCD CR with namespace "argocd" |
| 1689 | + a := makeTestArgoCD() |
| 1690 | + |
| 1691 | + // cr.Spec.ApplicationSet.LogFormat. |
| 1692 | + spec := argoproj.ArgoCDApplicationSet{ |
| 1693 | + LogFormat: tt.logFormat, |
| 1694 | + } |
| 1695 | + //nolint:staticcheck // intentionally setting deprecated field to exercise the backward-compatibility fallback |
| 1696 | + spec.Logformat = tt.logformat |
| 1697 | + a.Spec.ApplicationSet = &spec |
| 1698 | + |
| 1699 | + // Seed fake client with ArgoCD CR so that reconciler can read & write K8s objects without a real cluster |
| 1700 | + resObjs := []client.Object{a} |
| 1701 | + subresObjs := []client.Object{a} |
| 1702 | + runtimeObjs := []runtime.Object{} |
| 1703 | + sch := makeTestReconcilerScheme(argoproj.AddToScheme) |
| 1704 | + cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) |
| 1705 | + r := makeTestReconciler(cl, sch, testclient.NewSimpleClientset()) |
| 1706 | + |
| 1707 | + assert.NoError(t, r.reconcileApplicationSetController(a)) |
| 1708 | + |
| 1709 | + deployment := &appsv1.Deployment{} |
| 1710 | + assert.NoError(t, r.Get( |
| 1711 | + context.TODO(), |
| 1712 | + types.NamespacedName{ |
| 1713 | + Name: "argocd-applicationset-controller", |
| 1714 | + Namespace: a.Namespace, |
| 1715 | + }, |
| 1716 | + deployment)) |
| 1717 | + |
| 1718 | + cmd := deployment.Spec.Template.Spec.Containers[0].Command |
| 1719 | + logFormatIdx := -1 |
| 1720 | + for i, arg := range cmd { |
| 1721 | + if arg == "--logformat" { |
| 1722 | + logFormatIdx = i |
| 1723 | + break |
| 1724 | + } |
| 1725 | + } |
| 1726 | + |
| 1727 | + if assert.NotEqual(t, -1, logFormatIdx, |
| 1728 | + "--logFormat flag must be present in the applicationset-controller command") { |
| 1729 | + assert.Equal(t, tt.wantFormat, cmd[logFormatIdx+1], |
| 1730 | + "wrong value after --logformat flag (logFormat=%q logformat=%q)", |
| 1731 | + tt.logFormat, tt.logformat) |
| 1732 | + } |
| 1733 | + }) |
| 1734 | + } |
| 1735 | +} |
| 1736 | + |
1645 | 1737 | func TestGetApplicationSetContainerImage(t *testing.T) { |
1646 | 1738 | logf.SetLogger(ZapLogger(true)) |
1647 | 1739 |
|
|
0 commit comments