示例#1
0
func TestShow(t *testing.T) {
	authInfo := photon.AuthInfo{}
	response, err := json.Marshal(authInfo)
	if err != nil {
		t.Error("Not expecting error serializing expected status")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/auth",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = show(cxt)
	if err != nil {
		t.Error(err)
	}
}
func TestShowNetworks(t *testing.T) {
	expectedStruct := photon.Subnet{
		ID:         "network_id",
		Name:       "network_name",
		PortGroups: []string{"port", "group"},
		IsDefault:  false,
	}

	response, err := json.Marshal(expectedStruct)
	if err != nil {
		t.Error("Not expecting error serializing expected response")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/subnets/"+expectedStruct.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{expectedStruct.ID})
	cxt := cli.NewContext(nil, set, nil)
	err = showNetwork(cxt, os.Stdout)
	if err != nil {
		t.Error("Error showing networks: " + err.Error())
	}
}
func TestShowAvailabilityZone(t *testing.T) {
	expectedStruct := photon.AvailabilityZone{
		ID:   "availabilityzone_id",
		Name: "availabilityzone_name",
	}

	response, err := json.Marshal(expectedStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected response")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/availabilityzones/"+expectedStruct.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{expectedStruct.ID})
	cxt := cli.NewContext(nil, set, nil)
	err = showAvailabilityZone(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting show availabilityzone to fail: " + err.Error())
	}
}
func TestResizeCluster(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "RESIZE_CLUSTER",
		State:     "QUEUED",
		ID:        "fake_resize_cluster_task_id",
		Entity:    photon.Entity{ID: "fake_cluster_id"},
	}
	queuedTaskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected queued task")
	}

	completedTask := &photon.Task{
		Operation: "RESIZE_CLUSTER",
		State:     "COMPLETED",
		ID:        "fake_resize_cluster_task_id",
		Entity:    photon.Entity{ID: "fake_cluster_id"},
	}
	completedTaskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected completed task")
	}

	server := mocks.NewTestServer()
	defer server.Close()

	mocks.RegisterResponder(
		"POST",
		server.URL+"/clusters/fake_cluster_id/resize",
		mocks.CreateResponder(200, string(queuedTaskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/fake_resize_cluster_task_id",
		mocks.CreateResponder(200, string(completedTaskResponse[:])))
	mocks.Activate(true)

	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting argument parsing to fail")
	}

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_cluster_id", "50"})
	if err != nil {
		t.Error("Not expecting argument parsing to fail")
	}
	ctx := cli.NewContext(nil, set, globalCtx)

	err = resizeCluster(ctx, os.Stdout)
	if err != nil {
		t.Error("Not expecting error resizing cluster: " + err.Error())
	}
}
func TestEnableClusterType(t *testing.T) {
	deploymentId := "deployment1"
	queuedTask := &photon.Task{
		Operation: "CONFIGURE_CLUSTER",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: deploymentId},
	}
	completedTask := &photon.Task{
		Operation: "CONFIGURE_CLUSTER",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: deploymentId},
	}
	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected queuedTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/deployments/"+deploymentId+"/enable_cluster_type",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskResponse[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{deploymentId})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set.String("type", "SWARM", "Cluster type")
	set.String("image-id", "abcd", "image id")

	cxt := cli.NewContext(nil, set, globalCtx)
	err = enableClusterType(cxt)
	if err != nil {
		t.Error(err)
		t.Error("Not expecting deployment list hosts to fail")
	}
}
示例#6
0
func TestReleaseFloatingIp(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "RELEASE_FLOATING_IP",
		State:     "QUEUED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	completedTask := &photon.Task{
		Operation: "RELEASE_FLOATING_IP",
		State:     "COMPLETED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}

	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/vms/"+"fake_vm_ID"+"/release_floating_ip",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskResponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_vm_ID"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set.String("network_id", "fake_network_id", "network id")
	cxt := cli.NewContext(nil, set, globalCtx)

	err = releaseFloatingIp(cxt)
	if err != nil {
		t.Error("Not expecting error creating VM image: " + err.Error())
	}
}
func TestListDeploymentHosts(t *testing.T) {
	hostList := MockHostsPage{
		Items: []photon.Host{
			{
				Username: "******",
				Password: "******",
				Address:  "testIP",
				Tags:     []string{"CLOUD"},
				ID:       "host-test-id",
				State:    "COMPLETED",
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}

	response, err := json.Marshal(hostList)
	if err != nil {
		t.Error("Not expecting error serializing host list")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/deployments/1/hosts",
		mocks.CreateResponder(200, string(response[:])))

	hostList = MockHostsPage{
		Items:            []photon.Host{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}
	response, err = json.Marshal(hostList)
	if err != nil {
		t.Error("Not expecting error serializing expected taskLists")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"1"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)
	err = listDeploymentHosts(cxt)
	if err != nil {
		t.Error("Not expecting deployment list hosts to fail")
	}
}
示例#8
0
func TestVMNetworks(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "GET_NETWORKS",
		State:     "COMPLETED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	taskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

	networkMap := make(map[string]interface{})
	networkMap["network"] = "VMmgmtNetwork"
	networkMap["macAddress"] = "00:0c:29:7a:b4:d5"
	networkMap["ipAddress"] = "10.144.121.12"
	networkMap["netmask"] = "255.255.252.0"
	networkMap["isConnected"] = "true"
	networkConnectionMap := make(map[string]interface{})
	networkConnectionMap["networkConnections"] = []interface{}{networkMap}

	completedTask := &photon.Task{
		Operation:          "GET_NETWORKS",
		State:              "COMPLETED",
		ResourceProperties: networkConnectionMap,
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/vms/"+"fake_vm_ID"+"/subnets",
		mocks.CreateResponder(200, string(taskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_vm_ID"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = listVMNetworks(cxt)
	if err != nil {
		t.Error("Not expecting error getting vm networks: " + err.Error())
	}
}
func TestImageTasks(t *testing.T) {
	taskList := MockTasksPage{
		Items: []photon.Task{
			{
				Operation: "CREATE_IMAGE",
				State:     "COMPLETED",
				ID:        "1",
				Entity:    photon.Entity{ID: "1", Kind: "image"},
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}

	response, err := json.Marshal(taskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected status")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/images/1/tasks",
		mocks.CreateResponder(200, string(response[:])))

	taskList = MockTasksPage{
		Items:            []photon.Task{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}
	response, err = json.Marshal(taskList)
	if err != nil {
		t.Error("Not expecting error serializing expected taskLists")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"1"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)
	err = getImageTasks(cxt)
	if err != nil {
		t.Error("Not expecting error retrieving tenant tasks")
	}
}
func TestSetDeploymentSecurityGroups(t *testing.T) {
	deploymentId := "deployment1"
	queuedTask := &photon.Task{
		Operation: "UPDATE_DEPLOYMENT_SECURITY_GROUPS",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: deploymentId},
	}
	completedTask := &photon.Task{
		Operation: "UPDATE_DEPLOYMENT_SECURITY_GROUPS",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: deploymentId},
	}

	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected queuedTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/deployments/"+deploymentId+"/set_security_groups",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskResponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{deploymentId, "tenant\admingroup"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, globalCtx)

	err = setDeploymentSecurityGroups(cxt)
	if err != nil {
		t.Error(err)
		t.Error("Not expecting setDeploymentSecurityGroups to fail")
	}
}
示例#11
0
func TestListVMTasks(t *testing.T) {
	taskList := MockTasksPage{
		Items: []photon.Task{
			{
				Operation: "CREATE_VM",
				State:     "COMPLETED",
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}
	response, err := json.Marshal(taskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected taskLists")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/vms/"+"fake_vm_ID"+"/tasks",
		mocks.CreateResponder(200, string(response[:])))
	taskList = MockTasksPage{
		Items:            []photon.Task{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}
	response, err = json.Marshal(taskList)
	if err != nil {
		t.Error("Not expecting error serializing expected taskLists")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_vm_ID"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = getVMTasks(cxt)
	if err != nil {
		t.Error("Not expecting error showing VM tasks: " + err.Error())
	}
}
示例#12
0
func TestGetVMMksTicket(t *testing.T) {
	completedTask := &photon.Task{
		Operation: "GET_MKS_TICKET",
		State:     "COMPLETED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	mksMap := make(map[string]interface{})
	mksMap["ticket"] = "ticket-id"

	mksTask := &photon.Task{
		State:              "COMPLETED",
		ID:                 "fake-vm-task-ID",
		Entity:             photon.Entity{ID: "fake_vm_ID"},
		ResourceProperties: mksMap,
	}

	mksresponse, err := json.Marshal(mksTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/vms/"+"fake_vm_ID"+"/mks_ticket",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+completedTask.ID,
		mocks.CreateResponder(200, string(mksresponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_vm_ID"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = getVMMksTicket(cxt)
	if err != nil {
		t.Error("Not expecting error getting vm mks ticket: " + err.Error())
	}
}
示例#13
0
func TestSetVMMetadata(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "SET_METADATA",
		State:     "QUEUED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	taskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

	completedTask := &photon.Task{
		Operation: "SET_METADATA",
		State:     "COMPLETED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/vms/"+"fake_vm_ID"+"/set_metadata",
		mocks.CreateResponder(200, string(taskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_vm_ID"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set.String("metadata", "{\"a\":\"b\", \"c\":\"d\"}", "vm metadata")

	cxt := cli.NewContext(nil, set, nil)

	err = setVMMetadata(cxt)
	if err != nil {
		t.Error("Not expecting error setting vm metadata: " + err.Error())
	}
}
示例#14
0
func TestHostGetVMs(t *testing.T) {
	vmListStruct := photon.VMs{
		Items: []photon.VM{
			{
				Name:          "fake_vm_name",
				ID:            "fake_vm_ID",
				Flavor:        "fake_vm_flavor_name",
				State:         "STOPPED",
				SourceImageID: "fake_image_ID",
				Host:          "fake_host_ip",
				Datastore:     "fake_datastore_ID",
				AttachedDisks: []photon.AttachedDisk{
					{
						Name:       "d1",
						Kind:       "ephemeral-disk",
						Flavor:     "fake_ephemeral_flavor_ID",
						CapacityGB: 0,
						BootDisk:   true,
					},
				},
			},
		},
	}

	response, err := json.Marshal(vmListStruct)
	if err != nil {
		t.Error("Not expecting error serializing host list")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/hosts/1/vms",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"1"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)
	err = listHostVMs(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting deployment list hosts to fail")
	}
}
func TestResumeSystem(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "RESUME_SYSTEM",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: "1"},
	}
	completedTask := &photon.Task{
		Operation: "RESUME_SYSTEM",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "1"},
	}

	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected queuedTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error during serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/deployments/"+queuedTask.Entity.ID+"/resume_system",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskResponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{queuedTask.Entity.ID})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = resumeSystem(cxt)
	if err != nil {
		t.Error(err)
		t.Error("Not expecting resumeSystem to fail")
	}
}
func TestGetDeployment(t *testing.T) {
	auth := &photon.AuthInfo{
		Enabled: false,
	}
	stats := &photon.StatsInfo{
		Enabled: false,
	}
	getStruct := photon.Deployment{
		ImageDatastores: []string{"testname"},
		ID:              "1",
		Auth:            auth,
		State:           "COMPLETED",
		Stats:           stats,
	}

	response, err := json.Marshal(getStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected createTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/deployments/"+getStruct.ID,
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/deployments/1/vms",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{getStruct.ID})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = showDeployment(cxt)
	if err != nil {
		t.Error("Not expecting get deployment to fail")
	}
}
示例#17
0
func TestSetHostAvailabilityZone(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "SET_AVAILABILITYZONE",
		State:     "QUEUED",
		ID:        "fake-task-id",
		Entity:    photon.Entity{ID: "fake-host-id"},
	}
	completedTask := &photon.Task{
		Operation: "SET_AVAILABILITYZONE",
		State:     "COMPLETED",
		ID:        "fake-task-id",
		Entity:    photon.Entity{ID: "fake-host-id"},
	}

	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected createTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected createTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/hosts"+"/fake-host-id"+"/set_availability_zone",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskResponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake-host-id", "fake-availability-zone-id"})
	cxt := cli.NewContext(nil, set, nil)

	err = setHostAvailabilityZone(cxt, os.Stdout)
	if err != nil {
		t.Error("Error listing hosts: " + err.Error())
	}
}
func TestSetDefaultNetwork(t *testing.T) {
	completedTask := &photon.Task{
		Operation: "SET_DEFAULT_NETWORK",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "id"},
	}

	taskresponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/subnets/"+completedTask.Entity.ID+"/set_default",
		mocks.CreateResponder(200, string(taskresponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+completedTask.ID,
		mocks.CreateResponder(200, string(taskresponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{completedTask.Entity.ID})
	cxt := cli.NewContext(nil, set, globalCtx)

	err = setDefaultNetwork(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting set default network to fail", err)
	}
}
示例#19
0
func TestGetStatus(t *testing.T) {
	// test GetStatus when config file doesn't exist
	err := cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = getStatus(cxt)
	if err == nil {
		t.Error("Expected to receive error trying to get status when config file does not exist")
	}

	// test GetStatus with mock client and mock server
	expectedStruct := photon.Status{
		Status: "READY",
		Components: []photon.Component{
			{Component: "chairman", Message: "", Status: "READY"},
			{Component: "housekeeper", Message: "", Status: "READY"},
		},
	}
	response, err := json.Marshal(expectedStruct)
	if err != nil {
		t.Error("Not expecting error serializing expected status")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/status",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	err = getStatus(cxt)
	if err != nil {
		t.Error("Not expecting error getting status of mock client")
	}
}
func TestSetSecurityGroupsForProject(t *testing.T) {
	taskId := "task1"
	projectId := "project1"
	completedTask := photon.Task{
		ID:        taskId,
		Operation: "PUSH_TENANT_SECURITY_GROUPS",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: projectId},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected status")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/projects/"+projectId+"/set_security_groups",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+taskId,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{projectId, "sg1"})
	if err != nil {
		t.Error(err)
	}
	cxt := cli.NewContext(nil, set, nil)
	err = setSecurityGroupsForProject(cxt)
	if err != nil {
		t.Error(err)
	}
}
示例#21
0
func TestShowMonitorTask(t *testing.T) {
	task := photon.Task{
		Operation: "CREATE_FLAVOR",
		State:     "COMPLETED",
		ID:        "fake-flavor-task-id",
		Entity:    photon.Entity{ID: "fake-flavor-id", Kind: "vm"},
	}
	response, err := json.Marshal(task)
	if err != nil {
		t.Error("Not expecting error serializaing expected taskLists")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/fake-flavor-id",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake-flavor-id"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = showTask(cxt)
	if err != nil {
		t.Error("Not expecting error showing task: " + err.Error())
	}
	err = monitorTask(cxt)
	if err != nil {
		t.Error("Not expecting error monitoring task: " + err.Error())
	}
}
示例#22
0
func TestShowHost(t *testing.T) {
	expectedStruct := photon.Host{
		ID:       "506b13eb-f85d-4bad-a29e-e63a1e3eb043",
		Address:  "196.128.1.1",
		Tags:     []string{"CLOUD"},
		State:    "READY",
		Metadata: map[string]string{},
	}

	response, err := json.Marshal(expectedStruct)
	if err != nil {
		log.Fatal("Not expecting error serializing expected response")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/hosts/"+expectedStruct.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{expectedStruct.ID})
	if err != nil {
		log.Fatal("Not expecting arguments parsing to fail")
	}

	cxt := cli.NewContext(nil, set, nil)
	err = showHost(cxt, os.Stdout)
	if err != nil {
		t.Error("Error showing hosts: " + err.Error())
	}
}
func TestUpdateImageDatastores(t *testing.T) {
	deploymentId := "deployment1"
	completedTask := photon.Task{
		ID:        "task1",
		Operation: "UPDATE_IMAGE_DATASTORES",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: deploymentId},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error when serializing tasks")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/deployments/"+deploymentId+"/set_image_datastores",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{deploymentId})
	if err != nil {
		t.Error(err)
	}
	set.String("datastores", "ds1,ds2", "blob")
	ctx := cli.NewContext(nil, set, nil)

	err = updateImageDatastores(ctx)
	if err != nil {
		t.Error(err)
	}
}
func TestShowFlavor(t *testing.T) {
	getStruct := photon.Flavor{
		Name: "testname",
		ID:   "1",
		Kind: "persistent-disk",
		Cost: []photon.QuotaLineItem{{Key: "k", Value: 1, Unit: "B"}},
	}

	response, err := json.Marshal(getStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected createTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/flavors/"+getStruct.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{getStruct.ID})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = showFlavor(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting get deployment to fail")
	}
}
示例#25
0
func TestFindImagesByName(t *testing.T) {
	expectedImageList := MockImagesPage{
		Items: []photon.Image{
			{
				Name:            "test",
				Size:            10,
				State:           "COMPLETED",
				ID:              "1",
				ReplicationType: "EAGER",
				Settings: []photon.ImageSetting{
					{
						Name:         "test-setting",
						DefaultValue: "test-default-value",
					},
				},
			},
			{
				Name:            "test2",
				Size:            10,
				State:           "COMPLETED",
				ID:              "2",
				ReplicationType: "EAGER",
				Settings: []photon.ImageSetting{
					{
						Name:         "test-setting",
						DefaultValue: "test-default-value",
					},
				},
			},
		},
		NextPageLink:     "fake-next-page-link",
		PreviousPageLink: "",
	}

	response, err := json.Marshal(expectedImageList)
	if err != nil {
		t.Error("Not expecting error serializaing expected response")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/images?name=test",
		mocks.CreateResponder(200, string(response[:])))

	expectedImageList = MockImagesPage{
		Items:            []photon.Image{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}

	response, err = json.Marshal(expectedImageList)
	if err != nil {
		t.Error("Not expecting error serializaing expected response")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	set.String("name", "test", "image name")
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)
	err = listImages(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting an error listing images ", err)
	}
}
示例#26
0
func TestFindVMsByName(t *testing.T) {
	vmName := "fake_vm_name"

	tenantStruct := photon.Tenants{
		Items: []photon.Tenant{
			{
				Name: "fake_tenant_name",
				ID:   "fake_tenant_ID",
			},
		},
	}
	tenantResponse, err := json.Marshal(tenantStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected tenants")
	}

	projectListStruct := photon.ProjectList{
		Items: []photon.ProjectCompact{
			{
				Name: "fake_project_name",
				ID:   "fake_project_ID",
				ResourceTicket: photon.ProjectTicket{
					Limits: []photon.QuotaLineItem{{Key: "vm.test1", Value: 1, Unit: "B"}},
					Usage:  []photon.QuotaLineItem{{Key: "vm.test1", Value: 0, Unit: "B"}},
				},
			},
		},
	}
	listProjectResponse, err := json.Marshal(projectListStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected projectLists")
	}

	vmList := MockVMsPage{
		Items: []photon.VM{
			{
				Name:          vmName,
				ID:            "fake_vm_ID",
				Flavor:        "fake_vm_flavor_name",
				State:         "STOPPED",
				SourceImageID: "fake_image_ID",
				Host:          "fake_host_ip",
				Datastore:     "fake_datastore_ID",
				AttachedDisks: []photon.AttachedDisk{
					{
						Name:       "d1",
						Kind:       "ephemeral-disk",
						Flavor:     "fake_ephemeral_flavor_ID",
						CapacityGB: 0,
						BootDisk:   true,
					},
				},
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}
	listVmResponse, err := json.Marshal(vmList)
	if err != nil {
		t.Error("Not expecting error serializaing expected vmList")
	}

	vmList = MockVMsPage{
		Items:            []photon.VM{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}
	nextVmPageResponse, err := json.Marshal(vmList)
	if err != nil {
		t.Error("Not expecting error serializaing expected vmList")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants",
		mocks.CreateResponder(200, string(tenantResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants/"+"fake_tenant_ID"+"/projects?name="+"fake_project_name",
		mocks.CreateResponder(200, string(listProjectResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/projects/"+"fake_project_ID"+"/vms?name="+vmName,
		mocks.CreateResponder(200, string(listVmResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/fake-next-page-link",
		mocks.CreateResponder(200, string(nextVmPageResponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	set.String("tenant", "fake_tenant_name", "tenant name")
	set.String("project", "fake_project_name", "project name")
	set.String("name", vmName, "VM name")
	cxt := cli.NewContext(nil, set, nil)

	err = listVMs(cxt)
	if err != nil {
		t.Error("Not expecting error listing VMs by name: " + err.Error())
	}
}
示例#27
0
func TestCreateDisk(t *testing.T) {
	tenantStruct := photon.Tenants{
		Items: []photon.Tenant{
			{
				Name: "fake_tenant_name",
				ID:   "fake_tenant_ID",
			},
		},
	}
	tenantResponse, err := json.Marshal(tenantStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected tenantStruct")
	}

	projectStruct := photon.ProjectList{
		Items: []photon.ProjectCompact{
			{
				Name: "fake_project_name",
				ID:   "fake_project_ID",
			},
		},
	}
	projectResponse, err := json.Marshal(projectStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected projectStruct")
	}

	queuedTask := &photon.Task{
		Operation: "CREATE_DISK",
		State:     "QUEUED",
		ID:        "fake-disk-task-ID",
		Entity:    photon.Entity{ID: "fake_disk_ID"},
	}
	taskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

	completedTask := &photon.Task{
		Operation: "CREATE_DISK",
		State:     "COMPLETED",
		ID:        "fake-disk-task-IDS",
		Entity:    photon.Entity{ID: "fake_disk_ID"},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

	server = mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants",
		mocks.CreateResponder(200, string(tenantResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants/"+"fake_tenant_ID"+"/projects?name="+"fake_project_name",
		mocks.CreateResponder(200, string(projectResponse[:])))
	mocks.RegisterResponder(
		"POST",
		server.URL+"/projects/"+"fake_project_ID"+"/disks",
		mocks.CreateResponder(200, string(taskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("non-interactive", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set := flag.NewFlagSet("test", 0)
	set.String("name", "fake_disk_name", "disk name")
	set.String("flavor", "fake_flavor_name", "disk flavor")
	set.Int("capacityGB", 1, "disk capacity")
	set.String("affinities", "vm:fake_vm_id", "affinities")
	set.String("tenant", "fake_tenant_name", "tenant name")
	set.String("project", "fake_project_name", "project name")
	set.String("tags", "fake_disk_tag1, fake_disk_tag2", "Tags for disk")
	cxt := cli.NewContext(nil, set, globalCtx)

	err = createDisk(cxt)
	if err != nil {
		t.Error("Not expecting error creating project: " + err.Error())
	}
}
示例#28
0
func TestFindDisksByName(t *testing.T) {
	diskName := "fake_disk_name"

	tenantStruct := photon.Tenants{
		Items: []photon.Tenant{
			{
				Name: "fake_tenant_name",
				ID:   "fake_tenant_ID",
			},
		},
	}
	tenantResponse, err := json.Marshal(tenantStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected tenantStruct")
	}

	projectStruct := photon.ProjectList{
		Items: []photon.ProjectCompact{
			{
				Name: "fake_project_name",
				ID:   "fake_project_ID",
			},
		},
	}
	projectResponse, err := json.Marshal(projectStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected projectStruct")
	}

	diskList := MockDisksPage{
		Items: []photon.PersistentDisk{
			{
				Name:       diskName,
				ID:         "fake_disk_ID",
				Flavor:     "fake_flavor_name",
				Kind:       "persistent-disk",
				CapacityGB: 1,
				State:      "DETACHED",
				Datastore:  "fake_datastore_ID",
			},
		},
		NextPageLink:     "fake-next-page-link",
		PreviousPageLink: "",
	}
	response, err := json.Marshal(diskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected diskList")
	}

	server := mocks.NewTestServer()
	defer server.Close()

	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants",
		mocks.CreateResponder(200, string(tenantResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants/"+"fake_tenant_ID"+"/projects?name="+"fake_project_name",
		mocks.CreateResponder(200, string(projectResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/projects/"+"fake_project_ID"+"/disks?name="+diskName,
		mocks.CreateResponder(200, string(response[:])))

	diskList = MockDisksPage{
		Items:            []photon.PersistentDisk{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}
	response, err = json.Marshal(diskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected diskList")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	set.String("tenant", "fake_tenant_name", "tenant name")
	set.String("project", "fake_project_name", "project name")
	set.String("name", diskName, "disk name")
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}

	cxt := cli.NewContext(nil, set, nil)
	err = listDisks(cxt)
	if err != nil {
		t.Error("Not expecting an error listing disks by name", err)
	}
}
示例#29
0
func TestListImage(t *testing.T) {
	expectedImageList := MockImagesPage{
		Items: []photon.Image{
			{
				Name:            "test",
				Size:            10,
				State:           "COMPLETED",
				ID:              "1",
				ReplicationType: "EAGER",
				Settings: []photon.ImageSetting{
					{
						Name:         "test-setting",
						DefaultValue: "test-default-value",
					},
				},
			},
		},
		NextPageLink:     "fake-next-page-link",
		PreviousPageLink: "",
	}

	response, err := json.Marshal(expectedImageList)
	if err != nil {
		t.Error("Not expecting error serializaing expected response")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/images",
		mocks.CreateResponder(200, string(response[:])))

	expectedImageList = MockImagesPage{
		Items:            []photon.Image{},
		NextPageLink:     "",
		PreviousPageLink: "",
	}

	response, err = json.Marshal(expectedImageList)
	if err != nil {
		t.Error("Not expecting error serializaing expected response")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"fake-next-page-link",
		mocks.CreateResponder(200, string(response[:])))

	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	globalFlags := flag.NewFlagSet("global-flags", flag.ContinueOnError)
	globalFlags.String("output", "json", "output")
	err = globalFlags.Parse([]string{"--output=json"})
	if err != nil {
		t.Error(err)
	}
	globalCtx := cli.NewContext(nil, globalFlags, nil)
	commandFlags := flag.NewFlagSet("command-flags", flag.ContinueOnError)
	err = commandFlags.Parse([]string{})
	if err != nil {
		t.Error(err)
	}
	cxt := cli.NewContext(nil, commandFlags, globalCtx)
	err = listImages(cxt, os.Stdout)
	if err != nil {
		t.Errorf("Not expecting an error listing images: %s", err)
	}

	// Now validate the output
	var output bytes.Buffer
	err = listImages(cxt, &output)

	// Verify it didn't fail
	if err != nil {
		t.Errorf("List images failed: %s", err)
	}

	// Verify we printed a list of images: it should start with a bracket
	err = checkRegExp(`^\s*\[`, output)
	if err != nil {
		t.Errorf("List images didn't produce a JSON list that starts with a bracket (list): %s", err)
	}
	// and end with a bracket (two regular expressions because it's multiline, it's easier)
	err = checkRegExp(`\]\s*$`, output)
	if err != nil {
		t.Errorf("List images didn't produce JSON that ended in a bracket (list): %s", err)
	}
	// And spot check that we have the "id" field
	err = checkRegExp(`\"id\":\s*\".*\"`, output)
	if err != nil {
		t.Errorf("List images didn't produce a JSON field named 'id': %s", err)
	}
}
示例#30
0
func TestCreateDeleteImage(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "CREATE_IMAGE",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: "1"},
	}
	completedTask := &photon.Task{
		Operation: "CREATE_IMAGE",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "1"},
	}

	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}
	taskresponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected createTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/images",
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskresponse[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	set := flag.NewFlagSet("test", 0)
	set.String("name", "n", "testname")
	set.String("image_replication", "EAGER", "image replication")
	err = set.Parse([]string{"../../testdata/tty_tiny.ova"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = createImage(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error creating image: " + err.Error())
	}

	queuedTask = &photon.Task{
		Operation: "DELETE_IMAGE",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: "1"},
	}
	completedTask = &photon.Task{
		Operation: "DELETE_IMAGE",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "1"},
	}

	response, err = json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}
	taskresponse, err = json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected deleteTask")
	}

	mocks.RegisterResponder(
		"DELETE",
		server.URL+"/images/"+queuedTask.Entity.ID,
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(taskresponse[:])))

	globalSet := flag.NewFlagSet("global", 0)
	globalSet.Bool("non-interactive", true, "doc")
	err = globalSet.Parse([]string{"--non-interactive"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	globalCtx := cli.NewContext(nil, globalSet, nil)
	set = flag.NewFlagSet("test", 0)
	err = set.Parse([]string{queuedTask.Entity.ID})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}

	cxt = cli.NewContext(nil, set, globalCtx)
	err = deleteImage(cxt)
	if err != nil {
		t.Error("Not expecting error deleting image: " + err.Error())
	}
}