This repository was archived by the owner on Jun 24, 2026. It is now read-only.
forked from katanomi/pkg
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframework.go
More file actions
126 lines (105 loc) · 2.88 KB
/
Copy pathframework.go
File metadata and controls
126 lines (105 loc) · 2.88 KB
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
/*
Copyright 2021 The AlaudaDevops Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
"context"
"math/rand"
"os"
"testing"
"time"
. "github.com/AlaudaDevops/pkg/testing/framework/base"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"knative.dev/pkg/logging"
)
// fmw global variable to used by different test cases
var fmw = &Framework{}
// New sets a name to framework
func New(name string) *Framework {
fmw.Name = name
fmw.init()
return fmw
}
// Framework base framework for running automated test cases
type Framework struct {
Name string
TestContext
configures []Configure
}
func (f *Framework) init() {
f.Context = context.Background()
logger, err := zap.NewDevelopment(zap.ErrorOutput(zapcore.AddSync(GinkgoWriter)))
if err != nil {
panic(err)
}
f.SugaredLogger = logger.Sugar()
f.Context = logging.WithLogger(f.Context, f.SugaredLogger)
}
// MRun main testing.M run
func (f *Framework) MRun(m *testing.M) {
rand.Seed(time.Now().UnixNano())
result := m.Run()
os.Exit(result)
}
// Run start tests
func (f *Framework) Extensions(extensions ...SharedExtension) *Framework {
for _, extension := range extensions {
f.Context = extension.SetShardInfo(f.Context)
}
return f
}
// Config register configuration mutators which executed before case running
func (f *Framework) Config(configures ...Configure) *Framework {
f.configures = append(f.configures, configures...)
return f
}
// Run start tests
func (f *Framework) Run(t *testing.T) {
sConfig, rConfig := GinkgoConfiguration()
for _, configure := range f.configures {
configure.Config(&sConfig, &rConfig)
}
RegisterFailHandler(Fail)
RunSpecs(t, f.Name, sConfig, rConfig)
}
func (f *Framework) WithContext(ctx context.Context) *Framework {
f.Context = ctx
return f
}
// SynchronizedBeforeSuite basic before suite initialization
func (f *Framework) SynchronizedBeforeSuite(initFunc func()) *Framework {
SynchronizedBeforeSuite(func() []byte {
By("Setup")
if initFunc != nil {
By("Setup.Func")
initFunc()
}
return nil
}, func(_ []byte) {
// no-op for now
})
return f
}
// SynchronizedAfterSuite destroys the whole environment
func (f *Framework) SynchronizedAfterSuite(destroyFunc func()) *Framework {
SynchronizedAfterSuite(func() {}, func() {
By("Teardown")
if destroyFunc != nil {
By("Teardown.Func")
destroyFunc()
}
})
return f
}