|
1 | | -/* |
2 | | -Copyright 2021. |
3 | | -
|
4 | | -Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -you may not use this file except in compliance with the License. |
6 | | -You may obtain a copy of the License at |
7 | | -
|
8 | | - http://www.apache.org/licenses/LICENSE-2.0 |
9 | | -
|
10 | | -Unless required by applicable law or agreed to in writing, software |
11 | | -distributed under the License is distributed on an "AS IS" BASIS, |
12 | | -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | -See the License for the specific language governing permissions and |
14 | | -limitations under the License. |
15 | | -*/ |
| 1 | +// Copyright (c) 2018-2026 Splunk Inc. All rights reserved. |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
16 | 14 |
|
17 | 15 | package v1alpha1 |
18 | 16 |
|
19 | 17 | import ( |
20 | 18 | "context" |
21 | | - "net/http" |
22 | | - "net/url" |
23 | 19 | "testing" |
24 | 20 |
|
25 | | - "github.com/go-logr/logr" |
26 | 21 | appsv1alpha1 "github.com/splunk/splunk-operator/api/apps/v1alpha1" |
27 | | - corev1 "k8s.io/api/core/v1" |
28 | 22 | apierrors "k8s.io/apimachinery/pkg/api/errors" |
29 | | - "k8s.io/apimachinery/pkg/api/meta" |
30 | 23 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
31 | 24 | "k8s.io/apimachinery/pkg/runtime" |
32 | | - "k8s.io/client-go/rest" |
33 | | - "k8s.io/client-go/tools/record" |
34 | | - "sigs.k8s.io/controller-runtime/pkg/cache" |
35 | | - "sigs.k8s.io/controller-runtime/pkg/cache/informertest" |
36 | | - "sigs.k8s.io/controller-runtime/pkg/client" |
37 | 25 | "sigs.k8s.io/controller-runtime/pkg/client/fake" |
38 | | - "sigs.k8s.io/controller-runtime/pkg/config" |
39 | | - "sigs.k8s.io/controller-runtime/pkg/healthz" |
40 | | - logf "sigs.k8s.io/controller-runtime/pkg/log" |
41 | | - "sigs.k8s.io/controller-runtime/pkg/manager" |
42 | | - "sigs.k8s.io/controller-runtime/pkg/webhook" |
43 | 26 | ) |
44 | 27 |
|
45 | 28 | func TestAppValidatorAllowsValidObjects(t *testing.T) { |
@@ -202,78 +185,3 @@ func TestAppValidatorRejectsDuplicateAppTarget(t *testing.T) { |
202 | 185 | t.Fatalf("expected Invalid validation error, got %v", err) |
203 | 186 | } |
204 | 187 | } |
205 | | - |
206 | | -func TestAppValidatorRejectsWrongTypes(t *testing.T) { |
207 | | - validator := &AppValidator{} |
208 | | - wrongObj := &corev1.ConfigMap{} |
209 | | - |
210 | | - if _, err := validator.ValidateCreate(context.Background(), wrongObj); err == nil { |
211 | | - t.Fatalf("expected create validation to reject wrong object type") |
212 | | - } |
213 | | - if _, err := validator.ValidateUpdate(context.Background(), wrongObj, &appsv1alpha1.App{}); err == nil { |
214 | | - t.Fatalf("expected update validation to reject wrong old object type") |
215 | | - } |
216 | | - if _, err := validator.ValidateUpdate(context.Background(), &appsv1alpha1.App{}, wrongObj); err == nil { |
217 | | - t.Fatalf("expected update validation to reject wrong new object type") |
218 | | - } |
219 | | - if _, err := validator.ValidateDelete(context.Background(), wrongObj); err == nil { |
220 | | - t.Fatalf("expected delete validation to reject wrong object type") |
221 | | - } |
222 | | -} |
223 | | - |
224 | | -func TestSetupWebhookWithManager(t *testing.T) { |
225 | | - scheme := runtime.NewScheme() |
226 | | - if err := appsv1alpha1.AddToScheme(scheme); err != nil { |
227 | | - t.Fatalf("failed to add App scheme: %v", err) |
228 | | - } |
229 | | - |
230 | | - webhookServer := webhook.NewServer(webhook.Options{Port: 0}) |
231 | | - mgr := &testManager{ |
232 | | - scheme: scheme, |
233 | | - client: fake.NewClientBuilder().WithScheme(scheme).Build(), |
234 | | - webhookServer: webhookServer, |
235 | | - } |
236 | | - |
237 | | - if err := SetupWebhookWithManager(mgr); err != nil { |
238 | | - t.Fatalf("expected setup to succeed, got %v", err) |
239 | | - } |
240 | | - |
241 | | - handler, path := webhookServer.WebhookMux().Handler(&http.Request{ |
242 | | - URL: &url.URL{Path: AppValidationPath}, |
243 | | - }) |
244 | | - if path != AppValidationPath { |
245 | | - t.Fatalf("expected handler path %q, got %q", AppValidationPath, path) |
246 | | - } |
247 | | - if handler == nil { |
248 | | - t.Fatalf("expected webhook handler to be registered") |
249 | | - } |
250 | | -} |
251 | | - |
252 | | -var _ manager.Manager = &testManager{} |
253 | | - |
254 | | -type testManager struct { |
255 | | - scheme *runtime.Scheme |
256 | | - client client.Client |
257 | | - webhookServer webhook.Server |
258 | | -} |
259 | | - |
260 | | -func (m *testManager) AddMetricsServerExtraHandler(string, http.Handler) error { return nil } |
261 | | -func (m *testManager) Add(manager.Runnable) error { return nil } |
262 | | -func (m *testManager) Elected() <-chan struct{} { return nil } |
263 | | -func (m *testManager) AddHealthzCheck(string, healthz.Checker) error { return nil } |
264 | | -func (m *testManager) AddReadyzCheck(string, healthz.Checker) error { return nil } |
265 | | -func (m *testManager) Start(context.Context) error { return nil } |
266 | | -func (m *testManager) GetWebhookServer() webhook.Server { return m.webhookServer } |
267 | | -func (m *testManager) GetLogger() logr.Logger { return logf.Log.WithName("test-manager") } |
268 | | -func (m *testManager) GetControllerOptions() config.Controller { return config.Controller{} } |
269 | | -func (m *testManager) GetHTTPClient() *http.Client { return http.DefaultClient } |
270 | | -func (m *testManager) GetConfig() *rest.Config { return &rest.Config{} } |
271 | | -func (m *testManager) GetScheme() *runtime.Scheme { return m.scheme } |
272 | | -func (m *testManager) GetClient() client.Client { return m.client } |
273 | | -func (m *testManager) GetFieldIndexer() client.FieldIndexer { return nil } |
274 | | -func (m *testManager) GetCache() cache.Cache { return &informertest.FakeInformers{} } |
275 | | -func (m *testManager) GetEventRecorderFor(string) record.EventRecorder { |
276 | | - return record.NewFakeRecorder(1) |
277 | | -} |
278 | | -func (m *testManager) GetRESTMapper() meta.RESTMapper { return nil } |
279 | | -func (m *testManager) GetAPIReader() client.Reader { return m.client } |
0 commit comments