Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add sset creation test for harder paths #219

Open
wants to merge 4 commits into
base: feat/serverset_ctrl
Choose a base branch
from
Open
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
229 changes: 229 additions & 0 deletions internal/controller/serverset/serverset_crossplane_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package serverset

import (
"context"
"errors"
"fmt"
"testing"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/ionos-cloud/crossplane-provider-ionoscloud/apis/compute/v1alpha1"
"github.com/ionos-cloud/crossplane-provider-ionoscloud/pkg/kube"
)

func Test_serverSet_Create(t *testing.T) {
fakeObjList := func(obj client.ObjectList) error { return nil }
fakeGetObj := func(obj client.Object) error {
switch res := obj.(type) {
case *v1alpha1.Server:
res.Status.AtProvider.State = "AVAILABLE"
res.Status.AtProvider.ServerID = "test-id"
case *v1alpha1.Volume:
res.Status.AtProvider.State = "AVAILABLE"
res.Status.AtProvider.VolumeID = "test-id"
case *v1alpha1.Nic:
res.Status.AtProvider.State = "AVAILABLE"
res.Status.AtProvider.NicID = "test-id"
}
return nil
}
type fields struct {
kube client.Client
mg resource.Managed
bootVolumeController kubeBootVolumeControlManager
nicController kubeNicControlManager
serverController kubeServerControlManager
log logging.Logger
}
type args struct {
ctx context.Context
cr *v1alpha1.ServerSet
}

tests := []struct {
name string
fields fields
args args
want managed.ExternalCreation
wantErr error
}{
{
name: "server set successfully created",
cristiGuranIonos marked this conversation as resolved.
Show resolved Hide resolved
fields: fields{
kube: &test.MockClient{
MockList: test.NewMockListFn(nil, fakeObjList),
},
log: logging.NewNopLogger(),
mg: &fake.Managed{},
bootVolumeController: &kubeBootVolumeController{
kube: &test.MockClient{
MockCreate: test.NewMockCreateFn(nil),
MockList: test.NewMockListFn(nil, fakeObjList),
MockGet: test.NewMockGetFn(nil, fakeGetObj),
},
log: logging.NewNopLogger(),
},
serverController: &kubeServerController{
kube: &test.MockClient{
MockCreate: test.NewMockCreateFn(nil),
MockList: test.NewMockListFn(nil, fakeObjList),
MockGet: test.NewMockGetFn(nil, fakeGetObj),
},
log: logging.NewNopLogger(),
},
nicController: &kubeNicController{
kube: &test.MockClient{
MockCreate: test.NewMockCreateFn(nil),
MockList: test.NewMockListFn(nil, fakeObjList),
MockGet: test.NewMockGetFn(nil, fakeGetObj),
},
log: logging.NewNopLogger(),
},
},
args: args{
ctx: context.Background(),
cr: createBasicServerSet(),
},
want: managed.ExternalCreation{
ConnectionDetails: managed.ConnectionDetails{},
},
wantErr: nil,
},
{
name: "failed to create, too many volumes",
cristiGuranIonos marked this conversation as resolved.
Show resolved Hide resolved
fields: fields{
kube: &test.MockClient{
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
vols := obj.(*v1alpha1.VolumeList)
vols.Items = append(append(vols.Items, v1alpha1.Volume{}), v1alpha1.Volume{})
return nil
}),
},
log: logging.NewNopLogger(),
mg: &fake.Managed{},
},
args: args{
ctx: context.Background(),
cr: createBasicServerSet(),
},
want: managed.ExternalCreation{
ConnectionDetails: nil,
},
wantErr: errors.New("found too many volumes for index 0"),
},
{
name: "external create failed",
fields: fields{
kube: &test.MockClient{
MockList: test.NewMockListFn(nil, fakeObjList),
},
log: logging.NewNopLogger(),
mg: &fake.Managed{},
bootVolumeController: &kubeBootVolumeController{
kube: &test.MockClient{
MockCreate: test.NewMockCreateFn(nil),
MockList: test.NewMockListFn(nil, fakeObjList),
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
volume := obj.(*v1alpha1.Volume)
annotations := make(map[string]string)
annotations[meta.AnnotationKeyExternalCreateFailed] = "alas, we failed"
volume.SetAnnotations(annotations)
return nil
cristiGuranIonos marked this conversation as resolved.
Show resolved Hide resolved
}),
MockDelete: test.NewMockDeleteFn(nil),
},
log: logging.NewNopLogger(),
},
},
args: args{
ctx: context.Background(),
cr: createBasicServerSet(),
},
wantErr: fmt.Errorf("while waiting for BootVolume to be populated %w ", kube.ErrExternalCreateFailed),
},
{
name: "bad cloud init",
fields: fields{
kube: &test.MockClient{
MockList: test.NewMockListFn(nil, fakeObjList),
},
log: logging.NewNopLogger(),
mg: &fake.Managed{},
bootVolumeController: &kubeBootVolumeController{
kube: &test.MockClient{
MockCreate: test.NewMockCreateFn(nil),
MockList: test.NewMockListFn(nil, fakeObjList),
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
volume := obj.(*v1alpha1.Volume)
annotations := make(map[string]string)
annotations[meta.AnnotationKeyExternalCreateFailed] = "alas, we failed"
volume.SetAnnotations(annotations)
return nil
}),
MockDelete: test.NewMockDeleteFn(nil),
},
log: logging.NewNopLogger(),
},
},
args: args{
ctx: context.Background(),
cr: &v1alpha1.ServerSet{
ObjectMeta: metav1.ObjectMeta{
Name: serverSetName,
Annotations: map[string]string{
"crossplane.io/external-name": serverSetName,
},
},
Spec: v1alpha1.ServerSetSpec{
ForProvider: v1alpha1.ServerSetParameters{
Replicas: noReplicas,
Template: v1alpha1.ServerSetTemplate{
Metadata: v1alpha1.ServerSetMetadata{
Name: "servername",
},
},
BootVolumeTemplate: v1alpha1.BootVolumeTemplate{
Metadata: v1alpha1.ServerSetBootVolumeMetadata{
Name: "bootvolumename",
},
Spec: v1alpha1.ServerSetBootVolumeSpec{
Size: bootVolumeSize,
Image: bootVolumeImage,
Type: bootVolumeType,
UserData: "malformed",
},
},
},
},
},
},
wantErr: fmt.Errorf("while creating cloud init patcher for BootVolume bootvolumename-0-0 failed to decode base64 (illegal base64 data at input byte 8)"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &external{
kube: tt.fields.kube,
bootVolumeController: tt.fields.bootVolumeController,
nicController: tt.fields.nicController,
serverController: tt.fields.serverController,
log: tt.fields.log,
}

got, err := e.Create(tt.args.ctx, tt.args.cr)
if tt.wantErr != nil {
assert.ErrorContains(t, err, tt.wantErr.Error())
}
assert.Equal(t, tt.want, got)
})
}
}
Loading