Skip to content

Commit

Permalink
add VMWare Cloud Provider Disk Size Nil handle (#13156)
Browse files Browse the repository at this point in the history
* handle disk nil issue

* add providers_test.go to test vmware cloud requirements

* Add EE license header

* WithStorage() will not execute if DiskSize not defined

* adjust providers_test.go

* delete comments

* fix license and imports order

* fix license again :)
  • Loading branch information
mohamed-rafraf committed Mar 7, 2024
1 parent ccd1cb9 commit 78167a4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/ee/validation/machine/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,10 @@ func getVMwareCloudDirectorResourceRequirements(ctx context.Context, userClient
return nil, fmt.Errorf("failed to parse memory size: %w", err)
}

if err := capacity.WithStorage(int(*rawConfig.DiskSizeGB), "G"); err != nil {
return nil, fmt.Errorf("failed to parse disk size: %w", err)
if rawConfig.DiskSizeGB != nil {
if err := capacity.WithStorage(int(*rawConfig.DiskSizeGB), "G"); err != nil {
return nil, fmt.Errorf("failed to parse disk size: %w", err)
}
}

return NewResourceDetailsFromCapacity(capacity)
Expand Down
100 changes: 100 additions & 0 deletions pkg/ee/validation/machine/providers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build ee

/*
Kubermatic Enterprise Read-Only License
Version 1.0 ("KERO-1.0”)
Copyright © 2022 Kubermatic GmbH
1. You may only view, read and display for studying purposes the source
code of the software licensed under this license, and, to the extent
explicitly provided under this license, the binary code.
2. Any use of the software which exceeds the foregoing right, including,
without limitation, its execution, compilation, copying, modification
and distribution, is expressly prohibited.
3. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
END OF TERMS AND CONDITIONS
*/

package machine

import (
"context"
"encoding/json"
"testing"

vmwareclouddirectortypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/vmwareclouddirector/types"
"github.com/kubermatic/machine-controller/pkg/providerconfig/types"

"k8s.io/apimachinery/pkg/runtime"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

type MockCtrlRuntimeClient struct {
ctrlruntimeclient.Client
}

func TestGetVMwareCloudDirectorResourceRequirements(t *testing.T) {
testCases := []struct {
name string
config *types.Config
expectedErr bool
}{
{
name: "valid VMware configuration",
config: &types.Config{
CloudProvider: types.CloudProviderVMwareCloudDirector,
CloudProviderSpec: genFakeVMWareSpec(4, 2048, 20),
},
expectedErr: false,
},
{
name: "Should fail with DiskSize not defined",
config: &types.Config{
CloudProvider: types.CloudProviderVMwareCloudDirector,
CloudProviderSpec: genFakeVMWareSpec(4, 2048, 0),
},
expectedErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockClient := &MockCtrlRuntimeClient{}
_, err := getVMwareCloudDirectorResourceRequirements(context.Background(), mockClient, tc.config)
if err != nil {
if !tc.expectedErr {
t.Fatalf("unexpected error: %v", err)
}
}

if err == nil && tc.expectedErr {
t.Fatal("expected error, got none")
}
})
}
}

func genFakeVMWareSpec(cpu, ram, disk int64) runtime.RawExtension {
var diskSize *int64

if disk != 0 {
diskSize = new(int64)
*diskSize = disk
}
vmwareconfig := &vmwareclouddirectortypes.RawConfig{
CPUs: cpu,
MemoryMB: ram,
DiskSizeGB: diskSize,
}
rawBytes, _ := json.Marshal(vmwareconfig)
return runtime.RawExtension{
Raw: rawBytes,
}
}

0 comments on commit 78167a4

Please sign in to comment.