|
1 | 1 | /* |
2 | | -
|
3 | 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 3 | you may not use this file except in compliance with the License. |
5 | 4 | You may obtain a copy of the License at |
6 | 5 |
|
7 | | - http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
8 | 7 |
|
9 | 8 | Unless required by applicable law or agreed to in writing, software |
10 | 9 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 11 | See the License for the specific language governing permissions and |
13 | 12 | limitations under the License. |
14 | 13 | */ |
15 | | - |
16 | 14 | package mountpropagationinjector |
17 | 15 |
|
18 | 16 | import ( |
19 | | - "testing" |
20 | | - |
21 | 17 | "github.com/fluid-cloudnative/fluid/pkg/ddc/base" |
| 18 | + "github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api" |
| 19 | + |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + |
22 | 23 | corev1 "k8s.io/api/core/v1" |
23 | 24 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
24 | | - "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
25 | 27 | ) |
26 | 28 |
|
27 | | -func TestMutate(t *testing.T) { |
| 29 | +var _ = Describe("MountPropagationInjector Plugin", func() { |
28 | 30 | var ( |
29 | | - client client.Client |
| 31 | + plugin api.MutatingHandler |
30 | 32 | pod *corev1.Pod |
31 | 33 | ) |
32 | 34 |
|
33 | | - plugin, err := NewPlugin(client, "") |
34 | | - if err != nil { |
35 | | - t.Error("new plugin occurs error", err) |
36 | | - } |
37 | | - if plugin.GetName() != Name { |
38 | | - t.Errorf("GetName expect %v, got %v", Name, plugin.GetName()) |
39 | | - } |
40 | | - |
41 | | - runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") |
42 | | - if err != nil { |
43 | | - t.Errorf("fail to create the runtimeInfo with error %v", err) |
44 | | - } |
45 | | - |
46 | | - pod = &corev1.Pod{ |
47 | | - ObjectMeta: metav1.ObjectMeta{ |
48 | | - Name: "test", |
49 | | - Namespace: "test", |
50 | | - }, |
51 | | - } |
52 | | - |
53 | | - shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo}) |
54 | | - if err != nil { |
55 | | - t.Errorf("fail to mutate pod with error %v", err) |
56 | | - } |
57 | | - |
58 | | - if shouldStop { |
59 | | - t.Errorf("expect shouldStop as false, but got %v", shouldStop) |
60 | | - } |
61 | | - |
62 | | - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{}) |
63 | | - if err != nil { |
64 | | - t.Errorf("fail to mutate pod with error %v", err) |
65 | | - } |
66 | | - |
67 | | - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil}) |
68 | | - if err == nil { |
69 | | - t.Errorf("expect error is not nil") |
70 | | - } |
71 | | -} |
| 35 | + BeforeEach(func() { |
| 36 | + scheme := runtime.NewScheme() |
| 37 | + Expect(corev1.AddToScheme(scheme)).To(Succeed()) |
| 38 | + |
| 39 | + c := fake.NewClientBuilder(). |
| 40 | + WithScheme(scheme). |
| 41 | + Build() |
| 42 | + |
| 43 | + var err error |
| 44 | + plugin, err = NewPlugin(c, "") |
| 45 | + Expect(err).NotTo(HaveOccurred()) |
| 46 | + |
| 47 | + pod = &corev1.Pod{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "test", |
| 50 | + Namespace: "test", |
| 51 | + }, |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + It("returns correct plugin name", func() { |
| 56 | + Expect(plugin.GetName()).To(Equal(Name)) |
| 57 | + }) |
| 58 | + |
| 59 | + Context("when mutating a pod", func() { |
| 60 | + It("does not stop when runtimeInfo exists", func() { |
| 61 | + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") |
| 62 | + Expect(err).NotTo(HaveOccurred()) |
| 63 | + |
| 64 | + shouldStop, err := plugin.Mutate( |
| 65 | + pod, |
| 66 | + map[string]base.RuntimeInfoInterface{"test": runtimeInfo}, |
| 67 | + ) |
| 68 | + |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + Expect(shouldStop).To(BeFalse()) |
| 71 | + }) |
| 72 | + |
| 73 | + It("does not error when runtimeInfos is empty", func() { |
| 74 | + _, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{}) |
| 75 | + Expect(err).NotTo(HaveOccurred()) |
| 76 | + }) |
| 77 | + |
| 78 | + It("returns error when runtimeInfo is nil", func() { |
| 79 | + _, err := plugin.Mutate( |
| 80 | + pod, |
| 81 | + map[string]base.RuntimeInfoInterface{"test": nil}, |
| 82 | + ) |
| 83 | + Expect(err).To(HaveOccurred()) |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
0 commit comments