GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — func/cmd/invoke_test.go at main · stackbuilders/func Expand file tree/ Copy pathinvoke_test.goMore file actions 117 lines (105 loc) · 2.94 KB / Copy pathinvoke_test.goFile metadata and controls117 lines (105 loc) · 2.94 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 package cmd import ( "context" "errors" "fmt" "net" "net/http" "os" "sync/atomic" "testing" "time" fn "knative.dev/func/pkg/functions" "knative.dev/func/pkg/mock" . "knative.dev/func/pkg/testing" ) // TestInvoke command executes the invocation path. func TestInvoke(t *testing.T) { root := FromTempDirectory(t) var invoked int32 // Create a test function to be invoked if _, err := fn.New().Init(fn.Function{Runtime: "go", Root: root}); err != nil { t.Fatal(err) } // Mock Runner // Starts a service which sets invoked=1 on any request runner := mock.NewRunner() runner.RunFn = func(ctx context.Context, f fn.Function, _ string, _ time.Duration) (job *fn.Job, err error) { var ( l net.Listener h = http.NewServeMux() s = http.Server{Handler: h} ) if l, err = net.Listen("tcp4", "127.0.0.1:"); err != nil { t.Fatal(err) } h.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { atomic.StoreInt32(&invoked, 1) _, _ = res.Write([]byte("invoked")) }) go func() { if err = s.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) { fmt.Fprintf(os.Stderr, "error serving: %v", err) } }() host, port, _ := net.SplitHostPort(l.Addr().String()) errs := make(chan error, 10) stop := func() error { _ = s.Close(); return nil } return fn.NewJob(f, host, port, errs, stop, false) } // Run the mock http service function interloper f, err := fn.NewFunction(root) if err != nil { t.Fatal(err) } client := fn.New(fn.WithRunner(runner)) job, err := client.Run(t.Context(), f) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = job.Stop() }) // Test that the invoke command invokes cmd := NewInvokeCmd(NewClient) cmd.SetArgs([]string{}) if err := cmd.Execute(); err != nil { t.Fatal(err) } if atomic.LoadInt32(&invoked) != 1 { t.Fatal("function was not invoked") } } // TestInvokeExtensionsMapValid ensures well-formed key=value extensions are // parsed correctly, including values that themselves contain '='. func TestInvokeExtensionsMapValid(t *testing.T) { c := invokeConfig{Extensions: []string{"key=value", "foo=bar=baz"}} m, err := c.parseExtensions() if err != nil { t.Fatalf("unexpected error: %v", err) } if m["key"] != "value" { t.Fatalf("expected key=value, got %q", m["key"]) } if m["foo"] != "bar=baz" { t.Fatalf("expected foo=bar=baz, got %q", m["foo"]) } } // TestInvokeExtensionsMapMalformed ensures that extension entries missing '=' // or with an empty key return an error rather than being silently dropped. func TestInvokeExtensionsMapMalformed(t *testing.T) { cases := []struct { name string exts []string }{ {"missing equals", []string{"valid=ok", "badformat"}}, {"empty key", []string{"valid=ok", "=nokey"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { c := invokeConfig{Extensions: tc.exts} _, err := c.parseExtensions() if err == nil { t.Fatalf("expected error for %v, got nil", tc.exts) } }) } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |