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 | "fmt" |
| 26 | "os/exec" |
| 27 | "io/ioutil" |
| 28 | "github.com/opencord/voltha-go/common/log" |
| 29 | ) |
| 30 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 31 | type tstLog struct { |
| 32 | fp * os.File |
| 33 | fn string |
| 34 | } |
| 35 | |
| 36 | func (tr * tstLog) testLog(format string, a ...interface{}) { |
| 37 | var err error |
| 38 | if tr.fp == nil { |
| 39 | if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { |
| 40 | log.Errorf("Could not append to the results file") |
| 41 | tr.fp = nil |
| 42 | } |
| 43 | } |
| 44 | if tr.fp != nil { |
| 45 | tr.fp.Write([]byte(fmt.Sprintf(format, a...))) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func (tr * tstLog) testLogOnce(format string, a ...interface{}) { |
| 50 | var err error |
| 51 | if tr.fp == nil { |
| 52 | if tr.fp, err = os.OpenFile(tr.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil { |
| 53 | log.Errorf("Could not append to the results file") |
| 54 | tr.fp = nil |
| 55 | } |
| 56 | } |
| 57 | if tr.fp != nil { |
| 58 | tr.fp.Write([]byte(fmt.Sprintf(format, a...))) |
| 59 | } |
| 60 | tr.fp.Close() |
| 61 | tr.fp = nil |
| 62 | } |
| 63 | |
| 64 | func (tr * tstLog) close() { |
| 65 | if tr.fp != nil { |
| 66 | tr.fp.Close() |
| 67 | } |
| 68 | } |
| 69 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 70 | func main() { |
| 71 | var cmd *exec.Cmd |
| 72 | var cmdStr string |
| 73 | // Setup logging |
| 74 | if _, err := log.SetDefaultLogger(log.JSON, 0, nil); err != nil { |
| 75 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 76 | } |
| 77 | |
| 78 | defer log.CleanUp() |
| 79 | |
| 80 | log.Info("Running tests") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 81 | if err:= os.Chdir(os.Args[1]); err != nil { |
| 82 | log.Error("Could not change directory to %s: %v", os.Args[1], err) |
| 83 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 84 | tl := &tstLog{fn:"results.txt"} |
| 85 | {{range .}} |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 86 | cmdStr = "./"+"{{.}}"+".e" |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 87 | tl.testLogOnce("Running test suite '%s'\n", cmdStr[2:]) |
| 88 | |
| 89 | log.Infof("Running test suite %s",cmdStr) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 90 | cmd = exec.Command(cmdStr, "results.txt") |
| 91 | cmd.Stdin = os.Stdin |
| 92 | cmd.Stdout = os.Stdout |
| 93 | cmd.Stderr = os.Stderr |
| 94 | if err := cmd.Run(); err != nil { |
| 95 | log.Errorf("Test '%s' failed", cmdStr) |
| 96 | } |
| 97 | {{end}} |
| 98 | // Open the results file and output it. |
| 99 | if resFile, err := ioutil.ReadFile("results.txt"); err == nil { |
| 100 | fmt.Println(string(resFile)) |
| 101 | } else { |
| 102 | log.Error("Could not load the results file 'results.txt'") |
| 103 | } |
| 104 | log.Info("Tests complete") |
| 105 | } |