本文整理匯總了Golang中github.com/gophercloud/gophercloud/acceptance/tools.RandomString函數的典型用法代碼示例。如果您正苦於以下問題:Golang RandomString函數的具體用法?Golang RandomString怎麽用?Golang RandomString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了RandomString函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateSnapshot
// CreateSnapshot will create a volume snapshot based off of a given volume and
// with a random name. An error will be returned if the snapshot failed to be
// created.
func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (*snapshots.Snapshot, error) {
if testing.Short() {
t.Skip("Skipping test that requires snapshot creation in short mode.")
}
snapshotName := tools.RandomString("ACPTTEST", 16)
t.Logf("Attempting to create snapshot %s based on volume %s", snapshotName, volume.ID)
createOpts := snapshots.CreateOpts{
Name: snapshotName,
VolumeID: volume.ID,
}
snapshot, err := snapshots.Create(client, createOpts).Extract()
if err != nil {
return snapshot, err
}
err = snapshots.WaitForStatus(client, snapshot.ID, "available", 60)
if err != nil {
return snapshot, err
}
return snapshot, nil
}
示例2: CreateRouter
// CreateRouter creates a router on a specified Network ID. An error will be
// returned if the creation failed.
func CreateRouter(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*routers.Router, error) {
routerName := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create router: %s", routerName)
adminStateUp := true
gatewayInfo := routers.GatewayInfo{
NetworkID: networkID,
}
createOpts := routers.CreateOpts{
Name: routerName,
AdminStateUp: &adminStateUp,
GatewayInfo: &gatewayInfo,
}
router, err := routers.Create(client, createOpts).Extract()
if err != nil {
return router, err
}
if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
return router, err
}
t.Logf("Created router: %s", routerName)
return router, nil
}
示例3: CreateMonitor
// CreateMonitor will create a monitor with a random name for a specific pool.
// An error will be returned if the monitor could not be created.
func CreateMonitor(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer, pool *pools.Pool) (*monitors.Monitor, error) {
monitorName := tools.RandomString("TESTACCT-", 8)
t.Logf("Attempting to create monitor %s", monitorName)
createOpts := monitors.CreateOpts{
PoolID: pool.ID,
Name: monitorName,
Delay: 10,
Timeout: 5,
MaxRetries: 5,
Type: "PING",
}
monitor, err := monitors.Create(client, createOpts).Extract()
if err != nil {
return monitor, err
}
t.Logf("Successfully created monitor: %s", monitorName)
if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
return monitor, fmt.Errorf("Timed out waiting for loadbalancer to become active")
}
return monitor, nil
}
示例4: CreatePortsbinding
// CreatePortsbinding will create a port on the specified subnet. An error will be
// returned if the port could not be created.
func CreatePortsbinding(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, hostID string) (*portsbinding.Port, error) {
portName := tools.RandomString("TESTACC-", 8)
iFalse := false
t.Logf("Attempting to create port: %s", portName)
createOpts := portsbinding.CreateOpts{
CreateOptsBuilder: ports.CreateOpts{
NetworkID: networkID,
Name: portName,
AdminStateUp: &iFalse,
FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
},
HostID: hostID,
}
port, err := portsbinding.Create(client, createOpts).Extract()
if err != nil {
return port, err
}
t.Logf("Successfully created port: %s", portName)
return port, nil
}
示例5: CreateListener
// CreateListener will create a listener for a given load balancer on a random
// port with a random name. An error will be returned if the listener could not
// be created.
func CreateListener(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer) (*listeners.Listener, error) {
listenerName := tools.RandomString("TESTACCT-", 8)
listenerPort := tools.RandomInt(1, 100)
t.Logf("Attempting to create listener %s on port %d", listenerName, listenerPort)
createOpts := listeners.CreateOpts{
Name: listenerName,
LoadbalancerID: lb.ID,
Protocol: "TCP",
ProtocolPort: listenerPort,
}
listener, err := listeners.Create(client, createOpts).Extract()
if err != nil {
return listener, err
}
t.Logf("Successfully created listener %s", listenerName)
if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
return listener, fmt.Errorf("Timed out waiting for loadbalancer to become active")
}
return listener, nil
}
示例6: TestNetworksCRUD
func TestNetworksCRUD(t *testing.T) {
client, err := clients.NewNetworkV2Client()
if err != nil {
t.Fatalf("Unable to create a network client: %v", err)
}
// Create a network
network, err := CreateNetwork(t, client)
if err != nil {
t.Fatalf("Unable to create network: %v", err)
}
defer DeleteNetwork(t, client, network.ID)
PrintNetwork(t, network)
newName := tools.RandomString("TESTACC-", 8)
updateOpts := &networks.UpdateOpts{
Name: newName,
}
_, err = networks.Update(client, network.ID, updateOpts).Extract()
if err != nil {
t.Fatalf("Unable to update network: %v", err)
}
newNetwork, err := networks.Get(client, network.ID).Extract()
if err != nil {
t.Fatalf("Unable to retrieve network: %v", err)
}
PrintNetwork(t, newNetwork)
}
示例7: CreateExternalRouter
// CreateExternalRouter creates a router on the external network. This requires
// the OS_EXTGW_ID environment variable to be set. An error is returned if the
// creation failed.
func CreateExternalRouter(t *testing.T, client *gophercloud.ServiceClient) (*routers.Router, error) {
var router *routers.Router
choices, err := clients.AcceptanceTestChoicesFromEnv()
if err != nil {
return router, err
}
routerName := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create external router: %s", routerName)
adminStateUp := true
gatewayInfo := routers.GatewayInfo{
NetworkID: choices.ExternalNetworkID,
}
createOpts := routers.CreateOpts{
Name: routerName,
AdminStateUp: &adminStateUp,
GatewayInfo: &gatewayInfo,
}
router, err = routers.Create(client, createOpts).Extract()
if err != nil {
return router, err
}
if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
return router, err
}
t.Logf("Created router: %s", routerName)
return router, nil
}
示例8: CreatePool
// CreatePool will create a pool with a random name with a specified listener
// and loadbalancer. An error will be returned if the pool could not be
// created.
func CreatePool(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer) (*pools.Pool, error) {
poolName := tools.RandomString("TESTACCT-", 8)
t.Logf("Attempting to create pool %s", poolName)
createOpts := pools.CreateOpts{
Name: poolName,
Protocol: pools.ProtocolTCP,
LoadbalancerID: lb.ID,
LBMethod: pools.LBMethodLeastConnections,
}
pool, err := pools.Create(client, createOpts).Extract()
if err != nil {
return pool, err
}
t.Logf("Successfully created pool %s", poolName)
if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
return pool, fmt.Errorf("Timed out waiting for loadbalancer to become active")
}
return pool, nil
}
示例9: CreateVolumeFromImage
// CreateVolumeFromImage will create a volume from with a random name and size of
// 1GB. An error will be returned if the volume was unable to be created.
func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices) (*volumes.Volume, error) {
if testing.Short() {
t.Skip("Skipping test that requires volume creation in short mode.")
}
volumeName := tools.RandomString("ACPTTEST", 16)
t.Logf("Attempting to create volume: %s", volumeName)
createOpts := volumes.CreateOpts{
Size: 1,
Name: volumeName,
ImageID: choices.ImageID,
}
volume, err := volumes.Create(client, createOpts).Extract()
if err != nil {
return volume, err
}
err = volumes.WaitForStatus(client, volume.ID, "available", 60)
if err != nil {
return volume, err
}
return volume, nil
}
示例10: CreatePort
// CreatePort will create a port on the specified subnet. An error will be
// returned if the port could not be created.
func CreatePort(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) {
portName := tools.RandomString("TESTACC-", 8)
t.Logf("Attempting to create port: %s", portName)
createOpts := ports.CreateOpts{
NetworkID: networkID,
Name: portName,
AdminStateUp: gophercloud.Enabled,
FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}},
}
port, err := ports.Create(client, createOpts).Extract()
if err != nil {
return port, err
}
if err := WaitForPortToCreate(client, port.ID, 60); err != nil {
return port, err
}
newPort, err := ports.Get(client, port.ID).Extract()
if err != nil {
return newPort, err
}
t.Logf("Successfully created port: %s", portName)
return newPort, nil
}
示例11: CreateRule
// CreateRule will create a Firewall Rule with a random source address and
//source port, destination address and port. An error will be returned if
// the rule could not be created.
func CreateRule(t *testing.T, client *gophercloud.ServiceClient) (*rules.Rule, error) {
ruleName := tools.RandomString("TESTACC-", 8)
sourceAddress := fmt.Sprintf("192.168.1.%d", tools.RandomInt(1, 100))
sourcePort := strconv.Itoa(tools.RandomInt(1, 100))
destinationAddress := fmt.Sprintf("192.168.2.%d", tools.RandomInt(1, 100))
destinationPort := strconv.Itoa(tools.RandomInt(1, 100))
t.Logf("Attempting to create rule %s with source %s:%s and destination %s:%s",
ruleName, sourceAddress, sourcePort, destinationAddress, destinationPort)
createOpts := rules.CreateOpts{
Name: ruleName,
Protocol: rules.ProtocolTCP,
Action: "allow",
SourceIPAddress: sourceAddress,
SourcePort: sourcePort,
DestinationIPAddress: destinationAddress,
DestinationPort: destinationPort,
}
rule, err := rules.Create(client, createOpts).Extract()
if err != nil {
return rule, err
}
t.Logf("Rule %s successfully created", ruleName)
return rule, nil
}
示例12: CreateSubnetWithNoGateway
// CreateSubnetWithNoGateway will create a subnet with no gateway on the
// specified Network ID. An error will be returned if the subnet could not be
// created.
func CreateSubnetWithNoGateway(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) {
var noGateway = ""
subnetName := tools.RandomString("TESTACC-", 8)
subnetOctet := tools.RandomInt(1, 250)
subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet)
dhcpStart := fmt.Sprintf("192.168.%d.10", subnetOctet)
dhcpEnd := fmt.Sprintf("192.168.%d.200", subnetOctet)
createOpts := subnets.CreateOpts{
NetworkID: networkID,
CIDR: subnetCIDR,
IPVersion: 4,
Name: subnetName,
EnableDHCP: gophercloud.Disabled,
GatewayIP: &noGateway,
AllocationPools: []subnets.AllocationPool{
{
Start: dhcpStart,
End: dhcpEnd,
},
},
}
t.Logf("Attempting to create subnet: %s", subnetName)
subnet, err := subnets.Create(client, createOpts).Extract()
if err != nil {
return subnet, err
}
t.Logf("Successfully created subnet.")
return subnet, nil
}
示例13: CreateShareType
// CreateShareType will create a share type with a random name. An
// error will be returned if the share type was unable to be created.
func CreateShareType(t *testing.T, client *gophercloud.ServiceClient) (*sharetypes.ShareType, error) {
if testing.Short() {
t.Skip("Skipping test that requires share type creation in short mode.")
}
shareTypeName := tools.RandomString("ACPTTEST", 16)
t.Logf("Attempting to create share type: %s", shareTypeName)
extraSpecsOps := sharetypes.ExtraSpecsOpts{
DriverHandlesShareServers: true,
}
createOpts := sharetypes.CreateOpts{
Name: shareTypeName,
IsPublic: true,
ExtraSpecs: extraSpecsOps,
}
shareType, err := sharetypes.Create(client, createOpts).Extract()
if err != nil {
return shareType, err
}
return shareType, nil
}
示例14: TestLayer3RouterCreateDelete
func TestLayer3RouterCreateDelete(t *testing.T) {
client, err := clients.NewNetworkV2Client()
if err != nil {
t.Fatalf("Unable to create a network client: %v", err)
}
router, err := CreateExternalRouter(t, client)
if err != nil {
t.Fatalf("Unable to create router: %v", err)
}
defer DeleteRouter(t, client, router.ID)
PrintRouter(t, router)
newName := tools.RandomString("TESTACC-", 8)
updateOpts := routers.UpdateOpts{
Name: newName,
}
_, err = routers.Update(client, router.ID, updateOpts).Extract()
if err != nil {
t.Fatalf("Unable to update router: %v", err)
}
newRouter, err := routers.Get(client, router.ID).Extract()
if err != nil {
t.Fatalf("Unable to get router: %v", err)
}
PrintRouter(t, newRouter)
}
示例15: CreateMember
// CreateMember will create a member with a random name, port, address, and
// weight. An error will be returned if the member could not be created.
func CreateMember(t *testing.T, client *gophercloud.ServiceClient, lb *loadbalancers.LoadBalancer, pool *pools.Pool, subnetID, subnetCIDR string) (*pools.Member, error) {
memberName := tools.RandomString("TESTACCT-", 8)
memberPort := tools.RandomInt(100, 1000)
memberWeight := tools.RandomInt(1, 10)
cidrParts := strings.Split(subnetCIDR, "/")
subnetParts := strings.Split(cidrParts[0], ".")
memberAddress := fmt.Sprintf("%s.%s.%s.%d", subnetParts[0], subnetParts[1], subnetParts[2], tools.RandomInt(10, 100))
t.Logf("Attempting to create member %s", memberName)
createOpts := pools.CreateMemberOpts{
Name: memberName,
ProtocolPort: memberPort,
Weight: memberWeight,
Address: memberAddress,
SubnetID: subnetID,
}
t.Logf("Member create opts: %#v", createOpts)
member, err := pools.CreateMember(client, pool.ID, createOpts).Extract()
if err != nil {
return member, err
}
t.Logf("Successfully created member %s", memberName)
if err := WaitForLoadBalancerState(client, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
return member, fmt.Errorf("Timed out waiting for loadbalancer to become active")
}
return member, nil
}