@@ -22,6 +22,7 @@ import (
2222func Test_newCreateHandler (t * testing.T ) {
2323 scheme := runtime .NewScheme ()
2424 _ = appsv1 .AddToScheme (scheme )
25+ _ = v1 .AddToScheme (scheme )
2526 ctx := context .Background ()
2627
2728 now := metav1.Time {Time : time .Date (2024 , time .September , 1 , 0 , 0 , 0 , 0 , time .Local )}
@@ -30,7 +31,7 @@ func Test_newCreateHandler(t *testing.T) {
3031 action * castai.ClusterAction
3132 convertFn func (i map [string ]interface {}) client.Object
3233 err error
33- want * appsv1. Deployment
34+ want runtime. Object
3435 }{
3536 "should return error when action is of a different type" : {
3637 action : & castai.ClusterAction {
@@ -72,17 +73,17 @@ func Test_newCreateHandler(t *testing.T) {
7273 Version : appsv1 .SchemeGroupVersion .Version ,
7374 Resource : "deployments" ,
7475 },
75- Object : getObj (t , newDeployment (func (d * appsv1. Deployment ) {
76- d .Labels = map [string ]string {"changed" : "true" }
76+ Object : getObj (t , newDeployment (func (d runtime. Object ) {
77+ d .( * appsv1. Deployment ). Labels = map [string ]string {"changed" : "true" }
7778 })),
7879 },
7980 },
80- objs : []runtime.Object {newDeployment (func (d * appsv1. Deployment ) {
81- d .CreationTimestamp = now
81+ objs : []runtime.Object {newDeployment (func (d runtime. Object ) {
82+ d .( * appsv1. Deployment ). CreationTimestamp = now
8283 })},
83- want : newDeployment (func (d * appsv1. Deployment ) {
84- d .CreationTimestamp = now
85- d .Labels = map [string ]string {"changed" : "true" }
84+ want : newDeployment (func (d runtime. Object ) {
85+ d .( * appsv1. Deployment ). CreationTimestamp = now
86+ d .( * appsv1. Deployment ). Labels = map [string ]string {"changed" : "true" }
8687 }),
8788 convertFn : func (i map [string ]interface {}) client.Object {
8889 out := & appsv1.Deployment {}
@@ -98,24 +99,42 @@ func Test_newCreateHandler(t *testing.T) {
9899 Version : appsv1 .SchemeGroupVersion .Version ,
99100 Resource : "deployments" ,
100101 },
101- Object : getObj (t , newDeployment (func (d * appsv1. Deployment ) {
102+ Object : getObj (t , newDeployment (func (d runtime. Object ) {
102103 })),
103104 },
104105 },
105- objs : []runtime.Object {newDeployment (func (d * appsv1. Deployment ) {
106- d .CreationTimestamp = now
107- d .Finalizers = []string {"autoscaling.cast.ai/recommendation" }
106+ objs : []runtime.Object {newDeployment (func (d runtime. Object ) {
107+ d .( * appsv1. Deployment ). CreationTimestamp = now
108+ d .( * appsv1. Deployment ). Finalizers = []string {"autoscaling.cast.ai/recommendation" }
108109 })},
109- want : newDeployment (func (d * appsv1. Deployment ) {
110- d .CreationTimestamp = now
111- d .Finalizers = []string {"autoscaling.cast.ai/recommendation" }
110+ want : newDeployment (func (d runtime. Object ) {
111+ d .( * appsv1. Deployment ). CreationTimestamp = now
112+ d .( * appsv1. Deployment ). Finalizers = []string {"autoscaling.cast.ai/recommendation" }
112113 }),
113114 convertFn : func (i map [string ]interface {}) client.Object {
114115 out := & appsv1.Deployment {}
115116 _ = runtime .DefaultUnstructuredConverter .FromUnstructured (i , out )
116117 return out
117118 },
118119 },
120+ "should create new namespace" : {
121+ action : & castai.ClusterAction {
122+ ActionCreate : & castai.ActionCreate {
123+ GroupVersionResource : castai.GroupVersionResource {
124+ Group : v1 .SchemeGroupVersion .Group ,
125+ Version : v1 .SchemeGroupVersion .Version ,
126+ Resource : "namespaces" ,
127+ },
128+ Object : getObj (t , newNamespace ()),
129+ },
130+ },
131+ want : newNamespace (),
132+ convertFn : func (i map [string ]interface {}) client.Object {
133+ out := & v1.Namespace {}
134+ _ = runtime .DefaultUnstructuredConverter .FromUnstructured (i , out )
135+ return out
136+ },
137+ },
119138 }
120139
121140 for name , test := range tests {
@@ -156,8 +175,8 @@ func getObj(t *testing.T, obj runtime.Object) map[string]interface{} {
156175 return unstructured
157176}
158177
159- func newDeployment (opts ... func (d * appsv1. Deployment )) * appsv1. Deployment {
160- out := & appsv1.Deployment {
178+ func newDeployment (opts ... func (d runtime. Object )) runtime. Object {
179+ out := appsv1.Deployment {
161180 TypeMeta : metav1.TypeMeta {
162181 Kind : "Deployment" ,
163182 APIVersion : "apps/v1" ,
@@ -170,8 +189,26 @@ func newDeployment(opts ...func(d *appsv1.Deployment)) *appsv1.Deployment {
170189 Template : v1.PodTemplateSpec {},
171190 },
172191 }
192+ var obj runtime.Object = & out
193+ for _ , opt := range opts {
194+ opt (obj )
195+ }
196+ return obj
197+ }
198+
199+ func newNamespace (opts ... func (d runtime.Object )) runtime.Object {
200+ out := v1.Namespace {
201+ TypeMeta : metav1.TypeMeta {
202+ Kind : "Namespace" ,
203+ APIVersion : "v1" ,
204+ },
205+ ObjectMeta : metav1.ObjectMeta {
206+ Name : "bob-namespace" ,
207+ },
208+ }
209+ var obj runtime.Object = & out
173210 for _ , opt := range opts {
174- opt (out )
211+ opt (obj )
175212 }
176- return out
213+ return obj
177214}
0 commit comments