|
4 | 4 | package controllers |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
| 8 | + "errors" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/stretchr/testify/assert" |
10 | 12 | "github.com/stretchr/testify/require" |
| 13 | + corev1 "k8s.io/api/core/v1" |
11 | 14 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
12 | 15 | "k8s.io/apimachinery/pkg/runtime" |
13 | 16 | "k8s.io/apimachinery/pkg/types" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
14 | 18 | "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
15 | 20 | "sigs.k8s.io/controller-runtime/pkg/reconcile" |
16 | 21 |
|
17 | 22 | mcpv1beta1 "github.com/stacklok/toolhive/cmd/thv-operator/api/v1beta1" |
@@ -97,6 +102,167 @@ func TestVirtualMCPCompositeToolDefinitionReconciler_Reconcile(t *testing.T) { |
97 | 102 | } |
98 | 103 | } |
99 | 104 |
|
| 105 | +func TestVirtualMCPCompositeToolDefinitionReconciler_ReconcileNotFound(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + scheme := runtime.NewScheme() |
| 109 | + require.NoError(t, mcpv1beta1.AddToScheme(scheme)) |
| 110 | + |
| 111 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{ |
| 112 | + Client: fake.NewClientBuilder().WithScheme(scheme).Build(), |
| 113 | + } |
| 114 | + |
| 115 | + result, err := reconciler.Reconcile(t.Context(), reconcile.Request{ |
| 116 | + NamespacedName: types.NamespacedName{Name: "missing", Namespace: "default"}, |
| 117 | + }) |
| 118 | + |
| 119 | + require.NoError(t, err) |
| 120 | + assert.Equal(t, reconcile.Result{}, result) |
| 121 | +} |
| 122 | + |
| 123 | +func TestVirtualMCPCompositeToolDefinitionReconciler_ReconcileErrors(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + |
| 126 | + scheme := runtime.NewScheme() |
| 127 | + require.NoError(t, mcpv1beta1.AddToScheme(scheme)) |
| 128 | + |
| 129 | + definition := &mcpv1beta1.VirtualMCPCompositeToolDefinition{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{Name: "workflow", Namespace: "default"}, |
| 131 | + } |
| 132 | + getErr := errors.New("simulated get failure") |
| 133 | + listErr := errors.New("simulated list failure") |
| 134 | + |
| 135 | + t.Run("returns get failure", func(t *testing.T) { |
| 136 | + t.Parallel() |
| 137 | + |
| 138 | + fakeClient := fake.NewClientBuilder(). |
| 139 | + WithScheme(scheme). |
| 140 | + WithInterceptorFuncs(interceptor.Funcs{ |
| 141 | + Get: func( |
| 142 | + _ context.Context, |
| 143 | + _ client.WithWatch, |
| 144 | + _ client.ObjectKey, |
| 145 | + _ client.Object, |
| 146 | + _ ...client.GetOption, |
| 147 | + ) error { |
| 148 | + return getErr |
| 149 | + }, |
| 150 | + }). |
| 151 | + Build() |
| 152 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{Client: fakeClient} |
| 153 | + |
| 154 | + _, err := reconciler.Reconcile(t.Context(), reconcile.Request{ |
| 155 | + NamespacedName: types.NamespacedName{Name: "workflow", Namespace: "default"}, |
| 156 | + }) |
| 157 | + require.ErrorIs(t, err, getErr) |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("returns reference list failure", func(t *testing.T) { |
| 161 | + t.Parallel() |
| 162 | + |
| 163 | + fakeClient := fake.NewClientBuilder(). |
| 164 | + WithScheme(scheme). |
| 165 | + WithObjects(definition.DeepCopy()). |
| 166 | + WithInterceptorFuncs(interceptor.Funcs{ |
| 167 | + List: func( |
| 168 | + _ context.Context, |
| 169 | + _ client.WithWatch, |
| 170 | + list client.ObjectList, |
| 171 | + _ ...client.ListOption, |
| 172 | + ) error { |
| 173 | + if _, ok := list.(*mcpv1beta1.VirtualMCPServerList); ok { |
| 174 | + return listErr |
| 175 | + } |
| 176 | + return nil |
| 177 | + }, |
| 178 | + }). |
| 179 | + Build() |
| 180 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{Client: fakeClient} |
| 181 | + |
| 182 | + _, err := reconciler.Reconcile(t.Context(), reconcile.Request{ |
| 183 | + NamespacedName: types.NamespacedName{Name: definition.Name, Namespace: definition.Namespace}, |
| 184 | + }) |
| 185 | + require.ErrorIs(t, err, listErr) |
| 186 | + }) |
| 187 | +} |
| 188 | + |
| 189 | +func TestVirtualMCPCompositeToolDefinitionReconciler_MapVirtualMCPServer(t *testing.T) { |
| 190 | + t.Parallel() |
| 191 | + |
| 192 | + scheme := runtime.NewScheme() |
| 193 | + require.NoError(t, mcpv1beta1.AddToScheme(scheme)) |
| 194 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 195 | + |
| 196 | + server := virtualMCPServerWithCompositeToolRef("server", "current") |
| 197 | + server.Spec.Config.CompositeToolRefs = append(server.Spec.Config.CompositeToolRefs, |
| 198 | + vmcpconfig.CompositeToolRef{Name: "current"}) |
| 199 | + current := &mcpv1beta1.VirtualMCPCompositeToolDefinition{ |
| 200 | + ObjectMeta: metav1.ObjectMeta{Name: "current", Namespace: "default"}, |
| 201 | + Status: mcpv1beta1.VirtualMCPCompositeToolDefinitionStatus{ |
| 202 | + ReferencingVirtualServers: []string{"server"}, |
| 203 | + }, |
| 204 | + } |
| 205 | + stale := &mcpv1beta1.VirtualMCPCompositeToolDefinition{ |
| 206 | + ObjectMeta: metav1.ObjectMeta{Name: "stale", Namespace: "default"}, |
| 207 | + Status: mcpv1beta1.VirtualMCPCompositeToolDefinitionStatus{ |
| 208 | + ReferencingVirtualServers: []string{"server"}, |
| 209 | + }, |
| 210 | + } |
| 211 | + |
| 212 | + t.Run("returns current and stale definitions without duplicates", func(t *testing.T) { |
| 213 | + t.Parallel() |
| 214 | + |
| 215 | + fakeClient := fake.NewClientBuilder(). |
| 216 | + WithScheme(scheme). |
| 217 | + WithObjects(current.DeepCopy(), stale.DeepCopy()). |
| 218 | + Build() |
| 219 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{Client: fakeClient} |
| 220 | + |
| 221 | + assert.ElementsMatch(t, []reconcile.Request{ |
| 222 | + {NamespacedName: types.NamespacedName{Name: "current", Namespace: "default"}}, |
| 223 | + {NamespacedName: types.NamespacedName{Name: "stale", Namespace: "default"}}, |
| 224 | + }, reconciler.mapVirtualMCPServerToCompositeToolDefinitions(t.Context(), server.DeepCopy())) |
| 225 | + }) |
| 226 | + |
| 227 | + t.Run("returns nil for unrelated object type", func(t *testing.T) { |
| 228 | + t.Parallel() |
| 229 | + |
| 230 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{ |
| 231 | + Client: fake.NewClientBuilder().WithScheme(scheme).Build(), |
| 232 | + } |
| 233 | + pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod", Namespace: "default"}} |
| 234 | + |
| 235 | + assert.Nil(t, reconciler.mapVirtualMCPServerToCompositeToolDefinitions(t.Context(), pod)) |
| 236 | + }) |
| 237 | + |
| 238 | + t.Run("returns current definition if stale lookup fails", func(t *testing.T) { |
| 239 | + t.Parallel() |
| 240 | + |
| 241 | + listErr := errors.New("simulated definition list failure") |
| 242 | + fakeClient := fake.NewClientBuilder(). |
| 243 | + WithScheme(scheme). |
| 244 | + WithInterceptorFuncs(interceptor.Funcs{ |
| 245 | + List: func( |
| 246 | + _ context.Context, |
| 247 | + _ client.WithWatch, |
| 248 | + list client.ObjectList, |
| 249 | + _ ...client.ListOption, |
| 250 | + ) error { |
| 251 | + if _, ok := list.(*mcpv1beta1.VirtualMCPCompositeToolDefinitionList); ok { |
| 252 | + return listErr |
| 253 | + } |
| 254 | + return nil |
| 255 | + }, |
| 256 | + }). |
| 257 | + Build() |
| 258 | + reconciler := &VirtualMCPCompositeToolDefinitionReconciler{Client: fakeClient} |
| 259 | + |
| 260 | + assert.Equal(t, []reconcile.Request{{ |
| 261 | + NamespacedName: types.NamespacedName{Name: "current", Namespace: "default"}, |
| 262 | + }}, reconciler.mapVirtualMCPServerToCompositeToolDefinitions(t.Context(), server.DeepCopy())) |
| 263 | + }) |
| 264 | +} |
| 265 | + |
100 | 266 | func virtualMCPServerWithCompositeToolRef(name, definitionName string) mcpv1beta1.VirtualMCPServer { |
101 | 267 | return mcpv1beta1.VirtualMCPServer{ |
102 | 268 | ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default"}, |
|
0 commit comments