|
| 1 | +// Copyright 2026 The kpt and Nephio 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 fnruntime |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/kptdev/kpt/internal/types" |
| 23 | + fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" |
| 24 | + kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" |
| 25 | + "github.com/kptdev/kpt/pkg/lib/runneroptions" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "sigs.k8s.io/kustomize/kyaml/filesys" |
| 29 | + "sigs.k8s.io/kustomize/kyaml/yaml" |
| 30 | +) |
| 31 | + |
| 32 | +func TestFunctionRunner_Conditions(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + fsys := filesys.MakeFsInMemory() |
| 35 | + celEnv, err := runneroptions.NewCELEnvironment() |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + inputNodes := []*yaml.RNode{ |
| 39 | + yaml.MustParse("apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: app-config"), |
| 40 | + } |
| 41 | + |
| 42 | + testCases := []struct { |
| 43 | + name string |
| 44 | + fn *kptfile.Function |
| 45 | + condition string |
| 46 | + expectRun bool |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "builtin runtime - condition met", |
| 50 | + fn: &kptfile.Function{ |
| 51 | + Image: runneroptions.FuncGenPkgContext, |
| 52 | + }, |
| 53 | + condition: "resources.exists(r, r.kind == 'ConfigMap')", |
| 54 | + expectRun: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "builtin runtime - condition not met", |
| 58 | + fn: &kptfile.Function{ |
| 59 | + Image: runneroptions.FuncGenPkgContext, |
| 60 | + }, |
| 61 | + condition: "resources.exists(r, r.kind == 'Deployment')", |
| 62 | + expectRun: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "executable runtime - condition met", |
| 66 | + fn: &kptfile.Function{ |
| 67 | + Exec: "my-exec", |
| 68 | + }, |
| 69 | + condition: "resources.size() > 0", |
| 70 | + expectRun: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "executable runtime - condition not met", |
| 74 | + fn: &kptfile.Function{ |
| 75 | + Exec: "my-exec", |
| 76 | + }, |
| 77 | + condition: "resources.size() == 0", |
| 78 | + expectRun: false, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tc := range testCases { |
| 83 | + t.Run(tc.name, func(t *testing.T) { |
| 84 | + tc.fn.Condition = tc.condition |
| 85 | + results := fnresult.NewResultList() |
| 86 | + |
| 87 | + // Mock runner options |
| 88 | + opts := runneroptions.RunnerOptions{ |
| 89 | + CELEnvironment: celEnv, |
| 90 | + ResolveToImage: func(image string) string { return image }, |
| 91 | + } |
| 92 | + |
| 93 | + // We use a mock runner to avoid actual execution |
| 94 | + runner, err := NewRunner(ctx, fsys, tc.fn, types.UniquePath("pkg"), results, opts, nil) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + // Override the Run function to track if it's called |
| 98 | + wasRun := false |
| 99 | + runner.filter.Run = func(r io.Reader, w io.Writer) error { |
| 100 | + wasRun = true |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + _, err = runner.Filter(inputNodes) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + assert.Equal(t, tc.expectRun, wasRun, "Run state mismatch for: %s", tc.name) |
| 108 | + assert.Equal(t, !tc.expectRun, runner.WasSkipped(), "Skip state mismatch for: %s", tc.name) |
| 109 | + |
| 110 | + if !tc.expectRun { |
| 111 | + require.NotEmpty(t, results.Items) |
| 112 | + assert.True(t, results.Items[0].Skipped) |
| 113 | + assert.Equal(t, 0, results.Items[0].ExitCode) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments