diff --git a/acceptance/clients/clients.go b/acceptance/clients/clients.go deleted file mode 100644 index 00f7ea9..0000000 --- a/acceptance/clients/clients.go +++ /dev/null @@ -1,50 +0,0 @@ -// Package clients contains functions for creating service clients -// for utils services. -// That clients can be used in acceptance tests. -package clients - -import ( - "net/http" - "os" - - "github.com/gophercloud/gophercloud/v2/openstack" - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/utils/v2/client" - "github.com/gophercloud/utils/v2/env" - "github.com/gophercloud/utils/v2/gnocchi" - "github.com/gophercloud/utils/v2/openstack/clientconfig" -) - -// NewGnocchiV1Client returns a *ServiceClient for making calls -// to the Gnocchi v1 API. -// An error will be returned if authentication or client -// creation was not possible. -func NewGnocchiV1Client() (*gophercloud.ServiceClient, error) { - ao, err := clientconfig.AuthOptions(nil) - if err != nil { - return nil, err - } - - provider, err := openstack.NewClient(ao.IdentityEndpoint) - if err != nil { - return nil, err - } - - if os.Getenv("OS_DEBUG") != "" { - provider.HTTPClient = http.Client{ - Transport: &client.RoundTripper{ - Rt: &http.Transport{}, - Logger: &client.DefaultLogger{}, - }, - } - } - - err = openstack.Authenticate(provider, *ao) - if err != nil { - return nil, err - } - - return gnocchi.NewGnocchiV1(provider, gophercloud.EndpointOpts{ - Region: env.Getenv("OS_REGION_NAME"), - }) -} diff --git a/acceptance/gnocchi/metric/v1/archivepolicies.go b/acceptance/gnocchi/metric/v1/archivepolicies.go deleted file mode 100644 index a610d8d..0000000 --- a/acceptance/gnocchi/metric/v1/archivepolicies.go +++ /dev/null @@ -1,54 +0,0 @@ -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/archivepolicies" -) - -// CreateArchivePolicy will create a Gnocchi archive policy. An error will be returned if the -// archive policy could not be created. -func CreateArchivePolicy(t *testing.T, client *gophercloud.ServiceClient) (*archivepolicies.ArchivePolicy, error) { - policyName := tools.RandomString("TESTACCT-", 8) - createOpts := archivepolicies.CreateOpts{ - Name: policyName, - AggregationMethods: []string{ - "mean", - "sum", - }, - Definition: []archivepolicies.ArchivePolicyDefinitionOpts{ - { - Granularity: "1:00:00", - TimeSpan: "30 days, 0:00:00", - }, - { - Granularity: "24:00:00", - TimeSpan: "90 days, 0:00:00", - }, - }, - } - - t.Logf("Attempting to create a Gnocchi archive policy") - archivePolicy, err := archivepolicies.Create(client, createOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the Gnocchi archive policy.") - return archivePolicy, nil -} - -// DeleteArchivePolicy will delete a Gnocchi archive policy. -// A fatal error will occur if the delete was not successful. -func DeleteArchivePolicy(t *testing.T, client *gophercloud.ServiceClient, archivePolicyName string) { - t.Logf("Attempting to delete the Gnocchi archive policy: %s", archivePolicyName) - - err := archivepolicies.Delete(client, archivePolicyName).ExtractErr() - if err != nil { - t.Fatalf("Unable to delete the Gnocchi archive policy %s: %v", archivePolicyName, err) - } - - t.Logf("Deleted the Gnocchi archive policy: %s", archivePolicyName) -} diff --git a/acceptance/gnocchi/metric/v1/archivepolicies_test.go b/acceptance/gnocchi/metric/v1/archivepolicies_test.go deleted file mode 100644 index e64cbcc..0000000 --- a/acceptance/gnocchi/metric/v1/archivepolicies_test.go +++ /dev/null @@ -1,68 +0,0 @@ -//go:build acceptance || metric || archivepolicies -// +build acceptance metric archivepolicies - -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/acceptance/clients" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/archivepolicies" -) - -func TestArchivePoliciesCRUD(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - archivePolicy, err := CreateArchivePolicy(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi archive policy: %v", err) - } - defer DeleteArchivePolicy(t, client, archivePolicy.Name) - - tools.PrintResource(t, archivePolicy) - - updateOpts := archivepolicies.UpdateOpts{ - Definition: []archivepolicies.ArchivePolicyDefinitionOpts{ - { - Granularity: "1:00:00", - TimeSpan: "90 days, 0:00:00", - }, - { - Granularity: "24:00:00", - TimeSpan: "365 days, 0:00:00", - }, - }, - } - t.Logf("Attempting to update an archive policy %s", archivePolicy.Name) - newArchivePolicy, err := archivepolicies.Update(client, archivePolicy.Name, updateOpts).Extract() - if err != nil { - t.Fatalf("Unable to update a Gnocchi archive policy: %v", err) - } - - tools.PrintResource(t, newArchivePolicy) -} - -func TestArchivePoliciesList(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - allPages, err := archivepolicies.List(client).AllPages() - if err != nil { - t.Fatalf("Unable to list archive policies: %v", err) - } - - allArchivePolicies, err := archivepolicies.ExtractArchivePolicies(allPages) - if err != nil { - t.Fatalf("Unable to extract archive policies: %v", err) - } - - for _, archivePolicy := range allArchivePolicies { - tools.PrintResource(t, archivePolicy) - } -} diff --git a/acceptance/gnocchi/metric/v1/measures.go b/acceptance/gnocchi/metric/v1/measures.go deleted file mode 100644 index 983ea19..0000000 --- a/acceptance/gnocchi/metric/v1/measures.go +++ /dev/null @@ -1,132 +0,0 @@ -package v1 - -import ( - "testing" - "time" - - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/measures" -) - -// CreateMeasures will create measures inside a single Gnocchi metric. An error will be returned if the -// measures could not be created. -func CreateMeasures(t *testing.T, client *gophercloud.ServiceClient, metricID string) error { - currentTimestamp := time.Now().UTC() - pastHourTimestamp := currentTimestamp.Add(-1 * time.Hour) - currentValue := float64(tools.RandomInt(100, 200)) - pastHourValue := float64(tools.RandomInt(500, 600)) - measuresToCreate := []measures.MeasureOpts{ - { - Timestamp: ¤tTimestamp, - Value: currentValue, - }, - { - Timestamp: &pastHourTimestamp, - Value: pastHourValue, - }, - } - createOpts := measures.CreateOpts{ - Measures: measuresToCreate, - } - - t.Logf("Attempting to create measures inside a Gnocchi metric %s", metricID) - if err := measures.Create(client, metricID, createOpts).ExtractErr(); err != nil { - return err - } - - t.Logf("Successfully created measures inside the Gnocchi metric %s", metricID) - return nil -} - -// MeasuresBatchCreateMetrics will create measures inside different metrics via batch request. -// An error will be returned if measures could not be created. -func MeasuresBatchCreateMetrics(t *testing.T, client *gophercloud.ServiceClient, metricIDs ...string) error { - currentTimestamp := time.Now().UTC() - pastHourTimestamp := currentTimestamp.Add(-1 * time.Hour) - currentValue := float64(tools.RandomInt(100, 200)) - pastHourValue := float64(tools.RandomInt(500, 600)) - createOpts := make([]measures.MetricOpts, len(metricIDs)) - - // Populate batch options with provided metric IDs and generated values. - for i, m := range metricIDs { - createOpts[i] = measures.MetricOpts{ - ID: m, - Measures: []measures.MeasureOpts{ - { - Timestamp: ¤tTimestamp, - Value: currentValue, - }, - { - Timestamp: &pastHourTimestamp, - Value: pastHourValue, - }, - }, - } - } - - t.Logf("Attempting to create measures inside Gnocchi metrics via batch request") - if err := measures.BatchCreateMetrics(client, createOpts).ExtractErr(); err != nil { - return err - } - - t.Logf("Successfully created measures inside Gnocchi metrics") - return nil -} - -// MeasuresBatchCreateResourcesMetrics will create measures inside different metrics via batch request to resource IDs. -// The batchResourcesMetrics arguments is a mapping between resource IDs and corresponding metric names. -// An error will be returned if measures could not be created. -func MeasuresBatchCreateResourcesMetrics(t *testing.T, client *gophercloud.ServiceClient, batchResourcesMetrics map[string][]string) error { - currentTimestamp := time.Now().UTC() - pastHourTimestamp := currentTimestamp.Add(-1 * time.Hour) - currentValue := float64(tools.RandomInt(100, 200)) - pastHourValue := float64(tools.RandomInt(500, 600)) - - // measureSet is a set of measures for an every metric. - measureSet := []measures.MeasureOpts{ - { - Timestamp: ¤tTimestamp, - Value: currentValue, - }, - { - Timestamp: &pastHourTimestamp, - Value: pastHourValue, - }, - } - - // batchResourcesMetricsOpts is an internal slice representation of measures.BatchResourcesMetricsOpts stucts. - batchResourcesMetricsOpts := make([]measures.BatchResourcesMetricsOpts, 0) - - for resourceID, metricNames := range batchResourcesMetrics { - // resourcesMetricsOpts is an internal slice representation of measures.ResourcesMetricsOpts structs. - resourcesMetricsOpts := make([]measures.ResourcesMetricsOpts, 0) - - // Populate batch options for each metric of a resource. - for _, metricName := range metricNames { - resourcesMetricsOpts = append(resourcesMetricsOpts, measures.ResourcesMetricsOpts{ - MetricName: metricName, - Measures: measureSet, - }) - } - - // Save batch options of a resource. - batchResourcesMetricsOpts = append(batchResourcesMetricsOpts, measures.BatchResourcesMetricsOpts{ - ResourceID: resourceID, - ResourcesMetrics: resourcesMetricsOpts, - }) - } - - createOpts := measures.BatchCreateResourcesMetricsOpts{ - CreateMetrics: true, - BatchResourcesMetrics: batchResourcesMetricsOpts, - } - - t.Logf("Attempting to create measures inside Gnocchi metrics via batch request with resource IDs") - if err := measures.BatchCreateResourcesMetrics(client, createOpts).ExtractErr(); err != nil { - return err - } - - t.Logf("Successfully created measures inside Gnocchi metrics") - return nil -} diff --git a/acceptance/gnocchi/metric/v1/measures_test.go b/acceptance/gnocchi/metric/v1/measures_test.go deleted file mode 100644 index c2ff906..0000000 --- a/acceptance/gnocchi/metric/v1/measures_test.go +++ /dev/null @@ -1,120 +0,0 @@ -//go:build acceptance || metric || measures -// +build acceptance metric measures - -package v1 - -import ( - "testing" - - "github.com/gophercloud/utils/v2/acceptance/clients" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/measures" -) - -func TestMeasuresCRUD(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - // Create a single metric to test Create measures request. - metric, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metric.ID) - - // Test Create measures request. - if err := CreateMeasures(t, client, metric.ID); err != nil { - t.Fatalf("Unable to create measures inside the Gnocchi metric: %v", err) - } - - // Check created measures. - listOpts := measures.ListOpts{ - Refresh: true, - } - allPages, err := measures.List(client, metric.ID, listOpts).AllPages() - if err != nil { - t.Fatalf("Unable to list measures of the metric %s: %v", metric.ID, err) - } - - metricMeasures, err := measures.ExtractMeasures(allPages) - if err != nil { - t.Fatalf("Unable to extract measures: %v", metricMeasures) - } - - t.Log(metricMeasures) -} - -func TestMeasuresBatchCreateMetrics(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - // Create a couple of metrics to test BatchCreateMetrics requets. - metricToBatchOne, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metricToBatchOne.ID) - - metricToBatchTwo, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metricToBatchTwo.ID) - - // Test create batch request based on metrics IDs. - if err := MeasuresBatchCreateMetrics(t, client, metricToBatchOne.ID, metricToBatchTwo.ID); err != nil { - t.Fatalf("Unable to create measures inside Gnocchi metrics: %v", err) - } - - // Check measures of each metric after the BatchMetrics request. - listOpts := measures.ListOpts{ - Refresh: true, - } - allPagesMetricOne, err := measures.List(client, metricToBatchOne.ID, listOpts).AllPages() - if err != nil { - t.Fatalf("Unable to list measures of the metric %s: %v", metricToBatchOne.ID, err) - } - metricOneMeasures, err := measures.ExtractMeasures(allPagesMetricOne) - if err != nil { - t.Fatalf("Unable to extract measures: %v", metricOneMeasures) - } - - t.Logf("Measures for the metric: %s, %v", metricToBatchOne.ID, metricOneMeasures) - - allPagesMetricTwo, err := measures.List(client, metricToBatchTwo.ID, listOpts).AllPages() - if err != nil { - t.Fatalf("Unable to list measures of the metric %s: %v", metricToBatchTwo.ID, err) - } - metricTwoMeasures, err := measures.ExtractMeasures(allPagesMetricTwo) - if err != nil { - t.Fatalf("Unable to extract measures: %v", metricTwoMeasures) - } - - t.Logf("Measures for the metric: %s, %v", metricToBatchTwo.ID, metricTwoMeasures) -} - -func TestMeasuresBatchCreateResourcesMetrics(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - // Create a couple of resources with metrics to test BatchCreateResourcesMetrics requets. - batchResourcesMetrics, err := CreateResourcesToBatchMeasures(t, client) - if err != nil { - t.Fatalf("Unable to create Gnocchi resources and metrics: %v", err) - } - - // Test create batch request based on resource IDs. - if err := MeasuresBatchCreateResourcesMetrics(t, client, batchResourcesMetrics); err != nil { - t.Fatalf("Unable to create measures inside Gnocchi metrics: %v", err) - } - - // Delete resources. - for resourceID := range batchResourcesMetrics { - DeleteResource(t, client, "generic", resourceID) - } -} diff --git a/acceptance/gnocchi/metric/v1/metrics.go b/acceptance/gnocchi/metric/v1/metrics.go deleted file mode 100644 index bfe8fc6..0000000 --- a/acceptance/gnocchi/metric/v1/metrics.go +++ /dev/null @@ -1,44 +0,0 @@ -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/metrics" -) - -// CreateMetric will create Gnocchi metric. An error will be returned if the -// metric could not be created. -func CreateMetric(t *testing.T, client *gophercloud.ServiceClient) (*metrics.Metric, error) { - // Metric will be created assuming that your Gnocchi's indexer installation was configured with - // the "gnocchi-manage --noskip-archive-policies-creation" command. So Gnocchi has the default policies: - // "low", "medium", "high", "bool". - name := tools.RandomString("TESTACCT-", 8) - createOpts := metrics.CreateOpts{ - ArchivePolicyName: "low", - Name: name, - } - t.Logf("Attempting to create a Gnocchi metric") - - metric, err := metrics.Create(client, createOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the Gnocchi metric.") - return metric, nil -} - -// DeleteMetric will delete a Gnocchi metric with a specified ID. -// A fatal error will occur if the delete was not successful. -func DeleteMetric(t *testing.T, client *gophercloud.ServiceClient, metricID string) { - t.Logf("Attempting to delete the Gnocchi metric: %s", metricID) - - err := metrics.Delete(client, metricID).ExtractErr() - if err != nil { - t.Fatalf("Unable to delete the Gnocchi metric %s: %v", metricID, err) - } - - t.Logf("Deleted the Gnocchi metric: %s", metricID) -} diff --git a/acceptance/gnocchi/metric/v1/metrics_test.go b/acceptance/gnocchi/metric/v1/metrics_test.go deleted file mode 100644 index f20184b..0000000 --- a/acceptance/gnocchi/metric/v1/metrics_test.go +++ /dev/null @@ -1,67 +0,0 @@ -//go:build acceptance || metric || metrics -// +build acceptance metric metrics - -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/acceptance/clients" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/metrics" -) - -func TestMetricsCRUD(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - metric, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metric.ID) - - tools.PrintResource(t, metric) -} - -func TestMetricsList(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - metric1, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metric1.ID) - - metric2, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metric2.ID) - - metric3, err := CreateMetric(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi metric: %v", err) - } - defer DeleteMetric(t, client, metric3.ID) - - listOpts := metrics.ListOpts{} - allPages, err := metrics.List(client, listOpts).AllPages() - if err != nil { - t.Fatalf("Unable to list metrics: %v", err) - } - - allMetrics, err := metrics.ExtractMetrics(allPages) - if err != nil { - t.Fatalf("Unable to extract metrics: %v", err) - } - - for _, metric := range allMetrics { - tools.PrintResource(t, metric) - } -} diff --git a/acceptance/gnocchi/metric/v1/resources.go b/acceptance/gnocchi/metric/v1/resources.go deleted file mode 100644 index 85192ed..0000000 --- a/acceptance/gnocchi/metric/v1/resources.go +++ /dev/null @@ -1,134 +0,0 @@ -package v1 - -import ( - "testing" - "time" - - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/resources" - "github.com/hashicorp/go-uuid" -) - -// CreateGenericResource will create a Gnocchi resource with a generic type. -// An error will be returned if the resource could not be created. -func CreateGenericResource(t *testing.T, client *gophercloud.ServiceClient) (*resources.Resource, error) { - id, err := uuid.GenerateUUID() - if err != nil { - return nil, err - } - - randomDay := tools.RandomInt(1, 100) - now := time.Now().UTC().AddDate(0, 0, -randomDay) - metricName := tools.RandomString("TESTACCT-", 8) - createOpts := resources.CreateOpts{ - ID: id, - StartedAt: &now, - Metrics: map[string]interface{}{ - metricName: map[string]string{ - "archive_policy_name": "medium", - }, - }, - } - resourceType := "generic" - t.Logf("Attempting to create a generic Gnocchi resource") - - resource, err := resources.Create(client, resourceType, createOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the generic Gnocchi resource.") - return resource, nil -} - -// DeleteResource will delete a Gnocchi resource with specified type and ID. -// A fatal error will occur if the delete was not successful. -func DeleteResource(t *testing.T, client *gophercloud.ServiceClient, resourceType, resourceID string) { - t.Logf("Attempting to delete the Gnocchi resource: %s", resourceID) - - err := resources.Delete(client, resourceType, resourceID).ExtractErr() - if err != nil { - t.Fatalf("Unable to delete the Gnocchi resource %s: %v", resourceID, err) - } - - t.Logf("Deleted the Gnocchi resource: %s", resourceID) -} - -// CreateResourcesToBatchMeasures will create Gnocchi resources with metrics to test batch measures requests and -// return a map with references of resource IDs and metric names. -// An error will be returned if resources or metrics could not be created. -func CreateResourcesToBatchMeasures(t *testing.T, client *gophercloud.ServiceClient) (map[string][]string, error) { - // Prepare metric names. - firstMetricName := tools.RandomString("TESTACCT-", 8) - secondMetricName := tools.RandomString("TESTACCT-", 8) - thirdMetricName := tools.RandomString("TESTACCT-", 8) - - // Prepare the first resource. - firstResourceID, err := uuid.GenerateUUID() - if err != nil { - return nil, err - } - firstRandomDay := tools.RandomInt(1, 100) - firstStartTimestamp := time.Now().UTC().AddDate(0, 0, -firstRandomDay) - firstResourceCreateOpts := resources.CreateOpts{ - ID: firstResourceID, - StartedAt: &firstStartTimestamp, - Metrics: map[string]interface{}{ - firstMetricName: map[string]string{ - "archive_policy_name": "medium", - }, - secondMetricName: map[string]string{ - "archive_policy_name": "low", - }, - }, - } - firstResourceType := "generic" - - t.Logf("Attempting to create a generic Gnocchi resource") - firstResource, err := resources.Create(client, firstResourceType, firstResourceCreateOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the generic Gnocchi resource.") - tools.PrintResource(t, firstResource) - - // Prepare the second resource. - secondResourceID, err := uuid.GenerateUUID() - if err != nil { - return nil, err - } - secondRandomDay := tools.RandomInt(1, 100) - secondStartTimestamp := time.Now().UTC().AddDate(0, 0, -secondRandomDay) - secondResourceCreateOpts := resources.CreateOpts{ - ID: secondResourceID, - StartedAt: &secondStartTimestamp, - Metrics: map[string]interface{}{ - thirdMetricName: map[string]string{ - "archive_policy_name": "low", - }, - }, - } - secondResourceType := "generic" - - t.Logf("Attempting to create a generic Gnocchi resource") - secondResource, err := resources.Create(client, secondResourceType, secondResourceCreateOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the generic Gnocchi resource.") - tools.PrintResource(t, secondResource) - - resourcesReferenceMap := map[string][]string{ - firstResource.ID: { - firstMetricName, - secondMetricName, - }, - secondResource.ID: { - thirdMetricName, - }, - } - return resourcesReferenceMap, nil -} diff --git a/acceptance/gnocchi/metric/v1/resources_test.go b/acceptance/gnocchi/metric/v1/resources_test.go deleted file mode 100644 index 5cd85f5..0000000 --- a/acceptance/gnocchi/metric/v1/resources_test.go +++ /dev/null @@ -1,65 +0,0 @@ -//go:build acceptance || metric || resources -// +build acceptance metric resources - -package v1 - -import ( - "testing" - "time" - - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/acceptance/clients" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/resources" -) - -func TestResourcesCRUD(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - genericResource, err := CreateGenericResource(t, client) - if err != nil { - t.Fatalf("Unable to create a generic Gnocchi resource: %v", err) - } - defer DeleteResource(t, client, genericResource.Type, genericResource.ID) - - tools.PrintResource(t, genericResource) - - newStartedAt := time.Date(2018, 1, 1, 1, 1, 0, 0, time.UTC) - newMetrics := map[string]interface{}{} - updateOpts := &resources.UpdateOpts{ - StartedAt: &newStartedAt, - Metrics: &newMetrics, - } - t.Logf("Attempting to update a resource %s", genericResource.ID) - newGenericResource, err := resources.Update(client, genericResource.Type, genericResource.ID, updateOpts).Extract() - if err != nil { - t.Fatalf("Unable to update the generic Gnocchi resource: %v", err) - } - - tools.PrintResource(t, newGenericResource) -} - -func TestResourcesList(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - opts := resources.ListOpts{} - resourceType := "generic" - allPages, err := resources.List(client, opts, resourceType).AllPages() - if err != nil { - t.Fatalf("Unable to list resources: %v", err) - } - - allResources, err := resources.ExtractResources(allPages) - if err != nil { - t.Fatalf("Unable to extract resources: %v", err) - } - - for _, resource := range allResources { - tools.PrintResource(t, resource) - } -} diff --git a/acceptance/gnocchi/metric/v1/resourcetypes.go b/acceptance/gnocchi/metric/v1/resourcetypes.go deleted file mode 100644 index 9ff0d68..0000000 --- a/acceptance/gnocchi/metric/v1/resourcetypes.go +++ /dev/null @@ -1,58 +0,0 @@ -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/resourcetypes" -) - -// CreateResourceType creates Gnocchi resource type. An error will be returned if the -// resource type could not be created. -func CreateResourceType(t *testing.T, client *gophercloud.ServiceClient) (*resourcetypes.ResourceType, error) { - resourceTypeName := tools.RandomString("TESTACCT-", 8) - attributeStringName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) - attributeUUIDName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) - - createOpts := resourcetypes.CreateOpts{ - Name: resourceTypeName, - Attributes: map[string]resourcetypes.AttributeOpts{ - attributeStringName: { - Type: "string", - Details: map[string]interface{}{ - "max_length": 128, - "required": false, - }, - }, - attributeUUIDName: { - Type: "uuid", - Details: map[string]interface{}{ - "required": true, - }, - }, - }, - } - t.Logf("Attempting to create a Gnocchi resource type") - - resourceType, err := resourcetypes.Create(client, createOpts).Extract() - if err != nil { - return nil, err - } - - t.Logf("Successfully created the Gnocchi resource type.") - return resourceType, nil -} - -// DeleteResourceType deletes a Gnocchi resource type with the specified name. -// A fatal error will occur if the delete was not successful. -func DeleteResourceType(t *testing.T, client *gophercloud.ServiceClient, resourceTypeName string) { - t.Logf("Attempting to delete the Gnocchi resource type: %s", resourceTypeName) - - err := resourcetypes.Delete(client, resourceTypeName).ExtractErr() - if err != nil { - t.Fatalf("Unable to delete the Gnocchi resource type %s: %v", resourceTypeName, err) - } - - t.Logf("Deleted the Gnocchi resource type: %s", resourceTypeName) -} diff --git a/acceptance/gnocchi/metric/v1/resourcetypes_test.go b/acceptance/gnocchi/metric/v1/resourcetypes_test.go deleted file mode 100644 index 10f2d85..0000000 --- a/acceptance/gnocchi/metric/v1/resourcetypes_test.go +++ /dev/null @@ -1,110 +0,0 @@ -//go:build acceptance || metric || resourcetypes -// +build acceptance metric resourcetypes - -package v1 - -import ( - "testing" - - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - "github.com/gophercloud/utils/v2/acceptance/clients" - "github.com/gophercloud/utils/v2/gnocchi/metric/v1/resourcetypes" -) - -func TestResourceTypesList(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - allPages, err := resourcetypes.List(client).AllPages() - if err != nil { - t.Fatalf("Unable to list resource types: %v", err) - } - - allResourceTypes, err := resourcetypes.ExtractResourceTypes(allPages) - if err != nil { - t.Fatalf("Unable to extract resource types: %v", err) - } - - for _, resourceType := range allResourceTypes { - tools.PrintResource(t, resourceType) - } -} - -func TestResourceTypesCRUD(t *testing.T) { - client, err := clients.NewGnocchiV1Client() - if err != nil { - t.Fatalf("Unable to create a Gnocchi client: %v", err) - } - - resourceType, err := CreateResourceType(t, client) - if err != nil { - t.Fatalf("Unable to create a Gnocchi resource type: %v", err) - } - defer DeleteResourceType(t, client, resourceType.Name) - - tools.PrintResource(t, resourceType) - - // Populate attributes that will be deleted. - attributesToDelete := []string{} - for attributeName := range resourceType.Attributes { - attributesToDelete = append(attributesToDelete, attributeName) - } - - // New string attribute parameters. - newStringAttributeOpts := resourcetypes.AttributeOpts{ - Details: map[string]interface{}{ - "required": false, - "min_length": 32, - "max_length": 64, - }, - Type: "string", - } - newStringAttributeOptsName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) - - // New datetime attribute parameters. - newDatetimeAttributeOpts := resourcetypes.AttributeOpts{ - Details: map[string]interface{}{ - "required": true, - "options": map[string]interface{}{ - "fill": "2018-07-28T00:01:01Z", - }, - }, - Type: "datetime", - } - newDatetimeAttributeOptsName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) - - // Initial options for the Update request with the new resource type attributes. - updateOpts := resourcetypes.UpdateOpts{ - Attributes: []resourcetypes.AttributeUpdateOpts{ - { - Name: newStringAttributeOptsName, - Operation: resourcetypes.AttributeAdd, - Value: &newStringAttributeOpts, - }, - { - Name: newDatetimeAttributeOptsName, - Operation: resourcetypes.AttributeAdd, - Value: &newDatetimeAttributeOpts, - }, - }, - } - - // Add options to delete attributes. - for _, attributeToDelete := range attributesToDelete { - updateOpts.Attributes = append(updateOpts.Attributes, resourcetypes.AttributeUpdateOpts{ - Name: attributeToDelete, - Operation: resourcetypes.AttributeRemove, - }) - } - - t.Logf("Attempting to update a Gnocchi resource type \"%s\".", resourceType.Name) - newResourceType, err := resourcetypes.Update(client, resourceType.Name, updateOpts).Extract() - if err != nil { - t.Fatalf("Unable to update a Gnocchi resource type: %v", err) - } - t.Logf("Successfully updated the Gnocchi resource type \"%s\".", resourceType.Name) - - tools.PrintResource(t, newResourceType) -} diff --git a/acceptance/openstack/clientconfig/clientconfig_test.go b/acceptance/openstack/clientconfig/clientconfig_test.go deleted file mode 100644 index eb6b3c4..0000000 --- a/acceptance/openstack/clientconfig/clientconfig_test.go +++ /dev/null @@ -1,94 +0,0 @@ -//go:build acceptance || clientconfig -// +build acceptance clientconfig - -package clientconfig - -import ( - "net/http" - "os" - "strings" - "testing" - - "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" - - acc_compute "github.com/gophercloud/gophercloud/acceptance/openstack/compute/v2" - acc_tools "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - - osClient "github.com/gophercloud/utils/v2/client" - cc "github.com/gophercloud/utils/v2/openstack/clientconfig" -) - -func TestServerCreateDestroy(t *testing.T) { - // This will be populated by environment variables. - clientOpts := &cc.ClientOpts{} - - client, err := cc.NewServiceClient("compute", clientOpts) - if err != nil { - t.Fatalf("Unable to create client: %v", err) - } - - server, err := acc_compute.CreateServer(t, client) - if err != nil { - t.Fatalf("Unable to create server: %v", err) - } - defer acc_compute.DeleteServer(t, client, server) - - newServer, err := servers.Get(client, server.ID).Extract() - if err != nil { - t.Fatalf("Unable to get server %s: %v", server.ID, err) - } - - acc_tools.PrintResource(t, newServer) -} - -func TestEndpointType(t *testing.T) { - clientOpts := &cc.ClientOpts{ - EndpointType: "admin", - } - client, err := cc.NewServiceClient("identity", clientOpts) - if err != nil { - t.Fatalf("Unable to create client: %v", err) - } - - if !strings.Contains(client.Endpoint, "35357") { - t.Fatalf("Endpoint was not correctly set to admin interface") - } -} - -func TestCustomHTTPClient(t *testing.T) { - var logger osClient.Logger - - if os.Getenv("OS_DEBUG") != "" { - logger = &osClient.DefaultLogger{} - } - - httpClient := http.Client{ - Transport: &osClient.RoundTripper{ - Rt: &http.Transport{}, - Logger: logger, - }, - } - - clientOpts := &cc.ClientOpts{ - HTTPClient: &httpClient, - } - - client, err := cc.NewServiceClient("compute", clientOpts) - if err != nil { - t.Fatalf("Unable to create client: %v", err) - } - - allPages, err := servers.List(client, nil).AllPages() - if err != nil { - t.Fatalf("Unable to list servers: %v", err) - } - - allServers, err := servers.ExtractServers(allPages) - if err != nil { - t.Fatalf("Unable to extract servers: %v", err) - } - - for _, v := range allServers { - t.Logf("%#v", v) - } -} diff --git a/acceptance/openstack/objectstorage/v1/objects_test.go b/acceptance/openstack/objectstorage/v1/objects_test.go deleted file mode 100644 index 3428d81..0000000 --- a/acceptance/openstack/objectstorage/v1/objects_test.go +++ /dev/null @@ -1,612 +0,0 @@ -//go:build acceptance -// +build acceptance - -package v1 - -import ( - "bytes" - "io/ioutil" - "net/http" - "os" - "path" - "testing" - - th "github.com/gophercloud/gophercloud/testhelper" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" - - "github.com/gophercloud/utils/v2/openstack/clientconfig" - "github.com/gophercloud/utils/v2/openstack/objectstorage/v1/objects" -) - -func TestObjectStreamingUploadDownload(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - // Generate a random object name and random content. - oName := tools.RandomString("test-object-", 8) - content := tools.RandomString("", 10) - contentBuf := bytes.NewBuffer([]byte(content)) - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Content: contentBuf, - } - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: "-", - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - downloadedContent, err := ioutil.ReadAll(downloadResults[0].Content) - th.AssertNoErr(t, err) - th.AssertEquals(t, string(downloadedContent), content) - - tools.PrintResource(t, downloadResults[0]) -} - -func TestObjectFileUploadDownload(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a file with random content - source, err := CreateRandomFile(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempFile(t, source) - - // Create a destination file. - dest := tools.RandomString("/tmp/test-dest-", 8) - - // Create a random object name. - oName := tools.RandomString("test-object-", 8) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Path: source, - } - - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: dest, - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - defer DeleteTempFile(t, dest) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err := CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) -} - -func TestObjectStreamingSLO(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - defer DeleteContainer(t, client, cName+"_segments") - - // Generate a random object name and random content. - oName := tools.RandomString("test-object-", 8) - content := tools.RandomString("", 256) - contentBuf := bytes.NewBuffer([]byte(content)) - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Checksum: true, - Content: contentBuf, - SegmentSize: 62, - UseSLO: true, - } - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: "-", - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - // Compare the downloaded content with the uploaded. - downloadedContent, err := ioutil.ReadAll(downloadResults[0].Content) - th.AssertNoErr(t, err) - th.AssertEquals(t, string(downloadedContent), content) - - // Replace the object with the same object. - contentBuf = bytes.NewBuffer([]byte(content)) - uploadOpts.Content = contentBuf - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadResults, err = objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - // Compare the downloaded content with the uploaded. - downloadedContent, err = ioutil.ReadAll(downloadResults[0].Content) - th.AssertNoErr(t, err) - th.AssertEquals(t, string(downloadedContent), content) -} - -func TestObjectFileSLO(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a file with random content - source, err := CreateRandomFile(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempFile(t, source) - - // Create a destination file. - dest := tools.RandomString("/tmp/test-dest-", 8) - defer DeleteTempFile(t, dest) - - // Create a random object name. - oName := tools.RandomString("test-object-", 8) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - defer DeleteContainer(t, client, cName+"_segments") - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Path: source, - SegmentSize: 62, - UseSLO: true, - } - - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: dest, - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err := CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) - - tools.PrintResource(t, downloadResults[0]) - - // Replace the object with the same object. - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadResults, err = objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err = CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) - - tools.PrintResource(t, downloadResults[0]) - - // Replace the object with the same object. - // But skip identical segments - uploadOpts.SkipIdentical = true - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - th.AssertEquals(t, uploadResult.Status, "skip-identical") - - tools.PrintResource(t, uploadResult) - - // Replace the object with the same object. - // But only if changed. - uploadOpts.SkipIdentical = false - uploadOpts.Changed = true - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - th.AssertEquals(t, uploadResult.Status, "skip-changed") - - tools.PrintResource(t, uploadResult) -} - -func TestObjectFileDLO(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a file with random content - source, err := CreateRandomFile(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempFile(t, source) - - // Create a destination file. - dest := tools.RandomString("/tmp/test-dest-", 8) - defer DeleteTempFile(t, dest) - - // Create a random object name. - oName := tools.RandomString("test-object-", 8) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - defer DeleteContainer(t, client, cName+"_segments") - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Checksum: true, - Path: source, - SegmentSize: 62, - } - - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: dest, - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err := CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) - - // Replace the object with the same object. - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Download the object - downloadResults, err = objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err = CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) - - tools.PrintResource(t, downloadResults[0]) - - // Replace the object with the same object. - // But skip identical segments - uploadOpts.SkipIdentical = true - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - th.AssertEquals(t, uploadResult.Status, "skip-identical") - - tools.PrintResource(t, uploadResult) - - // Replace the object with the same object. - // But only if changed. - uploadOpts.SkipIdentical = false - uploadOpts.Changed = true - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - th.AssertEquals(t, uploadResult.Status, "skip-changed") - - tools.PrintResource(t, uploadResult) -} - -func TestObjectPseudoDirBasic(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - // Generate a random object name and random content. - oName := tools.RandomString("test-object-", 8) - - // Create the directory marker - uploadOpts := &objects.UploadOpts{ - DirMarker: true, - } - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Get the object - obj, err := GetObject(client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, obj.ContentType, "application/directory") -} - -func TestObjectPseudoDirFileStructure(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - // Create a temporary directory to hold files. - parentDir, err := CreateTempDir(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempDir(t, parentDir) - - oName := path.Base(parentDir) - - // Upload the directory - uploadOpts := &objects.UploadOpts{ - Path: parentDir, - } - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Create a file with random content - source, err := CreateRandomFile(t, parentDir) - th.AssertNoErr(t, err) - defer DeleteTempFile(t, source) - - oName = path.Join(oName, path.Base(source)) - - // Upload the file. - uploadOpts.Path = source - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Create a nested directory to hold files. - nestedDir, err := CreateTempDir(t, parentDir) - th.AssertNoErr(t, err) - defer DeleteTempDir(t, nestedDir) - - oName = path.Join(path.Base(parentDir), path.Base(nestedDir)) - - // Upload the nested directory - uploadOpts.Path = nestedDir - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Create a file in the nested directory with random content - nestedSource, err := CreateRandomFile(t, nestedDir) - th.AssertNoErr(t, err) - defer DeleteTempFile(t, nestedSource) - - oName = path.Join(oName, path.Base(nestedSource)) - - // Upload the file. - uploadOpts.Path = nestedSource - uploadResult, err = objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - // Create a temporary directory to download files. - downloadDir, err := CreateTempDir(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempDir(t, downloadDir) - - // Download the container to downloadDir - downloadOpts := &objects.DownloadOpts{ - OutDirectory: downloadDir, - } - downloadResults, err := objects.Download(client, cName, []string{}, downloadOpts) - th.AssertNoErr(t, err) - - // Compare the downloaded content - for _, dr := range downloadResults { - pseudoDir := dr.PseudoDir - stat, err := os.Stat(dr.Path) - th.AssertNoErr(t, err) - th.AssertEquals(t, stat.IsDir(), pseudoDir) - - if !pseudoDir { - v := path.Join("/tmp", dr.Object) - equals, err := CompareFiles(t, v, dr.Path) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) - } - } - - tools.PrintResource(t, downloadResults) -} - -func TestObjectFileHandleUploadDownload(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a file with random content - source, err := CreateRandomFile(t, "/tmp") - th.AssertNoErr(t, err) - defer DeleteTempFile(t, source) - - // Create a destination file. - dest := tools.RandomString("/tmp/test-dest-", 8) - - // Create a random object name. - oName := tools.RandomString("test-object-", 8) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - f, err := os.Open(source) - th.AssertNoErr(t, err) - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Checksum: true, - Content: f, - } - - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - err = f.Close() - th.AssertNoErr(t, err) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: dest, - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - defer DeleteTempFile(t, dest) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) - - equals, err := CompareFiles(t, source, dest) - th.AssertNoErr(t, err) - th.AssertEquals(t, equals, true) -} - -func TestObjectStreamReaderUploadDownload(t *testing.T) { - client, err := clientconfig.NewServiceClient("object-store", nil) - th.AssertNoErr(t, err) - - // Create a stream with content - r, err := http.Get("https://google.com") - th.AssertNoErr(t, err) - - // Create a destination file. - dest := tools.RandomString("/tmp/test-dest-", 8) - - // Create a random object name. - oName := tools.RandomString("test-object-", 8) - - // Create a test container to store the object. - cName, err := CreateContainer(t, client) - th.AssertNoErr(t, err) - defer DeleteContainer(t, client, cName) - - // Upload the object - uploadOpts := &objects.UploadOpts{ - Checksum: true, - Content: r.Body, - } - - uploadResult, err := objects.Upload(client, cName, oName, uploadOpts) - defer DeleteObject(t, client, cName, oName) - th.AssertNoErr(t, err) - th.AssertEquals(t, uploadResult.Success, true) - - tools.PrintResource(t, uploadResult) - - th.AssertNoErr(t, err) - - // Download the object - downloadOpts := &objects.DownloadOpts{ - OutFile: dest, - } - downloadResults, err := objects.Download(client, cName, []string{oName}, downloadOpts) - th.AssertNoErr(t, err) - defer DeleteTempFile(t, dest) - - th.AssertEquals(t, len(downloadResults), 1) - th.AssertEquals(t, downloadResults[0].Success, true) - - tools.PrintResource(t, downloadResults[0]) -} diff --git a/acceptance/openstack/objectstorage/v1/objectstorage.go b/acceptance/openstack/objectstorage/v1/objectstorage.go deleted file mode 100644 index d8711b8..0000000 --- a/acceptance/openstack/objectstorage/v1/objectstorage.go +++ /dev/null @@ -1,152 +0,0 @@ -package v1 - -import ( - "bytes" - "fmt" - "io/ioutil" - "os" - "testing" - - "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers" - "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects" - "github.com/gophercloud/gophercloud/v2" - "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" -) - -// CompareFiles will compare two files -func CompareFiles(t *testing.T, file1, file2 string) (bool, error) { - f1, err := os.Open(file1) - if err != nil { - return false, fmt.Errorf("unable to open %s: %s", file1, err) - } - defer f1.Close() - - f2, err := os.Open(file2) - if err != nil { - return false, fmt.Errorf("unable to open %s: %s", file2, err) - } - defer f2.Close() - - contents1, err := ioutil.ReadAll(f1) - if err != nil { - return false, fmt.Errorf("unable to read %s: %s", file1, err) - } - - contents2, err := ioutil.ReadAll(f2) - if err != nil { - return false, fmt.Errorf("unable to read %s: %s", file2, err) - } - - equal := bytes.Equal(contents1, contents2) - - return equal, nil -} - -// CreateContainer will create a container with a random name. -func CreateContainer(t *testing.T, client *gophercloud.ServiceClient) (string, error) { - cName := tools.RandomString("test-container-", 8) - res := containers.Create(client, cName, nil) - - t.Logf("creating container: %s", cName) - - return cName, res.Err -} - -// CreateRandomFile will create a file with random content. -func CreateRandomFile(t *testing.T, parentDir string) (string, error) { - tmpfile, err := CreateTempFile(t, parentDir) - if err != nil { - return "", fmt.Errorf("unable to create random file: %s", err) - } - - content := tools.RandomString("", 256) - tmpfile.Write([]byte(content)) - tmpfile.Close() - - return tmpfile.Name(), nil -} - -// CreateTempDir will create and return a temp directory. -func CreateTempDir(t *testing.T, parentDir string) (string, error) { - dirName, err := ioutil.TempDir(parentDir, "test-dir-") - if err != nil { - return "", err - } - - t.Logf("creating tempdir: %s", dirName) - return dirName, nil -} - -// CreateTempFile will create and return a temp file. -func CreateTempFile(t *testing.T, dir string) (*os.File, error) { - fileName := tools.RandomString("test-file-", 8) - t.Logf("creating tempfile: %s", fileName) - return ioutil.TempFile(dir, fileName) -} - -// DeleteContainer will delete a container. A fatal error will occur if the -// container failed to be deleted. This works best when used as a deferred -// function. -func DeleteContainer(t *testing.T, client *gophercloud.ServiceClient, cName string) { - t.Logf("deleting container %s", cName) - - allPages, err := objects.List(client, cName, nil).AllPages() - if err != nil { - t.Fatalf("unable to list container %s: %s", cName, err) - } - - allObjects, err := objects.ExtractNames(allPages) - if err != nil { - t.Fatalf("unable to extract container %s: %s", cName, err) - } - - for _, oName := range allObjects { - res := objects.Delete(client, cName, oName, nil) - if res.Err != nil { - t.Fatalf("unable to delete object: %s/%s: %s", cName, oName, oName) - } - } - - res := containers.Delete(client, cName) - if res.Err != nil { - t.Fatalf("unable to delete container %s: %s", cName, res.Err) - } -} - -// DeleteObject will delete an object. A fatal error will occur if the object -// failed to be deleted. This works best when used as a deferred function. -func DeleteObject(t *testing.T, client *gophercloud.ServiceClient, cName, oName string) { - t.Logf("deleting object %s/%s", cName, oName) - - res := objects.Delete(client, cName, oName, nil) - if res.Err != nil { - t.Fatalf("unable to delete object %s/%s: %s", cName, oName, res.Err) - } -} - -// DeleteTempFile will delete a temporary file. A fatal error will occur if the -// file could not be deleted. This works best when used as a deferred function. -func DeleteTempFile(t *testing.T, fileName string) { - t.Logf("deleting tempfile %s", fileName) - - if err := os.Remove(fileName); err != nil { - t.Fatalf("unable to delete tempfile %s: %s", fileName, err) - } -} - -// DeleteTempDir will delete a temporary directory. A fatal error will occur if -// the directory could not be deleted. This works best when used as a deferred -// function. -func DeleteTempDir(t *testing.T, dirName string) { - t.Logf("deleting tempdir %s", dirName) - - if err := os.RemoveAll(dirName); err != nil { - t.Fatalf("unable to delete tempdir %s: %s", dirName, err) - } -} - -// GetObject is an alias to objects.GetObject so we don't have to import -// gophercloud/gophercloud into objects_test.go and make things confusing. -func GetObject(client *gophercloud.ServiceClient, cName, oName string) (*objects.GetHeader, error) { - return objects.Get(client, cName, oName, nil).Extract() -}