Skip to content

Commit c206fa4

Browse files
authored
Ptr wrapper and unit tests (#456)
* added subnet to slice * added some unit tests for coverage * added more test cases * removed redundant unit test
1 parent bd49501 commit c206fa4

20 files changed

+2015
-103
lines changed

api/v1beta1/conversion_test.go

Lines changed: 713 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
Copyright (c) 2023 Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"github.com/oracle/cluster-api-provider-oci/api/v1beta2"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"sigs.k8s.io/controller-runtime/pkg/conversion"
26+
)
27+
28+
func TestOCICluster_ConvertTo(t *testing.T) {
29+
type fields struct {
30+
TypeMeta metav1.TypeMeta
31+
ObjectMeta metav1.ObjectMeta
32+
Spec OCIClusterSpec
33+
Status OCIClusterStatus
34+
}
35+
type args struct {
36+
dstRaw conversion.Hub
37+
}
38+
tests := []struct {
39+
name string
40+
fields fields
41+
args args
42+
wantErr bool
43+
}{
44+
{
45+
name: "successful conversion",
46+
fields: fields{
47+
TypeMeta: metav1.TypeMeta{},
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "test-cluster",
50+
Namespace: "default",
51+
},
52+
Spec: OCIClusterSpec{
53+
Region: "us-phoenix-1",
54+
},
55+
Status: OCIClusterStatus{
56+
AvailabilityDomains: map[string]OCIAvailabilityDomain{
57+
"AD-1": {
58+
Name: "Uocm:PHX-AD-1",
59+
FaultDomains: []string{"FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"},
60+
},
61+
},
62+
},
63+
},
64+
args: args{
65+
dstRaw: &v1beta2.OCICluster{},
66+
},
67+
wantErr: false,
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
src := &OCICluster{
74+
TypeMeta: tt.fields.TypeMeta,
75+
ObjectMeta: tt.fields.ObjectMeta,
76+
Spec: tt.fields.Spec,
77+
Status: tt.fields.Status,
78+
}
79+
if err := src.ConvertTo(tt.args.dstRaw); (err != nil) != tt.wantErr {
80+
t.Errorf("OCICluster.ConvertTo() error = %v, wantErr %v", err, tt.wantErr)
81+
}
82+
})
83+
}
84+
}
85+
86+
func Test_convertv1beta1NSGListTov1beta2NSGList(t *testing.T) {
87+
type args struct {
88+
in []*NSG
89+
}
90+
tests := []struct {
91+
name string
92+
args args
93+
want []*v1beta2.NSG
94+
wantErr bool
95+
}{
96+
{
97+
name: "normal conversion",
98+
args: args{
99+
in: []*NSG{
100+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
101+
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
102+
},
103+
},
104+
want: []*v1beta2.NSG{
105+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
106+
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
107+
},
108+
wantErr: false,
109+
},
110+
{
111+
name: "slice contains nil",
112+
args: args{
113+
in: []*NSG{
114+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
115+
nil,
116+
},
117+
},
118+
want: []*v1beta2.NSG{
119+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
120+
nil,
121+
},
122+
wantErr: false,
123+
},
124+
{
125+
name: "empty slice",
126+
args: args{
127+
in: []*NSG{},
128+
},
129+
want: []*v1beta2.NSG{},
130+
wantErr: false,
131+
},
132+
}
133+
for _, tt := range tests {
134+
t.Run(tt.name, func(t *testing.T) {
135+
got, err := convertv1beta1NSGListTov1beta2NSGList(tt.args.in)
136+
if (err != nil) != tt.wantErr {
137+
t.Errorf("convertv1beta1NSGListTov1beta2NSGList() error = %v, wantErr %v", err, tt.wantErr)
138+
return
139+
}
140+
if !reflect.DeepEqual(got, tt.want) {
141+
t.Errorf("convertv1beta1NSGListTov1beta2NSGList() = %v, want %v", got, tt.want)
142+
}
143+
})
144+
}
145+
}
146+
147+
func Test_convertv1beta2NSGListTov1beta1NSGList(t *testing.T) {
148+
type args struct {
149+
in []*v1beta2.NSG
150+
}
151+
tests := []struct {
152+
name string
153+
args args
154+
want []*NSG
155+
wantErr bool
156+
}{
157+
{
158+
name: "normal conversion",
159+
args: args{
160+
in: []*v1beta2.NSG{
161+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
162+
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
163+
},
164+
},
165+
want: []*NSG{
166+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
167+
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
168+
},
169+
wantErr: false,
170+
},
171+
{
172+
name: "slice contains nil",
173+
args: args{
174+
in: []*v1beta2.NSG{
175+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
176+
nil,
177+
},
178+
},
179+
want: []*NSG{
180+
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
181+
nil,
182+
},
183+
wantErr: false,
184+
},
185+
{
186+
name: "empty slice",
187+
args: args{
188+
in: []*v1beta2.NSG{},
189+
},
190+
want: []*NSG{},
191+
wantErr: false,
192+
},
193+
}
194+
for _, tt := range tests {
195+
t.Run(tt.name, func(t *testing.T) {
196+
got, err := convertv1beta2NSGListTov1beta1NSGList(tt.args.in)
197+
if (err != nil) != tt.wantErr {
198+
t.Errorf("convertv1beta2NSGListTov1beta1NSGList() error = %v, wantErr %v", err, tt.wantErr)
199+
return
200+
}
201+
if !reflect.DeepEqual(got, tt.want) {
202+
t.Errorf("convertv1beta2NSGListTov1beta1NSGList() = %v, want %v", got, tt.want)
203+
}
204+
})
205+
}
206+
}

api/v1beta2/ocicluster_webhook_test.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ package v1beta2
2121

