|
| 1 | +/* |
| 2 | +Copyright 2026 The Kube Bind 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 mapper_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | + |
| 26 | + "github.com/kube-bind/kube-bind/v2/konnector/engine/mapper" |
| 27 | +) |
| 28 | + |
| 29 | +var widgetGVR = schema.GroupVersionResource{Group: "example.org", Version: "v1", Resource: "widgets"} |
| 30 | + |
| 31 | +func TestIdentity_RoundTrips(t *testing.T) { |
| 32 | + m := mapper.Identity{} |
| 33 | + in := mapper.ObjectKey{Namespace: "team-a", Name: "w1"} |
| 34 | + |
| 35 | + prov, err := m.ToProvider(widgetGVR, in) |
| 36 | + require.NoError(t, err) |
| 37 | + require.Equal(t, in, prov, "Identity must not change the key on the way to the provider") |
| 38 | + |
| 39 | + back, err := m.ToConsumer(widgetGVR, prov) |
| 40 | + require.NoError(t, err) |
| 41 | + require.Equal(t, in, back, "Identity must round-trip") |
| 42 | +} |
| 43 | + |
| 44 | +// prefixMapper is an out-of-tree-style Mapper that prefixes the provider |
| 45 | +// namespace, exercising the interface the way a custom build would (v1's |
| 46 | +// "Prefixed" isolation). It lives in the test to prove the seam is usable and |
| 47 | +// that the round-trip contract is satisfiable by a non-identity mapping. |
| 48 | +type prefixMapper struct{ prefix string } |
| 49 | + |
| 50 | +func (p prefixMapper) ToProvider(_ schema.GroupVersionResource, key mapper.ObjectKey) (mapper.ObjectKey, error) { |
| 51 | + return mapper.ObjectKey{Namespace: p.prefix + key.Namespace, Name: key.Name}, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (p prefixMapper) ToConsumer(_ schema.GroupVersionResource, key mapper.ObjectKey) (mapper.ObjectKey, error) { |
| 55 | + return mapper.ObjectKey{Namespace: strings.TrimPrefix(key.Namespace, p.prefix), Name: key.Name}, nil |
| 56 | +} |
| 57 | + |
| 58 | +func TestMapper_NonIdentityRoundTrips(t *testing.T) { |
| 59 | + var m mapper.Mapper = prefixMapper{prefix: "consumer-7-"} |
| 60 | + in := mapper.ObjectKey{Namespace: "team-a", Name: "w1"} |
| 61 | + |
| 62 | + prov, err := m.ToProvider(widgetGVR, in) |
| 63 | + require.NoError(t, err) |
| 64 | + require.Equal(t, "consumer-7-team-a", prov.Namespace) |
| 65 | + require.Equal(t, "w1", prov.Name) |
| 66 | + |
| 67 | + back, err := m.ToConsumer(widgetGVR, prov) |
| 68 | + require.NoError(t, err) |
| 69 | + require.Equal(t, in, back, "a Mapper must round-trip: ToConsumer(ToProvider(k)) == k") |
| 70 | +} |
0 commit comments