blob: 3eb4008b850f76088a839cef21192edc23ecda2d [file] [log] [blame]
sslobodr1d1e50b2019-03-14 09:17:40 -04001/*
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
20package main
21
22import (
23 "os"
24 "fmt"
25 //slog "log"
26 "io/ioutil"
27 "encoding/json"
28 //"google.golang.org/grpc/grpclog"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080029 "github.com/opencord/voltha-lib-go/v3/pkg/log"
sslobodr1d1e50b2019-03-14 09:17:40 -040030)
31
32type TestCase struct {
33 Title string `json:"title"`
34 Result bool `json:"passed"`
35 Info []string `json:"info"`
36}
37
38type TestSuite struct {
39 Name string `json:"name"`
40 Info []string `json:"info"`
41 TestCases []TestCase `json:"testCases"`
42}
43
44type TestRun struct {
45 TestSuites []TestSuite
46}
47
48func (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
58func (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
67func 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
85func 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
106func (tr * TestRun) appendNew() {
107 tr.TestSuites = append(tr.TestSuites, TestSuite{})
108}
109
110func 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
125var statFn string
126var stats *TestRun