-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmcm_suite_test.go
134 lines (106 loc) · 4.84 KB
/
mcm_suite_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
package mcm
import (
"fmt"
"runtime"
"strings"
"testing"
"time"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/deployment"
"github.com/openshift-kni/eco-goinfra/pkg/nodes"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
"github.com/openshift-kni/eco-goinfra/pkg/reportxml"
"github.com/openshift-kni/eco-goinfra/pkg/serviceaccount"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/define"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/kmmparams"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/kmminittools"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/mcm/internal/tsparams"
. "github.com/openshift-kni/eco-gotests/tests/internal/inittools"
"github.com/openshift-kni/eco-gotests/tests/internal/reporter"
_ "github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/mcm/tests"
)
var (
prereqName = "kmm-tests-executor"
)
var _, currentFile, _, _ = runtime.Caller(0)
func TestManagedClusterModules(t *testing.T) {
_, reporterConfig := GinkgoConfiguration()
reporterConfig.JUnitReport = GeneralConfig.GetJunitReportPath(currentFile)
RegisterFailHandler(Fail)
RunSpecs(t, "KMM-HUB", Label(tsparams.Labels...), reporterConfig)
}
var _ = ReportAfterSuite("", func(report Report) {
reportxml.Create(
report, GeneralConfig.GetReportPath(), GeneralConfig.TCPrefix)
})
var _ = JustAfterEach(func() {
reporter.ReportIfFailed(
CurrentSpecReport(), currentFile, tsparams.ReporterNamespacesToDump, tsparams.ReporterCRDsToDump)
})
var _ = BeforeSuite(func() {
By("Prepare environment spoke for KMM-HUB tests execution")
if ModulesConfig.SpokeClusterName == "" || ModulesConfig.SpokeKubeConfig == "" {
Skip("Skipping test. No Spoke environment variables defined.")
}
By("Create helper ServiceAccount")
svcAccount, err := serviceaccount.
NewBuilder(ModulesConfig.SpokeAPIClient, prereqName, kmmparams.KmmOperatorNamespace).Create()
Expect(err).ToNot(HaveOccurred(), "error creating serviceaccount")
By("Create helper ClusterRoleBinding")
crb := define.ModuleCRB(*svcAccount, prereqName)
_, err = crb.Create()
Expect(err).ToNot(HaveOccurred(), "error creating clusterrolebinding")
By("Create helper Deployments")
nodeList, err := nodes.List(
ModulesConfig.SpokeAPIClient, metav1.ListOptions{LabelSelector: labels.Set(GeneralConfig.WorkerLabelMap).String()})
if err != nil {
Skip(fmt.Sprintf("Error listing worker nodes. Got error: '%v'", err))
}
for _, node := range nodeList {
glog.V(kmmparams.KmmLogLevel).Infof("Creating privileged deployment on node '%v'", node.Object.Name)
deploymentName := fmt.Sprintf("%s-%s", kmmparams.KmmTestHelperLabelName, node.Object.Name)
containerCfg, _ := pod.NewContainerBuilder("test", kmmparams.DTKImage,
[]string{"/bin/bash", "-c", "sleep INF"}).
WithSecurityContext(kmmparams.PrivilegedSC).GetContainerCfg()
deploymentCfg := deployment.NewBuilder(ModulesConfig.SpokeAPIClient, deploymentName, kmmparams.KmmOperatorNamespace,
map[string]string{kmmparams.KmmTestHelperLabelName: ""}, *containerCfg)
deploymentCfg.WithLabel(kmmparams.KmmTestHelperLabelName, "").
WithNodeSelector(map[string]string{"kubernetes.io/hostname": node.Object.Name}).
WithServiceAccountName("kmm-operator-module-loader")
_, err = deploymentCfg.CreateAndWaitUntilReady(10 * time.Minute)
if err != nil {
Skip(fmt.Sprintf("Could not create deploymentCfg on %s. Got error : %v", node.Object.Name, err))
}
}
})
var _ = AfterSuite(func() {
By("Cleanup environment after KMM tests execution")
glog.V(kmmparams.KmmLogLevel).Infof("Deleting test deployments")
By("Delete helper deployments")
testDeployments, err := deployment.List(ModulesConfig.SpokeAPIClient,
kmmparams.KmmOperatorNamespace, metav1.ListOptions{})
if err != nil {
Fail(fmt.Sprintf("Error cleaning up environment. Got error: %v", err))
}
for _, deploymentObj := range testDeployments {
glog.V(kmmparams.KmmLogLevel).Infof("Deployment: '%s'\n", deploymentObj.Object.Name)
if strings.Contains(deploymentObj.Object.Name, kmmparams.KmmTestHelperLabelName) {
glog.V(kmmparams.KmmLogLevel).Infof("Deleting deployment: '%s'\n", deploymentObj.Object.Name)
err = deploymentObj.DeleteAndWait(time.Minute)
Expect(err).ToNot(HaveOccurred(), "error deleting helper deployment")
}
}
By("Delete helper clusterrolebinding")
svcAccount := serviceaccount.NewBuilder(ModulesConfig.SpokeAPIClient, prereqName, kmmparams.KmmOperatorNamespace)
svcAccount.Exists()
crb := define.ModuleCRB(*svcAccount, prereqName)
err = crb.Delete()
Expect(err).ToNot(HaveOccurred(), "error deleting helper clusterrolebinding")
By("Delete helper service account")
err = svcAccount.Delete()
Expect(err).ToNot(HaveOccurred(), "error deleting helper serviceaccount")
})