blob: c2fddb9b22b5a9833abc2476ac81ecb1a2937c4b [file] [log] [blame]
sslobodrd6e07e72019-01-31 16:07:20 -05001/*
2 * Copyright 2018-present Open Networking Foundation
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// The template for the tester.
17// This template is filled in by the
18// test driver based on the configuration.
19
20package main
21
22import (
23 "os"
sslobodrd9daabf2019-02-05 13:14:21 -050024 "fmt"
sslobodrd6e07e72019-01-31 16:07:20 -050025 "time"
26 "os/exec"
27 "strings"
28 "context"
sslobodr13182842019-02-08 14:40:30 -050029 slog "log"
30 "io/ioutil"
31 "encoding/json"
32 "google.golang.org/grpc/grpclog"
sslobodrd6e07e72019-01-31 16:07:20 -050033 "github.com/opencord/voltha-go/common/log"
34)
35
sslobodr13182842019-02-08 14:40:30 -050036type TestCase struct {
37 Title string `json:"title"`
38 Result bool `json:"result"`
39 Info []string `json:"info"`
40}
41
42type TestSuite struct {
43 Name string `json:"name"`
44 TestCases []TestCase `json:"testCases"`
45}
46
47type TestRun struct {
48 TestSuites []TestSuite
49}
50
sslobodrd9daabf2019-02-05 13:14:21 -050051var resFile *tstLog
52type tstLog struct {
53 fp * os.File
54 fn string
55}
sslobodrd6e07e72019-01-31 16:07:20 -050056
sslobodrd9daabf2019-02-05 13:14:21 -050057func (tr * tstLog) testLog(format string, a ...interface{}) {
58 var err error
59 if tr.fp == nil {
60 if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
61 log.Errorf("Could not append to the results file")
62 tr.fp = nil
63 }
64 }
65 if tr.fp != nil {
66 tr.fp.Write([]byte(fmt.Sprintf(format, a...)))
67 }
68}
69
70func (tr * tstLog) testLogOnce(format string, a ...interface{}) {
71 var err error
72 if tr.fp == nil {
73 if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
74 log.Errorf("Could not append to the results file")
75 tr.fp = nil
76 }
77 }
78 if tr.fp != nil {
79 tr.fp.Write([]byte(fmt.Sprintf(format, a...)))
80 }
81 tr.fp.Close()
82 tr.fp = nil
83}
84
85func (tr * tstLog) close() {
86 if tr.fp != nil {
87 tr.fp.Close()
88 }
89}
90
91
92func startSut(cmdStr string) (*exec.Cmd, context.CancelFunc, error) {
sslobodrd6e07e72019-01-31 16:07:20 -050093 var err error = nil
94
95 cmdAry := strings.Fields(cmdStr)
96 log.Infof("Running the affinity router: %s",cmdStr)
97 //ctx, cncl := context.WithCancel(context.Background())
98 ctx, cncl := context.WithCancel(context.Background())
99 cmd := exec.CommandContext(ctx, cmdAry[0], cmdAry[1:]...)
100 cmd.Stdin = os.Stdin
101 cmd.Stdout = os.Stdout
102 cmd.Stderr = os.Stderr
103 if err = cmd.Start(); err != nil {
104 log.Errorf("Failed to run the affinity router: %s %v", cmdStr,err)
105 }
106 time.Sleep(1 * time.Second) // Give the command time to get started
sslobodrd9daabf2019-02-05 13:14:21 -0500107 return cmd, cncl, err
sslobodrd6e07e72019-01-31 16:07:20 -0500108}
109
sslobodrd9daabf2019-02-05 13:14:21 -0500110func cleanUp(cmd *exec.Cmd, cncl context.CancelFunc) {
sslobodrd6e07e72019-01-31 16:07:20 -0500111 cncl()
sslobodrd9daabf2019-02-05 13:14:21 -0500112 cmd.Wait() // This seems to hang
sslobodrd6e07e72019-01-31 16:07:20 -0500113 // Give the child processes time to terminate
sslobodrd9daabf2019-02-05 13:14:21 -0500114 //time.Sleep(1 * time.Second)
sslobodrd6e07e72019-01-31 16:07:20 -0500115}
116
sslobodr13182842019-02-08 14:40:30 -0500117func readStats(stats * TestRun) () {
118 // Check if the stats file exists
119 if _,err := os.Stat("stats.json"); err != nil {
120 // Nothing to do, just return
121 return
122 }
123 // The file is there, read it an unmarshal it into the stats struct
124 if statBytes, err := ioutil.ReadFile("stats.json"); err != nil {
125 log.Error(err)
126 return
127 } else if err := json.Unmarshal(statBytes, stats); err != nil {
128 log.Error(err)
129 return
130 }
131}
132
133func writeStats(stats * TestRun) () {
134 // Check if the stats file exists
135 // The file is there, read it an unmarshal it into the stats struct
136 if statBytes, err := json.MarshalIndent(stats, ""," "); err != nil {
137 log.Error(err)
138 return
139 } else if err := ioutil.WriteFile("stats.json.new", statBytes, 0644); err != nil {
140 log.Error(err)
141 return
142 }
143 os.Rename("stats.json", "stats.json~")
144 os.Rename("stats.json.new", "stats.json")
145}
146var stats TestRun
147
sslobodrd6e07e72019-01-31 16:07:20 -0500148func main() {
149 var err error
150
151 // Setup logging
152 if _, err = log.SetDefaultLogger(log.JSON, 0, nil); err != nil {
153 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
154 }
sslobodrd6e07e72019-01-31 16:07:20 -0500155 defer log.CleanUp()
156
sslobodr13182842019-02-08 14:40:30 -0500157 readStats(&stats)
158
159
160 grpclog.SetLogger(slog.New(os.Stderr, "grpc: ", slog.LstdFlags))
161
sslobodrd9daabf2019-02-05 13:14:21 -0500162 resFile = &tstLog{fn:os.Args[1]}
163 defer resFile.close()
sslobodrd6e07e72019-01-31 16:07:20 -0500164
sslobodrd6e07e72019-01-31 16:07:20 -0500165
166 // Initialize the servers
167 if err := serverInit(); err != nil {
168 log.Errorf("Error initializing server: %v", err)
169 return
170 }
171
172 // Start the sofware under test
sslobodrd9daabf2019-02-05 13:14:21 -0500173 cmd, cnclFunc, err := startSut("./{{.Command}}");
174 defer cleanUp(cmd, cnclFunc)
sslobodrd6e07e72019-01-31 16:07:20 -0500175 if err != nil {
176 return
177 }
178
179 // Initialize the clients
180 if err := clientInit(); err != nil {
181 log.Errorf("Error initializing client: %v", err)
182 return
183 }
184
185 log.Infof("The servers are: %v",servers)
186
187 // Run all the test cases now
188 log.Infof("Executing tests")
sslobodrd6e07e72019-01-31 16:07:20 -0500189 runTests()
sslobodr13182842019-02-08 14:40:30 -0500190 writeStats(&stats)
sslobodrd6e07e72019-01-31 16:07:20 -0500191
192}