Skip to content

Commit decf153

Browse files
committed
Add a test for reconcileInstanceConfigMap
1 parent e0b55ff commit decf153

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed

internal/controller/postgrescluster/instance_test.go

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3434

35+
"github.com/crunchydata/postgres-operator/internal/collector"
3536
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
37+
"github.com/crunchydata/postgres-operator/internal/feature"
3638
"github.com/crunchydata/postgres-operator/internal/initialize"
3739
"github.com/crunchydata/postgres-operator/internal/logging"
3840
"github.com/crunchydata/postgres-operator/internal/naming"
@@ -2018,3 +2020,286 @@ func TestCleanupDisruptionBudgets(t *testing.T) {
20182020
})
20192021
})
20202022
}
2023+
2024+
func TestReconcileInstanceConfigMap(t *testing.T) {
2025+
ctx := context.Background()
2026+
_, cc := setupKubernetes(t)
2027+
require.ParallelCapacity(t, 1)
2028+
2029+
r := &Reconciler{
2030+
Client: cc,
2031+
Owner: client.FieldOwner(t.Name()),
2032+
}
2033+
2034+
t.Run("LocalVolumeOtelDisabled", func(t *testing.T) {
2035+
ns := setupNamespace(t, cc)
2036+
cluster := testCluster()
2037+
cluster.Namespace = ns.Name
2038+
cluster.Name = "test-hippo-1"
2039+
assert.NilError(t, cc.Create(ctx, cluster))
2040+
2041+
spec := &v1beta1.PostgresInstanceSetSpec{}
2042+
instance := &appsv1.StatefulSet{
2043+
ObjectMeta: metav1.ObjectMeta{
2044+
Name: cluster.Name + "-instance",
2045+
Namespace: ns.Name,
2046+
},
2047+
}
2048+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2049+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2050+
2051+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2052+
assert.NilError(t, err)
2053+
assert.Equal(t, cm.Name, "test-hippo-1-instance-config")
2054+
assert.Equal(t, cm.Data["collector.yaml"], "")
2055+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2056+
})
2057+
2058+
t.Run("CloudRepoOtelDisabled", func(t *testing.T) {
2059+
ns := setupNamespace(t, cc)
2060+
cluster := testCluster()
2061+
cluster.Namespace = ns.Name
2062+
cluster.Name = "test-hippo-2"
2063+
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{{
2064+
Name: "repo1",
2065+
GCS: &v1beta1.RepoGCS{
2066+
Bucket: "test-bucket",
2067+
},
2068+
}}
2069+
assert.NilError(t, cc.Create(ctx, cluster))
2070+
2071+
spec := &v1beta1.PostgresInstanceSetSpec{}
2072+
instance := &appsv1.StatefulSet{
2073+
ObjectMeta: metav1.ObjectMeta{
2074+
Name: cluster.Name + "-instance",
2075+
Namespace: ns.Name,
2076+
},
2077+
}
2078+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2079+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2080+
2081+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2082+
assert.NilError(t, err)
2083+
assert.Equal(t, cm.Name, "test-hippo-2-instance-config")
2084+
assert.Equal(t, cm.Data["collector.yaml"], "")
2085+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2086+
})
2087+
2088+
t.Run("LocalVolumeOtelMetricsEnabled", func(t *testing.T) {
2089+
gate := feature.NewGate()
2090+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2091+
feature.OpenTelemetryMetrics: true,
2092+
}))
2093+
ctx := feature.NewContext(context.Background(), gate)
2094+
2095+
ns := setupNamespace(t, cc)
2096+
cluster := testCluster()
2097+
cluster.Namespace = ns.Name
2098+
cluster.Name = "test-hippo-3"
2099+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2100+
assert.NilError(t, cc.Create(ctx, cluster))
2101+
2102+
spec := &v1beta1.PostgresInstanceSetSpec{}
2103+
instance := &appsv1.StatefulSet{
2104+
ObjectMeta: metav1.ObjectMeta{
2105+
Name: cluster.Name + "-instance",
2106+
Namespace: ns.Name,
2107+
},
2108+
}
2109+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2110+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2111+
2112+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2113+
assert.NilError(t, err)
2114+
assert.Equal(t, cm.Name, "test-hippo-3-instance-config")
2115+
// We test the contents of the collector yaml elsewhere, I just want to
2116+
// make sure that it isn't empty here
2117+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2118+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2119+
})
2120+
2121+
t.Run("LocalVolumeOtelLogsEnabled", func(t *testing.T) {
2122+
gate := feature.NewGate()
2123+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2124+
feature.OpenTelemetryLogs: true,
2125+
}))
2126+
ctx := feature.NewContext(context.Background(), gate)
2127+
2128+
ns := setupNamespace(t, cc)
2129+
cluster := testCluster()
2130+
cluster.Namespace = ns.Name
2131+
cluster.Name = "test-hippo-4"
2132+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2133+
assert.NilError(t, cc.Create(ctx, cluster))
2134+
2135+
spec := &v1beta1.PostgresInstanceSetSpec{}
2136+
instance := &appsv1.StatefulSet{
2137+
ObjectMeta: metav1.ObjectMeta{
2138+
Name: cluster.Name + "-instance",
2139+
Namespace: ns.Name,
2140+
},
2141+
}
2142+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2143+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2144+
2145+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2146+
assert.NilError(t, err)
2147+
assert.Equal(t, cm.Name, "test-hippo-4-instance-config")
2148+
// We test the contents of the collector and logrotate configs elsewhere,
2149+
// I just want to test that they aren't empty here
2150+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2151+
assert.Assert(t, len(cm.Data["logrotate.conf"]) > 0)
2152+
})
2153+
2154+
t.Run("CloudRepoOtelMetricsEnabled", func(t *testing.T) {
2155+
gate := feature.NewGate()
2156+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2157+
feature.OpenTelemetryMetrics: true,
2158+
}))
2159+
ctx := feature.NewContext(context.Background(), gate)
2160+
2161+
ns := setupNamespace(t, cc)
2162+
cluster := testCluster()
2163+
cluster.Namespace = ns.Name
2164+
cluster.Name = "test-hippo-5"
2165+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2166+
assert.NilError(t, cc.Create(ctx, cluster))
2167+
2168+
spec := &v1beta1.PostgresInstanceSetSpec{}
2169+
instance := &appsv1.StatefulSet{
2170+
ObjectMeta: metav1.ObjectMeta{
2171+
Name: cluster.Name + "-instance",
2172+
Namespace: ns.Name,
2173+
},
2174+
}
2175+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2176+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2177+
2178+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2179+
assert.NilError(t, err)
2180+
assert.Equal(t, cm.Name, "test-hippo-5-instance-config")
2181+
// We test the contents of the collector yaml elsewhere, I just want to
2182+
// make sure that it isn't empty here
2183+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2184+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2185+
})
2186+
2187+
t.Run("CloudRepoOtelLogsEnabled", func(t *testing.T) {
2188+
gate := feature.NewGate()
2189+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2190+
feature.OpenTelemetryLogs: true,
2191+
}))
2192+
ctx := feature.NewContext(context.Background(), gate)
2193+
2194+
ns := setupNamespace(t, cc)
2195+
cluster := testCluster()
2196+
cluster.Namespace = ns.Name
2197+
cluster.Name = "test-hippo-6"
2198+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2199+
assert.NilError(t, cc.Create(ctx, cluster))
2200+
2201+
spec := &v1beta1.PostgresInstanceSetSpec{}
2202+
instance := &appsv1.StatefulSet{
2203+
ObjectMeta: metav1.ObjectMeta{
2204+
Name: cluster.Name + "-instance",
2205+
Namespace: ns.Name,
2206+
},
2207+
}
2208+
pgParameters := r.generatePostgresParameters(ctx, cluster, true)
2209+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2210+
2211+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, true)
2212+
assert.NilError(t, err)
2213+
assert.Equal(t, cm.Name, "test-hippo-6-instance-config")
2214+
// We test the contents of the collector and logrotate configs elsewhere,
2215+
// I just want to test that they aren't empty here
2216+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2217+
assert.Assert(t, len(cm.Data["logrotate.conf"]) > 0)
2218+
})
2219+
2220+
t.Run("BackupsDisabledOtelDisabled", func(t *testing.T) {
2221+
ns := setupNamespace(t, cc)
2222+
cluster := testCluster()
2223+
cluster.Namespace = ns.Name
2224+
cluster.Name = "test-hippo-7"
2225+
assert.NilError(t, cc.Create(ctx, cluster))
2226+
2227+
spec := &v1beta1.PostgresInstanceSetSpec{}
2228+
instance := &appsv1.StatefulSet{
2229+
ObjectMeta: metav1.ObjectMeta{
2230+
Name: cluster.Name + "-instance",
2231+
Namespace: ns.Name,
2232+
},
2233+
}
2234+
pgParameters := r.generatePostgresParameters(ctx, cluster, false)
2235+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2236+
2237+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, false)
2238+
assert.NilError(t, err)
2239+
assert.Equal(t, cm.Name, "test-hippo-7-instance-config")
2240+
assert.Equal(t, cm.Data["collector.yaml"], "")
2241+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2242+
})
2243+
2244+
t.Run("BackupsDisabledOtelMetricsEnabled", func(t *testing.T) {
2245+
gate := feature.NewGate()
2246+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2247+
feature.OpenTelemetryMetrics: true,
2248+
}))
2249+
ctx := feature.NewContext(context.Background(), gate)
2250+
2251+
ns := setupNamespace(t, cc)
2252+
cluster := testCluster()
2253+
cluster.Namespace = ns.Name
2254+
cluster.Name = "test-hippo-8"
2255+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2256+
assert.NilError(t, cc.Create(ctx, cluster))
2257+
2258+
spec := &v1beta1.PostgresInstanceSetSpec{}
2259+
instance := &appsv1.StatefulSet{
2260+
ObjectMeta: metav1.ObjectMeta{
2261+
Name: cluster.Name + "-instance",
2262+
Namespace: ns.Name,
2263+
},
2264+
}
2265+
pgParameters := r.generatePostgresParameters(ctx, cluster, false)
2266+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2267+
2268+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, false)
2269+
assert.NilError(t, err)
2270+
assert.Equal(t, cm.Name, "test-hippo-8-instance-config")
2271+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2272+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2273+
})
2274+
2275+
t.Run("BackupsDisabledOtelLogsEnabled", func(t *testing.T) {
2276+
gate := feature.NewGate()
2277+
assert.NilError(t, gate.SetFromMap(map[string]bool{
2278+
feature.OpenTelemetryLogs: true,
2279+
}))
2280+
ctx := feature.NewContext(context.Background(), gate)
2281+
2282+
ns := setupNamespace(t, cc)
2283+
cluster := testCluster()
2284+
cluster.Namespace = ns.Name
2285+
cluster.Name = "test-hippo-9"
2286+
cluster.Spec.Instrumentation = &v1beta1.InstrumentationSpec{}
2287+
assert.NilError(t, cc.Create(ctx, cluster))
2288+
2289+
spec := &v1beta1.PostgresInstanceSetSpec{}
2290+
instance := &appsv1.StatefulSet{
2291+
ObjectMeta: metav1.ObjectMeta{
2292+
Name: cluster.Name + "-instance",
2293+
Namespace: ns.Name,
2294+
},
2295+
}
2296+
pgParameters := r.generatePostgresParameters(ctx, cluster, false)
2297+
otelConfig := collector.NewConfigForPostgresPod(ctx, cluster, pgParameters)
2298+
2299+
cm, err := r.reconcileInstanceConfigMap(ctx, cluster, spec, instance, otelConfig, false)
2300+
assert.NilError(t, err)
2301+
assert.Equal(t, cm.Name, "test-hippo-9-instance-config")
2302+
assert.Assert(t, len(cm.Data["collector.yaml"]) > 0)
2303+
assert.Equal(t, cm.Data["logrotate.conf"], "")
2304+
})
2305+
}

0 commit comments

Comments
 (0)