hs-test: refactor test cases from ns suite
[vpp.git] / extras / hs-test / actions.go
1 package main
2
3 import (
4         "context"
5         "os"
6
7         "git.fd.io/govpp.git/api"
8         interfaces "github.com/edwarnicke/govpp/binapi/interface"
9         "github.com/edwarnicke/govpp/binapi/interface_types"
10         ip_types "github.com/edwarnicke/govpp/binapi/ip_types"
11         "github.com/edwarnicke/govpp/binapi/session"
12         "github.com/edwarnicke/govpp/binapi/tapv2"
13         "github.com/edwarnicke/vpphelper"
14 )
15
16 var (
17         workDir, _ = os.Getwd()
18 )
19
20 type ConfFn func(context.Context, api.Connection) error
21
22 type Actions struct {
23 }
24
25 func (a *Actions) ConfigureTap(args []string) *ActionResult {
26         var startup Stanza
27         startup.
28                 NewStanza("session").
29                 Append("enable").
30                 Append("use-app-socket-api").Close()
31
32         ctx, cancel := newVppContext()
33         defer cancel()
34         con, vppErrCh := vpphelper.StartAndDialContext(ctx,
35                 vpphelper.WithRootDir(workDir),
36                 vpphelper.WithVppConfig(configTemplate+startup.ToString()))
37         exitOnErrCh(ctx, cancel, vppErrCh)
38         ifaceClient := interfaces.NewServiceClient(con)
39
40         pref, err := ip_types.ParseIP4Prefix("10.10.10.2/24")
41         if err != nil {
42                 return NewActionResult(err, ActionResultWithDesc("failed to parse ip4 address"))
43         }
44         createTapReply, err := tapv2.NewServiceClient(con).TapCreateV2(ctx, &tapv2.TapCreateV2{
45                 HostIfNameSet:    true,
46                 HostIfName:       "tap0",
47                 HostIP4PrefixSet: true,
48                 HostIP4Prefix:    ip_types.IP4AddressWithPrefix(pref),
49         })
50         if err != nil {
51                 return NewActionResult(err, ActionResultWithDesc("failed to configure tap"))
52         }
53         ipPrefix, err := ip_types.ParseAddressWithPrefix("10.10.10.1/24")
54         if err != nil {
55                 return NewActionResult(err, ActionResultWithDesc("parsing ip address failed"))
56         }
57         ipAddress := &interfaces.SwInterfaceAddDelAddress{
58                 IsAdd:     true,
59                 SwIfIndex: createTapReply.SwIfIndex,
60                 Prefix:    ipPrefix,
61         }
62         _, errx := ifaceClient.SwInterfaceAddDelAddress(ctx, ipAddress)
63         if errx != nil {
64                 return NewActionResult(err, ActionResultWithDesc("configuring ip address failed"))
65         }
66         _, err = ifaceClient.SwInterfaceSetFlags(ctx, &interfaces.SwInterfaceSetFlags{
67                 SwIfIndex: createTapReply.SwIfIndex,
68                 Flags:     interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
69         })
70         if err != nil {
71                 return NewActionResult(err, ActionResultWithDesc("failed to set interface state"))
72         }
73         _, err = session.NewServiceClient(con).SessionEnableDisable(ctx, &session.SessionEnableDisable{
74                 IsEnable: true,
75         })
76         if err != nil {
77                 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
78         }
79         writeSyncFile(OkResult())
80         <-ctx.Done()
81         return nil
82 }