Cleanup framework package

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-02-12 19:52:01 -03:00
parent 4a33b1e163
commit b37270bdde
2 changed files with 29 additions and 34 deletions

View file

@ -31,8 +31,8 @@ import (
restclient "k8s.io/client-go/rest" restclient "k8s.io/client-go/rest"
"k8s.io/klog" "k8s.io/klog"
. "github.com/onsi/ginkgo" "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" "github.com/onsi/gomega"
) )
// RequestScheme define a scheme used in a test request. // RequestScheme define a scheme used in a test request.
@ -73,8 +73,8 @@ func NewDefaultFramework(baseName string) *Framework {
BaseName: baseName, BaseName: baseName,
} }
BeforeEach(f.BeforeEach) ginkgo.BeforeEach(f.BeforeEach)
AfterEach(f.AfterEach) ginkgo.AfterEach(f.AfterEach)
return f return f
} }
@ -83,46 +83,42 @@ func NewDefaultFramework(baseName string) *Framework {
func (f *Framework) BeforeEach() { func (f *Framework) BeforeEach() {
f.cleanupHandle = AddCleanupAction(f.AfterEach) f.cleanupHandle = AddCleanupAction(f.AfterEach)
By("Creating a kubernetes client")
kubeConfig, err := restclient.InClusterConfig() kubeConfig, err := restclient.InClusterConfig()
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
f.KubeConfig = kubeConfig f.KubeConfig = kubeConfig
f.KubeClientSet, err = kubernetes.NewForConfig(kubeConfig) f.KubeClientSet, err = kubernetes.NewForConfig(kubeConfig)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
By("Building a namespace api object")
ingressNamespace, err := CreateKubeNamespace(f.BaseName, f.KubeClientSet) ingressNamespace, err := CreateKubeNamespace(f.BaseName, f.KubeClientSet)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
f.Namespace = ingressNamespace f.Namespace = ingressNamespace
By("Starting new ingress controller")
err = f.NewIngressController(f.Namespace, f.BaseName) err = f.NewIngressController(f.Namespace, f.BaseName)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = WaitForPodsReady(f.KubeClientSet, DefaultTimeout, 1, f.Namespace, metav1.ListOptions{ err = WaitForPodsReady(f.KubeClientSet, DefaultTimeout, 1, f.Namespace, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=ingress-nginx", LabelSelector: "app.kubernetes.io/name=ingress-nginx",
}) })
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
} }
// AfterEach deletes the namespace, after reading its events. // AfterEach deletes the namespace, after reading its events.
func (f *Framework) AfterEach() { func (f *Framework) AfterEach() {
RemoveCleanupAction(f.cleanupHandle) RemoveCleanupAction(f.cleanupHandle)
By("Waiting for test namespace to no longer exist")
err := DeleteKubeNamespace(f.KubeClientSet, f.Namespace) err := DeleteKubeNamespace(f.KubeClientSet, f.Namespace)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
if CurrentGinkgoTestDescription().Failed { if ginkgo.CurrentGinkgoTestDescription().Failed {
log, err := f.NginxLogs() log, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred()) gomega.Expect(err).ToNot(gomega.HaveOccurred())
By("Dumping NGINX logs after a failure running a test") ginkgo.By("Dumping NGINX logs after a failure running a test")
Logf("%v", log) Logf("%v", log)
pod, err := getIngressNGINXPod(f.Namespace, f.KubeClientSet) pod, err := getIngressNGINXPod(f.Namespace, f.KubeClientSet)
@ -136,19 +132,19 @@ func (f *Framework) AfterEach() {
return return
} }
By("Dumping NGINX configuration after a failure running a test") ginkgo.By("Dumping NGINX configuration after a failure running a test")
Logf("%v", o) Logf("%v", o)
} }
} }
// IngressNginxDescribe wrapper function for ginkgo describe. Adds namespacing. // IngressNginxDescribe wrapper function for ginkgo describe. Adds namespacing.
func IngressNginxDescribe(text string, body func()) bool { func IngressNginxDescribe(text string, body func()) bool {
return Describe("[ingress-nginx] "+text, body) return ginkgo.Describe("[ingress-nginx] "+text, body)
} }
// MemoryLeakIt is wrapper function for ginkgo It. Adds "[MemoryLeak]" tag and makes static analysis easier. // MemoryLeakIt is wrapper function for ginkgo It. Adds "[MemoryLeak]" tag and makes static analysis easier.
func MemoryLeakIt(text string, body interface{}, timeout ...float64) bool { func MemoryLeakIt(text string, body interface{}, timeout ...float64) bool {
return It(text+" [MemoryLeak]", body, timeout...) return ginkgo.It(text+" [MemoryLeak]", body, timeout...)
} }
// GetNginxIP returns the number of TCP port where NGINX is running // GetNginxIP returns the number of TCP port where NGINX is running
@ -157,7 +153,7 @@ func (f *Framework) GetNginxIP() string {
CoreV1(). CoreV1().
Services(f.Namespace). Services(f.Namespace).
Get("ingress-nginx", metav1.GetOptions{}) Get("ingress-nginx", metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred(), "unexpected error obtaining NGINX IP address") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error obtaining NGINX IP address")
return s.Spec.ClusterIP return s.Spec.ClusterIP
} }
@ -167,7 +163,7 @@ func (f *Framework) GetNginxPodIP() []string {
CoreV1(). CoreV1().
Endpoints(f.Namespace). Endpoints(f.Namespace).
Get("ingress-nginx", metav1.GetOptions{}) Get("ingress-nginx", metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred(), "unexpected error obtaining NGINX IP address") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error obtaining NGINX IP address")
eips := make([]string, 0) eips := make([]string, 0)
for _, s := range e.Subsets { for _, s := range e.Subsets {
for _, a := range s.Addresses { for _, a := range s.Addresses {
@ -187,14 +183,14 @@ func (f *Framework) GetURL(scheme RequestScheme) string {
// WaitForNginxServer waits until the nginx configuration contains a particular server section // WaitForNginxServer waits until the nginx configuration contains a particular server section
func (f *Framework) WaitForNginxServer(name string, matcher func(cfg string) bool) { func (f *Framework) WaitForNginxServer(name string, matcher func(cfg string) bool) {
err := wait.Poll(Poll, DefaultTimeout, f.matchNginxConditions(name, matcher)) err := wait.Poll(Poll, DefaultTimeout, f.matchNginxConditions(name, matcher))
Expect(err).NotTo(HaveOccurred(), "unexpected error waiting for nginx server condition/s") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error waiting for nginx server condition/s")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
// WaitForNginxConfiguration waits until the nginx configuration contains a particular configuration // WaitForNginxConfiguration waits until the nginx configuration contains a particular configuration
func (f *Framework) WaitForNginxConfiguration(matcher func(cfg string) bool) { func (f *Framework) WaitForNginxConfiguration(matcher func(cfg string) bool) {
err := wait.Poll(Poll, DefaultTimeout, f.matchNginxConditions("", matcher)) err := wait.Poll(Poll, DefaultTimeout, f.matchNginxConditions("", matcher))
Expect(err).NotTo(HaveOccurred(), "unexpected error waiting for nginx server condition/s") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error waiting for nginx server condition/s")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
@ -236,7 +232,7 @@ func (f *Framework) matchNginxConditions(name string, matcher func(cfg string) b
} }
var match bool var match bool
errs := InterceptGomegaFailures(func() { errs := gomega.InterceptGomegaFailures(func() {
if klog.V(10) && len(o) > 0 { if klog.V(10) && len(o) > 0 {
klog.Infof("nginx.conf:\n%v", o) klog.Infof("nginx.conf:\n%v", o)
} }
@ -299,8 +295,8 @@ func (f *Framework) SetNginxConfigMapData(cmData map[string]string) {
func (f *Framework) SetConfigMapData(name string, cmData map[string]string) { func (f *Framework) SetConfigMapData(name string, cmData map[string]string) {
config, err := f.getConfigMap(name) config, err := f.getConfigMap(name)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
Expect(config).NotTo(BeNil(), "expected a configmap but none returned") gomega.Expect(config).NotTo(gomega.BeNil(), "expected a configmap but none returned")
config.Data = cmData config.Data = cmData
@ -308,7 +304,7 @@ func (f *Framework) SetConfigMapData(name string, cmData map[string]string) {
CoreV1(). CoreV1().
ConfigMaps(f.Namespace). ConfigMaps(f.Namespace).
Update(config) Update(config)
Expect(err).NotTo(HaveOccurred()) gomega.Expect(err).NotTo(gomega.HaveOccurred())
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
@ -321,13 +317,13 @@ func (f *Framework) CreateConfigMap(name string, data map[string]string) {
}, },
Data: data, Data: data,
}) })
Expect(err).NotTo(HaveOccurred(), "failed to create configMap") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to create configMap")
} }
// UpdateNginxConfigMapData updates single field in ingress-nginx's nginx-configuration map data // UpdateNginxConfigMapData updates single field in ingress-nginx's nginx-configuration map data
func (f *Framework) UpdateNginxConfigMapData(key string, value string) { func (f *Framework) UpdateNginxConfigMapData(key string, value string) {
config, err := f.GetNginxConfigMapData() config, err := f.GetNginxConfigMapData()
Expect(err).NotTo(HaveOccurred(), "unexpected error reading configmap") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error reading configmap")
config[key] = value config[key] = value
@ -340,10 +336,10 @@ func (f *Framework) UpdateNginxConfigMapData(key string, value string) {
func (f *Framework) DeleteNGINXPod(grace int64) { func (f *Framework) DeleteNGINXPod(grace int64) {
ns := f.Namespace ns := f.Namespace
pod, err := getIngressNGINXPod(ns, f.KubeClientSet) pod, err := getIngressNGINXPod(ns, f.KubeClientSet)
Expect(err).NotTo(HaveOccurred(), "expected ingress nginx pod to be running") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "expected ingress nginx pod to be running")
err = f.KubeClientSet.CoreV1().Pods(ns).Delete(pod.GetName(), metav1.NewDeleteOptions(grace)) err = f.KubeClientSet.CoreV1().Pods(ns).Delete(pod.GetName(), metav1.NewDeleteOptions(grace))
Expect(err).NotTo(HaveOccurred(), "unexpected error deleting ingress nginx pod") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error deleting ingress nginx pod")
err = wait.Poll(Poll, DefaultTimeout, func() (bool, error) { err = wait.Poll(Poll, DefaultTimeout, func() (bool, error) {
pod, err := getIngressNGINXPod(ns, f.KubeClientSet) pod, err := getIngressNGINXPod(ns, f.KubeClientSet)
@ -352,7 +348,7 @@ func (f *Framework) DeleteNGINXPod(grace int64) {
} }
return pod.GetName() != "", nil return pod.GetName() != "", nil
}) })
Expect(err).NotTo(HaveOccurred(), "unexpected error while waiting for ingress nginx pod to come up again") gomega.Expect(err).NotTo(gomega.HaveOccurred(), "unexpected error while waiting for ingress nginx pod to come up again")
} }
// UpdateDeployment runs the given updateFunc on the deployment and waits for it to be updated // UpdateDeployment runs the given updateFunc on the deployment and waits for it to be updated

View file

@ -107,7 +107,6 @@ func CreateKubeNamespace(baseName string, c kubernetes.Interface) (string, error
Logf("Unexpected error while creating namespace: %v", err) Logf("Unexpected error while creating namespace: %v", err)
return false, nil return false, nil
} }
Logf("Created namespace: %v", got.Name)
return true, nil return true, nil
}) })
if err != nil { if err != nil {