@@ -11,69 +11,44 @@ import (
11
11
. "github.com/onsi/ginkgo/v2"
12
12
. "github.com/onsi/gomega"
13
13
"github.com/stretchr/testify/require"
14
+ api_errors "k8s.io/apimachinery/pkg/api/errors"
14
15
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
+ "k8s.io/utils/ptr"
15
17
16
18
tu "github.com/kube-vip/kube-vip-cloud-provider/pkg/testutil"
17
19
"github.com/kube-vip/kube-vip-cloud-provider/test/e2e"
18
20
)
19
21
22
+ // Each suite load default manifest from scratch, so that changes on manifest objects won't impact other tests suites.
20
23
var f = e2e .NewFramework ()
21
24
22
- func TestDeployWithDifferentConfig (t * testing.T ) {
25
+ func TestDeployWithDefaultConfig (t * testing.T ) {
23
26
RegisterFailHandler (Fail )
24
27
RunSpecs (t , "deploy with default config" )
25
28
}
26
29
30
+ var _ = BeforeSuite (func () {
31
+ // By default, configMap only contains below 3 cidr:
32
+ // cidr-default: 192.168.0.200/29
33
+ // cidr-plunder: 192.168.0.210/29
34
+ // cidr-testing: 192.168.0.220/29
35
+
36
+ require .NoError (f .T (), f .Deployment .EnsureResources ())
37
+ })
38
+
27
39
var _ = AfterSuite (func () {
28
- // Make sure even if test failed, the kube-vip-cloud-provider deployments are cleaned up
40
+ // Reset resource requests for other tests.
29
41
require .NoError (f .T (), f .Deployment .DeleteResources ())
30
42
})
31
43
32
- var _ = Describe ("Default config" , func () {
33
- Context ("Deploy service in default namespace" , func () {
34
- BeforeEach (func () {
35
- // By default, kube-vip-cloud-provider will provide ip for service in any namespaces.
36
- require .NoError (f .T (), f .Deployment .EnsureResources ())
37
- })
38
-
39
- f .NamespacedTest ("create-services" , func (namespace string ) {
40
- Specify ("Service should be reconciled, has ip assigned and correct label" , func () {
41
- ctx := context .TODO ()
42
- By ("Create a service type LB" )
43
- svc := tu .NewService ("test1" , tu .TweakNamespace ("default" ))
44
- _ , err := f .Client .CoreV1 ().Services (svc .Namespace ).Create (ctx , svc , meta_v1.CreateOptions {})
45
- require .NoError (f .T (), err )
46
-
47
- By ("Service should have a valid IP assigned, and related kube-vip annotations and labels" )
48
- require .Eventually (f .T (), func () bool {
49
- svc , err = f .Client .CoreV1 ().Services (svc .Namespace ).Get (ctx , svc .Name , meta_v1.GetOptions {})
50
- if err != nil {
51
- return false
52
- }
53
- return e2e .ServiceIsReconciled (svc ) && e2e .ServiceHasIPAssigned (svc )
54
- }, 30 * time .Second , time .Second , fmt .Sprintf ("Service is not successfully reconciled %v, with error %v" , svc , err ))
55
- })
56
- })
57
-
58
- AfterEach (func () {
59
- // Reset resource requests for other tests.
60
- require .NoError (f .T (), f .Deployment .DeleteResources ())
61
- })
62
- })
63
-
64
- Context ("Deploy service in namespace that kube-vip-cloud-provider is not configured to" , func () {
65
- BeforeEach (func () {
66
- // Update configmap to only allocate ip for service in test-2 namespace
67
- f .Deployment .ConfigMap .Data = map [string ]string {
68
- "cidr-test-2" : "10.0.0.1/24" ,
69
- }
70
- require .NoError (f .T (), f .Deployment .EnsureResources ())
71
- })
44
+ var watchedNamespaces = []string {"default" , "testing" , "plunder" }
72
45
46
+ var _ = Describe ("Default config" , func () {
47
+ Context ("Deploy service in namespace that kube-vip-cloud-provider is configured and is not configured" , func () {
73
48
f .NamespacedTest ("create-services-in-different-namespace" , func (namespace string ) {
74
- Specify ("Service not be reconcile if namespace is not configured namespace test-2 , service in default namespace should be reconciled" , func () {
49
+ Specify ("Service not be reconcile if namespace is not configured namespace testing , service in default namespace should be reconciled" , func () {
75
50
ctx := context .TODO ()
76
- By ("Create a service type LB in namespace that's not test-2 " )
51
+ By ("Create a service type LB in namespace that's not testing " )
77
52
svc := tu .NewService ("test1" , tu .TweakNamespace (namespace ))
78
53
_ , err := f .Client .CoreV1 ().Services (svc .Namespace ).Create (ctx , svc , meta_v1.CreateOptions {})
79
54
require .NoError (f .T (), err )
@@ -87,25 +62,33 @@ var _ = Describe("Default config", func() {
87
62
return ! e2e .ServiceIsReconciled (svc ) && ! e2e .ServiceHasIPAssigned (svc )
88
63
}, 30 * time .Second , time .Second , fmt .Sprintf ("Service is not supposed to have label or annotation %v, with error %v" , svc , err ))
89
64
90
- By ("Create a service type LB in test-2 namespace" )
91
- svc = tu .NewService ("test2" , tu .TweakNamespace ("test-2" ))
92
- _ , err = f .Client .CoreV1 ().Services (svc .Namespace ).Create (ctx , svc , meta_v1.CreateOptions {})
93
- require .NoError (f .T (), err )
94
-
95
- By ("Service should have a valid IP assigned, and related kube-vip annotations and labels" )
96
- require .Eventually (f .T (), func () bool {
97
- svc , err = f .Client .CoreV1 ().Services (svc .Namespace ).Get (ctx , svc .Name , meta_v1.GetOptions {})
98
- if err != nil {
65
+ for _ , ns := range watchedNamespaces {
66
+ By ("Create a service type LB in watched namespace" )
67
+ svc = tu .NewService (fmt .Sprintf ("test-%s" , ns ), tu .TweakNamespace (ns ))
68
+ _ , err = f .Client .CoreV1 ().Services (svc .Namespace ).Create (ctx , svc , meta_v1.CreateOptions {})
69
+ require .NoError (f .T (), err )
70
+
71
+ By ("Service should have a valid IP assigned, and related kube-vip annotations and labels" )
72
+ require .Eventually (f .T (), func () bool {
73
+ svc , err = f .Client .CoreV1 ().Services (svc .Namespace ).Get (ctx , svc .Name , meta_v1.GetOptions {})
74
+ if err != nil {
75
+ return false
76
+ }
77
+ return e2e .ServiceIsReconciled (svc ) && e2e .ServiceHasIPAssigned (svc )
78
+ }, 30 * time .Second , time .Second , fmt .Sprintf ("Service is not successfully reconciled %v, with error %v" , svc , err ))
79
+
80
+ By ("Clean up the service" )
81
+ err := f .Client .CoreV1 ().Services (ns ).Delete (context .TODO (), svc .Name ,
82
+ meta_v1.DeleteOptions {PropagationPolicy : ptr .To (meta_v1 .DeletePropagationBackground )})
83
+ require .Eventually (f .T (), func () bool {
84
+ svc , err = f .Client .CoreV1 ().Services (svc .Namespace ).Get (ctx , svc .Name , meta_v1.GetOptions {})
85
+ if api_errors .IsNotFound (err ) {
86
+ return true
87
+ }
99
88
return false
100
- }
101
- return e2e .ServiceIsReconciled (svc ) && e2e .ServiceHasIPAssigned (svc )
102
- }, 30 * time .Second , time .Second , fmt .Sprintf ("Service is not successfully reconciled %v, with error %v" , svc , err ))
89
+ }, 30 * time .Second , time .Second , fmt .Sprintf ("Service is not successfully deleted %v, with error %v" , svc , err ))
90
+ }
103
91
})
104
- }, "test-2" )
105
-
106
- AfterEach (func () {
107
- // Reset resource requests for other tests.
108
- require .NoError (f .T (), f .Deployment .DeleteResources ())
109
- })
92
+ }, watchedNamespaces [1 :]... ) // Don't delete default namespace.
110
93
})
111
94
})
0 commit comments