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

Minor UT coverage improvements #753

Merged
merged 1 commit into from
Jul 15, 2022
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
16 changes: 0 additions & 16 deletions cloud/scope/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"

. "github.com/onsi/gomega"
)

const (
Expand Down Expand Up @@ -91,16 +88,3 @@ func newBootstrapSecret(clusterName, machineName string) *corev1.Secret {
},
}
}

func createObject(g *WithT, obj client.Object, namespace string) {
if obj.DeepCopyObject() != nil {
obj.SetNamespace(namespace)
g.Expect(testEnv.Create(ctx, obj)).To(Succeed())
}
}

func cleanupObject(g *WithT, obj client.Object) {
if obj.DeepCopyObject() != nil {
g.Expect(testEnv.Cleanup(ctx, obj)).To(Succeed())
}
}
34 changes: 20 additions & 14 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scope

import (
"context"
"errors"
"testing"

Expand Down Expand Up @@ -145,20 +146,6 @@ func TestCreateMachine(t *testing.T) {
t.Run("Should create Machine", func(t *testing.T) {
scope := setupMachineScope(clusterName, machineName, mockvpc)
scope.IBMVPCMachine.Spec = vpcMachine.Spec
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
capiv1beta1.ClusterLabelName: scope.Cluster.Name,
},
Name: scope.Machine.Name,
Namespace: "default",
},
Data: map[string][]byte{
"value": []byte("user data"),
},
}
createObject(g, secret, "default")
defer cleanupObject(g, secret)
instance := &vpcv1.Instance{
Name: &scope.Machine.Name,
}
Expand Down Expand Up @@ -203,6 +190,25 @@ func TestCreateMachine(t *testing.T) {
g.Expect(err).To(Not(BeNil()))
})

t.Run("Failed to retrieve bootstrap data, secret value key is missing", func(t *testing.T) {
scope := setupMachineScope(clusterName, machineName, mockvpc)
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
capiv1beta1.ClusterLabelName: clusterName,
},
Name: machineName,
Namespace: "default",
},
Data: map[string][]byte{
"val": []byte("user data"),
}}
g.Expect(scope.Client.Update(context.Background(), secret)).To(Succeed())
mockvpc.EXPECT().ListInstances(gomock.AssignableToTypeOf(listInstancesOptions)).Return(instanceCollection, detailedResponse, nil)
_, err := scope.CreateMachine()
g.Expect(err).To(Not(BeNil()))
})

