Skip to content

Commit 9e99199

Browse files
authored
Merge pull request #21 from Smirl/better-tests
Better tests
2 parents 19b35e6 + f1d8942 commit 9e99199

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

.github/workflows/test.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- uses: actions/setup-go@v2
18+
with:
19+
go-version: ^1.17
20+
21+
- name: Run tests
22+
run: make test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ testbin
2323
*.swp
2424
*.swo
2525
*~
26+
27+
.vscode/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ vet: ## Run go vet against code.
5757

5858
.PHONY: test
5959
test: manifests generate fmt vet envtest ## Run tests.
60-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out -test.v
60+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out -test.v -ginkgo.v
6161

6262
##@ Build
6363

controllers/digitalocean/floatingipbinding_controller_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,62 @@ limitations under the License.
1717
package digitalocean
1818

1919
import (
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+
2856
var _ = 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
})

controllers/digitalocean/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var _ = BeforeSuite(func() {
7272

7373
//+kubebuilder:scaffold:scheme
7474

75-
httpClient := http.Client{Transport: http.DefaultTransport.(*http.Transport).Clone()}
75+
httpClient := http.Client{Transport: &http.Transport{}}
7676
httpmock.ActivateNonDefault(&httpClient)
7777
doClient := godo.NewClient(&httpClient)
7878

0 commit comments

Comments
 (0)