Skip to content

Commit 25c9abd

Browse files
committed
more formatting && tests
Signed-off-by: Atif Ali <[email protected]>
1 parent 05d4797 commit 25c9abd

File tree

2 files changed

+99
-21
lines changed

2 files changed

+99
-21
lines changed

pkg/sync/sync_context.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,18 +973,30 @@ func formatValue(v interface{}) string {
973973

974974
// Special case for volumeClaimTemplates
975975
if templates, ok := v.([]interface{}); ok {
976-
names := []string{}
976+
var details []string
977977
for _, t := range templates {
978978
if template, ok := t.(map[string]interface{}); ok {
979979
if metadata, ok := template["metadata"].(map[string]interface{}); ok {
980980
if name, ok := metadata["name"].(string); ok {
981-
names = append(names, name)
981+
// Only include templates that have changes
982+
if spec, ok := template["spec"].(map[string]interface{}); ok {
983+
if resources, ok := spec["resources"].(map[string]interface{}); ok {
984+
if requests, ok := resources["requests"].(map[string]interface{}); ok {
985+
if storage, ok := requests["storage"].(string); ok {
986+
details = append(details, fmt.Sprintf("%s(%s)", name, storage))
987+
continue
988+
}
989+
}
990+
}
991+
}
992+
details = append(details, name)
982993
}
983994
}
984995
}
985996
}
986-
return fmt.Sprintf("templates: [%s]", strings.Join(names, ", "))
997+
return fmt.Sprintf("[%s]", strings.Join(details, ", "))
987998
}
999+
9881000
if str, ok := v.(string); ok {
9891001
return fmt.Sprintf("%q", str)
9901002
}

pkg/sync/sync_context_test.go

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,44 +2065,88 @@ func TestStatefulSetImmutableFieldErrors(t *testing.T) {
20652065
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
20662066
},
20672067
{
2068-
name: "volumeClaimTemplates change from nil",
2068+
name: "volumeClaimTemplates change with storage size",
20692069
currentSpec: map[string]interface{}{
2070-
"serviceName": "test-svc",
2070+
"volumeClaimTemplates": []interface{}{
2071+
map[string]interface{}{
2072+
"metadata": map[string]interface{}{
2073+
"name": "data",
2074+
},
2075+
"spec": map[string]interface{}{
2076+
"resources": map[string]interface{}{
2077+
"requests": map[string]interface{}{
2078+
"storage": "1Gi",
2079+
},
2080+
},
2081+
},
2082+
},
2083+
},
20712084
},
20722085
desiredSpec: map[string]interface{}{
2073-
"serviceName": "test-svc",
20742086
"volumeClaimTemplates": []interface{}{
20752087
map[string]interface{}{
20762088
"metadata": map[string]interface{}{
20772089
"name": "data",
20782090
},
2091+
"spec": map[string]interface{}{
2092+
"resources": map[string]interface{}{
2093+
"requests": map[string]interface{}{
2094+
"storage": "2Gi",
2095+
},
2096+
},
2097+
},
20792098
},
20802099
},
20812100
},
20822101
expectedMessage: `one or more objects failed to apply, reason: attempting to change immutable fields:
20832102
- volumeClaimTemplates:
2084-
from: <nil>
2085-
to: templates: [data]
2103+
from: [data(1Gi)]
2104+
to: [data(2Gi)]
20862105
20872106
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
20882107
},
20892108
{
2090-
name: "multiple immutable field changes",
2109+
name: "selector change",
20912110
currentSpec: map[string]interface{}{
2092-
"serviceName": "old-svc",
2093-
"podManagementPolicy": "OrderedReady",
2111+
"selector": map[string]interface{}{
2112+
"matchLabels": map[string]interface{}{
2113+
"app": "old-app",
2114+
},
2115+
},
20942116
},
20952117
desiredSpec: map[string]interface{}{
2096-
"serviceName": "new-svc",
2097-
"podManagementPolicy": "Parallel",
2118+
"selector": map[string]interface{}{
2119+
"matchLabels": map[string]interface{}{
2120+
"app": "new-app",
2121+
},
2122+
},
20982123
},
20992124
expectedMessage: `one or more objects failed to apply, reason: attempting to change immutable fields:
2100-
- podManagementPolicy:
2101-
from: "OrderedReady"
2102-
to: "Parallel"
2103-
- serviceName:
2104-
from: "old-svc"
2105-
to: "new-svc"
2125+
- selector:
2126+
from: map[matchLabels:map[app:old-app]]
2127+
to: map[matchLabels:map[app:new-app]]
2128+
2129+
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
2130+
},
2131+
{
2132+
name: "volumeClaimTemplates change from nil",
2133+
currentSpec: map[string]interface{}{
2134+
"serviceName": "test-svc",
2135+
},
2136+
desiredSpec: map[string]interface{}{
2137+
"serviceName": "test-svc",
2138+
"volumeClaimTemplates": []interface{}{
2139+
map[string]interface{}{
2140+
"metadata": map[string]interface{}{
2141+
"name": "data",
2142+
},
2143+
},
2144+
},
2145+
},
2146+
expectedMessage: `one or more objects failed to apply, reason: attempting to change immutable fields:
2147+
- volumeClaimTemplates:
2148+
from: <nil>
2149+
to: [data]
21062150
21072151
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
21082152
},
@@ -2133,8 +2177,28 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
21332177
},
21342178
expectedMessage: `one or more objects failed to apply, reason: attempting to change immutable fields:
21352179
- volumeClaimTemplates:
2136-
from: templates: [data1]
2137-
to: templates: [data1, data2]
2180+
from: [data1]
2181+
to: [data1, data2]
2182+
2183+
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
2184+
},
2185+
{
2186+
name: "multiple immutable field changes",
2187+
currentSpec: map[string]interface{}{
2188+
"serviceName": "old-svc",
2189+
"podManagementPolicy": "OrderedReady",
2190+
},
2191+
desiredSpec: map[string]interface{}{
2192+
"serviceName": "new-svc",
2193+
"podManagementPolicy": "Parallel",
2194+
},
2195+
expectedMessage: `one or more objects failed to apply, reason: attempting to change immutable fields:
2196+
- podManagementPolicy:
2197+
from: "OrderedReady"
2198+
to: "Parallel"
2199+
- serviceName:
2200+
from: "old-svc"
2201+
to: "new-svc"
21382202
21392203
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
21402204
},
@@ -2184,6 +2248,8 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
21842248
sc.resourceOps = mockResourceOps
21852249

21862250
_, message := sc.applyObject(task, false, false)
2251+
t.Logf("+++Actual error message:\n%s", message)
2252+
t.Logf("+++Expected error message:\n%s", tt.expectedMessage)
21872253
assert.Equal(t, tt.expectedMessage, message)
21882254
})
21892255
}

0 commit comments

Comments
 (0)