<commit_before><commit_msg>Acceptance tests for Rackspace server create/delete.<commit_after>// +build acceptance

package v2

import (
	"testing"

	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/acceptance/tools"
	os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
	"github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
	th "github.com/rackspace/gophercloud/testhelper"
)

func createServer(t *testing.T, client *gophercloud.ServiceClient) *os.Server {
	options, err := optionsFromEnv()
	th.AssertNoErr(t, err)

	r := servers.Create(client, &os.CreateOpts{
		Name:      tools.RandomString("Gophercloud-", 8),
		ImageRef:  options.imageID,
		FlavorRef: options.flavorID,
	})
	t.Logf("\n%s", r.PrettyPrintJSON())
	s, err := r.Extract()
	th.AssertNoErr(t, err)
	return s
}

func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
	err := servers.Delete(client, server.ID)
	th.AssertNoErr(t, err)
}

func logServer(t *testing.T, server *os.Server, index int) {
	if index == -1 {
		t.Logf("             id=[%s]", server.ID)
	} else {
		t.Logf("[%02d]             id=[%s]", index, server.ID)
	}
	t.Logf("           name=[%s]", server.Name)
	t.Logf("      tenant ID=[%s]", server.TenantID)
	t.Logf("        user ID=[%s]", server.UserID)
	t.Logf("        updated=[%s]", server.Updated)
	t.Logf("        created=[%s]", server.Created)
	t.Logf("        host ID=[%s]", server.HostID)
	t.Logf("    access IPv4=[%s]", server.AccessIPv4)
	t.Logf("    access IPv6=[%s]", server.AccessIPv6)
	t.Logf("          image=[%v]", server.Image)
	t.Logf("         flavor=[%v]", server.Flavor)
	t.Logf("      addresses=[%v]", server.Addresses)
	t.Logf("       metadata=[%v]", server.Metadata)
	t.Logf("          links=[%v]", server.Links)
	t.Logf("        keyname=[%s]", server.KeyName)
	t.Logf(" admin password=[%s]", server.AdminPass)
	t.Logf("         status=[%s]", server.Status)
	t.Logf("       progress=[%d]", server.Progress)
}

func TestCreateServer(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	t.Logf("Creating a new server:")
	s := createServer(t, client)
	defer func() {
		t.Logf("Deleting server")
		deleteServer(t, client, s)
	}()

	t.Logf("Waiting for server to become active ...")
	err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
	th.AssertNoErr(t, err)

	t.Logf("Server launched:")
	logServer(t, s, -1)
}
{'fraction_non_alphanumeric': 0.18423236514522823, 'fraction_numerical': 0.006224066390041493, 'mean_word_length': 3.6724806201550386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1362063', 'n_tokens_mistral': 939, 'n_tokens_neox': 918, 'n_words': 176}