@@ -17,18 +17,62 @@ limitations under the License.
1717package digitalocean
1818
1919import (
20+ "time"
21+
22+ "github.com/digitalocean/godo"
23+ "github.com/jarcoal/httpmock"
2024 . "github.com/onsi/ginkgo/v2"
2125 . "github.com/onsi/gomega"
26+ v1 "k8s.io/api/core/v1"
2227 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2328 "sigs.k8s.io/controller-runtime/pkg/client"
2429
2530 digitaloceanv1beta1 "github.com/smirl/digitalocean-floating-ip-controller/apis/digitalocean/v1beta1"
2631)
2732
33+ type floatingIPRoot struct {
34+ FloatingIP * godo.FloatingIP `json:"floating_ip"`
35+ }
36+ type actionRoot struct {
37+ Event * godo.Action `json:"action"`
38+ }
39+
40+ const TestIP string = "1.2.3.4"
41+
42+ var (
43+ node1 = v1.Node {
44+ ObjectMeta : metav1.ObjectMeta {Name : "node1" },
45+ Spec : v1.NodeSpec {ProviderID : "digitalocean://12345678" },
46+ }
47+ getResponseUnassigned = floatingIPRoot {
48+ FloatingIP : & godo.FloatingIP {IP : TestIP },
49+ }
50+ // getResponseAssigned = floatingIPRoot{
51+ // FloatingIP: &godo.FloatingIP{IP: TestIP, Droplet: &godo.Droplet{ID: 12345678}},
52+ // }
53+ assignResponse = actionRoot {Event : & godo.Action {}}
54+ )
55+
2856var _ = Context ("Floating IP Controller" , func () {
2957
3058 Describe ("when a new resources is created" , func () {
31- It ("should create a floating ip" , func () {
59+ It ("should assign a floating ip to a node" , func () {
60+
61+ By ("Adding Node" )
62+ Expect (k8sClient .Create (ctx , & node1 )).Should (Succeed (), "failed to create test binding" )
63+
64+ By ("Adding httpmocks" )
65+ httpmock .RegisterResponder (
66+ "GET" ,
67+ "/v2/floating_ips/1.2.3.4" ,
68+ httpmock .NewJsonResponderOrPanic (200 , getResponseUnassigned ),
69+ )
70+ httpmock .RegisterResponder (
71+ "POST" ,
72+ "/v2/floating_ips/1.2.3.4/actions" ,
73+ httpmock .NewJsonResponderOrPanic (200 , assignResponse ),
74+ )
75+
3276 By ("Creating a binding" )
3377 key := client.ObjectKey {
3478 Name : "floatingipbinding-sample" ,
@@ -44,7 +88,19 @@ var _ = Context("Floating IP Controller", func() {
4488 },
4589 }
4690 Expect (k8sClient .Create (ctx , binding )).Should (Succeed (), "failed to create test binding" )
91+ Expect (httpmock .GetCallCountInfo ()).To (HaveLen (2 ))
92+
93+ By ("Checking the status has updated" )
94+ Eventually (
95+ func () bool {
96+ binding := & digitaloceanv1beta1.FloatingIPBinding {}
97+ Expect (k8sClient .Get (ctx , key , binding )).Should (Succeed (), "failed to get binding" )
98+ return binding .Status .AssignedDropletName == "node1" && binding .Status .AssignedDropletID == 12345678
99+ },
100+ time .Second * 1 , time .Millisecond * 100 ,
101+ ).Should (BeTrue (), "Certificate should be set" )
47102 })
103+
48104 })
49105
50106})
0 commit comments