hs-test: refactor test cases from ns suite
[vpp.git] / extras / hs-test / main.go
1 package main
2
3 import (
4         "context"
5         "encoding/json"
6         "fmt"
7         "os"
8         "os/signal"
9         "reflect"
10 )
11
12 var actions Actions
13
14 func newVppContext() (context.Context, context.CancelFunc) {
15         ctx, cancel := signal.NotifyContext(
16                 context.Background(),
17                 os.Interrupt,
18         )
19         return ctx, cancel
20 }
21
22 func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
23         // If we already have an error, log it and exit
24         select {
25         case err := <-errCh:
26                 fmt.Printf("%v", err)
27         default:
28         }
29         go func(ctx context.Context, errCh <-chan error) {
30                 <-errCh
31                 cancel()
32         }(ctx, errCh)
33 }
34
35 func writeSyncFile(res *ActionResult) error {
36         syncFile := "/tmp/sync/rc"
37
38         var jsonRes JsonResult
39
40         jsonRes.ErrOutput = res.ErrOutput
41         jsonRes.StdOutput = res.StdOutput
42         if res.Err != nil {
43                 jsonRes.Code = 1
44                 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
45         } else {
46                 jsonRes.Code = 0
47         }
48
49         str, err := json.Marshal(jsonRes)
50         if err != nil {
51                 return fmt.Errorf("error marshaling json result data! %v", err)
52         }
53
54         _, err = os.Open(syncFile)
55         if err != nil {
56                 // expecting the file does not exist
57                 f, e := os.Create(syncFile)
58                 if e != nil {
59                         return fmt.Errorf("failed to open sync file")
60                 }
61                 defer f.Close()
62                 f.Write([]byte(str))
63         } else {
64                 return fmt.Errorf("sync file exists, delete the file first")
65         }
66         return nil
67 }
68
69 func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
70         res := &ActionResult{
71                 Err: err,
72         }
73         for _, o := range opts {
74                 o(res)
75         }
76         return res
77 }
78
79 type ActionResultOptionFn func(res *ActionResult)
80
81 func ActionResultWithDesc(s string) ActionResultOptionFn {
82         return func(res *ActionResult) {
83                 res.Desc = s
84         }
85 }
86
87 func ActionResultWithStderr(s string) ActionResultOptionFn {
88         return func(res *ActionResult) {
89                 res.ErrOutput = s
90         }
91 }
92
93 func ActionResultWithStdout(s string) ActionResultOptionFn {
94         return func(res *ActionResult) {
95                 res.StdOutput = s
96         }
97 }
98
99 func OkResult() *ActionResult {
100         return NewActionResult(nil)
101 }
102
103 func processArgs() *ActionResult {
104         nArgs := len(os.Args) - 1 // skip program name
105         if nArgs < 1 {
106                 return NewActionResult(fmt.Errorf("internal: no action specified!"))
107         }
108         action := os.Args[1]
109         methodValue := reflect.ValueOf(&actions).MethodByName(action)
110         if !methodValue.IsValid() {
111                 return NewActionResult(fmt.Errorf("internal unknown action %s!", action))
112         }
113         methodIface := methodValue.Interface()
114         fn := methodIface.(func([]string) *ActionResult)
115         return fn(os.Args)
116 }
117
118 func main() {
119         if len(os.Args) == 0 {
120                 fmt.Println("args required")
121                 return
122         }
123
124         if os.Args[1] == "rm" {
125                 topology, err := LoadTopology(NetworkTopologyDir, os.Args[2])
126                 if err != nil {
127                         fmt.Printf("falied to load topologies: %v\n", err)
128                         os.Exit(1)
129                 }
130                 topology.Unconfigure()
131                 os.Exit(0)
132         }
133
134         var err error
135         res := processArgs()
136         err = writeSyncFile(res)
137         if err != nil {
138                 fmt.Printf("failed to write to sync file: %v\n", err)
139         }
140 }