This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest.go
172 lines (150 loc) · 4.81 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2020-2022 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package tnf
import (
"fmt"
"time"
expect "github.com/google/goexpect"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/test-network-function/test-network-function/pkg/tnf/identifier"
"github.com/test-network-function/test-network-function/pkg/tnf/reel"
)
// ClaimFilePrintf prints to claim and junit report files.
func ClaimFilePrintf(format string, args ...interface{}) {
message := fmt.Sprintf(format+"\n", args...)
_, err := ginkgo.GinkgoWriter.Write([]byte(message))
if err != nil {
logrus.Errorf("Ginkgo writer could not write msg '%s' because: %s", message, err)
}
}
const (
// ERROR represents an errored test.
ERROR = iota
// SUCCESS represents a successful test.
SUCCESS
// FAILURE represents a failed test.
FAILURE
)
// TestsExtraInfo a collection of messages per test that is added to the claim file
// use WriteTestExtraInfo for writing to it
var TestsExtraInfo = []map[string][]string{}
// ExitCodeMap maps a test result value to a more appropriate Unix return code.
var ExitCodeMap = map[int]int{
SUCCESS: 0,
FAILURE: 1,
ERROR: 2,
}
// Tester provides the interface for a Test.
type Tester interface {
// Args returns the CLI command as a string array.
Args() []string
// GetIdentifier returns the tnf.Test specific identifier.
GetIdentifier() identifier.Identifier
// Result returns the result of the test (ERROR, SUCCESS, or FAILURE).
Result() int
// Timeout returns the test timeout as a Duration.
Timeout() time.Duration
}
// Test runs a chain of Handlers.
type Test struct {
runner *reel.Reel
tester Tester
chain []reel.Handler
}
// Run performs a test, returning the result and any encountered errors.
func (t *Test) Run() (int, error) {
err := t.runner.Run(t)
return t.tester.Result(), err
}
func (t *Test) dispatch(fp reel.StepFunc) *reel.Step {
for _, handler := range t.chain {
step := fp(handler)
if step != nil {
return step
}
}
return nil
}
// ReelFirst calls the current Handler's ReelFirst function.
func (t *Test) ReelFirst() *reel.Step {
fp := func(handler reel.Handler) *reel.Step {
return handler.ReelFirst()
}
return t.dispatch(fp)
}
// ReelMatch calls the current Handler's ReelMatch function.
func (t *Test) ReelMatch(pattern, before, match string) *reel.Step {
fp := func(handler reel.Handler) *reel.Step {
return handler.ReelMatch(pattern, before, match)
}
return t.dispatch(fp)
}
// ReelTimeout calls the current Handler's ReelTimeout function.
func (t *Test) ReelTimeout() *reel.Step {
fp := func(handler reel.Handler) *reel.Step {
return handler.ReelTimeout()
}
return t.dispatch(fp)
}
// ReelEOF calls the current Handler's ReelEOF function.
func (t *Test) ReelEOF() {
for _, handler := range t.chain {
handler.ReelEOF()
}
}
// RunAndValidate runs the test and checks the result
func (t *Test) RunAndValidate() {
t.RunAndValidateWithFailureCallback(nil)
}
// RunAndValidateWithFailureCallback runs the test, checks the result/error and invokes the cb on failure
func (t *Test) RunAndValidateWithFailureCallback(cb func()) {
testResult, err := t.Run()
if testResult == FAILURE && cb != nil {
cb()
}
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(testResult).To(gomega.Equal(SUCCESS))
}
// RunWithCallbacks runs the test, invokes the cb on failure/error/success
// This is useful when the testcase needs to continue whether this test result is success or not
func (t *Test) RunWithCallbacks(successCb, failureCb func(), errorCb func(error)) {
testResult, err := t.Run()
switch testResult {
case SUCCESS:
if successCb != nil {
successCb()
}
case FAILURE:
if failureCb != nil {
failureCb()
}
case ERROR:
if errorCb != nil {
errorCb(err)
}
}
}
// NewTest creates a new Test given a chain of Handlers.
func NewTest(expecter *expect.Expecter, tester Tester, chain []reel.Handler, errorChannel <-chan error, opts ...reel.Option) (*Test, error) {
args := tester.Args()
runner, err := reel.NewReel(expecter, args, errorChannel, opts...)
if err != nil {
return nil, err
}
return &Test{runner: runner, tester: tester, chain: chain}, nil
}