@@ -1127,6 +1127,14 @@ metadata:
1127
1127
}
1128
1128
1129
1129
func TestRemarshalResources (t * testing.T ) {
1130
+ getRequests := func (un * unstructured.Unstructured ) map [string ]interface {} {
1131
+ return un .Object ["spec" ].(map [string ]interface {})["containers" ].([]interface {})[0 ].(map [string ]interface {})["resources" ].(map [string ]interface {})["requests" ].(map [string ]interface {})
1132
+ }
1133
+
1134
+ setRequests := func (un * unstructured.Unstructured , requests map [string ]interface {}) {
1135
+ un .Object ["spec" ].(map [string ]interface {})["containers" ].([]interface {})[0 ].(map [string ]interface {})["resources" ].(map [string ]interface {})["requests" ] = requests
1136
+ }
1137
+
1130
1138
manifest := []byte (`
1131
1139
apiVersion: v1
1132
1140
kind: Pod
@@ -1142,14 +1150,27 @@ spec:
1142
1150
` )
1143
1151
un := unstructured.Unstructured {}
1144
1152
err := yaml .Unmarshal (manifest , & un )
1145
- assert .NoError (t , err )
1146
- requestsBefore := un .Object ["spec" ].(map [string ]interface {})["containers" ].([]interface {})[0 ].(map [string ]interface {})["resources" ].(map [string ]interface {})["requests" ].(map [string ]interface {})
1147
- t .Log (requestsBefore )
1148
- newUn := remarshal (& un , applyOptions (diffOptionsForTest ()))
1149
- requestsAfter := newUn .Object ["spec" ].(map [string ]interface {})["containers" ].([]interface {})[0 ].(map [string ]interface {})["resources" ].(map [string ]interface {})["requests" ].(map [string ]interface {})
1150
- t .Log (requestsAfter )
1151
- assert .Equal (t , float64 (0.2 ), requestsBefore ["cpu" ])
1152
- assert .Equal (t , "200m" , requestsAfter ["cpu" ])
1153
+ require .NoError (t , err )
1154
+
1155
+ testCases := []struct {
1156
+ name string
1157
+ cpu any
1158
+ expectedCPU any
1159
+ }{
1160
+ {"from float" , 0.2 , "200m" },
1161
+ {"from float64" , float64 (0.2 ), "200m" },
1162
+ {"from string" , "0.2" , "200m" },
1163
+ {"from invalid" , "invalid" , "invalid" },
1164
+ }
1165
+
1166
+ for _ , tc := range testCases {
1167
+ t .Run (tc .name , func (t * testing.T ) {
1168
+ setRequests (& un , map [string ]interface {}{"cpu" : tc .cpu })
1169
+ newUn := remarshal (& un , applyOptions (diffOptionsForTest ()))
1170
+ requestsAfter := getRequests (newUn )
1171
+ assert .Equal (t , tc .expectedCPU , requestsAfter ["cpu" ])
1172
+ })
1173
+ }
1153
1174
}
1154
1175
1155
1176
func ExampleDiff () {
0 commit comments