Exemplo n.º 1
0
func TestSnapshots(t *testing.T) {

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

	v, err := volumes.Create(client, &volumes.CreateOpts{
		Name: "gophercloud-test-volume",
		Size: 1,
	}).Extract()
	th.AssertNoErr(t, err)

	err = volumes.WaitForStatus(client, v.ID, "available", 120)
	th.AssertNoErr(t, err)

	t.Logf("Created volume: %v\n", v)

	ss, err := snapshots.Create(client, &snapshots.CreateOpts{
		Name:     "gophercloud-test-snapshot",
		VolumeID: v.ID,
	}).Extract()
	th.AssertNoErr(t, err)

	err = snapshots.WaitForStatus(client, ss.ID, "available", 120)
	th.AssertNoErr(t, err)

	t.Logf("Created snapshot: %+v\n", ss)

	err = snapshots.Delete(client, ss.ID).ExtractErr()
	th.AssertNoErr(t, err)

	err = gophercloud.WaitFor(120, func() (bool, error) {
		_, err := snapshots.Get(client, ss.ID).Extract()
		if err != nil {
			return true, nil
		}

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

	t.Log("Deleted snapshot\n")

	err = volumes.Delete(client, v.ID).ExtractErr()
	th.AssertNoErr(t, err)

	err = gophercloud.WaitFor(120, func() (bool, error) {
		_, err := volumes.Get(client, v.ID).Extract()
		if err != nil {
			return true, nil
		}

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

	t.Log("Deleted volume\n")
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
func TestStackEvents(t *testing.T) {
	// Create a provider client for making the HTTP requests.
	// See common.go in this directory for more information.
	client := newClient(t)

	stackName := "postman_stack_2"
	resourceName := "hello_world"
	var eventID string

	createOpts := osStacks.CreateOpts{
		Name:     stackName,
		Template: template,
		Timeout:  5,
	}
	stack, err := stacks.Create(client, createOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created stack: %+v\n", stack)
	defer func() {
		err := stacks.Delete(client, stackName, stack.ID).ExtractErr()
		th.AssertNoErr(t, err)
		t.Logf("Deleted stack (%s)", stackName)
	}()
	err = gophercloud.WaitFor(60, func() (bool, error) {
		getStack, err := stacks.Get(client, stackName, stack.ID).Extract()
		if err != nil {
			return false, err
		}
		if getStack.Status == "CREATE_COMPLETE" {
			return true, nil
		}
		return false, nil
	})

	err = stackevents.List(client, stackName, stack.ID, nil).EachPage(func(page pagination.Page) (bool, error) {
		events, err := osStackEvents.ExtractEvents(page)
		th.AssertNoErr(t, err)
		t.Logf("listed events: %+v\n", events)
		eventID = events[0].ID
		return false, nil
	})
	th.AssertNoErr(t, err)

	err = stackevents.ListResourceEvents(client, stackName, stack.ID, resourceName, nil).EachPage(func(page pagination.Page) (bool, error) {
		resourceEvents, err := osStackEvents.ExtractResourceEvents(page)
		th.AssertNoErr(t, err)
		t.Logf("listed resource events: %+v\n", resourceEvents)
		return false, nil
	})
	th.AssertNoErr(t, err)

	event, err := stackevents.Get(client, stackName, stack.ID, resourceName, eventID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("retrieved event: %+v\n", event)
}
Exemplo n.º 4
0
func waitForLB(client *gophercloud.ServiceClient, id int, state lbs.Status) {
	gophercloud.WaitFor(60, func() (bool, error) {
		lb, err := lbs.Get(client, id).Extract()
		if err != nil {
			return false, err
		}
		if lb.Status != state {
			return false, nil
		}
		return true, nil
	})
}
Exemplo n.º 5
0
func (c context) WaitUntilActive(id string) {
	err := gophercloud.WaitFor(60, func() (bool, error) {
		inst, err := instances.Get(c.client, id).Extract()
		if err != nil {
			return false, err
		}
		if inst.Status == "ACTIVE" {
			return true, nil
		}
		return false, nil
	})
	c.AssertNoErr(err)
}
Exemplo n.º 6
0
Arquivo: util.go Projeto: 40a/bootkube
// WaitForStatus will continually poll a server until it successfully transitions to a specified
// status. It will do this for at most the number of seconds specified.
func WaitForStatus(c *gophercloud.ServiceClient, id, status string, secs int) error {
	return gophercloud.WaitFor(secs, func() (bool, error) {
		current, err := Get(c, id).Extract()
		if err != nil {
			return false, err
		}

		if current.Status == status {
			return true, nil
		}

		return false, nil
	})
}
Exemplo n.º 7
0
// WaitUntilDeleted will continually poll a snapshot until it has been
// successfully deleted, i.e. returns a 404 status.
func (snapshot Snapshot) WaitUntilDeleted(c *gophercloud.ServiceClient, timeout int) error {
	return gophercloud.WaitFor(timeout, func() (bool, error) {
		// Poll resource
		_, err := Get(c, snapshot.ID).Extract()

		// Check for a 404
		if casted, ok := err.(*perigee.UnexpectedResponseCodeError); ok && casted.Actual == 404 {
			return true, nil
		} else if err != nil {
			return false, err
		}

		return false, nil
	})
}
Exemplo n.º 8
0
func TestStackResources(t *testing.T) {
	// Create a provider client for making the HTTP requests.
	// See common.go in this directory for more information.
	client := newClient(t)

	stackName := "postman_stack_2"

	createOpts := stacks.CreateOpts{
		Name:     stackName,
		Template: template,
		Timeout:  5,
	}
	stack, err := stacks.Create(client, createOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created stack: %+v\n", stack)
	defer func() {
		err := stacks.Delete(client, stackName, stack.ID).ExtractErr()
		th.AssertNoErr(t, err)
		t.Logf("Deleted stack (%s)", stackName)
	}()
	err = gophercloud.WaitFor(60, func() (bool, error) {
		getStack, err := stacks.Get(client, stackName, stack.ID).Extract()
		if err != nil {
			return false, err
		}
		if getStack.Status == "CREATE_COMPLETE" {
			return true, nil
		}
		return false, nil
	})

	resourceName := "hello_world"
	resource, err := stackresources.Get(client, stackName, stack.ID, resourceName).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Got stack resource: %+v\n", resource)

	metadata, err := stackresources.Metadata(client, stackName, stack.ID, resourceName).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Got stack resource metadata: %+v\n", metadata)

	err = stackresources.List(client, stackName, stack.ID, stackresources.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		resources, err := stackresources.ExtractResources(page)
		th.AssertNoErr(t, err)
		t.Logf("resources: %+v\n", resources)
		return false, nil
	})
	th.AssertNoErr(t, err)
}
Exemplo n.º 9
0
// WaitUntilComplete will continually poll a snapshot until it successfully
// transitions to a specified state. It will do this for at most the number of
// seconds specified.
func (snapshot Snapshot) WaitUntilComplete(c *gophercloud.ServiceClient, timeout int) error {
	return gophercloud.WaitFor(timeout, func() (bool, error) {
		// Poll resource
		current, err := Get(c, snapshot.ID).Extract()
		if err != nil {
			return false, err
		}

		// Has it been built yet?
		if current.Progress == "100%" {
			return true, nil
		}

		return false, nil
	})
}
Exemplo n.º 10
0
func TestStacks(t *testing.T) {
	// Create a provider client for making the HTTP requests.
	// See common.go in this directory for more information.
	client := newClient(t)

	stackName1 := "gophercloud-test-stack-2"
	createOpts := stacks.CreateOpts{
		Name:     stackName1,
		Template: template,
		Timeout:  5,
	}
	stack, err := stacks.Create(client, createOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created stack: %+v\n", stack)
	defer func() {
		err := stacks.Delete(client, stackName1, stack.ID).ExtractErr()
		th.AssertNoErr(t, err)
		t.Logf("Deleted stack (%s)", stackName1)
	}()
	err = gophercloud.WaitFor(60, func() (bool, error) {
		getStack, err := stacks.Get(client, stackName1, stack.ID).Extract()
		if err != nil {
			return false, err
		}
		if getStack.Status == "CREATE_COMPLETE" {
			return true, nil
		}
		return false, nil
	})

	updateOpts := stacks.UpdateOpts{
		Template: template,
		Timeout:  20,
	}
	err = stacks.Update(client, stackName1, stack.ID, updateOpts).ExtractErr()
	th.AssertNoErr(t, err)
	err = gophercloud.WaitFor(60, func() (bool, error) {
		getStack, err := stacks.Get(client, stackName1, stack.ID).Extract()
		if err != nil {
			return false, err
		}
		if getStack.Status == "UPDATE_COMPLETE" {
			return true, nil
		}
		return false, nil
	})

	t.Logf("Updated stack")

	err = stacks.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
		stackList, err := stacks.ExtractStacks(page)
		th.AssertNoErr(t, err)

		t.Logf("Got stack list: %+v\n", stackList)

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

	getStack, err := stacks.Get(client, stackName1, stack.ID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Got stack: %+v\n", getStack)

	abandonedStack, err := stacks.Abandon(client, stackName1, stack.ID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Abandonded stack %+v\n", abandonedStack)
	th.AssertNoErr(t, err)
}
Exemplo n.º 11
0
func TestStackTemplates(t *testing.T) {
	// Create a provider client for making the HTTP requests.
	// See common.go in this directory for more information.
	client := newClient(t)

	stackName := "postman_stack_2"

	createOpts := osStacks.CreateOpts{
		Name:     stackName,
		Template: template,
		Timeout:  5,
	}
	stack, err := stacks.Create(client, createOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created stack: %+v\n", stack)
	defer func() {
		err := stacks.Delete(client, stackName, stack.ID).ExtractErr()
		th.AssertNoErr(t, err)
		t.Logf("Deleted stack (%s)", stackName)
	}()
	err = gophercloud.WaitFor(60, func() (bool, error) {
		getStack, err := stacks.Get(client, stackName, stack.ID).Extract()
		if err != nil {
			return false, err
		}
		if getStack.Status == "CREATE_COMPLETE" {
			return true, nil
		}
		return false, nil
	})

	tmpl, err := stacktemplates.Get(client, stackName, stack.ID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("retrieved template: %+v\n", tmpl)

	validateOpts := osStacktemplates.ValidateOpts{
		Template: map[string]interface{}{
			"heat_template_version": "2013-05-23",
			"description":           "Simple template to test heat commands",
			"parameters": map[string]interface{}{
				"flavor": map[string]interface{}{
					"default": "m1.tiny",
					"type":    "string",
				},
			},
			"resources": map[string]interface{}{
				"hello_world": map[string]interface{}{
					"type": "OS::Nova::Server",
					"properties": map[string]interface{}{
						"key_name": "heat_key",
						"flavor": map[string]interface{}{
							"get_param": "flavor",
						},
						"image":     "ad091b52-742f-469e-8f3c-fd81cadf0743",
						"user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n",
					},
				},
			},
		},
	}
	validatedTemplate, err := stacktemplates.Validate(client, validateOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("validated template: %+v\n", validatedTemplate)
}