|
| 1 | +/* |
| 2 | +Copyright (c) 2021 VMware, Inc. All Rights Reserved. |
| 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 | + http://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 | +package vslm |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/kr/pretty" |
| 24 | + "github.com/vmware/govmomi" |
| 25 | + "github.com/vmware/govmomi/cns" |
| 26 | + cnstypes "github.com/vmware/govmomi/cns/types" |
| 27 | + "github.com/vmware/govmomi/vim25/soap" |
| 28 | +) |
| 29 | + |
| 30 | +func TestClient(t *testing.T) { |
| 31 | + url := os. Getenv( "VSLM_VC_URL") // export VC_URL='https://[email protected]:[email protected]/sdk' |
| 32 | + datacenter := os.Getenv("VSLM_DATACENTER") // export DATACENTER='test-vpx-1614463083-15492-hostpool' |
| 33 | + volumePath := os.Getenv("VOLUME_PATH") // export VOLUME_PATH='https://10.186.43.166/folder/27b74660-98cd-3fe5-514f-02009d24d7ab/vm-1.vmdk?dcPath=datacenter&dsName=vsanDatastore' |
| 34 | + if url == "" || datacenter == "" || volumePath == "" { |
| 35 | + t.Skip("VC_URL or DATACENTER is not set") |
| 36 | + } |
| 37 | + |
| 38 | + u, err := soap.ParseURL(url) |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + c, err := govmomi.NewClient(ctx, u, true) |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + vslmClient, err := NewClient(ctx, c.Client) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + globalObjectManager := NewGlobalObjectManager(vslmClient) |
| 55 | + vStorageObject, err := globalObjectManager.RegisterDisk(ctx, volumePath, "volume-name1") |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + t.Logf("Successfully registered disk with path %s as FCD with storage object id %s", volumePath, vStorageObject.Config.Id.Id) |
| 60 | + |
| 61 | + containerCluster := cnstypes.CnsContainerCluster{ |
| 62 | + ClusterType: string(cnstypes.CnsClusterTypeKubernetes), |
| 63 | + ClusterId: "demo-cluster-id", |
| 64 | + |
| 65 | + ClusterFlavor: string(cnstypes.CnsClusterFlavorVanilla), |
| 66 | + ClusterDistribution: "KUBERNETES", |
| 67 | + } |
| 68 | + |
| 69 | + // Test CreateVolume API |
| 70 | + var cnsVolumeCreateSpecList []cnstypes.CnsVolumeCreateSpec |
| 71 | + cnsVolumeCreateSpec := cnstypes.CnsVolumeCreateSpec{ |
| 72 | + Name: "pvc-abc123", |
| 73 | + VolumeType: string(cnstypes.CnsVolumeTypeBlock), |
| 74 | + Metadata: cnstypes.CnsVolumeMetadata{ |
| 75 | + ContainerCluster: containerCluster, |
| 76 | + }, |
| 77 | + BackingObjectDetails: &cnstypes.CnsBlockBackingDetails{ |
| 78 | + CnsBackingObjectDetails: cnstypes.CnsBackingObjectDetails{ |
| 79 | + CapacityInMb: 5120, |
| 80 | + }, |
| 81 | + BackingDiskId: vStorageObject.Config.Id.Id, |
| 82 | + }, |
| 83 | + } |
| 84 | + c.UseServiceVersion("vsan") |
| 85 | + cnsClient, err := cns.NewClient(ctx, c.Client) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + cnsVolumeCreateSpecList = append(cnsVolumeCreateSpecList, cnsVolumeCreateSpec) |
| 90 | + t.Logf("Creating volume using the spec: %+v", pretty.Sprint(cnsVolumeCreateSpec)) |
| 91 | + createTask, err := cnsClient.CreateVolume(ctx, cnsVolumeCreateSpecList) |
| 92 | + if err != nil { |
| 93 | + t.Errorf("Failed to create volume. Error: %+v \n", err) |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + createTaskInfo, err := cns.GetTaskInfo(ctx, createTask) |
| 97 | + if err != nil { |
| 98 | + t.Errorf("Failed to create volume. Error: %+v \n", err) |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + createTaskResult, err := cns.GetTaskResult(ctx, createTaskInfo) |
| 102 | + if err != nil { |
| 103 | + t.Errorf("Failed to create volume. Error: %+v \n", err) |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + if createTaskResult == nil { |
| 107 | + t.Fatalf("Empty create task results") |
| 108 | + t.FailNow() |
| 109 | + } |
| 110 | + createVolumeOperationRes := createTaskResult.GetCnsVolumeOperationResult() |
| 111 | + if createVolumeOperationRes.Fault != nil { |
| 112 | + t.Fatalf("Failed to create volume: fault=%+v", createVolumeOperationRes.Fault) |
| 113 | + } |
| 114 | + volumeId := createVolumeOperationRes.VolumeId.Id |
| 115 | + volumeCreateResult := (createTaskResult).(*cnstypes.CnsVolumeCreateResult) |
| 116 | + t.Logf("volumeCreateResult %+v", volumeCreateResult) |
| 117 | + t.Logf("Volume created sucessfully. volumeId: %s", volumeId) |
| 118 | +} |
0 commit comments