t.Run("Failed to create instance", func(t *testing.T) {
scope := setupMachineScope(clusterName, machineName, mockvpc)
mockvpc.EXPECT().ListInstances(gomock.AssignableToTypeOf(listInstancesOptions)).Return(instanceCollection, detailedResponse, nil)
Expand Down
21 changes: 3 additions & 18 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
)

Expand Down Expand Up @@ -101,18 +99,6 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (scope *PowerVSClu

spec := params.IBMPowerVSCluster.Spec

auth, err := authenticator.GetAuthenticator()
if err != nil {
err = errors.Wrap(err, "failed to get authenticator")
return
}

account, err := servicesutils.GetAccount(auth)
if err != nil {
err = errors.Wrap(err, "failed to get account")
return
}

rc, err := resourcecontroller.NewService(resourcecontroller.ServiceOptions{})
if err != nil {
return
Expand Down Expand Up @@ -143,10 +129,9 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (scope *PowerVSClu

options := powervs.ServiceOptions{
IBMPIOptions: &ibmpisession.IBMPIOptions{
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
UserAccount: account,
Region: region,
Zone: *res.RegionID,
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
Region: region,
Zone: *res.RegionID,
},
CloudInstanceID: spec.ServiceInstanceID,
}
Expand Down
21 changes: 3 additions & 18 deletions cloud/scope/powervs_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)
Expand Down Expand Up @@ -94,18 +92,6 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc

spec := params.IBMPowerVSImage.Spec

auth, err := authenticator.GetAuthenticator()
if err != nil {
err = errors.Wrap(err, "failed to get authenticator")
return
}

account, err := servicesutils.GetAccount(auth)
if err != nil {
err = errors.Wrap(err, "failed to get account")
return
}

rc, err := resourcecontroller.NewService(resourcecontroller.ServiceOptions{})
if err != nil {
return
Expand Down Expand Up @@ -136,10 +122,9 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc

options := powervs.ServiceOptions{
IBMPIOptions: &ibmpisession.IBMPIOptions{
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
UserAccount: account,
Region: region,
Zone: *res.RegionID,
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
Region: region,
Zone: *res.RegionID,
},
CloudInstanceID: spec.ServiceInstanceID,
}
Expand Down
21 changes: 3 additions & 18 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
Expand Down Expand Up @@ -124,18 +122,6 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac

m := params.IBMPowerVSMachine

auth, err := authenticator.GetAuthenticator()
if err != nil {
err = errors.Wrap(err, "failed to get authenticator")
return
}

account, err := servicesutils.GetAccount(auth)
if err != nil {
err = errors.Wrap(err, "failed to get account")
return
}

rc, err := resourcecontroller.NewService(resourcecontroller.ServiceOptions{})
if err != nil {
return
Expand Down Expand Up @@ -168,10 +154,9 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac

serviceOptions := powervs.ServiceOptions{
IBMPIOptions: &ibmpisession.IBMPIOptions{
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
UserAccount: account,
Region: region,
Zone: *res.RegionID,
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
Region: region,
Zone: *res.RegionID,
},
CloudInstanceID: m.Spec.ServiceInstanceID,
}
Expand Down
35 changes: 20 additions & 15 deletions cloud/scope/powervs_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scope

import (
"context"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -223,21 +224,6 @@ func TestCreateMachinePVS(t *testing.T) {

t.Run("Should create Machine", func(t *testing.T) {
scope := setupPowerVSMachineScope(clusterName, machineName, core.StringPtr(pvsImage), core.StringPtr(pvsNetwork), true, mockpowervs)
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
capiv1beta1.ClusterLabelName: scope.Cluster.Name,
},
Name: scope.Machine.Name,
Namespace: "default",
},
Data: map[string][]byte{
"value": []byte("user data"),
},
}
createObject(g, secret, "default")
defer cleanupObject(g, secret)

mockpowervs.EXPECT().GetAllInstance().Return(pvmInstances, nil)
mockpowervs.EXPECT().GetAllImage().Return(images, nil)
mockpowervs.EXPECT().GetAllNetwork().Return(networks, nil)
Expand Down Expand Up @@ -281,6 +267,25 @@ func TestCreateMachinePVS(t *testing.T) {
g.Expect(err).To(Not(BeNil()))
})

t.Run("Failed to retrieve bootstrap data, secret value key is missing", func(t *testing.T) {
scope := setupPowerVSMachineScope(clusterName, machineName, core.StringPtr(pvsImage), core.StringPtr(pvsNetwork), true, mockpowervs)
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
capiv1beta1.ClusterLabelName: clusterName,
},
Name: machineName,
Namespace: "default",
},
Data: map[string][]byte{
"val": []byte("user data"),
}}
g.Expect(scope.Client.Update(context.Background(), secret)).To(Succeed())
mockpowervs.EXPECT().GetAllInstance().Return(pvmInstances, nil)
_, err := scope.CreateMachine()
g.Expect(err).To(Not(BeNil()))
})

t.Run("Invalid memory value", func(t *testing.T) {
scope := setupPowerVSMachineScope(clusterName, machineName, core.StringPtr(pvsImage), core.StringPtr(pvsNetwork), true, mockpowervs)
scope.IBMPowerVSMachine.Spec.Memory = "illegal"
Expand Down
6 changes: 6 additions & 0 deletions pkg/cloud/services/powervs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/IBM-Cloud/power-go-client/power/models"

"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

var _ PowerVS = &Service{}
Expand Down Expand Up @@ -128,6 +129,11 @@ func NewService(options ServiceOptions) (PowerVS, error) {
return nil, err
}
options.Authenticator = auth
account, err := utils.GetAccount(auth)
if err != nil {
return nil, err
}
options.IBMPIOptions.UserAccount = account
session, err := ibmpisession.NewIBMPISession(options.IBMPIOptions)
if err != nil {
return nil, err
Expand Down
102 changes: 102 additions & 0 deletions pkg/endpoints/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,105 @@ func TestParseFlags(t *testing.T) {
})
}
}

func TestFetchVPCEndpoint(t *testing.T) {
testCases := []struct {
name string
region string
serviceEndpoint []ServiceEndpoint
expectedOutput string
}{
{
name: "Return constructed endpoint",
region: "us-south",
serviceEndpoint: []ServiceEndpoint{},
expectedOutput: "https://us-south.iaas.cloud.ibm.com/v1",
},
{
name: "Return fetched endpoint",
region: "us-south",
serviceEndpoint: []ServiceEndpoint{
{
ID: "vpc",
URL: "https://vpchost:8080",
Region: "us-south",
},
},
expectedOutput: "https://vpchost:8080",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := FetchVPCEndpoint(tc.region, tc.serviceEndpoint)
require.Equal(t, tc.expectedOutput, out)
})
}
}

func TestFetchPVSEndpoint(t *testing.T) {
testCases := []struct {
name string
region string
serviceEndpoint []ServiceEndpoint
expectedOutput string
}{
{
name: "Return empty endpoint",
region: "us-south",
serviceEndpoint: []ServiceEndpoint{},
expectedOutput: "",
},
{
name: "Return fetched endpoint",
region: "us-south",
serviceEndpoint: []ServiceEndpoint{
{
ID: "powervs",
URL: "https://powervshost:8080",
Region: "us-south",
},
},
expectedOutput: "https://powervshost:8080",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := FetchPVSEndpoint(tc.region, tc.serviceEndpoint)
require.Equal(t, tc.expectedOutput, out)
})
}
}

func TestFetchRCEndpoint(t *testing.T) {
testCases := []struct {
name string
serviceEndpoint []ServiceEndpoint
expectedOutput string
}{
{
name: "Return empty endpoint",
serviceEndpoint: []ServiceEndpoint{},
expectedOutput: "",
},
{
name: "Return fetched endpoint",
serviceEndpoint: []ServiceEndpoint{
{
ID: "rc",
URL: "https://rchost:8080",
Region: "us-south",
},
},
expectedOutput: "https://rchost:8080",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := FetchRCEndpoint(tc.serviceEndpoint)
require.Equal(t, tc.expectedOutput, out)
})
}
}