Skip to content

Commit

Permalink
add testcase for wasm eval
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed Aug 18, 2022
1 parent 3d84647 commit 045e496
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 17 deletions.
19 changes: 19 additions & 0 deletions e2e/testdata/fn-eval/wasm-function/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 Google LLC
#
# 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.

testType: eval
image: gcr.io/kpt-fn-demo/set-namespace:v0.5.0
args:
namespace: staging
allowWasm: true
11 changes: 11 additions & 0 deletions e2e/testdata/fn-eval/wasm-function/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff --git a/resources.yaml b/resources.yaml
index e8efba7..2ac7759 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -15,5 +15,6 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
+ namespace: staging
spec:
replicas: 3
1 change: 1 addition & 0 deletions e2e/testdata/fn-eval/wasm-function/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
19 changes: 19 additions & 0 deletions e2e/testdata/fn-eval/wasm-function/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 Google LLC
#
# 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.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Google LLC
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/resources.yaml b/resources.yaml
index 7a494c9..1d36135 100644
index ac634f3..ef2a1b1 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -15,12 +15,24 @@ apiVersion: apps/v1
Expand Down
15 changes: 15 additions & 0 deletions e2e/testdata/fn-render/basicpipeline-wasm/.expected/setup.sh
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
#! /usr/bin/env bash
# Copyright 2022 Google LLC
#
# 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.

export KPT_FN_WASM_RUNTIME=wasmtime
15 changes: 15 additions & 0 deletions e2e/testdata/fn-render/basicpipeline-wasm/.expected/teardown.sh
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
#! /usr/bin/env bash
# Copyright 2022 Google LLC
#
# 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.

unset KPT_FN_WASM_RUNTIME
2 changes: 1 addition & 1 deletion e2e/testdata/fn-render/basicpipeline-wasm/resources.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Google LLC
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
7 changes: 6 additions & 1 deletion pkg/test/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
execScript string = "exec.sh"
CommandFnEval string = "eval"
CommandFnRender string = "render"

allowWasmFlag string = "--allow-alpha-wasm"
)

// NewRunner returns a new runner for pkg
Expand Down Expand Up @@ -195,6 +197,9 @@ func (r *Runner) runFnEval() error {
if destDir != "" {
kptArgs = append(kptArgs, "-o", destDir)
}
if r.testCase.Config.AllowWasm {
kptArgs = append(kptArgs, allowWasmFlag)
}
if r.testCase.Config.ImagePullPolicy != "" {
kptArgs = append(kptArgs, "--image-pull-policy", r.testCase.Config.ImagePullPolicy)
}
Expand Down Expand Up @@ -364,7 +369,7 @@ func (r *Runner) runFnRender() error {
}

if r.testCase.Config.AllowWasm {
kptArgs = append(kptArgs, "--allow-alpha-wasm")
kptArgs = append(kptArgs, allowWasmFlag)
}

if r.testCase.Config.DisableOutputTruncate {
Expand Down
1 change: 1 addition & 0 deletions thirdparty/cmdconfig/commands/cmdeval/cmdeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
// fn eval should remove all files when all resources
// are deleted.
ContinueOnEmptyResult: true,
AllowWasm: r.AllowWasm,
Selector: r.Selector,
Exclusion: r.Exclusion,
}
Expand Down
23 changes: 10 additions & 13 deletions thirdparty/kyaml/runfn/runfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,18 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
return nil, err
}
}
var fltr *runtimeutil.FunctionFilter
fltr := &runtimeutil.FunctionFilter{
FunctionConfig: fnConfig,
DeferFailure: spec.DeferFailure,
}
fnResult := &fnresult.Result{
// TODO(droot): This is required for making structured results subpackage aware.
// Enable this once test harness supports filepath based assertions.
// Pkg: string(r.uniquePath),
}
if spec.Container.Image != "" {
fnResult.Image = spec.Container.Image

// If AllowWasm is true, we try to use the image field as a wasm image.
// TODO: we can be smarter here. If the image doesn't support wasm/js platform,
// it should fallback to run it as container fn.
Expand Down Expand Up @@ -382,16 +387,13 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
AllowMount: true,
},
}
fltr = &runtimeutil.FunctionFilter{
Run: c.Run,
FunctionConfig: fnConfig,
DeferFailure: spec.DeferFailure,
}
fnResult.Image = spec.Container.Image
fltr.Run = c.Run
}
}

if spec.Exec.Path != "" {
fnResult.ExecPath = r.OriginalExec

if r.AllowWasm && strings.HasSuffix(spec.Exec.Path, ".wasm") {
wFn, err := fnruntime.NewWasmFn(&fnruntime.FsLoader{Filename: spec.Exec.Path})
if err != nil {
Expand All @@ -404,12 +406,7 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
Args: r.ExecArgs,
FnResult: fnResult,
}
fltr = &runtimeutil.FunctionFilter{
Run: e.Run,
FunctionConfig: fnConfig,
DeferFailure: spec.DeferFailure,
}
fnResult.ExecPath = r.OriginalExec
fltr.Run = e.Run
}
}

Expand Down

0 comments on commit 045e496

Please sign in to comment.