Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
714 changes: 713 additions & 1 deletion api/v1beta1/conversion_test.go

Large diffs are not rendered by default.

206 changes: 206 additions & 0 deletions api/v1beta1/ocicluster_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
Copyright (c) 2023 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"reflect"
"testing"

"github.com/oracle/cluster-api-provider-oci/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func TestOCICluster_ConvertTo(t *testing.T) {
type fields struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
Spec OCIClusterSpec
Status OCIClusterStatus
}
type args struct {
dstRaw conversion.Hub
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "successful conversion",
fields: fields{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "default",
},
Spec: OCIClusterSpec{
Region: "us-phoenix-1",
},
Status: OCIClusterStatus{
AvailabilityDomains: map[string]OCIAvailabilityDomain{
"AD-1": {
Name: "Uocm:PHX-AD-1",
FaultDomains: []string{"FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"},
},
},
},
},
args: args{
dstRaw: &v1beta2.OCICluster{},
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
src := &OCICluster{
TypeMeta: tt.fields.TypeMeta,
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
if err := src.ConvertTo(tt.args.dstRaw); (err != nil) != tt.wantErr {
t.Errorf("OCICluster.ConvertTo() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_convertv1beta1NSGListTov1beta2NSGList(t *testing.T) {
type args struct {
in []*NSG
}
tests := []struct {
name string
args args
want []*v1beta2.NSG
wantErr bool
}{
{
name: "normal conversion",
args: args{
in: []*NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
},
},
want: []*v1beta2.NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
},
wantErr: false,
},
{
name: "slice contains nil",
args: args{
in: []*NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
nil,
},
},
want: []*v1beta2.NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
nil,
},
wantErr: false,
},
{
name: "empty slice",
args: args{
in: []*NSG{},
},
want: []*v1beta2.NSG{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := convertv1beta1NSGListTov1beta2NSGList(tt.args.in)
if (err != nil) != tt.wantErr {
t.Errorf("convertv1beta1NSGListTov1beta2NSGList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertv1beta1NSGListTov1beta2NSGList() = %v, want %v", got, tt.want)
}
})
}
}

func Test_convertv1beta2NSGListTov1beta1NSGList(t *testing.T) {
type args struct {
in []*v1beta2.NSG
}
tests := []struct {
name string
args args
want []*NSG
wantErr bool
}{
{
name: "normal conversion",
args: args{
in: []*v1beta2.NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
},
},
want: []*NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
{ID: func() *string { s := "nsg2"; return &s }(), Name: "NSG-2"},
},
wantErr: false,
},
{
name: "slice contains nil",
args: args{
in: []*v1beta2.NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
nil,
},
},
want: []*NSG{
{ID: func() *string { s := "nsg1"; return &s }(), Name: "NSG-1"},
nil,
},
wantErr: false,
},
{
name: "empty slice",
args: args{
in: []*v1beta2.NSG{},
},
want: []*NSG{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := convertv1beta2NSGListTov1beta1NSGList(tt.args.in)
if (err != nil) != tt.wantErr {
t.Errorf("convertv1beta2NSGListTov1beta1NSGList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertv1beta2NSGListTov1beta1NSGList() = %v, want %v", got, tt.want)
}
})
}
}
164 changes: 164 additions & 0 deletions api/v1beta2/ocicluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ package v1beta2

import (
"context"
"reflect"
"strings"
"testing"

"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/oracle/oci-go-sdk/v65/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestOCICluster_ValidateCreate(t *testing.T) {
Expand Down Expand Up @@ -494,6 +498,55 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
}
}

func TestOCIClusterWebhook_ValidateDelete(t *testing.T) {
tests := []struct {
name string
obj runtime.Object
wantErr bool
errMsg string
}{
{
name: "valid OCICluster object",
obj: &OCICluster{ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"}},
wantErr: false,
},
{
name: "invalid object type",
obj: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "not-a-cluster"}},
wantErr: true,
errMsg: "expected an OCICluster object",
},
{
name: "nil object",
obj: nil,
wantErr: true,
errMsg: "expected an OCICluster object",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &OCIClusterWebhook{}
warnings, err := w.ValidateDelete(context.Background(), tt.obj)

if tt.wantErr {
if err == nil {
t.Errorf("expected error but got nil")
} else if !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("expected error containing %q but got %q", tt.errMsg, err.Error())
}
} else if err != nil {
t.Errorf("unexpected error: %v", err)
}

// In both cases warnings should be nil
if warnings != nil {
t.Errorf("expected no warnings but got: %v", warnings)
}
})
}
}

func TestOCICluster_ValidateUpdate(t *testing.T) {
goodSubnets := []*Subnet{
&Subnet{
Expand Down Expand Up @@ -789,3 +842,114 @@ func TestOCICluster_CreateDefault(t *testing.T) {
})
}
}

func TestOCICluster_GetConditions(t *testing.T) {
tests := []struct {
name string
cluster *OCICluster
want clusterv1.Conditions
}{
{
name: "no conditions set, returns empty",
cluster: &OCICluster{
Status: OCIClusterStatus{
Conditions: nil,
},
},
want: nil,
},
{
name: "returns existing conditions",
cluster: &OCICluster{
Status: OCIClusterStatus{
Conditions: clusterv1.Conditions{
{
Type: clusterv1.ReadyCondition,
Status: corev1.ConditionTrue,
},
},
},
},
want: clusterv1.Conditions{
{
Type: clusterv1.ReadyCondition,
Status: corev1.ConditionTrue,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cluster.GetConditions(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetConditions() = %v, want %v", got, tt.want)
}
})
}
}

func TestOCICluster_SetConditions(t *testing.T) {
tests := []struct {
name string
conditions clusterv1.Conditions
}{
{
name: "set single condition",
conditions: clusterv1.Conditions{
{
Type: "Ready",
Status: corev1.ConditionTrue,
},
},
},
{
name: "set empty conditions",
conditions: clusterv1.Conditions{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &OCICluster{}
c.SetConditions(tt.conditions)

if !reflect.DeepEqual(c.Status.Conditions, tt.conditions) {
t.Errorf("SetConditions() got = %v, want %v", c.Status.Conditions, tt.conditions)
}
})
}
}

func TestOCICluster_GetOCIResourceIdentifier(t *testing.T) {
tests := []struct {
name string
spec OCIClusterSpec
want string
}{
{
name: "with resource identifier",
spec: OCIClusterSpec{
OCIResourceIdentifier: "resource-123",
},
want: "resource-123",
},
{
name: "empty resource identifier",
spec: OCIClusterSpec{
OCIResourceIdentifier: "",
},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &OCICluster{
Spec: tt.spec,
}
if got := c.GetOCIResourceIdentifier(); got != tt.want {
t.Errorf("GetOCIResourceIdentifier() = %v, want %v", got, tt.want)
}
})
}
}
Loading