hs-test: auto register test actions
[vpp.git] / extras / hs-test / actions.go
index aa82f49..9885f87 100755 (executable)
@@ -10,6 +10,7 @@ import (
        "git.fd.io/govpp.git/api"
        "github.com/edwarnicke/exechelper"
        "github.com/edwarnicke/govpp/binapi/af_packet"
+       "github.com/edwarnicke/govpp/binapi/ethernet_types"
        interfaces "github.com/edwarnicke/govpp/binapi/interface"
        "github.com/edwarnicke/govpp/binapi/interface_types"
        ip_types "github.com/edwarnicke/govpp/binapi/ip_types"
@@ -18,16 +19,9 @@ import (
        "github.com/edwarnicke/vpphelper"
 )
 
-func RegisterActions() {
-       cfgTable = make(map[string]func([]string) *ActionResult)
-       reg("echo-srv-internal", Configure2Veths)
-       reg("echo-cln-internal", Configure2Veths)
-       reg("echo-client", RunEchoClient)
-       reg("echo-server", RunEchoServer)
-       reg("vpp-proxy", ConfigureVppProxy)
-       reg("vpp-envoy", ConfigureEnvoyProxy)
-       reg("http-tps", ConfigureHttpTps)
-       reg("2veths", Configure2Veths)
+type ConfFn func(context.Context, api.Connection) error
+
+type Actions struct {
 }
 
 func configureProxyTcp(ifName0, ipAddr0, ifName1, ipAddr1 string) ConfFn {
@@ -48,7 +42,18 @@ func configureProxyTcp(ifName0, ipAddr0, ifName1, ipAddr1 string) ConfFn {
        }
 }
 
-func ConfigureVppProxy(args []string) *ActionResult {
+func (a *Actions) RunHttpCliSrv(args []string) *ActionResult {
+       cmd := fmt.Sprintf("http cli server")
+       return ApiCliInband("/tmp/Configure2Veths", cmd)
+}
+
+func (a *Actions) RunHttpCliCln(args []string) *ActionResult {
+       cmd := fmt.Sprintf("http cli client uri http://10.10.10.1/80 query %s", getArgs())
+       fmt.Println(cmd)
+       return ApiCliInband("/tmp/Configure2Veths", cmd)
+}
+
+func (a *Actions) ConfigureVppProxy(args []string) *ActionResult {
        ctx, cancel := newVppContext()
        defer cancel()
 
@@ -65,7 +70,7 @@ func ConfigureVppProxy(args []string) *ActionResult {
        return nil
 }
 
-func ConfigureEnvoyProxy(args []string) *ActionResult {
+func (a *Actions) ConfigureEnvoyProxy(args []string) *ActionResult {
        var startup Stanza
        startup.
                NewStanza("session").
@@ -111,7 +116,7 @@ func ApiCliInband(root, cmd string) *ActionResult {
        return NewActionResult(err, ActionResultWithStdout(cliInbandReply.Reply))
 }
 
-func RunEchoClient(args []string) *ActionResult {
+func (a *Actions) RunEchoClient(args []string) *ActionResult {
        outBuff := bytes.NewBuffer([]byte{})
        errBuff := bytes.NewBuffer([]byte{})
 
@@ -124,7 +129,7 @@ func RunEchoClient(args []string) *ActionResult {
                ActionResultWithStderr(string(errBuff.String())))
 }
 
-func RunEchoServer(args []string) *ActionResult {
+func (a *Actions) RunEchoServer(args []string) *ActionResult {
        cmd := fmt.Sprintf("vpp_echo server TX=RX socket-name /tmp/echo-srv/var/run/app_ns_sockets/1 use-app-socket-api uri %s://10.10.10.1/12344", args[2])
        errCh := exechelper.Start(cmd)
        select {
@@ -136,20 +141,68 @@ func RunEchoServer(args []string) *ActionResult {
        return nil
 }
 
-func RunEchoSrvInternal() *ActionResult {
+func (a *Actions) RunEchoSrvInternal(args []string) *ActionResult {
        cmd := fmt.Sprintf("test echo server %s uri tcp://10.10.10.1/1234", getArgs())
-       return ApiCliInband("/tmp/2veths", cmd)
+       return ApiCliInband("/tmp/Configure2Veths", cmd)
 }
 
-func RunEchoClnInternal() *ActionResult {
+func (a *Actions) RunEchoClnInternal(args []string) *ActionResult {
        cmd := fmt.Sprintf("test echo client %s uri tcp://10.10.10.1/1234", getArgs())
-       return ApiCliInband("/tmp/2veths", cmd)
+       return ApiCliInband("/tmp/Configure2Veths", cmd)
+}
+
+func (a *Actions) RunVclEchoServer(args []string) *ActionResult {
+       f, err := os.Create("vcl_1.conf")
+       if err != nil {
+               return NewActionResult(err, ActionResultWithStderr(("create vcl config: ")))
+       }
+       fmt.Fprintf(f, vclTemplate, "/tmp/echo-srv/var/run/app_ns_sockets/1", "1")
+       f.Close()
+
+       os.Setenv("VCL_CONFIG", "/vcl_1.conf")
+       cmd := fmt.Sprintf("vcl_test_server -p %s 12346", args[2])
+       errCh := exechelper.Start(cmd)
+       select {
+       case err := <-errCh:
+               writeSyncFile(NewActionResult(err, ActionResultWithDesc("vcl_test_server: ")))
+       default:
+       }
+       writeSyncFile(OkResult())
+       return nil
 }
-func configure2vethsTopo(ifName, interfaceAddress, namespaceId string, secret uint64) ConfFn {
+
+func (a *Actions) RunVclEchoClient(args []string) *ActionResult {
+       outBuff := bytes.NewBuffer([]byte{})
+       errBuff := bytes.NewBuffer([]byte{})
+
+       f, err := os.Create("vcl_2.conf")
+       if err != nil {
+               return NewActionResult(err, ActionResultWithStderr(("create vcl config: ")))
+       }
+       fmt.Fprintf(f, vclTemplate, "/tmp/echo-cln/var/run/app_ns_sockets/2", "2")
+       f.Close()
+
+       os.Setenv("VCL_CONFIG", "/vcl_2.conf")
+       cmd := fmt.Sprintf("vcl_test_client -U -p %s 10.10.10.1 12346", args[2])
+       err = exechelper.Run(cmd,
+               exechelper.WithStdout(outBuff), exechelper.WithStderr(errBuff),
+               exechelper.WithStdout(os.Stdout), exechelper.WithStderr(os.Stderr))
+
+       return NewActionResult(err, ActionResultWithStdout(string(outBuff.String())),
+               ActionResultWithStderr(string(errBuff.String())))
+}
+
+func configure2vethsTopo(ifName, interfaceAddress, namespaceId string, secret uint64, optionalHardwareAddress ...string) ConfFn {
        return func(ctx context.Context,
                vppConn api.Connection) error {
 
-               swIfIndex, err := configureAfPacket(ctx, vppConn, ifName, interfaceAddress)
+               var swIfIndex interface_types.InterfaceIndex
+               var err error
+               if optionalHardwareAddress == nil {
+                       swIfIndex, err = configureAfPacket(ctx, vppConn, ifName, interfaceAddress)
+               } else {
+                       swIfIndex, err = configureAfPacket(ctx, vppConn, ifName, interfaceAddress, optionalHardwareAddress[0])
+               }
                if err != nil {
                        fmt.Printf("failed to create af packet: %v", err)
                }
@@ -174,7 +227,7 @@ func configure2vethsTopo(ifName, interfaceAddress, namespaceId string, secret ui
        }
 }
 
-func Configure2Veths(args []string) *ActionResult {
+func (a *Actions) Configure2Veths(args []string) *ActionResult {
        var startup Stanza
        startup.
                NewStanza("session").
@@ -191,6 +244,8 @@ func Configure2Veths(args []string) *ActionResult {
        var fn func(context.Context, api.Connection) error
        if args[2] == "srv" {
                fn = configure2vethsTopo("vppsrv", "10.10.10.1/24", "1", 1)
+       } else if args[2] == "srv-with-preset-hw-addr" {
+               fn = configure2vethsTopo("vppsrv", "10.10.10.1/24", "1", 1, "00:00:5e:00:53:01")
        } else {
                fn = configure2vethsTopo("vppcln", "10.10.10.2/24", "2", 2)
        }
@@ -204,14 +259,23 @@ func Configure2Veths(args []string) *ActionResult {
 }
 
 func configureAfPacket(ctx context.Context, vppCon api.Connection,
-       name, interfaceAddress string) (interface_types.InterfaceIndex, error) {
+       name, interfaceAddress string, optionalHardwareAddress ...string) (interface_types.InterfaceIndex, error) {
+       var err error
        ifaceClient := interfaces.NewServiceClient(vppCon)
-       afPacketCreate := &af_packet.AfPacketCreateV2{
+       afPacketCreate := af_packet.AfPacketCreateV2{
                UseRandomHwAddr: true,
                HostIfName:      name,
                NumRxQueues:     1,
        }
-       afPacketCreateRsp, err := af_packet.NewServiceClient(vppCon).AfPacketCreateV2(ctx, afPacketCreate)
+       if len(optionalHardwareAddress) > 0 {
+               afPacketCreate.HwAddr, err = ethernet_types.ParseMacAddress(optionalHardwareAddress[0])
+               if err != nil {
+                       fmt.Printf("failed to parse mac address: %v", err)
+                       return 0, err
+               }
+               afPacketCreate.UseRandomHwAddr = false
+       }
+       afPacketCreateRsp, err := af_packet.NewServiceClient(vppCon).AfPacketCreateV2(ctx, &afPacketCreate)
        if err != nil {
                fmt.Printf("failed to create af packet: %v", err)
                return 0, err
@@ -242,7 +306,7 @@ func configureAfPacket(ctx context.Context, vppCon api.Connection,
        return afPacketCreateRsp.SwIfIndex, nil
 }
 
-func ConfigureHttpTps(args []string) *ActionResult {
+func (a *Actions) ConfigureHttpTps(args []string) *ActionResult {
        ctx, cancel := newVppContext()
        defer cancel()
        con, vppErrCh := vpphelper.StartAndDialContext(ctx,