sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [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 | "fmt" |
| 25 | //slog "log" |
| 26 | "io/ioutil" |
| 27 | "encoding/json" |
| 28 | //"google.golang.org/grpc/grpclog" |
Scott Baker | ec6e61b | 2019-10-23 10:21:27 -0700 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/pkg/log" |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type TestCase struct { |
| 33 | Title string `json:"title"` |
| 34 | Result bool `json:"passed"` |
| 35 | Info []string `json:"info"` |
| 36 | } |
| 37 | |
| 38 | type TestSuite struct { |
| 39 | Name string `json:"name"` |
| 40 | Info []string `json:"info"` |
| 41 | TestCases []TestCase `json:"testCases"` |
| 42 | } |
| 43 | |
| 44 | type TestRun struct { |
| 45 | TestSuites []TestSuite |
| 46 | } |
| 47 | |
| 48 | func (tr * TestRun) testLog(format string, a ...interface{}) { |
| 49 | |
| 50 | tridx := len(tr.TestSuites) - 1 |
| 51 | tcidx := len(tr.TestSuites[tridx].TestCases) - 1 |
| 52 | |
| 53 | tr.TestSuites[tridx].TestCases[tcidx].Info = |
| 54 | append(tr.TestSuites[tridx].TestCases[tcidx].Info, fmt.Sprintf(format, a...)) |
| 55 | |
| 56 | } |
| 57 | |
| 58 | func (tr * TestRun) suiteLog(format string, a ...interface{}) { |
| 59 | |
| 60 | tridx := len(tr.TestSuites) - 1 |
| 61 | |
| 62 | tr.TestSuites[tridx].Info = |
| 63 | append(tr.TestSuites[tridx].Info, fmt.Sprintf(format, a...)) |
| 64 | |
| 65 | } |
| 66 | |
| 67 | func readStats(statFn string) (*TestRun, error) { |
| 68 | // Check if the stats file exists |
| 69 | if _,err := os.Stat(statFn); err != nil { |
| 70 | // Nothing to do, just return |
| 71 | return &TestRun{}, nil |
| 72 | } |
| 73 | rtrn := &TestRun{} |
| 74 | // The file is there, read it an unmarshal it into the stats struct |
| 75 | if statBytes, err := ioutil.ReadFile(statFn); err != nil { |
| 76 | log.Error(err) |
| 77 | return nil, err |
| 78 | } else if err := json.Unmarshal(statBytes, rtrn); err != nil { |
| 79 | log.Error(err) |
| 80 | return nil, err |
| 81 | } |
| 82 | return rtrn, nil |
| 83 | } |
| 84 | |
| 85 | func writeStats(statFn string, stats * TestRun) error { |
| 86 | // Check if the stats file exists |
| 87 | // The file is there, read it an unmarshal it into the stats struct |
| 88 | if statBytes, err := json.MarshalIndent(stats, ""," "); err != nil { |
| 89 | log.Error(err) |
| 90 | return err |
| 91 | } else if err := ioutil.WriteFile(statFn+".new", statBytes, 0644); err != nil { |
| 92 | log.Error(err) |
| 93 | return err |
| 94 | } |
| 95 | if err := os.Rename(statFn, statFn+"~"); err != nil { |
| 96 | log.Error(err) |
| 97 | return err |
| 98 | } |
| 99 | if err := os.Rename(statFn+".new", statFn); err != nil { |
| 100 | log.Error(err) |
| 101 | return err |
| 102 | } |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | func (tr * TestRun) appendNew() { |
| 107 | tr.TestSuites = append(tr.TestSuites, TestSuite{}) |
| 108 | } |
| 109 | |
| 110 | func initStats(statFn string) error { |
| 111 | s := &TestRun{} |
| 112 | |
| 113 | if statBytes, err := json.MarshalIndent(s, ""," "); err != nil { |
| 114 | log.Error(err) |
| 115 | return err |
| 116 | } else if err := ioutil.WriteFile(statFn, statBytes, 0644); err != nil { |
| 117 | log.Error(err) |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | |
| 125 | var statFn string |
| 126 | var stats *TestRun |