|
| 1 | +/* |
| 2 | +Copyright 2026 The Knative 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 wasmmodule_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + api "github.com/cardil/knative-serving-wasm/pkg/apis/wasm/v1alpha1" |
| 25 | + "github.com/cardil/knative-serving-wasm/pkg/reconciler/wasmmodule" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | +) |
| 29 | + |
| 30 | +// TestBuildRunnerConfigMatchesGolden verifies that the JSON produced by |
| 31 | +// BuildRunnerConfig matches the documented wire format that the runner parses. |
| 32 | +// If this test fails, both the golden file and the runner must be updated together. |
| 33 | +func TestBuildRunnerConfigMatchesGolden(t *testing.T) { |
| 34 | + t.Parallel() |
| 35 | + |
| 36 | + trueVal := true |
| 37 | + module := &api.WasmModule{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{Name: "test"}, |
| 39 | + Spec: api.WasmModuleSpec{ |
| 40 | + Image: "example.com/img:latest", |
| 41 | + Args: []string{"--verbose"}, |
| 42 | + Env: []corev1.EnvVar{ |
| 43 | + {Name: "GREETING", Value: "hello"}, |
| 44 | + {Name: "PORT", Value: "8080"}, |
| 45 | + }, |
| 46 | + VolumeMounts: []corev1.VolumeMount{ |
| 47 | + {Name: "data", MountPath: "/mnt/data", ReadOnly: false}, |
| 48 | + {Name: "ro", MountPath: "/mnt/ro", ReadOnly: true}, |
| 49 | + }, |
| 50 | + Volumes: []corev1.Volume{ |
| 51 | + {Name: "data"}, |
| 52 | + {Name: "ro"}, |
| 53 | + }, |
| 54 | + Network: &api.NetworkSpec{ |
| 55 | + Inherit: false, |
| 56 | + AllowIPNameLookup: &trueVal, |
| 57 | + TCP: &api.TCPSpec{ |
| 58 | + Connect: []string{"example.com:443"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + got, err := wasmmodule.BuildRunnerConfig(module) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("BuildRunnerConfig() error: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + goldenBytes, err := os.ReadFile("testdata/wasi_config.golden.json") |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("read golden file: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + assertJSONEqual(t, got, string(goldenBytes)) |
| 75 | +} |
| 76 | + |
| 77 | +func assertJSONEqual(t *testing.T, got, want string) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + var gotMap, wantMap any |
| 81 | + |
| 82 | + if err := json.Unmarshal([]byte(got), &gotMap); err != nil { |
| 83 | + t.Fatalf("unmarshal got: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + if err := json.Unmarshal([]byte(want), &wantMap); err != nil { |
| 87 | + t.Fatalf("unmarshal want: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + gotJSON, err := json.MarshalIndent(gotMap, "", " ") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("marshal got normalized JSON: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + wantJSON, err := json.MarshalIndent(wantMap, "", " ") |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("marshal want normalized JSON: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + if string(gotJSON) != string(wantJSON) { |
| 101 | + t.Errorf("BuildRunnerConfig output does not match golden.\ngot:\n%s\n\nwant:\n%s", gotJSON, wantJSON) |
| 102 | + } |
| 103 | +} |
0 commit comments