github.com/vmware/govmomi@v0.43.0/vapi/vcenter/consumptiondomains/consumptiondomains_test.go (about)

     1  /*
     2  Copyright (c) 2024-2024 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  
    17  package consumptiondomains_test
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/vapi/rest"
    27  	"github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/associations"
    28  	"github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/zones"
    29  	"github.com/vmware/govmomi/vim25"
    30  
    31  	_ "github.com/vmware/govmomi/vapi/simulator"
    32  	_ "github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/simulator"
    33  )
    34  
    35  func TestConsumptionDomains(t *testing.T) {
    36  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    37  		rc := rest.NewClient(vc)
    38  
    39  		err := rc.Login(ctx, simulator.DefaultLogin)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  
    44  		zm := zones.NewManager(rc)
    45  
    46  		// Create a zone
    47  		zoneId, err := zm.CreateZone(zones.CreateSpec{
    48  			Zone:        "test-zone-1",
    49  			Description: "placeholder description",
    50  		})
    51  
    52  		if err != nil {
    53  			t.Error(err)
    54  		}
    55  
    56  		// List all zones and find the one created earlier
    57  		zonesList, err := zm.ListZones()
    58  
    59  		if err != nil {
    60  			t.Error(err)
    61  		}
    62  
    63  		assert.Equal(t, 1, len(zonesList))
    64  		assert.Equal(t, "test-zone-1", zonesList[0].Zone)
    65  		assert.Equal(t, "placeholder description", zonesList[0].Info.Description)
    66  
    67  		// Query zone by ID
    68  		zone, err := zm.GetZone(zoneId)
    69  
    70  		if err != nil {
    71  			t.Error(err)
    72  		}
    73  
    74  		assert.Equal(t, "placeholder description", zone.Description)
    75  
    76  		am := associations.NewManager(rc)
    77  
    78  		// Create a cluster association
    79  		err = am.AddAssociations(zoneId, "domain-c9")
    80  
    81  		if err != nil {
    82  			t.Error(err)
    83  		}
    84  
    85  		// Query the associations for the test zone
    86  		assc, err := am.GetAssociations(zoneId)
    87  
    88  		if err != nil {
    89  			t.Error(err)
    90  		}
    91  
    92  		assert.Equal(t, 1, len(assc))
    93  		assert.Equal(t, "domain-c9", assc[0])
    94  
    95  		// Delete the cluster association
    96  		err = am.RemoveAssociations(zoneId, "domain-c9")
    97  
    98  		if err != nil {
    99  			t.Error(err)
   100  		}
   101  
   102  		// Verify that the association is removed
   103  		assc, err = am.GetAssociations(zoneId)
   104  
   105  		if err != nil {
   106  			t.Error(err)
   107  		}
   108  
   109  		assert.Equal(t, 0, len(assc))
   110  
   111  		// Delete the zone
   112  		err = zm.DeleteZone(zoneId)
   113  
   114  		if err != nil {
   115  			t.Error(err)
   116  		}
   117  
   118  		// Verify the zone is removed
   119  		zone, err = zm.GetZone(zoneId)
   120  
   121  		if err == nil {
   122  			t.Error(err)
   123  		}
   124  
   125  		zonesList, err = zm.ListZones()
   126  
   127  		if err != nil {
   128  			t.Error(err)
   129  		}
   130  
   131  		assert.Equal(t, 0, len(zonesList))
   132  	})
   133  }