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" |
| 29 | "github.com/opencord/voltha-go/common/log" |
| 30 | ) |
| 31 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 32 | var resFile *tstLog |
| 33 | type tstLog struct { |
| 34 | fp * os.File |
| 35 | fn string |
| 36 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 37 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 38 | func (tr * tstLog) testLog(format string, a ...interface{}) { |
| 39 | var err error |
| 40 | if tr.fp == nil { |
| 41 | if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { |
| 42 | log.Errorf("Could not append to the results file") |
| 43 | tr.fp = nil |
| 44 | } |
| 45 | } |
| 46 | if tr.fp != nil { |
| 47 | tr.fp.Write([]byte(fmt.Sprintf(format, a...))) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (tr * tstLog) testLogOnce(format string, a ...interface{}) { |
| 52 | var err error |
| 53 | if tr.fp == nil { |
| 54 | if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { |
| 55 | log.Errorf("Could not append to the results file") |
| 56 | tr.fp = nil |
| 57 | } |
| 58 | } |
| 59 | if tr.fp != nil { |
| 60 | tr.fp.Write([]byte(fmt.Sprintf(format, a...))) |
| 61 | } |
| 62 | tr.fp.Close() |
| 63 | tr.fp = nil |
| 64 | } |
| 65 | |
| 66 | func (tr * tstLog) close() { |
| 67 | if tr.fp != nil { |
| 68 | tr.fp.Close() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | func startSut(cmdStr string) (*exec.Cmd, context.CancelFunc, error) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 74 | var err error = nil |
| 75 | |
| 76 | cmdAry := strings.Fields(cmdStr) |
| 77 | log.Infof("Running the affinity router: %s",cmdStr) |
| 78 | //ctx, cncl := context.WithCancel(context.Background()) |
| 79 | ctx, cncl := context.WithCancel(context.Background()) |
| 80 | cmd := exec.CommandContext(ctx, cmdAry[0], cmdAry[1:]...) |
| 81 | cmd.Stdin = os.Stdin |
| 82 | cmd.Stdout = os.Stdout |
| 83 | cmd.Stderr = os.Stderr |
| 84 | if err = cmd.Start(); err != nil { |
| 85 | log.Errorf("Failed to run the affinity router: %s %v", cmdStr,err) |
| 86 | } |
| 87 | time.Sleep(1 * time.Second) // Give the command time to get started |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 88 | return cmd, cncl, err |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 89 | } |
| 90 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 91 | func cleanUp(cmd *exec.Cmd, cncl context.CancelFunc) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 92 | cncl() |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 93 | cmd.Wait() // This seems to hang |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 94 | // Give the child processes time to terminate |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 95 | //time.Sleep(1 * time.Second) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func main() { |
| 99 | var err error |
| 100 | |
| 101 | // Setup logging |
| 102 | if _, err = log.SetDefaultLogger(log.JSON, 0, nil); err != nil { |
| 103 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 104 | } |
| 105 | |
| 106 | defer log.CleanUp() |
| 107 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 108 | resFile = &tstLog{fn:os.Args[1]} |
| 109 | defer resFile.close() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 110 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 111 | |
| 112 | // Initialize the servers |
| 113 | if err := serverInit(); err != nil { |
| 114 | log.Errorf("Error initializing server: %v", err) |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | // Start the sofware under test |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 119 | cmd, cnclFunc, err := startSut("./{{.Command}}"); |
| 120 | defer cleanUp(cmd, cnclFunc) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 121 | if err != nil { |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | // Initialize the clients |
| 126 | if err := clientInit(); err != nil { |
| 127 | log.Errorf("Error initializing client: %v", err) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | log.Infof("The servers are: %v",servers) |
| 132 | |
| 133 | // Run all the test cases now |
| 134 | log.Infof("Executing tests") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 135 | runTests() |
| 136 | |
| 137 | } |