Example #1
0
func TestListAll(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	MockListResponse(t)

	allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages()
	th.AssertNoErr(t, err)
	actual, err := ExtractVolumes(allPages)
	th.AssertNoErr(t, err)

	expected := []Volume{
		Volume{
			ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
			Name: "vol-001",
		},
		Volume{
			ID:   "96c3bda7-c82a-4f50-be73-ca7621794835",
			Name: "vol-002",
		},
	}

	th.CheckDeepEquals(t, expected, actual)

}
Example #2
0
func uploadLargeObject(t *testing.T, containerName, objectName string, sizeFile int64, sizePieces int) {
	letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	sizeFile = int64(sizeFile * 1000000)

	tempFile, err := ioutil.TempFile("", "tmp")
	th.AssertNoErr(t, err)

	defer func() {
		err := os.Remove(tempFile.Name())
		th.AssertNoErr(t, err)
	}()

	data := make([]byte, sizeFile)
	for i := range data {
		data[i] = byte(letters[rand.Intn(len(letters))])
	}

	err = ioutil.WriteFile(tempFile.Name(), data, 0777)
	th.AssertNoErr(t, err)

	_, err = exec.Command("rack", "files", "large-object", "upload", "--output", "json",
		"--container", containerName, "--name", objectName, "--file", tempFile.Name(), "--size-pieces", strconv.Itoa(sizePieces)).Output()
	th.AssertNoErr(t, err)

}
Example #3
0
func TestListImageDetails(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)

		w.Header().Add("Content-Type", "application/json")
		r.ParseForm()
		marker := r.Form.Get("marker")
		switch marker {
		case "":
			fmt.Fprintf(w, ListOutput)
		case "e19a734c-c7e6-443a-830c-242209c4d65d":
			fmt.Fprintf(w, `{ "images": [] }`)
		default:
			t.Fatalf("Unexpected marker: [%s]", marker)
		}
	})

	count := 0
	err := ListDetail(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := ExtractImages(page)
		th.AssertNoErr(t, err)
		th.CheckDeepEquals(t, ExpectedImageSlice, actual)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.CheckEquals(t, 1, count)
}
func updateStack(t *testing.T, stackID string) {
	output, err := exec.Command("rack", "orchestration", "stack", "update", "--output", "json", "--id", stackID,
		"--template-url", updateTemplateURL).Output()
	th.AssertNoErr(t, err)
	var stack stackUpdatedResponse
	err = json.Unmarshal(output, &stack)
	th.AssertNoErr(t, err)
}
func previewStack(t *testing.T) {
	output, err := exec.Command("rack", "orchestration", "stack", "preview", "--output", "json", "--template-url",
		createTemplateURL, "--name", "rackAcceptanceTestStackCreated").Output()
	th.AssertNoErr(t, err)
	var stack stackPreviewedResponse
	err = json.Unmarshal(output, &stack)
	th.AssertNoErr(t, err)
}
Example #6
0
// IsSuccessful ensures that a CreateResult was successful and contains the correct token and
// service catalog.
func IsSuccessful(t *testing.T, result CreateResult) {
	token, err := result.ExtractToken()
	th.AssertNoErr(t, err)
	th.CheckDeepEquals(t, ExpectedToken, token)

	serviceCatalog, err := result.ExtractServiceCatalog()
	th.AssertNoErr(t, err)
	th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog)
}
Example #7
0
func updateServer(t *testing.T, serverID string) {
	output, err := exec.Command("rack", "servers", "instance", "update", "--output", "json", "--id", serverID,
		"--rename", "rackAcceptanceTestUpdated").Output()
	th.AssertNoErr(t, err)
	var server osServers.Server
	err = json.Unmarshal(output, &server)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, "rackAcceptanceTestUpdated", server.Name)
}
Example #8
0
func rebootServer(t *testing.T, serverID string) {
	output, err := exec.Command("rack", "servers", "instance", "reboot", "--output", "json", "--id", serverID, "--soft", "--wait-for-completion").Output()
	th.AssertNoErr(t, err)
	type result struct {
		result string
	}
	var res result
	err = json.Unmarshal(output, &res)
	th.AssertNoErr(t, err)
}
Example #9
0
func createServer(t *testing.T) *osServers.Server {
	output, err := exec.Command("rack", "servers", "instance", "create", "--output", "json", "--image-id",
		"09de0a66-3156-48b4-90a5-1cf25a905207", "--flavor-id", "3", "--name", "rackAcceptanceTestCreated", "--wait-for-completion").Output()
	th.AssertNoErr(t, err)

	var server osServers.Server
	err = json.Unmarshal(output, &server)
	th.AssertNoErr(t, err)

	return &server
}
Example #10
0
func TestListAllContainerNames(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleListContainerNamesSuccessfully(t)

	allPages, err := List(fake.ServiceClient(), &ListOpts{Full: false}).AllPages()
	th.AssertNoErr(t, err)
	actual, err := ExtractNames(allPages)
	th.AssertNoErr(t, err)
	th.CheckDeepEquals(t, ExpectedListNames, actual)
}
Example #11
0
func TestAllPagesLinked(t *testing.T) {
	pager := createLinked(t)
	defer testhelper.TeardownHTTP()

	page, err := pager.AllPages()
	testhelper.AssertNoErr(t, err)

	expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	actual, err := ExtractLinkedInts(page)
	testhelper.AssertNoErr(t, err)
	testhelper.CheckDeepEquals(t, expected, actual)
}
Example #12
0
func TestListAllServers(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleServerListSuccessfully(t)

	allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
	th.AssertNoErr(t, err)
	actual, err := ExtractServers(allPages)
	th.AssertNoErr(t, err)
	th.CheckDeepEquals(t, ServerHerp, actual[0])
	th.CheckDeepEquals(t, ServerDerp, actual[1])
}
Example #13
0
func deleteServer(t *testing.T, serverID string) {
	fmt.Println("Deleting server...")
	output, err := exec.Command("rack", "servers", "instance", "delete", "--output", "json", "--id", serverID, "--wait-for-completion").Output()
	th.AssertNoErr(t, err)

	type result struct {
		result string
	}
	var res result
	err = json.Unmarshal(output, &res)
	th.AssertNoErr(t, err)
}
Example #14
0
func TestAllPagesSingle(t *testing.T) {
	pager := setupSinglePaged()
	defer testhelper.TeardownHTTP()

	page, err := pager.AllPages()
	testhelper.AssertNoErr(t, err)

	expected := []int{1, 2, 3}
	actual, err := ExtractSingleInts(page)
	testhelper.AssertNoErr(t, err)
	testhelper.CheckDeepEquals(t, expected, actual)
}
Example #15
0
func TestAllPagesMarker(t *testing.T) {
	pager := createMarkerPaged(t)
	defer testhelper.TeardownHTTP()

	page, err := pager.AllPages()
	testhelper.AssertNoErr(t, err)

	expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"}
	actual, err := ExtractMarkerStrings(page)
	testhelper.AssertNoErr(t, err)
	testhelper.CheckDeepEquals(t, expected, actual)
}
func adoptStack(t *testing.T, adoptData []byte) *stackCreatedResponse {
	// write adoptData to a temporary file
	adoptFile, err := ioutil.TempFile(os.TempDir(), "adoptFile")
	th.AssertNoErr(t, err)
	defer os.Remove(adoptFile.Name())
	adoptFile.Write(adoptData)

	output, err := exec.Command("rack", "orchestration", "stack", "adopt", "--adopt-file", adoptFile.Name(), "--name", "rackAcceptanceTestStackCreated", "--output", "json").Output()
	th.AssertNoErr(t, err)
	var stack stackCreatedResponse
	err = json.Unmarshal(output, &stack)
	th.AssertNoErr(t, err)
	return &stack
}
Example #17
0
func TestUploadHandlePipe(t *testing.T) {
	cmd := &commandUpload{}

	actual := &handler.Resource{
		Params: &paramsUpload{},
	}

	f, err := ioutil.TempFile("", "bar")
	th.AssertNoErr(t, err)
	defer os.Remove(f.Name())

	err = cmd.HandlePipe(actual, f.Name())
	th.AssertNoErr(t, err)
}
Example #18
0
func TestCreatePreTable(t *testing.T) {
	cmd := &commandAbandon{
		Ctx: &handler.Context{
			ServiceClient: client.ServiceClient(),
		},
	}

	resource := &handler.Resource{
		Params: &paramsCreate{},
	}

	expected := "{\"ID\":\"16ef0584-4458-41eb-87c8-0dc8d5f66c87\",\"Links\":[{\"Href\":\"http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87\",\"Rel\":\"self\"}]}"
	resource.Result = &osStacks.CreatedStack{
		ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
		Links: []gophercloud.Link{
			gophercloud.Link{
				Href: "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
				Rel:  "self",
			},
		},
	}
	err := cmd.PreTable(resource)
	th.AssertNoErr(t, err)
	actual, _ := json.Marshal(resource.Result)
	th.AssertEquals(t, expected, string(actual))
}
Example #19
0
func TestCreateExecute(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "POST")
		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusAccepted)
		fmt.Fprintf(w, `{"stack": {"id": "3095aefc-09fb-4bc7-b1f0-f21a304e864c"}}`)
	})
	cmd := &commandCreate{
		Ctx: &handler.Context{
			ServiceClient: client.ServiceClient(),
		},
	}
	templateOpts := new(osStacks.Template)
	templateOpts.Bin = []byte(`"heat_template_version": "2014-10-16"`)
	actual := &handler.Resource{
		Params: &paramsCreate{
			opts: &osStacks.CreateOpts{
				Name:         "stack1",
				TemplateOpts: templateOpts,
				Parameters: map[string]string{
					"img":    "foo",
					"flavor": "bar",
				},
			},
		},
	}
	cmd.Execute(actual)
	th.AssertNoErr(t, actual.Err)
}
Example #20
0
func TestListHandleSingle(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("stack-name", "", "")
	flagset.String("stack-id", "", "")
	flagset.Set("stack-name", "stack1")
	flagset.Set("stack-id", "id1")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandList{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}

	expected := &handler.Resource{
		Params: &paramsList{
			stackName: "stack1",
			stackID:   "id1",
		},
	}
	actual := &handler.Resource{
		Params: &paramsList{},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, expected.Params.(*paramsList).stackName, actual.Params.(*paramsList).stackName)
	th.AssertEquals(t, expected.Params.(*paramsList).stackID, actual.Params.(*paramsList).stackID)
}
Example #21
0
func TestCreateHandleSingle(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "server1")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandCreate{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsCreate{
			opts: &servers.CreateOpts{
				Name: "server1",
			},
		},
	}
	actual := &handler.Resource{
		Params: &paramsCreate{
			opts: &servers.CreateOpts{},
		},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, expected.Params.(*paramsCreate).opts.Name, actual.Params.(*paramsCreate).opts.Name)
}
Example #22
0
func TestRebootHandleSingle(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`)
	})
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "server1Name")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandReboot{
		Ctx: &handler.Context{
			CLIContext:    c,
			ServiceClient: client.ServiceClient(),
		},
	}
	expected := &handler.Resource{
		Params: &paramsReboot{
			serverID: "server1",
		},
	}
	actual := &handler.Resource{
		Params: &paramsReboot{},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, expected.Params.(*paramsReboot).serverID, actual.Params.(*paramsReboot).serverID)
}
Example #23
0
func TestEnumerateMarker(t *testing.T) {
	pager := createMarkerPaged(t)
	defer testhelper.TeardownHTTP()

	callCount := 0
	err := pager.EachPage(func(page Page) (bool, error) {
		actual, err := ExtractMarkerStrings(page)
		if err != nil {
			return false, err
		}

		t.Logf("Handler invoked with %v", actual)

		var expected []string
		switch callCount {
		case 0:
			expected = []string{"aaa", "bbb", "ccc"}
		case 1:
			expected = []string{"ddd", "eee", "fff"}
		case 2:
			expected = []string{"ggg", "hhh", "iii"}
		default:
			t.Fatalf("Unexpected call count: %d", callCount)
			return false, nil
		}

		testhelper.CheckDeepEquals(t, expected, actual)

		callCount++
		return true, nil
	})
	testhelper.AssertNoErr(t, err)
	testhelper.AssertEquals(t, callCount, 3)
}
Example #24
0
func TestListEventsExecute(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/stacks/stack1/id1/resources/resource1/events", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, `{"events": [{"event_time": "2014-06-03T20:59:46", "resource_name":"resource1"}]}`)
	})
	cmd := &commandListEvents{
		Ctx: &handler.Context{
			ServiceClient: client.ServiceClient(),
		},
	}
	actual := &handler.Resource{
		Params: &paramsListEvents{
			stackName:    "stack1",
			stackID:      "id1",
			resourceName: "resource1",
			opts:         &osStackEvents.ListResourceEventsOpts{},
		},
	}
	cmd.Execute(actual)
	th.AssertNoErr(t, actual.Err)
}
Example #25
0
func TestUpdateStack(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleUpdateSuccessfully(t)

	updateOpts := UpdateOpts{
		Template: `
    {
      "heat_template_version": "2013-05-23",
      "description": "Simple template to test heat commands",
      "parameters": {
        "flavor": {
          "default": "m1.tiny",
          "type": "string"
        }
      },
      "resources": {
        "hello_world": {
          "type":"OS::Nova::Server",
          "properties": {
            "key_name": "heat_key",
            "flavor": {
              "get_param": "flavor"
            },
            "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
            "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
          }
        }
      }
    }`,
	}
	err := Update(fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada", updateOpts).ExtractErr()
	th.AssertNoErr(t, err)
}
Example #26
0
func TestDeleteExecute(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	})
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "container1")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandDelete{
		Ctx: &handler.Context{
			ServiceClient: client.ServiceClient(),
			CLIContext:    c,
		},
	}
	actual := &handler.Resource{
		Params: &paramsDelete{
			container: "container1",
		},
	}
	cmd.Execute(actual)
	th.AssertNoErr(t, actual.Err)
}
Example #27
0
func TestCreateOpts(t *testing.T) {
	opts := CreateOpts{
		Name:       "createdserver",
		ImageRef:   "image-id",
		FlavorRef:  "flavor-id",
		KeyPair:    "mykey",
		DiskConfig: diskconfig.Manual,
	}

	expected := `
	{
		"server": {
			"name": "createdserver",
			"imageRef": "image-id",
			"flavorRef": "flavor-id",
			"flavorName": "",
			"imageName": "",
			"key_name": "mykey",
			"OS-DCF:diskConfig": "MANUAL"
		}
	}
	`
	actual, err := opts.ToServerCreateMap()
	th.AssertNoErr(t, err)
	th.CheckJSONEquals(t, expected, actual)
}
Example #28
0
func TestCreateWithOptionalFields(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/networks", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "POST")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
		th.TestHeader(t, r, "Content-Type", "application/json")
		th.TestHeader(t, r, "Accept", "application/json")
		th.TestJSONRequest(t, r, `
{
	"network": {
			"name": "sample_network",
			"admin_state_up": true,
			"shared": true,
			"tenant_id": "12345"
	}
}
		`)

		w.WriteHeader(http.StatusCreated)
	})

	iTrue := true
	options := os.CreateOpts{Name: "sample_network", AdminStateUp: &iTrue, Shared: &iTrue, TenantID: "12345"}
	_, err := Create(fake.ServiceClient(), options).Extract()
	th.AssertNoErr(t, err)
}
Example #29
0
func TestList(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	os.MockListResponse(t)

	count := 0

	err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := ExtractSnapshots(page)
		if err != nil {
			t.Errorf("Failed to extract snapshots: %v", err)
			return false, err
		}

		expected := []Snapshot{
			Snapshot{
				ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
				Name: "snapshot-001",
			},
			Snapshot{
				ID:   "96c3bda7-c82a-4f50-be73-ca7621794835",
				Name: "snapshot-002",
			},
		}

		th.CheckDeepEquals(t, expected, actual)

		return true, nil
	})

	th.AssertEquals(t, 1, count)
	th.AssertNoErr(t, err)
}
Example #30
0
func TestListHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("sort-dir", "", "")
	flagset.String("sort-key", "", "")
	flagset.String("name", "", "")
	flagset.String("status", "", "")
	flagset.String("marker", "", "")
	flagset.Set("sort-dir", "asc")
	flagset.Set("sort-key", "name")
	flagset.Set("name", "stacks*")
	flagset.Set("status", "CREATE_COMPLETE")
	flagset.Set("marker", "1fd3-4f9f-44df-1b5c")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandList{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsList{
			opts: &osStacks.ListOpts{
				SortKey: "name",
				SortDir: "asc",
				Name:    "stacks*",
				Status:  "CREATE_COMPLETE",
				Marker:  "1fd3-4f9f-44df-1b5c",
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsList).opts, *actual.Params.(*paramsList).opts)
}