-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.go
121 lines (93 loc) · 3.16 KB
/
init.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
package globalhelper
import (
"fmt"
"os"
"time"
"github.com/golang/glog"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
testclient "github.com/test-network-function/cnfcert-tests-verification/tests/utils/client"
"github.com/test-network-function/cnfcert-tests-verification/tests/utils/config"
"k8s.io/client-go/kubernetes"
)
var (
apiclient *testclient.ClientSet
conf *config.Config
)
func SetTestK8sAPIClient(client kubernetes.Interface) {
if apiclient == nil {
apiclient = &testclient.ClientSet{}
}
//nolint:godox
// TODO: Add more client interfaces as needed
apiclient.CoreV1Interface = client.CoreV1()
apiclient.AppsV1Interface = client.AppsV1()
apiclient.RbacV1Interface = client.RbacV1()
apiclient.NodeV1Interface = client.NodeV1()
apiclient.K8sClient = client
}
func UnsetTestK8sAPIClient() {
apiclient = nil
}
func GetAPIClient() *testclient.ClientSet {
if apiclient != nil {
return apiclient
}
var err error
apiclient, err = config.DefineClients()
if err != nil {
glog.Fatalf("can not load api client. Please check KUBECONFIG env var - %s", err)
}
return apiclient
}
func GetConfiguration() *config.Config {
if conf != nil {
return conf
}
var err error
conf, err = config.NewConfig()
if err != nil {
glog.Fatalf("can not load configuration - %s", err)
}
return conf
}
func GenerateDirectories(randomStr string) (reportDir, configDir string) {
reportDir = GetConfiguration().General.TnfReportDir + "/" + randomStr
configDir = GetConfiguration().General.TnfConfigDir + "/" + randomStr
err := os.MkdirAll(reportDir, os.ModePerm)
if err != nil {
glog.Error("could not create dest directory= %s, err=%s", reportDir, err)
}
err = os.MkdirAll(configDir, os.ModePerm)
if err != nil {
glog.Error("could not create dest directory= %s, err=%s", reportDir, err)
}
return reportDir, configDir
}
func BeforeEachSetupWithRandomNamespace(incomingNamespace string) (randomNamespace, randomReportDir, randomConfigDir string) {
randomNamespace = incomingNamespace + "-" + GenerateRandomString(10)
By(fmt.Sprintf("Create %s namespace", randomNamespace))
err := CreateNamespace(randomNamespace)
Expect(err).ToNot(HaveOccurred())
By("Generate directories")
randomReportDir, randomConfigDir = GenerateDirectories(randomNamespace)
return randomNamespace, randomReportDir, randomConfigDir
}
func AfterEachCleanupWithRandomNamespace(randomNamespace, randomReportDir, randomConfigDir string, waitingTime time.Duration) {
// logfile := "cnf-certsuite.log"
// By("Print logs")
// myFile, err := os.ReadFile(randomReportDir + "/" + logfile)
// if err != nil {
// glog.Errorf("can not read file %s - %s", logfile, err)
// }
// fmt.Println(string(myFile))
By(fmt.Sprintf("Remove reports from report directory: %s", randomReportDir))
err := RemoveContentsFromReportDir(randomReportDir)
Expect(err).ToNot(HaveOccurred())
By(fmt.Sprintf("Remove configs from config directory: %s", randomConfigDir))
err = RemoveContentsFromConfigDir(randomConfigDir)
Expect(err).ToNot(HaveOccurred())
By(fmt.Sprintf("Remove %s namespace", randomNamespace))
err = DeleteNamespaceAndWait(randomNamespace, waitingTime)
Expect(err).ToNot(HaveOccurred())
}