2222
import (
2323
"context"
24+
"reflect"
2425
"strings"
2526
"testing"
2627

2728
"github.com/onsi/gomega"
2829
. "github.com/onsi/gomega"
2930
"github.com/oracle/oci-go-sdk/v65/common"
31+
corev1 "k8s.io/api/core/v1"
3032
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/runtime"
34+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3135
)
3236

3337
func TestOCICluster_ValidateCreate(t *testing.T) {
@@ -494,6 +498,55 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
494498
}
495499
}
496500

501+
func TestOCIClusterWebhook_ValidateDelete(t *testing.T) {
502+
tests := []struct {
503+
name string
504+
obj runtime.Object
505+
wantErr bool
506+
errMsg string
507+
}{
508+
{
509+
name: "valid OCICluster object",
510+
obj: &OCICluster{ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"}},
511+
wantErr: false,
512+
},
513+
{
514+
name: "invalid object type",
515+
obj: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "not-a-cluster"}},
516+
wantErr: true,
517+
errMsg: "expected an OCICluster object",
518+
},
519+
{
520+
name: "nil object",
521+
obj: nil,
522+
wantErr: true,
523+
errMsg: "expected an OCICluster object",
524+
},
525+
}
526+
527+
for _, tt := range tests {
528+
t.Run(tt.name, func(t *testing.T) {
529+
w := &OCIClusterWebhook{}
530+
warnings, err := w.ValidateDelete(context.Background(), tt.obj)
531+
532+
if tt.wantErr {
533+
if err == nil {
534+
t.Errorf("expected error but got nil")
535+
} else if !strings.Contains(err.Error(), tt.errMsg) {
536+
t.Errorf("expected error containing %q but got %q", tt.errMsg, err.Error())
537+
}
538+
} else if err != nil {
539+
t.Errorf("unexpected error: %v", err)
540+
}
541+
542+
// In both cases warnings should be nil
543+
if warnings != nil {
544+
t.Errorf("expected no warnings but got: %v", warnings)
545+
}
546+
})
547+
}
548+
}
549+
497550
func TestOCICluster_ValidateUpdate(t *testing.T) {
498551
goodSubnets := []*Subnet{
499552
&Subnet{
@@ -789,3 +842,114 @@ func TestOCICluster_CreateDefault(t *testing.T) {
789842
})
790843
}
791844
}
845+
846+
func TestOCICluster_GetConditions(t *testing.T) {
847+
tests := []struct {
848+
name string
849+
cluster *OCICluster
850+
want clusterv1.Conditions
851+
}{
852+
{
853+
name: "no conditions set, returns empty",
854+
cluster: &OCICluster{
855+
Status: OCIClusterStatus{
856+
Conditions: nil,
857+
},
858+
},
859+
want: nil,
860+
},
861+
{
862+
name: "returns existing conditions",
863+
cluster: &OCICluster{
864+
Status: OCIClusterStatus{
865+
Conditions: clusterv1.Conditions{
866+
{
867+
Type: clusterv1.ReadyCondition,
868+
Status: corev1.ConditionTrue,
869+
},
870+
},
871+
},
872+
},
873+
want: clusterv1.Conditions{
874+
{
875+
Type: clusterv1.ReadyCondition,
876+
Status: corev1.ConditionTrue,
877+
},
878+
},
879+
},
880+
}
881+
882+
for _, tt := range tests {
883+
t.Run(tt.name, func(t *testing.T) {
884+
if got := tt.cluster.GetConditions(); !reflect.DeepEqual(got, tt.want) {
885+
t.Errorf("GetConditions() = %v, want %v", got, tt.want)
886+
}
887+
})
888+
}
889+
}
890+
891+
func TestOCICluster_SetConditions(t *testing.T) {
892+
tests := []struct {
893+
name string
894+
conditions clusterv1.Conditions
895+
}{
896+
{
897+
name: "set single condition",
898+
conditions: clusterv1.Conditions{
899+
{
900+
Type: "Ready",
901+
Status: corev1.ConditionTrue,
902+
},
903+
},
904+
},
905+
{
906+
name: "set empty conditions",
907+
conditions: clusterv1.Conditions{},
908+
},
909+
}
910+
911+
for _, tt := range tests {
912+
t.Run(tt.name, func(t *testing.T) {
913+
c := &OCICluster{}
914+
c.SetConditions(tt.conditions)
915+
916+
if !reflect.DeepEqual(c.Status.Conditions, tt.conditions) {
917+
t.Errorf("SetConditions() got = %v, want %v", c.Status.Conditions, tt.conditions)
918+
}
919+
})
920+
}
921+
}
922+
923+
func TestOCICluster_GetOCIResourceIdentifier(t *testing.T) {
924+
tests := []struct {
925+
name string
926+
spec OCIClusterSpec
927+
want string
928+
}{
929+
{
930+
name: "with resource identifier",
931+
spec: OCIClusterSpec{
932+
OCIResourceIdentifier: "resource-123",
933+
},
934+
want: "resource-123",
935+
},
936+
{
937+
name: "empty resource identifier",
938+
spec: OCIClusterSpec{
939+
OCIResourceIdentifier: "",
940+
},
941+
want: "",
942+
},
943+
}
944+
945+
for _, tt := range tests {
946+
t.Run(tt.name, func(t *testing.T) {
947+
c := &OCICluster{
948+
Spec: tt.spec,
949+
}
950+
if got := c.GetOCIResourceIdentifier(); got != tt.want {
951+
t.Errorf("GetOCIResourceIdentifier() = %v, want %v", got, tt.want)
952+
}
953+
})
954+
}
955+
}

0 commit comments

Comments
 (0)