1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
//go:build acceptance || compute || usage
// +build acceptance compute usage
package v2
import (
"strings"
"testing"
"time"
"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/usage"
"github.com/gophercloud/gophercloud/pagination"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestUsageSingleTenant(t *testing.T) {
// TODO(emilien): This test is failing for now
t.Skip("This is not passing now, will fix later")
clients.RequireLong(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
server, err := CreateServer(t, client)
th.AssertNoErr(t, err)
DeleteServer(t, client, server)
endpointParts := strings.Split(client.Endpoint, "/")
tenantID := endpointParts[4]
end := time.Now()
start := end.AddDate(0, -1, 0)
opts := usage.SingleTenantOpts{
Start: &start,
End: &end,
}
err = usage.SingleTenant(client, tenantID, opts).EachPage(func(page pagination.Page) (bool, error) {
tenantUsage, err := usage.ExtractSingleTenant(page)
th.AssertNoErr(t, err)
tools.PrintResource(t, tenantUsage)
if tenantUsage.TotalHours == 0 {
t.Fatalf("TotalHours should not be 0")
}
return true, nil
})
th.AssertNoErr(t, err)
}
func TestUsageAllTenants(t *testing.T) {
t.Skip("This is not passing in OpenLab. Works locally")
clients.RequireLong(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
server, err := CreateServer(t, client)
th.AssertNoErr(t, err)
DeleteServer(t, client, server)
end := time.Now()
start := end.AddDate(0, -1, 0)
opts := usage.AllTenantsOpts{
Detailed: true,
Start: &start,
End: &end,
}
err = usage.AllTenants(client, opts).EachPage(func(page pagination.Page) (bool, error) {
allUsage, err := usage.ExtractAllTenants(page)
th.AssertNoErr(t, err)
tools.PrintResource(t, allUsage)
if len(allUsage) == 0 {
t.Fatalf("No usage returned")
}
if allUsage[0].TotalHours == 0 {
t.Fatalf("TotalHours should not be 0")
}
return true, nil
})
th.AssertNoErr(t, err)
}
|