Skip to content

Commit b8370b5

Browse files
committed
Add even more unit tests
1 parent acf5e0b commit b8370b5

4 files changed

Lines changed: 1415 additions & 0 deletions

File tree

internal/controller/argo_test.go

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,3 +1851,377 @@ var _ = Describe("ConvertArgoHelmParametersToMap", func() {
18511851
})
18521852
})
18531853
})
1854+
1855+
var _ = Describe("newApplicationValues", func() {
1856+
It("should return extraParametersNested YAML with all extra parameters", func() {
1857+
pattern := &api.Pattern{
1858+
Spec: api.PatternSpec{
1859+
ExtraParameters: []api.PatternParameter{
1860+
{Name: "global.extraParam1", Value: "extraValue1"},
1861+
{Name: "global.extraParam2", Value: "extraValue2"},
1862+
},
1863+
},
1864+
}
1865+
result := newApplicationValues(pattern)
1866+
Expect(result).To(ContainSubstring("extraParametersNested:"))
1867+
Expect(result).To(ContainSubstring("global.extraParam1: extraValue1"))
1868+
Expect(result).To(ContainSubstring("global.extraParam2: extraValue2"))
1869+
})
1870+
1871+
It("should return only the header when no extra parameters exist", func() {
1872+
pattern := &api.Pattern{
1873+
Spec: api.PatternSpec{
1874+
ExtraParameters: []api.PatternParameter{},
1875+
},
1876+
}
1877+
result := newApplicationValues(pattern)
1878+
Expect(result).To(Equal("extraParametersNested:\n"))
1879+
})
1880+
1881+
It("should handle a single extra parameter", func() {
1882+
pattern := &api.Pattern{
1883+
Spec: api.PatternSpec{
1884+
ExtraParameters: []api.PatternParameter{
1885+
{Name: "key", Value: "value"},
1886+
},
1887+
},
1888+
}
1889+
result := newApplicationValues(pattern)
1890+
Expect(result).To(Equal("extraParametersNested:\n key: value\n"))
1891+
})
1892+
})
1893+
1894+
var _ = Describe("removeApplication", func() {
1895+
Context("when the application exists", func() {
1896+
It("should delete the application without error", func() {
1897+
app := &argoapi.Application{
1898+
ObjectMeta: metav1.ObjectMeta{
1899+
Name: "test-app",
1900+
Namespace: "openshift-gitops",
1901+
},
1902+
}
1903+
argoClient := argoclient.NewSimpleClientset(app)
1904+
1905+
err := removeApplication(argoClient, "test-app", "openshift-gitops")
1906+
Expect(err).ToNot(HaveOccurred())
1907+
1908+
// Verify the application is gone
1909+
_, err = argoClient.ArgoprojV1alpha1().Applications("openshift-gitops").Get(
1910+
context.Background(), "test-app", metav1.GetOptions{})
1911+
Expect(err).To(HaveOccurred())
1912+
Expect(kerrors.IsNotFound(err)).To(BeTrue())
1913+
})
1914+
})
1915+
1916+
Context("when the application does not exist", func() {
1917+
It("should return an error", func() {
1918+
argoClient := argoclient.NewSimpleClientset()
1919+
err := removeApplication(argoClient, "nonexistent", "openshift-gitops")
1920+
Expect(err).To(HaveOccurred())
1921+
})
1922+
})
1923+
})
1924+
1925+
var _ = Describe("newArgoGiteaApplication", func() {
1926+
var pattern *api.Pattern
1927+
1928+
BeforeEach(func() {
1929+
tmpFalse := false
1930+
pattern = &api.Pattern{
1931+
ObjectMeta: metav1.ObjectMeta{
1932+
Name: "test-pattern",
1933+
Namespace: "default",
1934+
},
1935+
Spec: api.PatternSpec{
1936+
ClusterGroupName: "hub",
1937+
GitConfig: api.GitConfig{
1938+
TargetRepo: "https://github.com/test/repo",
1939+
TargetRevision: "main",
1940+
},
1941+
GitOpsConfig: &api.GitOpsConfig{
1942+
ManualSync: false,
1943+
},
1944+
MultiSourceConfig: api.MultiSourceConfig{
1945+
Enabled: &tmpFalse,
1946+
},
1947+
},
1948+
Status: api.PatternStatus{
1949+
AppClusterDomain: "apps.example.com",
1950+
ClusterPlatform: "AWS",
1951+
ClusterVersion: "4.14.0",
1952+
},
1953+
}
1954+
PatternsOperatorConfig = GitOpsConfig{}
1955+
})
1956+
1957+
It("should create the Gitea application with correct name", func() {
1958+
app := newArgoGiteaApplication(pattern)
1959+
Expect(app.Name).To(Equal(GiteaApplicationName))
1960+
Expect(app.Namespace).To(Equal(getClusterWideArgoNamespace()))
1961+
})
1962+
1963+
It("should set the pattern label", func() {
1964+
app := newArgoGiteaApplication(pattern)
1965+
Expect(app.Labels).To(HaveKeyWithValue("validatedpatterns.io/pattern", "test-pattern"))
1966+
})
1967+
1968+
It("should set the destination namespace to GiteaNamespace", func() {
1969+
app := newArgoGiteaApplication(pattern)
1970+
Expect(app.Spec.Destination.Namespace).To(Equal(GiteaNamespace))
1971+
})
1972+
1973+
It("should set destination to in-cluster", func() {
1974+
app := newArgoGiteaApplication(pattern)
1975+
Expect(app.Spec.Destination.Name).To(Equal("in-cluster"))
1976+
})
1977+
1978+
It("should set the project to default", func() {
1979+
app := newArgoGiteaApplication(pattern)
1980+
Expect(app.Spec.Project).To(Equal("default"))
1981+
})
1982+
1983+
It("should include helm parameters for gitea admin secret and console href", func() {
1984+
app := newArgoGiteaApplication(pattern)
1985+
Expect(app.Spec.Source).ToNot(BeNil())
1986+
Expect(app.Spec.Source.Helm).ToNot(BeNil())
1987+
1988+
params := app.Spec.Source.Helm.Parameters
1989+
paramMap := make(map[string]string)
1990+
for _, p := range params {
1991+
paramMap[p.Name] = p.Value
1992+
}
1993+
Expect(paramMap).To(HaveKeyWithValue("gitea.admin.existingSecret", GiteaAdminSecretName))
1994+
Expect(paramMap).To(HaveKey("gitea.console.href"))
1995+
Expect(paramMap["gitea.console.href"]).To(ContainSubstring("apps.example.com"))
1996+
Expect(paramMap).To(HaveKey("gitea.config.server.ROOT_URL"))
1997+
})
1998+
1999+
It("should have the foreground propagation finalizer", func() {
2000+
app := newArgoGiteaApplication(pattern)
2001+
Expect(controllerutil.ContainsFinalizer(app, argoapi.ForegroundPropagationPolicyFinalizer)).To(BeTrue())
2002+
})
2003+
2004+
It("should set a sync policy when not manual sync", func() {
2005+
app := newArgoGiteaApplication(pattern)
2006+
Expect(app.Spec.SyncPolicy).ToNot(BeNil())
2007+
Expect(app.Spec.SyncPolicy.Automated).ToNot(BeNil())
2008+
})
2009+
2010+
It("should have nil sync policy when manual sync is enabled", func() {
2011+
pattern.Spec.GitOpsConfig.ManualSync = true
2012+
app := newArgoGiteaApplication(pattern)
2013+
Expect(app.Spec.SyncPolicy).To(BeNil())
2014+
})
2015+
})
2016+
2017+
var _ = Describe("newArgoCD", func() {
2018+
It("should create an ArgoCD with the correct name and namespace", func() {
2019+
argo := newArgoCD("test-argo", "test-ns")
2020+
Expect(argo.Name).To(Equal("test-argo"))
2021+
Expect(argo.Namespace).To(Equal("test-ns"))
2022+
})
2023+
2024+
It("should have the argoproj.io/finalizer", func() {
2025+
argo := newArgoCD("test-argo", "test-ns")
2026+
Expect(argo.Finalizers).To(ContainElement("argoproj.io/finalizer"))
2027+
})
2028+
2029+
It("should have HA disabled", func() {
2030+
argo := newArgoCD("test-argo", "test-ns")
2031+
Expect(argo.Spec.HA.Enabled).To(BeFalse())
2032+
})
2033+
2034+
It("should have monitoring disabled", func() {
2035+
argo := newArgoCD("test-argo", "test-ns")
2036+
Expect(argo.Spec.Monitoring.Enabled).To(BeFalse())
2037+
})
2038+
2039+
It("should have notifications disabled", func() {
2040+
argo := newArgoCD("test-argo", "test-ns")
2041+
Expect(argo.Spec.Notifications.Enabled).To(BeFalse())
2042+
})
2043+
2044+
It("should have SSO configured with Dex provider", func() {
2045+
argo := newArgoCD("test-argo", "test-ns")
2046+
Expect(argo.Spec.SSO).ToNot(BeNil())
2047+
Expect(argo.Spec.SSO.Provider).To(Equal(argooperator.SSOProviderTypeDex))
2048+
Expect(argo.Spec.SSO.Dex).ToNot(BeNil())
2049+
Expect(argo.Spec.SSO.Dex.OpenShiftOAuth).To(BeTrue())
2050+
})
2051+
2052+
It("should have server route enabled with reencrypt TLS", func() {
2053+
argo := newArgoCD("test-argo", "test-ns")
2054+
Expect(argo.Spec.Server.Route.Enabled).To(BeTrue())
2055+
Expect(argo.Spec.Server.Route.TLS).ToNot(BeNil())
2056+
Expect(argo.Spec.Server.Route.TLS.Termination).To(Equal(routev1.TLSTerminationReencrypt))
2057+
})
2058+
2059+
It("should have resource exclusions for tekton", func() {
2060+
argo := newArgoCD("test-argo", "test-ns")
2061+
Expect(argo.Spec.ResourceExclusions).To(ContainSubstring("tekton.dev"))
2062+
Expect(argo.Spec.ResourceExclusions).To(ContainSubstring("TaskRun"))
2063+
Expect(argo.Spec.ResourceExclusions).To(ContainSubstring("PipelineRun"))
2064+
})
2065+
2066+
It("should have resource health checks for Subscription", func() {
2067+
argo := newArgoCD("test-argo", "test-ns")
2068+
Expect(argo.Spec.ResourceHealthChecks).To(HaveLen(1))
2069+
Expect(argo.Spec.ResourceHealthChecks[0].Group).To(Equal("operators.coreos.com"))
2070+
Expect(argo.Spec.ResourceHealthChecks[0].Kind).To(Equal("Subscription"))
2071+
})
2072+
2073+
It("should have init containers for CA cert fetching", func() {
2074+
argo := newArgoCD("test-argo", "test-ns")
2075+
Expect(argo.Spec.Repo.InitContainers).To(HaveLen(1))
2076+
Expect(argo.Spec.Repo.InitContainers[0].Name).To(Equal("fetch-ca"))
2077+
})
2078+
2079+
It("should have correct RBAC policy", func() {
2080+
argo := newArgoCD("test-argo", "test-ns")
2081+
Expect(argo.Spec.RBAC.Policy).ToNot(BeNil())
2082+
Expect(*argo.Spec.RBAC.Policy).To(ContainSubstring("cluster-admins"))
2083+
})
2084+
})
2085+
2086+
var _ = Describe("commonSyncPolicy", func() {
2087+
It("should return automated sync policy when not deleting and not manual", func() {
2088+
pattern := &api.Pattern{
2089+
Spec: api.PatternSpec{
2090+
GitOpsConfig: &api.GitOpsConfig{ManualSync: false},
2091+
},
2092+
}
2093+
policy := commonSyncPolicy(pattern)
2094+
Expect(policy).ToNot(BeNil())
2095+
Expect(policy.Automated).ToNot(BeNil())
2096+
Expect(policy.Automated.Prune).To(BeFalse())
2097+
})
2098+
2099+
It("should return nil sync policy when manual sync is enabled", func() {
2100+
pattern := &api.Pattern{
2101+
Spec: api.PatternSpec{
2102+
GitOpsConfig: &api.GitOpsConfig{ManualSync: true},
2103+
},
2104+
}
2105+
policy := commonSyncPolicy(pattern)
2106+
Expect(policy).To(BeNil())
2107+
})
2108+
})
2109+
2110+
var _ = Describe("applicationName", func() {
2111+
It("should return pattern name combined with cluster group name", func() {
2112+
pattern := &api.Pattern{
2113+
ObjectMeta: metav1.ObjectMeta{Name: "my-pattern"},
2114+
Spec: api.PatternSpec{ClusterGroupName: "hub"},
2115+
}
2116+
Expect(applicationName(pattern)).To(Equal("my-pattern-hub"))
2117+
})
2118+
2119+
It("should handle different cluster group names", func() {
2120+
pattern := &api.Pattern{
2121+
ObjectMeta: metav1.ObjectMeta{Name: "industrial-edge"},
2122+
Spec: api.PatternSpec{ClusterGroupName: "factory"},
2123+
}
2124+
Expect(applicationName(pattern)).To(Equal("industrial-edge-factory"))
2125+
})
2126+
})
2127+
2128+
var _ = Describe("syncApplication", func() {
2129+
Context("when no sync is in progress", func() {
2130+
It("should set the sync operation with prune", func() {
2131+
app := &argoapi.Application{
2132+
ObjectMeta: metav1.ObjectMeta{
2133+
Name: "test-app",
2134+
Namespace: "openshift-gitops",
2135+
},
2136+
}
2137+
argoFakeClient := argoclient.NewSimpleClientset(app)
2138+
2139+
err := syncApplication(argoFakeClient, app, true)
2140+
Expect(err).ToNot(HaveOccurred())
2141+
Expect(app.Operation).ToNot(BeNil())
2142+
Expect(app.Operation.Sync).ToNot(BeNil())
2143+
Expect(app.Operation.Sync.Prune).To(BeTrue())
2144+
Expect(app.Operation.Sync.SyncOptions).To(ContainElement("Force=true"))
2145+
})
2146+
2147+
It("should set the sync operation without prune", func() {
2148+
app := &argoapi.Application{
2149+
ObjectMeta: metav1.ObjectMeta{
2150+
Name: "test-app",
2151+
Namespace: "openshift-gitops",
2152+
},
2153+
}
2154+
argoFakeClient := argoclient.NewSimpleClientset(app)
2155+
2156+
err := syncApplication(argoFakeClient, app, false)
2157+
Expect(err).ToNot(HaveOccurred())
2158+
Expect(app.Operation).ToNot(BeNil())
2159+
Expect(app.Operation.Sync.Prune).To(BeFalse())
2160+
})
2161+
})
2162+
2163+
Context("when a matching sync is already in progress", func() {
2164+
It("should return nil without updating", func() {
2165+
app := &argoapi.Application{
2166+
ObjectMeta: metav1.ObjectMeta{
2167+
Name: "test-app",
2168+
Namespace: "openshift-gitops",
2169+
},
2170+
Operation: &argoapi.Operation{
2171+
Sync: &argoapi.SyncOperation{
2172+
Prune: true,
2173+
SyncOptions: []string{"Force=true"},
2174+
},
2175+
},
2176+
}
2177+
argoFakeClient := argoclient.NewSimpleClientset(app)
2178+
2179+
err := syncApplication(argoFakeClient, app, true)
2180+
Expect(err).ToNot(HaveOccurred())
2181+
})
2182+
})
2183+
})
2184+
2185+
var _ = Describe("getChildApplications", func() {
2186+
Context("when child applications exist", func() {
2187+
It("should return the child applications", func() {
2188+
parentApp := &argoapi.Application{
2189+
ObjectMeta: metav1.ObjectMeta{
2190+
Name: "parent-app",
2191+
Namespace: "openshift-gitops",
2192+
},
2193+
}
2194+
childApp := &argoapi.Application{
2195+
ObjectMeta: metav1.ObjectMeta{
2196+
Name: "child-app",
2197+
Namespace: "openshift-gitops",
2198+
Labels: map[string]string{
2199+
"app.kubernetes.io/instance": "parent-app",
2200+
},
2201+
},
2202+
}
2203+
argoFakeClient := argoclient.NewSimpleClientset(parentApp, childApp)
2204+
2205+
children, err := getChildApplications(argoFakeClient, parentApp)
2206+
Expect(err).ToNot(HaveOccurred())
2207+
Expect(children).To(HaveLen(1))
2208+
Expect(children[0].Name).To(Equal("child-app"))
2209+
})
2210+
})
2211+
2212+
Context("when no child applications exist", func() {
2213+
It("should return an empty list", func() {
2214+
parentApp := &argoapi.Application{
2215+
ObjectMeta: metav1.ObjectMeta{
2216+
Name: "parent-app",
2217+
Namespace: "openshift-gitops",
2218+
},
2219+
}
2220+
argoFakeClient := argoclient.NewSimpleClientset(parentApp)
2221+
2222+
children, err := getChildApplications(argoFakeClient, parentApp)
2223+
Expect(err).ToNot(HaveOccurred())
2224+
Expect(children).To(BeEmpty())
2225+
})
2226+
})
2227+
})

0 commit comments

Comments
 (0)