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")
	}
}
Example #2
0
func TestAcquireFloatingIp(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "ACQUIRE_FLOATING_IP",
		State:     "QUEUED",
		ID:        "fake-vm-task-ID",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	completedTask := &photon.Task{
		Operation: "ACQUIRE_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 serializing expected queuedTask")
	}
	taskResponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/vms/"+"fake_vm_ID"+"/acquire_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 = acquireFloatingIp(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error creating VM image: " + 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 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, os.Stdout)
	if err != nil {
		t.Error("Not expecting deployment list hosts to fail")
	}
}
Example #5
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, os.Stdout)
	if err != nil {
		t.Error("Not expecting error getting vm networks: " + err.Error())
	}
}
func TestFlavorTasks(t *testing.T) {
	taskList := MockTasksPage{
		Items: []photon.Task{
			{
				Operation: "CREATE_FLAVOR",
				State:     "COMPLETED",
				ID:        "1",
				Entity:    photon.Entity{ID: "fake-id", Kind: "flavor"},
			},
		},
		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+"/flavors/fake-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-id"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)
	err = getFlavorTasks(cxt, os.Stdout)
	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")
	}
}
func TestSetDefaultVirtualNetwork(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")
	}

	info := &photon.Info{
		NetworkType: SOFTWARE_DEFINED,
	}

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

	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[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/info",
		mocks.CreateResponder(200, string(infoString[:])))
	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)
	}
}
func TestCreateVirtualSubnet(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "CREATE_NETWORK",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: "network-ID"},
	}
	completedTask := &photon.Task{
		Operation: "CREATE_NETWORK",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "network-ID"},
	}
	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected queuedTask")
	}
	taskresponse, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializing expected completedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"POST",
		server.URL+"/projects/project_id/subnets",
		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)
	set.String("name", "network_name", "network name")
	set.String("routingType", "ROUTED", "routing type")
	set.Int("size", 256, "network size")
	set.Int("staticIpSize", 0, "reserved static ip size")
	set.String("projectId", "project_id", "project ID")

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

	err = createVirtualNetwork(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting create network to fail", err)
	}
}
Example #10
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, os.Stdout)
	if err != nil {
		t.Error("Not expecting error getting vm mks ticket: " + err.Error())
	}
}
Example #11
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, os.Stdout)
	if err != nil {
		t.Error("Not expecting error setting vm metadata: " + err.Error())
	}
}
func TestListResourceTicketTasks(t *testing.T) {
	taskList := MockTasksPage{
		Items: []photon.Task{
			{
				Operation: "CREATE_RESOURCE_TICKET",
				State:     "COMPLETED",
				ID:        "fake-rt-task-id",
				Entity:    photon.Entity{ID: rtID},
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}
	response, err := json.Marshal(taskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected taskLists")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/resource-tickets/"+rtID+"/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[:])))

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

	err = getResourceTicketTasks(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing resource ticket tasks: " + err.Error())
	}
}
Example #13
0
func TestListDisks(t *testing.T) {
	diskList := MockDisksPage{
		Items: []photon.PersistentDisk{
			{
				Name:       "fake_disk_name",
				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: "",
	}
	listResponse, err := json.Marshal(diskList)
	if err != nil {
		t.Error("Not expecting error serializaing expected disksList")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/projects/"+"fake_project_ID"+"/disks",
		mocks.CreateResponder(200, string(listResponse[:])))

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

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

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

	err = listDisks(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error listing disks: " + err.Error())
	}
}
func TestListProjectTasks(t *testing.T) {
	taskList := MockTasksPage{
		Items: []photon.Task{
			{
				Operation: "CREATE_PROJECT",
				State:     "COMPLETED",
				ID:        "fake_project_task_id",
				Entity:    photon.Entity{ID: "fake_project_ID", Kind: "project"},
			},
		},
		NextPageLink:     "/fake-next-page-link",
		PreviousPageLink: "",
	}

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

	mocks.RegisterResponder(
		"GET",
		server.URL+"/projects/"+"fake_project_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[:])))

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

	err = getProjectTasks(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing project tasks: " + err.Error())
	}
}
func TestUpdateImageDatastores(t *testing.T) {
	deploymentId := "deployment1"
	queuedTask := &photon.Task{
		Operation: "UPDATE_IMAGE_DATASTORES",
		State:     "QUEUED",
		Entity:    photon.Entity{ID: "1"},
	}
	completedTask := photon.Task{
		ID:        "task1",
		Operation: "UPDATE_IMAGE_DATASTORES",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: deploymentId},
	}
	response, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error when serializing tasks")
	}
	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_image_datastores",
		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{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 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, os.Stdout)
	if err != nil {
		t.Error("Not expecting get deployment to fail")
	}
}
Example #18
0
func mockDeploymentsForList(t *testing.T, server *httptest.Server, numDeployments int) error {
	var deployments []photon.Deployment

	for i := 0; i < numDeployments; i++ {
		deployment := photon.Deployment{
			ID: strconv.Itoa(i),
		}
		deployments = append(deployments, deployment)
	}

	expectedStruct := photon.Deployments{
		Items: deployments,
	}

	response, err := json.Marshal(expectedStruct)
	if err != nil {
		return err
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/deployments",
		mocks.CreateResponder(200, string(response[:])))
	return nil
}
func TestShowTenant(t *testing.T) {
	tenantStruct := &photon.Tenant{
		Name: "fake_tenant_name",
		ID:   "fake_tenant_ID",
	}
	response, err := json.Marshal(tenantStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected tenant")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants"+"/"+"fake_tenant_ID",
		mocks.CreateResponder(200, string(response[:])))

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

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

	err = showTenant(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing tenant: " + err.Error())
	}
}
Example #20
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 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 TestShowProject(t *testing.T) {
	projectStruct := photon.ProjectCompact{
		Name: "fake_project_name",
		ID:   "fake_project_ID",
		ResourceTicket: photon.ProjectTicket{
			TenantTicketID:   "fake_ticket_id",
			TenantTicketName: "fake_ticket_name",
			Limits:           []photon.QuotaLineItem{{Key: "vm.test1", Value: 1, Unit: "B"}},
			Usage:            []photon.QuotaLineItem{{Key: "vm.test1", Value: 0, Unit: "B"}},
		},
	}
	response, err := json.Marshal(projectStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected project")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/projects/"+"fake_project_ID",
		mocks.CreateResponder(200, string(response[:])))

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

	err = showProject(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing project: " + err.Error())
	}
}
func TestShowPhysicalNetwork(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 = showPhysicalNetwork(cxt, os.Stdout)
	if err != nil {
		t.Error("Error showing networks: " + err.Error())
	}
}
Example #24
0
func TestShowDisk(t *testing.T) {
	diskStruct := photon.PersistentDisk{
		Name:       "fake_disk_name",
		ID:         "fake_disk_ID",
		Flavor:     "fake_flavor_name",
		Kind:       "persistent-disk",
		CapacityGB: 1,
		State:      "DETACHED",
		Datastore:  "fake_datastore_ID",
		Tags:       []string{"fake_disk_tag1", "fake_disk_tag2"},
		VMs:        []string{"fake_vm_id"},
	}
	response, err := json.Marshal(diskStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected disk")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/disks/"+"fake_disk_ID",
		mocks.CreateResponder(200, string(response[:])))

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

	err = showDisk(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing disk: " + err.Error())
	}
}
Example #25
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 TestDeleteProject(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "DELETE_PROJECT",
		State:     "QUEUED",
		ID:        "fake-project-task-id",
		Entity:    photon.Entity{ID: "fake_project_ID"},
	}
	taskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

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

	mocks.RegisterResponder(
		"DELETE",
		server.URL+"/projects/"+"fake_project_ID",
		mocks.CreateResponder(200, string(taskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+queuedTask.ID,
		mocks.CreateResponder(200, string(response[:])))

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

	cxt := cli.NewContext(nil, set, nil)
	err = deleteProject(cxt)
	if err != nil {
		t.Error("Not expecting error deleting project: " + err.Error())
	}
}
Example #27
0
func TestSuspendVM(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "SUSPEND_VM",
		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: "SUSPEND_VM",
		State:     "COMPLETED",
		ID:        "fake-vm-task-IDS",
		Entity:    photon.Entity{ID: "fake_vm_ID"},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected completedTask")
	}

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

	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 = suspendVM(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error suspending VM: " + err.Error())
	}
}
Example #28
0
func TestDetachDisk(t *testing.T) {
	queuedTask := &photon.Task{
		Operation: "DETACH_DISK",
		State:     "QUEUED",
		ID:        "fake-vm-task-ID",
	}
	taskResponse, err := json.Marshal(queuedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

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

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

	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("disk", "fake_disk_ID", "detach disk")
	cxt := cli.NewContext(nil, set, nil)

	err = detachDisk(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error detaching disk: " + err.Error())
	}
}
func TestClusterTriggerMaintenance(t *testing.T) {
	// Start mock server
	server := mocks.NewTestServer()
	defer server.Close()

	// Create mock response
	completedTask := &photon.Task{
		Operation: "TRIGGER_CLUSTER_MAINTENANCE",
		State:     "COMPLETED",
		ID:        "fake_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")
	}

	// Register mock response with mock server
	mocks.RegisterResponder(
		"POST",
		server.URL+"/clusters/fake_cluster_id/trigger_maintenance",
		mocks.CreateResponder(200, string(completedTaskResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/fake_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)

	commandFlags := flag.NewFlagSet("command-flags", flag.ContinueOnError)
	err = commandFlags.Parse([]string{"fake_cluster_id"})
	if err != nil {
		t.Error(err)
	}
	ctx := cli.NewContext(nil, commandFlags, nil)

	err = triggerMaintenance(ctx)
	if err != nil {
		t.Error("Not expecting error for cluster trigger maintenance: " + err.Error())
	}
}
func TestClusterCertToFile(t *testing.T) {
	cluster := &photon.Cluster{
		Name:        "fake_cluster_name",
		State:       "ERROR",
		ID:          "fake_cluster_id",
		Type:        "KUBERNETES",
		WorkerCount: 50,
		ExtendedProperties: map[string]string{
			photon.ExtendedPropertyRegistryCACert: "-----BEGIN CERTIFICATE-----\nMIIFmzCCA4OgAwIBAgIJAIAZmLcInJMeMA0GCSqGSIb3DQEBCwUAMGQxCzAJBgNV\n-----END CERTIFICATE-----",
		},
	}
	clusterResponse, err := json.Marshal(cluster)
	if err != nil {
		t.Error("Not expecting error serializing expected cluster")
	}

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

	mocks.RegisterResponder(
		"GET",
		server.URL+"/clusters/"+cluster.ID,
		mocks.CreateResponder(200, string(clusterResponse[:])))

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

	commandFlags := flag.NewFlagSet("command-flags", flag.ContinueOnError)
	err = commandFlags.Parse([]string{"fake_cluster_id", "test.cert"})
	if err != nil {
		t.Error(err)
	}
	ctx := cli.NewContext(nil, commandFlags, nil)

	err = certToFile(ctx)
	if err != nil {
		t.Error("Not expecting error for cluster trigger maintenance: " + err.Error())
	}

	content, err := readCACert("test.cert")

	if err != nil {
		t.Error("ReadCACert function failed" + err.Error())
	}
	expected := "-----BEGIN CERTIFICATE-----\nMIIFmzCCA4OgAwIBAgIJAIAZmLcInJMeMA0GCSqGSIb3DQEBCwUAMGQxCzAJBgNV\n-----END CERTIFICATE-----"
	if strings.Compare(content, expected) != 0 {
		t.Error("expected CACert :" + expected + " actual CACert read:" + content)
	}

	err = os.Remove("test.cert")

	if err != nil {
		t.Error("Error deleting test.cert file")
	}
}