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" |
| 24 | "time" |
| 25 | "os/exec" |
| 26 | "strings" |
| 27 | "context" |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 28 | //slog "log" |
| 29 | //"google.golang.org/grpc/grpclog" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 31 | ) |
| 32 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 33 | func startSut(cmdStr string) (*exec.Cmd, context.CancelFunc, error) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 34 | var err error = nil |
| 35 | |
| 36 | cmdAry := strings.Fields(cmdStr) |
| 37 | log.Infof("Running the affinity router: %s",cmdStr) |
| 38 | //ctx, cncl := context.WithCancel(context.Background()) |
| 39 | ctx, cncl := context.WithCancel(context.Background()) |
| 40 | cmd := exec.CommandContext(ctx, cmdAry[0], cmdAry[1:]...) |
| 41 | cmd.Stdin = os.Stdin |
| 42 | cmd.Stdout = os.Stdout |
| 43 | cmd.Stderr = os.Stderr |
| 44 | if err = cmd.Start(); err != nil { |
| 45 | log.Errorf("Failed to run the affinity router: %s %v", cmdStr,err) |
| 46 | } |
| 47 | time.Sleep(1 * time.Second) // Give the command time to get started |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 48 | return cmd, cncl, err |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 49 | } |
| 50 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 51 | func cleanUp(cmd *exec.Cmd, cncl context.CancelFunc) { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 52 | cncl() |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 53 | cmd.Wait() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func main() { |
| 57 | var err error |
| 58 | |
| 59 | // Setup logging |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 60 | if _, err = log.SetDefaultLogger(log.JSON, 1, nil); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 61 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 62 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 63 | defer log.CleanUp() |
| 64 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 65 | if len(os.Args) < 2 { |
| 66 | log.Fatalf("Stat file name parameter missing for %s. Aborting...", os.Args[0]) |
| 67 | } else { |
| 68 | statFn = os.Args[1] |
| 69 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 70 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 71 | if stats,err = readStats(statFn); err != nil { |
| 72 | log.Error(err) |
| 73 | return |
| 74 | } |
| 75 | defer writeStats(statFn, stats) |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 76 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 77 | // Add a stat entry for this run |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 78 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 79 | stats.appendNew() |
| 80 | tsIdx := len(stats.TestSuites) - 1 |
| 81 | stats.TestSuites[tsIdx].Name = os.Args[0] |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 82 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 83 | |
| 84 | // Initialize the servers |
| 85 | if err := serverInit(); err != nil { |
| 86 | log.Errorf("Error initializing server: %v", err) |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | // Start the sofware under test |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 91 | cmd, cnclFunc, err := startSut("./{{.Command}}"); |
| 92 | defer cleanUp(cmd, cnclFunc) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 93 | if err != nil { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | // Initialize the clients |
| 98 | if err := clientInit(); err != nil { |
| 99 | log.Errorf("Error initializing client: %v", err) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | log.Infof("The servers are: %v",servers) |
| 104 | |
| 105 | // Run all the test cases now |
| 106 | log.Infof("Executing tests") |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 107 | |
| 108 | //log.Infof("Stats struct: %v", stats) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 109 | runTests() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 110 | |
| 111 | } |