|
| 1 | +//go:build provisioner |
| 2 | +// +build provisioner |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright © 2023 VMware, Inc. All Rights Reserved. |
| 6 | +SPDX-License-Identifier: MPL-2.0 |
| 7 | +*/ |
| 8 | + |
| 9 | +package provisioner |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "os" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 20 | + "github.com/pkg/errors" |
| 21 | + |
| 22 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" |
| 23 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/proxy" |
| 24 | + provisioner "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/provisioner" |
| 25 | + testhelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestAcceptanceForProvisionerResource(t *testing.T) { |
| 29 | + var provider = initTestProvider(t) |
| 30 | + |
| 31 | + provisionerResourceName := fmt.Sprintf("%s.%s", ResourceName, resourceVar) |
| 32 | + provisionerName := acctest.RandomWithPrefix("tf-prv-test") |
| 33 | + |
| 34 | + resource.Test(t, resource.TestCase{ |
| 35 | + PreCheck: testhelper.TestPreCheck(t), |
| 36 | + ProviderFactories: testhelper.GetTestProviderFactories(provider), |
| 37 | + CheckDestroy: nil, |
| 38 | + Steps: []resource.TestStep{ |
| 39 | + { |
| 40 | + Config: getTestProvisionerWithResourceConfigValue(provisionerName), |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + checkResourceAttributes(provider, provisionerResourceName, provisionerName), |
| 43 | + ), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func checkResourceAttributes(provider *schema.Provider, resourceName, prvName string) resource.TestCheckFunc { |
| 50 | + var check = []resource.TestCheckFunc{ |
| 51 | + verifyProvisionerResourceCreation(provider, resourceName, prvName), |
| 52 | + } |
| 53 | + |
| 54 | + check = append(check, metaResourceAttributeCheck(resourceName)...) |
| 55 | + |
| 56 | + return resource.ComposeTestCheckFunc(check...) |
| 57 | +} |
| 58 | + |
| 59 | +func getTestProvisionerWithResourceConfigValue(prvName string) string { |
| 60 | + return fmt.Sprintf(` |
| 61 | + resource "%s" "%s" { |
| 62 | + name = "%s" |
| 63 | + management_cluster = "%s" |
| 64 | + %s |
| 65 | + } |
| 66 | + `, ResourceName, resourceVar, prvName, eksManagementCluster, testhelper.MetaTemplate) |
| 67 | +} |
| 68 | + |
| 69 | +func verifyProvisionerResourceCreation( |
| 70 | + provider *schema.Provider, |
| 71 | + resourceName string, |
| 72 | + provisionerName string, |
| 73 | +) resource.TestCheckFunc { |
| 74 | + return func(s *terraform.State) error { |
| 75 | + if provider == nil { |
| 76 | + return fmt.Errorf("provider not initialised") |
| 77 | + } |
| 78 | + |
| 79 | + rs, ok := s.RootModule().Resources[resourceName] |
| 80 | + if !ok { |
| 81 | + return fmt.Errorf("not found resource: %s", resourceName) |
| 82 | + } |
| 83 | + |
| 84 | + if rs.Primary.ID == "" { |
| 85 | + return fmt.Errorf("ID not set, resource: %s", resourceName) |
| 86 | + } |
| 87 | + |
| 88 | + config := authctx.TanzuContext{ |
| 89 | + ServerEndpoint: os.Getenv(authctx.ServerEndpointEnvVar), |
| 90 | + Token: os.Getenv(authctx.VMWCloudAPITokenEnvVar), |
| 91 | + VMWCloudEndPoint: os.Getenv(authctx.VMWCloudEndpointEnvVar), |
| 92 | + TLSConfig: &proxy.TLSConfig{}, |
| 93 | + } |
| 94 | + |
| 95 | + err := config.Setup() |
| 96 | + if err != nil { |
| 97 | + return errors.Wrap(err, "unable to set the context") |
| 98 | + } |
| 99 | + |
| 100 | + fn := &provisioner.VmwareTanzuManageV1alpha1ManagementclusterProvisionerFullName{ |
| 101 | + ManagementClusterName: "eks", |
| 102 | + Name: provisionerName, |
| 103 | + } |
| 104 | + |
| 105 | + resp, err := config.TMCConnection.ProvisionerResourceService.ProvisionerResourceServiceGet(fn) |
| 106 | + if err != nil { |
| 107 | + return errors.Wrap(err, "provisioner resource not found") |
| 108 | + } |
| 109 | + |
| 110 | + if resp == nil { |
| 111 | + return errors.Wrapf(err, "provisioner resource is empty, resource: %s", resourceName) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func metaResourceAttributeCheck(resourceName string) []resource.TestCheckFunc { |
| 119 | + return []resource.TestCheckFunc{ |
| 120 | + resource.TestCheckResourceAttr(resourceName, "meta.#", "1"), |
| 121 | + resource.TestCheckResourceAttr(resourceName, "meta.0.description", "resource with description"), |
| 122 | + resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key1", "value1"), |
| 123 | + resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key2", "value2"), |
| 124 | + resource.TestCheckResourceAttrSet(resourceName, "meta.0.uid"), |
| 125 | + } |
| 126 | +} |
0 commit comments