|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 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 | +*/ |
| 16 | + |
| 17 | +package crd_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/mock" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/crd" |
| 27 | + "inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/crd/mocks" |
| 28 | +) |
| 29 | + |
| 30 | +func createTempFile(t *testing.T, dir, content, pattern string) { |
| 31 | + tempFile, err := os.CreateTemp(dir, pattern) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + _, err = tempFile.WriteString(content) |
| 35 | + require.NoError(t, err) |
| 36 | + tempFile.Close() |
| 37 | +} |
| 38 | + |
| 39 | +func TestInstallCRDs(t *testing.T) { |
| 40 | + ctx := context.TODO() |
| 41 | + mockClient := new(mocks.MockClient) |
| 42 | + |
| 43 | + // Mock calls for valid CRDs |
| 44 | + mockClient.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(nil) |
| 45 | + |
| 46 | + // Valid CRD content |
| 47 | + validCRD := `apiVersion: apiextensions.k8s.io/v1 |
| 48 | +kind: CustomResourceDefinition |
| 49 | +metadata: |
| 50 | + name: valid-crd.example.com |
| 51 | +spec: |
| 52 | + group: example.com |
| 53 | + names: |
| 54 | + kind: ValidCrd |
| 55 | + listKind: ValidCrdList |
| 56 | + plural: validcrds |
| 57 | + singular: validcrd |
| 58 | + scope: Namespaced |
| 59 | + versions: |
| 60 | + - name: v1 |
| 61 | + served: true |
| 62 | + storage: true` |
| 63 | + |
| 64 | + // Non-CRD YAML content |
| 65 | + nonCRDYAML := `apiVersion: v1 |
| 66 | +kind: ConfigMap |
| 67 | +metadata: |
| 68 | + name: test-configmap` |
| 69 | + |
| 70 | + // Invalid file content |
| 71 | + invalidContent := "invalid content" |
| 72 | + |
| 73 | + tests := []struct { |
| 74 | + description string |
| 75 | + setup func(dir string) |
| 76 | + expectError bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + description: "Directory with valid CRD file", |
| 80 | + setup: func(dir string) { |
| 81 | + createTempFile(t, dir, validCRD, "valid-*.yaml") |
| 82 | + }, |
| 83 | + expectError: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + description: "Directory with invalid content file", |
| 87 | + setup: func(dir string) { |
| 88 | + createTempFile(t, dir, invalidContent, "invalid-*.yaml") |
| 89 | + }, |
| 90 | + expectError: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + description: "Directory with non-CRD YAML file", |
| 94 | + setup: func(dir string) { |
| 95 | + createTempFile(t, dir, nonCRDYAML, "non-crd-*.yaml") |
| 96 | + }, |
| 97 | + expectError: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + description: "Directory with mixed valid and invalid files", |
| 101 | + setup: func(dir string) { |
| 102 | + createTempFile(t, dir, validCRD, "valid-*.yaml") |
| 103 | + createTempFile(t, dir, invalidContent, "invalid-*.yaml") |
| 104 | + }, |
| 105 | + expectError: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + description: "Empty directory", |
| 109 | + setup: func(dir string) {}, // No files created |
| 110 | + expectError: false, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, test := range tests { |
| 115 | + t.Run(test.description, func(t *testing.T) { |
| 116 | + tempDir := t.TempDir() |
| 117 | + test.setup(tempDir) |
| 118 | + |
| 119 | + err := crd.InstallCRDs(ctx, mockClient, tempDir) |
| 120 | + if test.expectError { |
| 121 | + require.Error(t, err, "Expected an error but got nil") |
| 122 | + } else { |
| 123 | + require.NoError(t, err, "Expected no error but got one") |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + // Assert mockClient expectations |
| 129 | + mockClient.AssertExpectations(t) |
| 130 | +} |
0 commit comments