|
1 | 1 | package proxy |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | corev1 "k8s.io/api/core/v1" |
8 | 9 | "k8s.io/apimachinery/pkg/api/errors" |
9 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 11 | "k8s.io/apimachinery/pkg/labels" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
11 | 13 | "k8s.io/apiserver/pkg/authentication/user" |
12 | 14 | apirequest "k8s.io/apiserver/pkg/endpoints/request" |
13 | 15 | "k8s.io/apiserver/pkg/registry/rest" |
@@ -101,6 +103,27 @@ func TestCreateProjectOK(t *testing.T) { |
101 | 103 | } |
102 | 104 | } |
103 | 105 |
|
| 106 | +func TestCreateProjectValidation(t *testing.T) { |
| 107 | + mockClient := &fake.Clientset{} |
| 108 | + storage := NewREST(mockClient.CoreV1().Namespaces(), &mockLister{}, nil, nil) |
| 109 | + |
| 110 | + validationCalled := false |
| 111 | + validationFunc := func(ctx context.Context, obj runtime.Object) error { |
| 112 | + validationCalled = true |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + _, err := storage.Create(apirequest.NewContext(), &projectapi.Project{ |
| 117 | + ObjectMeta: metav1.ObjectMeta{Name: "foo"}, |
| 118 | + }, validationFunc, &metav1.CreateOptions{}) |
| 119 | + if err != nil { |
| 120 | + t.Errorf("Unexpected non-nil error: %#v", err) |
| 121 | + } |
| 122 | + if !validationCalled { |
| 123 | + t.Errorf("Expected validation function to be called") |
| 124 | + } |
| 125 | +} |
| 126 | + |
104 | 127 | func TestGetProjectOK(t *testing.T) { |
105 | 128 | mockClient := fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) |
106 | 129 | storage := NewREST(mockClient.CoreV1().Namespaces(), &mockLister{}, nil, nil) |
@@ -136,9 +159,26 @@ func TestDeleteProject(t *testing.T) { |
136 | 159 | t.Errorf("Expected status=success, got: %#v", status) |
137 | 160 | } |
138 | 161 | if len(mockClient.Actions()) != 1 { |
139 | | - t.Errorf("Expected client action for delete") |
| 162 | + t.Errorf("Expected client action for delete, got %v", mockClient.Actions()) |
140 | 163 | } |
141 | 164 | if !mockClient.Actions()[0].Matches("delete", "namespaces") { |
142 | | - t.Errorf("Expected call to delete-namespace") |
| 165 | + t.Errorf("Expected call to delete-namespace, got %#v", mockClient.Actions()[0]) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestDeleteProjectValidation(t *testing.T) { |
| 170 | + mockClient := &fake.Clientset{} |
| 171 | + storage := REST{ |
| 172 | + client: mockClient.CoreV1().Namespaces(), |
| 173 | + } |
| 174 | + validationCalled := false |
| 175 | + validationFunc := func(ctx context.Context, obj runtime.Object) error { |
| 176 | + validationCalled = true |
| 177 | + return nil |
| 178 | + } |
| 179 | + |
| 180 | + storage.Delete(apirequest.NewContext(), "foo", validationFunc, &metav1.DeleteOptions{}) |
| 181 | + if !validationCalled { |
| 182 | + t.Errorf("Expected validation function to be called") |
143 | 183 | } |
144 | 184 | } |
0 commit comments