Skip to content

Commit 042acc1

Browse files
committed
Fix issue where the presence of --target-timeline was adding --target-action.
Adjust tests and add more test cases.
1 parent 0cd8943 commit 042acc1

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

internal/controller/postgrescluster/pgbackrest.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,16 @@ func (r *Reconciler) reconcileRestoreJob(ctx context.Context,
11661166
"--pg1-path=" + pgdata,
11671167
"--repo=" + regexRepoIndex.FindString(repoName)}...)
11681168

1169+
// Look specifically for the "--target" flag, NOT flags that contain
1170+
// "--target" (e.g. "--target-timeline")
1171+
targetRegex, err := regexp.Compile("--target[ =]")
1172+
if err != nil {
1173+
return err
1174+
}
11691175
var deltaOptFound, foundTarget bool
11701176
for _, opt := range opts {
11711177
switch {
1172-
case strings.Contains(opt, "--target"):
1178+
case targetRegex.Match([]byte(opt)):
11731179
foundTarget = true
11741180
case strings.Contains(opt, "--delta"):
11751181
deltaOptFound = true

internal/controller/postgrescluster/pgbackrest_test.go

+87-1
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,9 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
17781778
configCount, jobCount, pvcCount int
17791779
invalidSourceRepo, invalidSourceCluster, invalidOptions bool
17801780
expectedClusterCondition *metav1.Condition
1781+
expectedEventMessage string
1782+
expectedCommandPieces []string
1783+
missingCommandPieces []string
17811784
}
17821785

17831786
for _, dedicated := range []bool{true, false} {
@@ -1800,6 +1803,8 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18001803
configCount: 1, jobCount: 1, pvcCount: 1,
18011804
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: false,
18021805
expectedClusterCondition: nil,
1806+
expectedCommandPieces: []string{"--stanza=", "--pg1-path=", "--repo=", "--delta"},
1807+
missingCommandPieces: []string{"--target-action"},
18031808
},
18041809
}, {
18051810
desc: "invalid source cluster",
@@ -1813,6 +1818,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18131818
configCount: 0, jobCount: 0, pvcCount: 0,
18141819
invalidSourceRepo: false, invalidSourceCluster: true, invalidOptions: false,
18151820
expectedClusterCondition: nil,
1821+
expectedEventMessage: "does not exist",
18161822
},
18171823
}, {
18181824
desc: "invalid source repo",
@@ -1826,6 +1832,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18261832
configCount: 1, jobCount: 0, pvcCount: 0,
18271833
invalidSourceRepo: true, invalidSourceCluster: false, invalidOptions: false,
18281834
expectedClusterCondition: nil,
1835+
expectedEventMessage: "does not have a repo named",
18291836
},
18301837
}, {
18311838
desc: "invalid option: --repo=",
@@ -1840,6 +1847,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18401847
configCount: 1, jobCount: 0, pvcCount: 1,
18411848
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
18421849
expectedClusterCondition: nil,
1850+
expectedEventMessage: "Option '--repo' is not allowed: please use the 'repoName' field instead.",
18431851
},
18441852
}, {
18451853
desc: "invalid option: --repo ",
@@ -1854,6 +1862,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18541862
configCount: 1, jobCount: 0, pvcCount: 1,
18551863
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
18561864
expectedClusterCondition: nil,
1865+
expectedEventMessage: "Option '--repo' is not allowed: please use the 'repoName' field instead.",
18571866
},
18581867
}, {
18591868
desc: "invalid option: stanza",
@@ -1868,6 +1877,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18681877
configCount: 1, jobCount: 0, pvcCount: 1,
18691878
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
18701879
expectedClusterCondition: nil,
1880+
expectedEventMessage: "Option '--stanza' is not allowed: the operator will automatically set this option",
18711881
},
18721882
}, {
18731883
desc: "invalid option: pg1-path",
@@ -1882,6 +1892,68 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
18821892
configCount: 1, jobCount: 0, pvcCount: 1,
18831893
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
18841894
expectedClusterCondition: nil,
1895+
expectedEventMessage: "Option '--pg1-path' is not allowed: the operator will automatically set this option",
1896+
},
1897+
}, {
1898+
desc: "invalid option: target-action",
1899+
dataSource: &v1beta1.DataSource{PostgresCluster: &v1beta1.PostgresClusterDataSource{
1900+
ClusterName: "invalid-target-action-option", RepoName: "repo1",
1901+
Options: []string{"--target-action"},
1902+
}},
1903+
clusterBootstrapped: false,
1904+
sourceClusterName: "invalid-target-action-option",
1905+
sourceClusterRepos: []v1beta1.PGBackRestRepo{{Name: "repo1"}},
1906+
result: testResult{
1907+
configCount: 1, jobCount: 0, pvcCount: 1,
1908+
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
1909+
expectedClusterCondition: nil,
1910+
expectedEventMessage: "Option '--target-action' is not allowed: the operator will automatically set this option",
1911+
},
1912+
}, {
1913+
desc: "invalid option: link-map",
1914+
dataSource: &v1beta1.DataSource{PostgresCluster: &v1beta1.PostgresClusterDataSource{
1915+
ClusterName: "invalid-link-map-option", RepoName: "repo1",
1916+
Options: []string{"--link-map"},
1917+
}},
1918+
clusterBootstrapped: false,
1919+
sourceClusterName: "invalid-link-map-option",
1920+
sourceClusterRepos: []v1beta1.PGBackRestRepo{{Name: "repo1"}},
1921+
result: testResult{
1922+
configCount: 1, jobCount: 0, pvcCount: 1,
1923+
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: true,
1924+
expectedClusterCondition: nil,
1925+
expectedEventMessage: "Option '--link-map' is not allowed: the operator will automatically set this option",
1926+
},
1927+
}, {
1928+
desc: "valid option: target-timeline",
1929+
dataSource: &v1beta1.DataSource{PostgresCluster: &v1beta1.PostgresClusterDataSource{
1930+
ClusterName: "valid-target-timeline-option", RepoName: "repo1",
1931+
Options: []string{"--target-timeline=1"},
1932+
}},
1933+
clusterBootstrapped: false,
1934+
sourceClusterName: "valid-target-timeline-option",
1935+
sourceClusterRepos: []v1beta1.PGBackRestRepo{{Name: "repo1"}},
1936+
result: testResult{
1937+
configCount: 1, jobCount: 1, pvcCount: 1,
1938+
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: false,
1939+
expectedClusterCondition: nil,
1940+
expectedCommandPieces: []string{"--stanza=", "--pg1-path=", "--repo=", "--delta", "--target-timeline=1"},
1941+
missingCommandPieces: []string{"--target=", "--target-action=promote"},
1942+
},
1943+
}, {
1944+
desc: "valid option: target",
1945+
dataSource: &v1beta1.DataSource{PostgresCluster: &v1beta1.PostgresClusterDataSource{
1946+
ClusterName: "valid-target-option", RepoName: "repo1",
1947+
Options: []string{"--target=some-date"},
1948+
}},
1949+
clusterBootstrapped: false,
1950+
sourceClusterName: "valid-target-option",
1951+
sourceClusterRepos: []v1beta1.PGBackRestRepo{{Name: "repo1"}},
1952+
result: testResult{
1953+
configCount: 1, jobCount: 1, pvcCount: 1,
1954+
invalidSourceRepo: false, invalidSourceCluster: false, invalidOptions: false,
1955+
expectedClusterCondition: nil,
1956+
expectedCommandPieces: []string{"--stanza=", "--pg1-path=", "--repo=", "--delta", "--target=some-date", "--target-action=promote"},
18851957
},
18861958
}, {
18871959
desc: "cluster bootstrapped init condition missing",
@@ -2004,6 +2076,16 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
20042076
if len(restoreJobs.Items) == 1 {
20052077
assert.Assert(t, restoreJobs.Items[0].Labels[naming.LabelStartupInstance] != "")
20062078
assert.Assert(t, restoreJobs.Items[0].Annotations[naming.PGBackRestConfigHash] != "")
2079+
for _, cmd := range tc.result.expectedCommandPieces {
2080+
assert.Assert(t, cmp.Contains(
2081+
strings.Join(restoreJobs.Items[0].Spec.Template.Spec.Containers[0].Command, " "),
2082+
cmd))
2083+
}
2084+
for _, cmd := range tc.result.missingCommandPieces {
2085+
assert.Assert(t, !strings.Contains(
2086+
strings.Join(restoreJobs.Items[0].Spec.Template.Spec.Containers[0].Command, " "),
2087+
cmd))
2088+
}
20072089
}
20082090

20092091
dataPVCs := &corev1.PersistentVolumeClaimList{}
@@ -2041,7 +2123,11 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
20412123
"involvedObject.namespace": namespace,
20422124
"reason": "InvalidDataSource",
20432125
})
2044-
return len(events.Items) == 1, err
2126+
eventExists := len(events.Items) > 0
2127+
if eventExists {
2128+
assert.Assert(t, cmp.Contains(events.Items[0].Message, tc.result.expectedEventMessage))
2129+
}
2130+
return eventExists, err
20452131
}))
20462132
}
20472133
})

0 commit comments

Comments
 (0)