sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 23 | "os" |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 24 | "fmt" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 25 | "time" |
| 26 | "os/exec" |
| 27 | "strings" |
| 28 | "context" |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 29 | slog "log" |
| 30 | "io/ioutil" |
| 31 | "encoding/json" |
| 32 | "google.golang.org/grpc/grpclog" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 33 | "github.com/opencord/voltha-go/common/log" |
| 34 | ) |
| 35 | |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 36 | type TestCase struct { |
| 37 | Title string `json:"title"` |
| 38 | Result bool `json:"result"` |
| 39 | Info []string `json:"info"` |
| 40 | } |
| 41 | |
| 42 | type TestSuite struct { |
| 43 | Name string `json:"name"` |
| 44 | TestCases []TestCase `json:"testCases"` |
| 45 | } |
| 46 | |
| 47 | type TestRun struct { |
| 48 | TestSuites []TestSuite |
| 49 | } |
| 50 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 51 | var resFile *tstLog |
| 52 | type tstLog struct { |
| 53 | fp * os.File |
| 54 | fn string |
| 55 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 56 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 57 | func (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 | |
| 70 | func (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 | |
| 85 | func (tr * tstLog) close() { |
| 86 | if tr.fp != nil { |
| 87 | tr.fp.Close() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | func startSut(cmdStr string) (*exec.Cmd, context.CancelFunc, error) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 93 | 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 |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 107 | return cmd, cncl, err |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 108 | } |
| 109 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 110 | func cleanUp(cmd *exec.Cmd, cncl context.CancelFunc) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 111 | cncl() |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 112 | cmd.Wait() // This seems to hang |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 113 | // Give the child processes time to terminate |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 114 | //time.Sleep(1 * time.Second) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 115 | } |
| 116 | |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 117 | func 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 | |
| 133 | func 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 | } |
| 146 | var stats TestRun |
| 147 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 148 | func 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 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 155 | defer log.CleanUp() |
| 156 | |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 157 | readStats(&stats) |
| 158 | |
| 159 | |
| 160 | grpclog.SetLogger(slog.New(os.Stderr, "grpc: ", slog.LstdFlags)) |
| 161 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 162 | resFile = &tstLog{fn:os.Args[1]} |
| 163 | defer resFile.close() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 164 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 165 | |
| 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 |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 173 | cmd, cnclFunc, err := startSut("./{{.Command}}"); |
| 174 | defer cleanUp(cmd, cnclFunc) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 175 | 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") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 189 | runTests() |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 190 | writeStats(&stats) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 191 | |
| 192 | } |