Exemple #1
0
func (c *context) createBackup() {
	opts := backups.CreateOpts{
		Name:       tools.RandomString("backup_", 5),
		InstanceID: c.instanceID,
	}

	backup, err := backups.Create(c.client, opts).Extract()

	c.Logf("Created backup %#v", backup)
	c.AssertNoErr(err)

	err = gophercloud.WaitFor(60, func() (bool, error) {
		b, err := backups.Get(c.client, backup.ID).Extract()
		if err != nil {
			return false, err
		}
		if b.Status == "COMPLETED" {
			return true, nil
		}
		return false, nil
	})
	c.AssertNoErr(err)

	c.backupID = backup.ID
}
func createNetworkServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, networkID string) (*servers.Server, error) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	name := tools.RandomString("ACPTTEST", 16)
	t.Logf("Attempting to create server: %s\n", name)

	pwd := tools.MakeNewPassword("")

	networks := make([]servers.Network, 1)
	networks[0] = servers.Network{
		UUID: networkID,
	}

	server, err := servers.Create(client, servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
		AdminPass: pwd,
		Networks:  networks,
	}).Extract()
	if err != nil {
		t.Fatalf("Unable to create server: %v", err)
	}

	th.AssertEquals(t, pwd, server.AdminPass)

	return server, err
}
func TestBootFromVolume(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	options, err := optionsFromEnv()
	th.AssertNoErr(t, err)

	name := tools.RandomString("Gophercloud-", 8)
	t.Logf("Creating server [%s].", name)

	bd := []osBFV.BlockDevice{
		osBFV.BlockDevice{
			UUID:       options.imageID,
			SourceType: osBFV.Image,
			VolumeSize: 10,
		},
	}

	server, err := bootfromvolume.Create(client, servers.CreateOpts{
		Name:        name,
		FlavorRef:   "performance1-1",
		BlockDevice: bd,
	}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created server: %+v\n", server)
	defer deleteServer(t, client, server)

	getServer(t, client, server)

	listServers(t, client)
}
func createServer(t *testing.T, client *gophercloud.ServiceClient, keyName string) *os.Server {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	options, err := optionsFromEnv()
	th.AssertNoErr(t, err)

	name := tools.RandomString("Gophercloud-", 8)

	opts := &servers.CreateOpts{
		Name:       name,
		ImageRef:   options.imageID,
		FlavorRef:  options.flavorID,
		DiskConfig: diskconfig.Manual,
	}

	if keyName != "" {
		opts.KeyPair = keyName
	}

	t.Logf("Creating server [%s].", name)
	s, err := servers.Create(client, opts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Creating server.")

	err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
	th.AssertNoErr(t, err)
	t.Logf("Server created successfully.")

	return s
}
Exemple #5
0
func (c *context) updateUser() {
	opts := users.UpdateOpts{Name: tools.RandomString("new_name_", 5)}
	err := users.Update(c.client, c.instanceID, c.users[0], opts).ExtractErr()
	c.Logf("Updated user %s", c.users[0])
	c.AssertNoErr(err)
	c.users[0] = opts.Name
}
Exemple #6
0
func createLB(t *testing.T, client *gophercloud.ServiceClient, count int) []int {
	ids := []int{}

	for i := 0; i < count; i++ {
		opts := lbs.CreateOpts{
			Name:     tools.RandomString("test_", 5),
			Port:     80,
			Protocol: "HTTP",
			VIPs: []vips.VIP{
				vips.VIP{Type: vips.PUBLIC},
			},
		}

		lb, err := lbs.Create(client, opts).Extract()
		th.AssertNoErr(t, err)

		t.Logf("Created LB %d - waiting for it to build...", lb.ID)
		waitForLB(client, lb.ID, lbs.ACTIVE)
		t.Logf("LB %d has reached ACTIVE state", lb.ID)

		ids = append(ids, lb.ID)
	}

	return ids
}
func createServerInGroup(t *testing.T, computeClient *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	name := tools.RandomString("ACPTTEST", 16)
	t.Logf("Attempting to create server: %s\n", name)

	pwd := tools.MakeNewPassword("")

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
		AdminPass: pwd,
	}
	server, err := servers.Create(computeClient, schedulerhints.CreateOptsExt{
		serverCreateOpts,
		schedulerhints.SchedulerHints{
			Group: serverGroup.ID,
		},
	}).Extract()
	if err != nil {
		t.Fatalf("Unable to create server: %v", err)
	}

	th.AssertEquals(t, pwd, server.AdminPass)

	return server, err
}
Exemple #8
0
func createTestImage(t *testing.T, client *gophercloud.ServiceClient) images.Image {
	//creating image
	imageName := tools.RandomString("ACCPT", 16)
	containerFormat := "ami"
	createResult := images.Create(client, images.CreateOpts{Name: imageName,
		ContainerFormat: containerFormat,
		DiskFormat:      containerFormat})
	th.AssertNoErr(t, createResult.Err)
	image, err := createResult.Extract()
	th.AssertNoErr(t, err)
	t.Logf("Image %v", image)

	//checking status
	image, err = images.Get(client, image.ID).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, image.Status, images.ImageStatusQueued)

	//uploading image data
	data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
	uploadResult := images.Upload(client, image.ID, bytes.NewReader(data))
	th.AssertNoErr(t, uploadResult.Err)

	//checking status
	image, err = images.Get(client, image.ID).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, image.Status, images.ImageStatusActive)
	th.AssertEquals(t, image.SizeBytes, 9)
	return *image
}
func TestMultiEphemeral(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	name := tools.RandomString("Gophercloud-", 8)
	t.Logf("Creating server [%s].", name)

	bd := []bootfromvolume.BlockDevice{
		bootfromvolume.BlockDevice{
			BootIndex:           0,
			UUID:                choices.ImageID,
			SourceType:          bootfromvolume.Image,
			DestinationType:     "local",
			DeleteOnTermination: true,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
	}

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
	}
	server, err := bootfromvolume.Create(client, bootfromvolume.CreateOptsExt{
		serverCreateOpts,
		bd,
	}).Extract()
	th.AssertNoErr(t, err)
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal(err)
	}

	t.Logf("Created server: %+v\n", server)
	defer servers.Delete(client, server.ID)
	t.Logf("Deleting server [%s]...", name)
}
Exemple #10
0
func TestSecGroups(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	serverID, needsDeletion := findServer(t, client)

	groupID := createSecGroup(t, client)

	listSecGroups(t, client)

	newName := tools.RandomString("secgroup_", 5)
	updateSecGroup(t, client, groupID, newName)

	getSecGroup(t, client, groupID)

	addRemoveRules(t, client, groupID)

	addServerToSecGroup(t, client, serverID, newName)

	removeServerFromSecGroup(t, client, serverID, newName)

	if needsDeletion {
		servers.Delete(client, serverID)
	}

	deleteSecGroup(t, client, groupID)
}
func TestUpdateServer(t *testing.T) {
	client, err := newClient()
	if err != nil {
		t.Fatalf("Unable to create a compute client: %v", err)
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	server, err := createServer(t, client, choices)
	if err != nil {
		t.Fatal(err)
	}
	defer servers.Delete(client, server.ID)

	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal(err)
	}

	alternateName := tools.RandomString("ACPTTEST", 16)
	for alternateName == server.Name {
		alternateName = tools.RandomString("ACPTTEST", 16)
	}

	t.Logf("Attempting to rename the server to %s.", alternateName)

	updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
	if err != nil {
		t.Fatalf("Unable to rename server: %v", err)
	}

	if updated.ID != server.ID {
		t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
	}

	err = tools.WaitFor(func() (bool, error) {
		latest, err := servers.Get(client, updated.ID).Extract()
		if err != nil {
			return false, err
		}

		return latest.Name == alternateName, nil
	})
}
Exemple #12
0
func (c *context) createDBs() {
	dbs := []string{
		tools.RandomString("db_", 5),
		tools.RandomString("db_", 5),
		tools.RandomString("db_", 5),
	}

	opts := db.BatchCreateOpts{
		db.CreateOpts{Name: dbs[0]},
		db.CreateOpts{Name: dbs[1]},
		db.CreateOpts{Name: dbs[2]},
	}

	err := db.Create(c.client, c.instanceID, opts).ExtractErr()
	c.Logf("Created three databases on instance %s: %s, %s, %s", c.instanceID, dbs[0], dbs[1], dbs[2])
	c.AssertNoErr(err)

	c.DBIDs = dbs
}
Exemple #13
0
func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) {
	opts := secgroups.UpdateOpts{
		Name:        newName,
		Description: tools.RandomString("dec_", 10),
	}
	group, err := secgroups.Update(client, id, opts).Extract()
	th.AssertNoErr(t, err)

	t.Logf("Updated %s's name to %s", group.ID, group.Name)
}
func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	var network networks.Network

	networkingClient, err := networkingClient()
	if err != nil {
		t.Fatalf("Unable to create a networking client: %v", err)
	}

	pager := networks.List(networkingClient, networks.ListOpts{
		Name:  choices.NetworkName,
		Limit: 1,
	})
	pager.EachPage(func(page pagination.Page) (bool, error) {
		networks, err := networks.ExtractNetworks(page)
		if err != nil {
			t.Errorf("Failed to extract networks: %v", err)
			return false, err
		}

		if len(networks) == 0 {
			t.Fatalf("No networks to attach to server")
			return false, err
		}

		network = networks[0]

		return false, nil
	})

	name := tools.RandomString("ACPTTEST", 16)
	t.Logf("Attempting to create server: %s\n", name)

	pwd := tools.MakeNewPassword("")

	server, err := servers.Create(client, servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
		Networks: []servers.Network{
			servers.Network{UUID: network.ID},
		},
		AdminPass: pwd,
	}).Extract()
	if err != nil {
		t.Fatalf("Unable to create server: %v", err)
	}

	th.AssertEquals(t, pwd, server.AdminPass)

	return server, err
}
Exemple #15
0
func TestCreateServerWithKeyPair(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
	publicKey := privateKey.PublicKey
	pub, err := ssh.NewPublicKey(&publicKey)
	th.AssertNoErr(t, err)
	pubBytes := ssh.MarshalAuthorizedKey(pub)
	pk := string(pubBytes)

	kp, err := keypairs.Create(client, keypairs.CreateOpts{
		Name:      keyName,
		PublicKey: pk,
	}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created key pair: %s\n", kp)

	choices, err := ComputeChoicesFromEnv()
	th.AssertNoErr(t, err)

	name := tools.RandomString("Gophercloud-", 8)
	t.Logf("Creating server [%s] with key pair.", name)

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
	}

	server, err := servers.Create(client, keypairs.CreateOptsExt{
		serverCreateOpts,
		keyName,
	}).Extract()
	th.AssertNoErr(t, err)
	defer servers.Delete(client, server.ID)
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatalf("Unable to wait for server: %v", err)
	}

	server, err = servers.Get(client, server.ID).Extract()
	t.Logf("Created server: %+v\n", server)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, server.KeyName, keyName)

	t.Logf("Deleting key pair [%s]...", kp.Name)
	err = keypairs.Delete(client, keyName).ExtractErr()
	th.AssertNoErr(t, err)

	t.Logf("Deleting server [%s]...", name)
}
Exemple #16
0
func (c *context) changeUserPwd() {
	opts := os.BatchCreateOpts{}

	for _, name := range c.users[:1] {
		opts = append(opts, os.CreateOpts{Name: name, Password: tools.RandomString("", 5)})
	}

	err := users.ChangePassword(c.client, c.instanceID, opts).ExtractErr()
	c.Logf("Updated 2 users' passwords")
	c.AssertNoErr(err)
}
func (c *context) updateConfigGrp() {
	opts := os.UpdateOpts{
		Name: tools.RandomString("new_name_", 5),
		Values: map[string]interface{}{
			"connect_timeout": 250,
		},
	}
	err := config.Update(c.client, c.configGroupID, opts).ExtractErr()
	c.Logf("Updated config group %s", c.configGroupID)
	c.AssertNoErr(err)
}
func createServerKeyPair(t *testing.T, client *gophercloud.ServiceClient) *oskey.KeyPair {
	name := tools.RandomString("importedkey-", 8)
	pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlIQ3r+zd97kb9Hzmujd3V6pbO53eb3Go4q2E8iqVGWQfZTrFdL9KACJnqJIm9HmncfRkUTxE37hqeGCCv8uD+ZPmPiZG2E60OX1mGDjbbzAyReRwYWXgXHopggZTLak5k4mwZYaxwaufbVBDRn847e01lZnaXaszEToLM37NLw+uz29sl3TwYy2R0RGHPwPc160aWmdLjSyd1Nd4c9pvvOP/EoEuBjIC6NJJwg2Rvg9sjjx9jYj0QUgc8CqKLN25oMZ69kNJzlFylKRUoeeVr89txlR59yehJWk6Uw6lYFTdJmcmQOFVAJ12RMmS1hLWCM8UzAgtw+EDa0eqBxBDl smash@winter"

	k, err := keypairs.Create(client, oskey.CreateOpts{
		Name:      name,
		PublicKey: pubkey,
	}).Extract()
	th.AssertNoErr(t, err)

	return k
}
Exemple #19
0
func (c *context) createUsers() {
	c.users = []string{
		tools.RandomString("user_", 5),
		tools.RandomString("user_", 5),
		tools.RandomString("user_", 5),
	}

	db1 := db.CreateOpts{Name: c.DBIDs[0]}
	db2 := db.CreateOpts{Name: c.DBIDs[1]}
	db3 := db.CreateOpts{Name: c.DBIDs[2]}

	opts := os.BatchCreateOpts{
		os.CreateOpts{
			Name:      c.users[0],
			Password:  tools.RandomString("db_", 5),
			Databases: db.BatchCreateOpts{db1, db2, db3},
		},
		os.CreateOpts{
			Name:      c.users[1],
			Password:  tools.RandomString("db_", 5),
			Databases: db.BatchCreateOpts{db1, db2},
		},
		os.CreateOpts{
			Name:      c.users[2],
			Password:  tools.RandomString("db_", 5),
			Databases: db.BatchCreateOpts{db3},
		},
	}

	err := users.Create(c.client, c.instanceID, opts).ExtractErr()
	c.Logf("Created three users on instance %s: %s, %s, %s", c.instanceID, c.users[0], c.users[1], c.users[2])
	c.AssertNoErr(err)
}
Exemple #20
0
func (c context) createUsers() {
	users := []string{
		tools.RandomString("user_", 5),
		tools.RandomString("user_", 5),
		tools.RandomString("user_", 5),
	}

	db1 := db.CreateOpts{Name: "db1"}
	db2 := db.CreateOpts{Name: "db2"}
	db3 := db.CreateOpts{Name: "db3"}

	opts := u.BatchCreateOpts{
		u.CreateOpts{
			Name:      users[0],
			Password:  tools.RandomString("", 5),
			Databases: db.BatchCreateOpts{db1, db2, db3},
		},
		u.CreateOpts{
			Name:      users[1],
			Password:  tools.RandomString("", 5),
			Databases: db.BatchCreateOpts{db1, db2},
		},
		u.CreateOpts{
			Name:      users[2],
			Password:  tools.RandomString("", 5),
			Databases: db.BatchCreateOpts{db3},
		},
	}

	err := u.Create(c.client, c.instanceID, opts).ExtractErr()
	c.AssertNoErr(err)
	c.Logf("Created three users on instance %s: %s, %s, %s", c.instanceID, users[0], users[1], users[2])
	c.users = users
}
Exemple #21
0
func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string {
	opts := secgroups.CreateOpts{
		Name:        tools.RandomString("secgroup_", 5),
		Description: "something",
	}

	group, err := secgroups.Create(client, opts).Extract()
	th.AssertNoErr(t, err)

	t.Logf("Created secgroup %s %s", group.ID, group.Name)

	return group.ID
}
Exemple #22
0
func findServer(t *testing.T) string {
	var serverIP string

	client, err := newComputeClient()
	th.AssertNoErr(t, err)

	err = servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
		sList, err := servers.ExtractServers(page)
		th.AssertNoErr(t, err)

		for _, s := range sList {
			serverIP = s.AccessIPv4
			t.Logf("Found an existing server: ID [%s] Public IP [%s]", s.ID, serverIP)
			break
		}

		return true, nil
	})
	th.AssertNoErr(t, err)

	if serverIP == "" {
		t.Log("No server found, creating one")

		imageRef := os.Getenv("RS_IMAGE_ID")
		if imageRef == "" {
			t.Fatalf("OS var RS_IMAGE_ID undefined")
		}
		flavorRef := os.Getenv("RS_FLAVOR_ID")
		if flavorRef == "" {
			t.Fatalf("OS var RS_FLAVOR_ID undefined")
		}

		opts := &servers.CreateOpts{
			Name:       tools.RandomString("lb_test_", 5),
			ImageRef:   imageRef,
			FlavorRef:  flavorRef,
			DiskConfig: diskconfig.Manual,
		}

		s, err := servers.Create(client, opts).Extract()
		th.AssertNoErr(t, err)
		serverIP = s.AccessIPv4

		t.Logf("Created server %s, waiting for it to build", s.ID)
		err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
		th.AssertNoErr(t, err)
		t.Logf("Server created successfully.")
	}

	return serverIP
}
Exemple #23
0
func createUser(t *testing.T, client *gophercloud.ServiceClient) string {
	t.Log("Creating user")

	opts := users.CreateOpts{
		Username: tools.RandomString("user_", 5),
		Enabled:  os.Disabled,
		Email:    "*****@*****.**",
	}

	user, err := users.Create(client, opts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created user %s", user.ID)

	return user.ID
}
func TestActionRebuild(t *testing.T) {
	t.Parallel()

	client, err := newClient()
	if err != nil {
		t.Fatalf("Unable to create a compute client: %v", err)
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	server, err := createServer(t, client, choices)
	if err != nil {
		t.Fatal(err)
	}
	defer servers.Delete(client, server.ID)

	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal(err)
	}

	t.Logf("Attempting to rebuild server %s", server.ID)

	rebuildOpts := servers.RebuildOpts{
		Name:      tools.RandomString("ACPTTEST", 16),
		AdminPass: tools.MakeNewPassword(server.AdminPass),
		ImageID:   choices.ImageID,
	}

	rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
	if err != nil {
		t.Fatal(err)
	}

	if rebuilt.ID != server.ID {
		t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
	}

	if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
		t.Fatal(err)
	}

	if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
		t.Fatal(err)
	}
}
Exemple #25
0
func TestCreateKeyPair(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	name := tools.RandomString("createdkey-", 8)
	k, err := keypairs.Create(client, os.CreateOpts{Name: name}).Extract()
	th.AssertNoErr(t, err)
	defer deleteKeyPair(t, client, name)

	t.Logf("Created a new keypair:")
	t.Logf("        name=[%s]", k.Name)
	t.Logf(" fingerprint=[%s]", k.Fingerprint)
	t.Logf("   publickey=[%s]", tools.Elide(k.PublicKey))
	t.Logf("  privatekey=[%s]", tools.Elide(k.PrivateKey))
	t.Logf("      userid=[%s]", k.UserID)
}
Exemple #26
0
func (c *context) createInstance() {
	opts := instances.CreateOpts{
		FlavorRef: "1",
		Size:      1,
		Name:      tools.RandomString("gopher_db", 5),
	}

	instance, err := instances.Create(c.client, opts).Extract()
	th.AssertNoErr(c.test, err)

	c.Logf("Creating %s. Waiting...", instance.ID)
	c.WaitUntilActive(instance.ID)
	c.Logf("Created instance %s", instance.ID)

	c.instanceID = instance.ID
}
func (c *context) createConfigGrp() {
	opts := os.CreateOpts{
		Name: tools.RandomString("config_", 5),
		Values: map[string]interface{}{
			"connect_timeout":  300,
			"join_buffer_size": 900000,
		},
	}

	cg, err := config.Create(c.client, opts).Extract()

	c.AssertNoErr(err)
	c.Logf("Created config group %#v", cg)

	c.configGroupID = cg.ID
}
Exemple #28
0
func TestListAllContainers(t *testing.T) {
	// Create a new client to execute the HTTP requests. See common.go for newClient body.
	client := newClient(t)

	numContainers := 20

	// Create a slice of random container names.
	cNames := make([]string, numContainers)
	for i := 0; i < numContainers; i++ {
		cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
	}

	// Create numContainers containers.
	for i := 0; i < len(cNames); i++ {
		res := containers.Create(client, cNames[i], nil)
		th.AssertNoErr(t, res.Err)
	}
	// Delete the numContainers containers after function completion.
	defer func() {
		for i := 0; i < len(cNames); i++ {
			res := containers.Delete(client, cNames[i])
			th.AssertNoErr(t, res.Err)
		}
	}()

	// List all the numContainer names that were just created. To just list those,
	// the 'prefix' parameter is used.
	allPages, err := containers.List(client, &containers.ListOpts{Full: true, Limit: 5, Prefix: "gophercloud-test-container-"}).AllPages()
	th.AssertNoErr(t, err)
	containerInfoList, err := containers.ExtractInfo(allPages)
	th.AssertNoErr(t, err)
	for _, n := range containerInfoList {
		t.Logf("Container: Name [%s] Count [%d] Bytes [%d]",
			n.Name, n.Count, n.Bytes)
	}
	th.AssertEquals(t, numContainers, len(containerInfoList))

	// List the info for all the numContainer containers that were created.
	allPages, err = containers.List(client, &containers.ListOpts{Full: false, Limit: 2, Prefix: "gophercloud-test-container-"}).AllPages()
	th.AssertNoErr(t, err)
	containerNamesList, err := containers.ExtractNames(allPages)
	th.AssertNoErr(t, err)
	for _, n := range containerNamesList {
		t.Logf("Container: Name [%s]", n)
	}
	th.AssertEquals(t, numContainers, len(containerNamesList))
}
Exemple #29
0
func (c *context) createReplica() {
	opts := instances.CreateOpts{
		FlavorRef: "2",
		Size:      1,
		Name:      tools.RandomString("gopher_db", 5),
		ReplicaOf: c.instanceID,
	}

	repl, err := instances.Create(c.client, opts).Extract()
	th.AssertNoErr(c.test, err)

	c.Logf("Creating replica of %s. Waiting...", c.instanceID)
	c.WaitUntilActive(repl.ID)
	c.Logf("Created replica %#v", repl)

	c.replicaID = repl.ID
}
Exemple #30
0
func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) {
	var serverID string
	var needsDeletion bool

	err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
		sList, err := servers.ExtractServers(page)
		th.AssertNoErr(t, err)

		for _, s := range sList {
			serverID = s.ID
			needsDeletion = false

			t.Logf("Found an existing server: ID [%s]", serverID)
			break
		}

		return true, nil
	})
	th.AssertNoErr(t, err)

	if serverID == "" {
		t.Log("No server found, creating one")

		choices, err := ComputeChoicesFromEnv()
		th.AssertNoErr(t, err)

		opts := &servers.CreateOpts{
			Name:      tools.RandomString("secgroup_test_", 5),
			ImageRef:  choices.ImageID,
			FlavorRef: choices.FlavorID,
		}

		s, err := servers.Create(client, opts).Extract()
		th.AssertNoErr(t, err)
		serverID = s.ID

		t.Logf("Created server %s, waiting for it to build", s.ID)
		err = servers.WaitForStatus(client, serverID, "ACTIVE", 300)
		th.AssertNoErr(t, err)

		needsDeletion = true
	}

	return serverID, needsDeletion
}