Skip to content

Commit

Permalink
Add acceptance test
Browse files Browse the repository at this point in the history
We add an acceptance test to internal/acceptance_test.  We've had to add
a new helper function getBrokerAPIClient() and move some shared code
into getHeadersAndQueryParamsAndIAMVersion() and apiClientSetMeta().
  • Loading branch information
eamonnotoole committed Sep 19, 2024
1 parent 4477391 commit 8412fd1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
29 changes: 29 additions & 0 deletions internal/acceptance_test/data_source_morpheus_details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package acceptancetest

import (
"testing"

api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf"
)

func TestAccDataSourceMorpheusDetails(t *testing.T) {
acc := &atf.Acc{
PreCheck: testAccPreCheck,
Providers: testAccProviders,
ResourceName: "hpegl_vmaas_morpheus_details",
GetAPI: func(attr map[string]string) (interface{}, error) {
cl, cfg := getBrokerAPIClient()
iClient := api_client.BrokerAPIService{
Client: cl,
Cfg: cfg,
}

return iClient.GetMorpheusDetails(getAccContext())
},
}

acc.RunDataSourceTests(t)
}
57 changes: 45 additions & 12 deletions internal/acceptance_test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ import (
)

func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
headers, queryParam, iamVersion := getHeadersAndQueryParamsAndIAMVersion()
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_API_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}
apiClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(apiClient, iamVersion)
if err != nil {
log.Printf("[WARN] Error: %s", err)
}

return apiClient, cfg
}

func getBrokerAPIClient() (*api_client.APIClient, api_client.Configuration) {
headers, queryParam, iamVersion := getHeadersAndQueryParamsAndIAMVersion()
// No need to set the default query params for broker API
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
}
brokerAPIClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(brokerAPIClient, iamVersion)
if err != nil {
log.Printf("[WARN] Error: %s", err)
}

// Return the configuration with the default query params
cfgForReturn := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}

return brokerAPIClient, cfgForReturn
}

func getHeadersAndQueryParamsAndIAMVersion() (map[string]string, map[string]string, string) {
var headers map[string]string
if utils.GetEnvBool("TF_ACC_MOCK_IAM") {
headers = make(map[string]string)
Expand All @@ -31,13 +70,12 @@ func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
} else {
queryParam[constants.SpaceKey] = os.Getenv("HPEGL_VMAAS_SPACE_NAME")
}
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_API_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}
apiClient := api_client.NewAPIClient(&cfg)
err := apiClient.SetMeta(nil, func(ctx *context.Context, meta interface{}) {

return headers, queryParam, iamVersion
}

func apiClientSetMeta(apiClient *api_client.APIClient, iamVersion string) error {
return apiClient.SetMeta(nil, func(ctx *context.Context, meta interface{}) {
d := &utils.ResourceData{
Data: map[string]interface{}{
"iam_service_url": os.Getenv("HPEGL_IAM_SERVICE_URL"),
Expand Down Expand Up @@ -68,11 +106,6 @@ func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
*ctx = context.WithValue(*ctx, api_client.ContextAccessToken, token)
}
})
if err != nil {
log.Printf("[WARN] Error: %s", err)
}

return apiClient, cfg
}

func toInt(s string) int {
Expand Down

0 comments on commit 8412fd1

Please sign in to comment.