Skip to content

Commit

Permalink
Add fake reactor to verify namespace on create
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis authored and skitt committed May 16, 2024
1 parent 326f6bf commit cdbf402
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/fake/basic_reactors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ var _ = Describe("Create", func() {
Expect(err).To(HaveOccurred())
})
})

Context("with namespace verification", func() {
BeforeEach(func() {
fake.AddVerifyNamespaceReactor(&t.client.Fake, "pods")
})

When("the namespace does not exist", func() {
It("should return an error", func() {
_, err := t.doCreate(t.pod)
Expect(resource.IsMissingNamespaceErr(err)).To(BeTrue())
Expect(resource.ExtractMissingNamespaceFromErr(err)).To(Equal(testNamespace))
})
})

When("the namespace does exist", func() {
It("should succeed", func() {
_, err := t.client.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: testNamespace},
}, metav1.CreateOptions{})
Expect(err).To(Succeed())

t.assertCreateSuccess(t.pod)
})
})
})
})

var _ = Describe("Update", func() {
Expand Down
50 changes: 50 additions & 0 deletions pkg/fake/verify_namespace_reactor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fake

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"
)

func AddVerifyNamespaceReactor(f *testing.Fake, resources ...string) {
f.Lock()
defer f.Unlock()

reactors := f.ReactionChain[0:]

react := func(a testing.Action) (bool, runtime.Object, error) {
action := a.(testing.CreateAction)

namespace := action.GetNamespace()

_, err := invokeReactors(testing.NewGetAction(corev1.SchemeGroupVersion.WithResource("namespaces"), "", namespace),
reactors)
if err != nil {
return true, nil, err
}

return false, nil, nil
}

for _, res := range resources {
f.PrependReactor("create", res, react)
}
}

0 comments on commit cdbf402

Please sign in to comment.