@@ -4036,3 +4036,171 @@ func TestDropSELinuxChangePolicy(t *testing.T) {
4036
4036
})
4037
4037
}
4038
4038
}
4039
+
4040
+ func TestValidateAllowSidecarResizePolicy (t * testing.T ) {
4041
+ restartPolicyAlways := api .ContainerRestartPolicyAlways
4042
+ testCases := []struct {
4043
+ name string
4044
+ oldPodSpec * api.PodSpec
4045
+ wantOption bool
4046
+ }{
4047
+ {
4048
+ name : "old pod spec is nil" ,
4049
+ wantOption : false ,
4050
+ },
4051
+ {
4052
+ name : "one sidecar container + one regular init container, no resize policy set on any of them" ,
4053
+ oldPodSpec : & api.PodSpec {
4054
+ InitContainers : []api.Container {
4055
+ {
4056
+ Name : "c1-restartable-init" ,
4057
+ Image : "image" ,
4058
+ RestartPolicy : & restartPolicyAlways ,
4059
+ },
4060
+ {
4061
+ Name : "c1-init" ,
4062
+ Image : "image" ,
4063
+ },
4064
+ },
4065
+ },
4066
+ wantOption : false ,
4067
+ },
4068
+ {
4069
+ name : "one sidecar container + one regular init container, resize policy set on regular init container" ,
4070
+ oldPodSpec : & api.PodSpec {
4071
+ InitContainers : []api.Container {
4072
+ {
4073
+ Name : "c1-restartable-init" ,
4074
+ Image : "image" ,
4075
+ RestartPolicy : & restartPolicyAlways ,
4076
+ },
4077
+ {
4078
+ Name : "c1-init" ,
4079
+ Image : "image" ,
4080
+ ResizePolicy : []api.ContainerResizePolicy {
4081
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4082
+ },
4083
+ },
4084
+ },
4085
+ },
4086
+ wantOption : false ,
4087
+ },
4088
+ {
4089
+ name : "one sidecar container + one regular init container, resize policy set on sidecar container" ,
4090
+ oldPodSpec : & api.PodSpec {
4091
+ InitContainers : []api.Container {
4092
+ {
4093
+ Name : "c1-restartable-init" ,
4094
+ Image : "image" ,
4095
+ RestartPolicy : & restartPolicyAlways ,
4096
+ ResizePolicy : []api.ContainerResizePolicy {
4097
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4098
+ },
4099
+ },
4100
+ {
4101
+ Name : "c1-init" ,
4102
+ Image : "image" ,
4103
+ },
4104
+ },
4105
+ },
4106
+ wantOption : true ,
4107
+ },
4108
+ {
4109
+ name : "one sidecar container + one regular init container, resize policy set on both of them" ,
4110
+ oldPodSpec : & api.PodSpec {
4111
+ InitContainers : []api.Container {
4112
+ {
4113
+ Name : "c1-restartable-init" ,
4114
+ Image : "image" ,
4115
+ RestartPolicy : & restartPolicyAlways ,
4116
+ ResizePolicy : []api.ContainerResizePolicy {
4117
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4118
+ },
4119
+ },
4120
+ {
4121
+ Name : "c1-init" ,
4122
+ Image : "image" ,
4123
+ ResizePolicy : []api.ContainerResizePolicy {
4124
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4125
+ },
4126
+ },
4127
+ },
4128
+ },
4129
+ wantOption : true ,
4130
+ },
4131
+ {
4132
+ name : "two sidecar containers, resize policy set on one of them" ,
4133
+ oldPodSpec : & api.PodSpec {
4134
+ InitContainers : []api.Container {
4135
+ {
4136
+ Name : "c1-restartable-init" ,
4137
+ Image : "image" ,
4138
+ RestartPolicy : & restartPolicyAlways ,
4139
+ ResizePolicy : []api.ContainerResizePolicy {
4140
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4141
+ },
4142
+ },
4143
+ {
4144
+ Name : "c2-restartable-init" ,
4145
+ Image : "image" ,
4146
+ RestartPolicy : & restartPolicyAlways ,
4147
+ },
4148
+ },
4149
+ },
4150
+ wantOption : true ,
4151
+ },
4152
+ {
4153
+ name : "two regular init containers, resize policy set on both of them" ,
4154
+ oldPodSpec : & api.PodSpec {
4155
+ InitContainers : []api.Container {
4156
+ {
4157
+ Name : "c1-init" ,
4158
+ Image : "image" ,
4159
+ ResizePolicy : []api.ContainerResizePolicy {
4160
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4161
+ },
4162
+ },
4163
+ {
4164
+ Name : "c2-init" ,
4165
+ Image : "image" ,
4166
+ ResizePolicy : []api.ContainerResizePolicy {
4167
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4168
+ },
4169
+ },
4170
+ },
4171
+ },
4172
+ wantOption : false ,
4173
+ },
4174
+ {
4175
+ name : "two non-init containers, resize policy set on both of them" ,
4176
+ oldPodSpec : & api.PodSpec {
4177
+ Containers : []api.Container {
4178
+ {
4179
+ Name : "c1" ,
4180
+ Image : "image" ,
4181
+ ResizePolicy : []api.ContainerResizePolicy {
4182
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4183
+ },
4184
+ },
4185
+ {
4186
+ Name : "c2" ,
4187
+ Image : "image" ,
4188
+ ResizePolicy : []api.ContainerResizePolicy {
4189
+ {ResourceName : api .ResourceCPU , RestartPolicy : api .NotRequired },
4190
+ },
4191
+ },
4192
+ },
4193
+ },
4194
+ wantOption : false ,
4195
+ },
4196
+ }
4197
+
4198
+ for _ , tc := range testCases {
4199
+ t .Run (tc .name , func (t * testing.T ) {
4200
+ gotOptions := GetValidationOptionsFromPodSpecAndMeta (& api.PodSpec {}, tc .oldPodSpec , nil , nil )
4201
+ if tc .wantOption != gotOptions .AllowSidecarResizePolicy {
4202
+ t .Errorf ("Got AllowSidecarResizePolicy=%t, want %t" , gotOptions .AllowSidecarResizePolicy , tc .wantOption )
4203
+ }
4204
+ })
4205
+ }
4206
+ }
0 commit comments