@@ -5,11 +5,16 @@ import (
55 "testing"
66
77 apiv1 "github.com/acorn-io/acorn/pkg/apis/api.acorn.io/v1"
8+ v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
9+ "github.com/acorn-io/acorn/pkg/scheme"
810 "github.com/stretchr/testify/assert"
11+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ kclient "sigs.k8s.io/controller-runtime/pkg/client"
13+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
914)
1015
1116func TestProjectCreateValidation (t * testing.T ) {
12- validator := & Validator {apiv1 .LocalRegion }
17+ validator := & Validator {DefaultRegion : apiv1 .LocalRegion }
1318
1419 tests := []struct {
1520 name string
@@ -82,11 +87,12 @@ func TestProjectCreateValidation(t *testing.T) {
8287}
8388
8489func TestProjectUpdateValidation (t * testing.T ) {
85- validator := & Validator {apiv1 .LocalRegion }
90+ validator := & Validator {DefaultRegion : apiv1 .LocalRegion }
8691 tests := []struct {
87- name string
88- newProject apiv1.Project
89- wantError bool
92+ name string
93+ newProject , oldProject apiv1.Project
94+ client kclient.Client
95+ wantError bool
9096 }{
9197 {
9298 name : "Update project to have default region, no supported regions" ,
@@ -99,6 +105,11 @@ func TestProjectUpdateValidation(t *testing.T) {
99105 },
100106 {
101107 name : "Update project to have default region and supported region" ,
108+ oldProject : apiv1.Project {
109+ Status : apiv1.ProjectStatus {
110+ DefaultRegion : "my-region" ,
111+ },
112+ },
102113 newProject : apiv1.Project {
103114 Spec : apiv1.ProjectSpec {
104115 DefaultRegion : "my-region" ,
@@ -108,6 +119,11 @@ func TestProjectUpdateValidation(t *testing.T) {
108119 },
109120 {
110121 name : "Update project to have default region and non-existent supported regions" ,
122+ oldProject : apiv1.Project {
123+ Status : apiv1.ProjectStatus {
124+ DefaultRegion : "my-region" ,
125+ },
126+ },
111127 newProject : apiv1.Project {
112128 Spec : apiv1.ProjectSpec {
113129 DefaultRegion : "my-region" ,
@@ -125,11 +141,149 @@ func TestProjectUpdateValidation(t *testing.T) {
125141 },
126142 },
127143 },
144+ {
145+ name : "Update project remove a supported region, no apps" ,
146+ oldProject : apiv1.Project {
147+ Spec : apiv1.ProjectSpec {
148+ DefaultRegion : "my-region" ,
149+ SupportedRegions : []string {"my-region" , "my-other-region" },
150+ },
151+ },
152+ newProject : apiv1.Project {
153+ Spec : apiv1.ProjectSpec {
154+ DefaultRegion : "my-region" ,
155+ SupportedRegions : []string {"my-region" },
156+ },
157+ },
158+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).Build (),
159+ },
160+ {
161+ name : "Update project remove a supported region, no apps in project" ,
162+ oldProject : apiv1.Project {
163+ Spec : apiv1.ProjectSpec {
164+ DefaultRegion : "my-region" ,
165+ SupportedRegions : []string {"my-region" , "my-other-region" },
166+ },
167+ },
168+ newProject : apiv1.Project {
169+ Spec : apiv1.ProjectSpec {
170+ DefaultRegion : "my-region" ,
171+ SupportedRegions : []string {"my-region" },
172+ },
173+ },
174+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithLists (
175+ & apiv1.AppList {
176+ Items : []apiv1.App {
177+ {
178+ ObjectMeta : metav1.ObjectMeta {
179+ Name : "my-app" ,
180+ Namespace : "other-project" ,
181+ },
182+ Spec : v1.AppInstanceSpec {
183+ Region : "my-region" ,
184+ },
185+ },
186+ },
187+ },
188+ ).Build (),
189+ },
190+ {
191+ name : "Update project remove a supported region, no apps in removed region" ,
192+ oldProject : apiv1.Project {
193+ Spec : apiv1.ProjectSpec {
194+ DefaultRegion : "my-region" ,
195+ SupportedRegions : []string {"my-region" , "my-other-region" },
196+ },
197+ },
198+ newProject : apiv1.Project {
199+ Spec : apiv1.ProjectSpec {
200+ DefaultRegion : "my-region" ,
201+ SupportedRegions : []string {"my-region" },
202+ },
203+ },
204+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithLists (
205+ & apiv1.AppList {
206+ Items : []apiv1.App {
207+ {
208+ ObjectMeta : metav1.ObjectMeta {
209+ Name : "my-app" ,
210+ },
211+ Spec : v1.AppInstanceSpec {
212+ Region : "my-region" ,
213+ },
214+ },
215+ },
216+ },
217+ ).Build (),
218+ },
219+ {
220+ name : "Update project remove a supported region with apps in removed region" ,
221+ wantError : true ,
222+ oldProject : apiv1.Project {
223+ Spec : apiv1.ProjectSpec {
224+ DefaultRegion : "my-region" ,
225+ SupportedRegions : []string {"my-region" , "my-other-region" },
226+ },
227+ },
228+ newProject : apiv1.Project {
229+ Spec : apiv1.ProjectSpec {
230+ DefaultRegion : "my-region" ,
231+ SupportedRegions : []string {"my-region" },
232+ },
233+ },
234+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithLists (
235+ & apiv1.AppList {
236+ Items : []apiv1.App {
237+ {
238+ ObjectMeta : metav1.ObjectMeta {
239+ Name : "my-app" ,
240+ },
241+ Spec : v1.AppInstanceSpec {
242+ Region : "my-other-region" ,
243+ },
244+ },
245+ },
246+ },
247+ ).Build (),
248+ },
249+ {
250+ name : "Update project remove a supported region with apps defaulted to removed region" ,
251+ wantError : true ,
252+ oldProject : apiv1.Project {
253+ Spec : apiv1.ProjectSpec {
254+ DefaultRegion : "my-region" ,
255+ SupportedRegions : []string {"my-region" , "my-other-region" },
256+ },
257+ },
258+ newProject : apiv1.Project {
259+ Spec : apiv1.ProjectSpec {
260+ DefaultRegion : "my-region" ,
261+ SupportedRegions : []string {"my-region" },
262+ },
263+ },
264+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithLists (
265+ & apiv1.AppList {
266+ Items : []apiv1.App {
267+ {
268+ ObjectMeta : metav1.ObjectMeta {
269+ Name : "my-app" ,
270+ },
271+ Status : v1.AppInstanceStatus {
272+ Defaults : v1.Defaults {
273+ Region : "my-other-region" ,
274+ },
275+ },
276+ },
277+ },
278+ },
279+ ).Build (),
280+ },
128281 }
129282
130283 for _ , tt := range tests {
131284 t .Run (tt .name , func (t * testing.T ) {
132- err := validator .ValidateUpdate (context .Background (), & tt .newProject , nil )
285+ validator .Client = tt .client
286+ err := validator .ValidateUpdate (context .Background (), & tt .newProject , & tt .oldProject )
133287 if ! tt .wantError {
134288 if err != nil {
135289 t .Fatal (err )
0 commit comments