blob: f539a0279b9f5cdb466d4181d6eaeccf0c4561d8 [file] [log] [blame]
sslobodrd6e07e72019-01-31 16:07:20 -05001/*
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"
sslobodrd9daabf2019-02-05 13:14:21 -050024 "fmt"
sslobodrd6e07e72019-01-31 16:07:20 -050025 "time"
26 "os/exec"
27 "strings"
28 "context"
29 "github.com/opencord/voltha-go/common/log"
30)
31
sslobodrd9daabf2019-02-05 13:14:21 -050032var resFile *tstLog
33type tstLog struct {
34 fp * os.File
35 fn string
36}
sslobodrd6e07e72019-01-31 16:07:20 -050037
sslobodrd9daabf2019-02-05 13:14:21 -050038func (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
51func (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
66func (tr * tstLog) close() {
67 if tr.fp != nil {
68 tr.fp.Close()
69 }
70}
71
72
73func startSut(cmdStr string) (*exec.Cmd, context.CancelFunc, error) {
sslobodrd6e07e72019-01-31 16:07:20 -050074 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
sslobodrd9daabf2019-02-05 13:14:21 -050088 return cmd, cncl, err
sslobodrd6e07e72019-01-31 16:07:20 -050089}
90
sslobodrd9daabf2019-02-05 13:14:21 -050091func cleanUp(cmd *exec.Cmd, cncl context.CancelFunc) {
sslobodrd6e07e72019-01-31 16:07:20 -050092 cncl()
sslobodrd9daabf2019-02-05 13:14:21 -050093 cmd.Wait() // This seems to hang
sslobodrd6e07e72019-01-31 16:07:20 -050094 // Give the child processes time to terminate
sslobodrd9daabf2019-02-05 13:14:21 -050095 //time.Sleep(1 * time.Second)
sslobodrd6e07e72019-01-31 16:07:20 -050096}
97
98func 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
sslobodrd9daabf2019-02-05 13:14:21 -0500108 resFile = &tstLog{fn:os.Args[1]}
109 defer resFile.close()
sslobodrd6e07e72019-01-31 16:07:20 -0500110
sslobodrd6e07e72019-01-31 16:07:20 -0500111
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
sslobodrd9daabf2019-02-05 13:14:21 -0500119 cmd, cnclFunc, err := startSut("./{{.Command}}");
120 defer cleanUp(cmd, cnclFunc)
sslobodrd6e07e72019-01-31 16:07:20 -0500121 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")
sslobodrd6e07e72019-01-31 16:07:20 -0500135 runTests()
136
137}