|
| 1 | +// Copyright 2026 The kpt Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package registry |
| 16 | + |
| 17 | +import ( |
| 18 | + "io" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +type fakeBuiltin struct { |
| 25 | + name string |
| 26 | +} |
| 27 | + |
| 28 | +func (f *fakeBuiltin) ImageName() string { return f.name } |
| 29 | +func (f *fakeBuiltin) Run(_ io.Reader, _ io.Writer) error { return nil } |
| 30 | + |
| 31 | +func TestNormalizeImage(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + input string |
| 34 | + expected string |
| 35 | + }{ |
| 36 | + {"ghcr.io/kptdev/starlark:v0.5.0", "ghcr.io/kptdev/starlark"}, |
| 37 | + {"ghcr.io/kptdev/starlark@sha256:abc123", "ghcr.io/kptdev/starlark"}, |
| 38 | + {"ghcr.io/kptdev/starlark:v0.5.0@sha256:abc123", "ghcr.io/kptdev/starlark"}, |
| 39 | + {"ghcr.io/kptdev/starlark", "ghcr.io/kptdev/starlark"}, |
| 40 | + } |
| 41 | + for _, tc := range tests { |
| 42 | + t.Run(tc.input, func(t *testing.T) { |
| 43 | + assert.Equal(t, tc.expected, normalizeImage(tc.input)) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestRegisterAndLookup(t *testing.T) { |
| 49 | + registry = map[string]BuiltinFunction{} |
| 50 | + |
| 51 | + fn := &fakeBuiltin{name: "ghcr.io/kptdev/test:v1.0"} |
| 52 | + Register(fn) |
| 53 | + |
| 54 | + result := Lookup("ghcr.io/kptdev/test:v2.0") |
| 55 | + assert.NotNil(t, result) |
| 56 | + assert.Equal(t, fn, result) |
| 57 | + |
| 58 | + result = Lookup("ghcr.io/kptdev/unknown") |
| 59 | + assert.Nil(t, result) |
| 60 | +} |
| 61 | + |
| 62 | +func TestList(t *testing.T) { |
| 63 | + registry = map[string]BuiltinFunction{} |
| 64 | + |
| 65 | + Register(&fakeBuiltin{name: "ghcr.io/kptdev/fn1:v1"}) |
| 66 | + Register(&fakeBuiltin{name: "ghcr.io/kptdev/fn2:v1"}) |
| 67 | + |
| 68 | + list := List() |
| 69 | + assert.Len(t, list, 2) |
| 70 | +} |
0 commit comments