|
| 1 | +// Copyright (c) 2016 VMware, Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// This product is licensed to you under the Apache License, Version 2.0 (the "License"). |
| 4 | +// You may not use this product except in compliance with the License. |
| 5 | +// |
| 6 | +// This product may include a number of subcomponents with separate copyright notices and |
| 7 | +// license terms. Your use of these subcomponents is subject to the terms and conditions |
| 8 | +// of the subcomponent's license, as noted in the LICENSE file. |
| 9 | + |
| 10 | +package command |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/json" |
| 14 | + "flag" |
| 15 | + "net/http" |
| 16 | + "os" |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/vmware/photon-controller-cli/photon/client" |
| 20 | + "github.com/vmware/photon-controller-cli/photon/mocks" |
| 21 | + |
| 22 | + "github.com/urfave/cli" |
| 23 | + "github.com/vmware/photon-controller-go-sdk/photon" |
| 24 | +) |
| 25 | + |
| 26 | +type MockHostsPage struct { |
| 27 | + Items []photon.Host `json:"items"` |
| 28 | + NextPageLink string `json:"nextPageLink"` |
| 29 | + PreviousPageLink string `json:"previousPageLink"` |
| 30 | +} |
| 31 | + |
| 32 | +func TestListInfrastructureHosts(t *testing.T) { |
| 33 | + hostList := MockHostsPage{ |
| 34 | + Items: []photon.Host{ |
| 35 | + { |
| 36 | + Username: "u", |
| 37 | + Password: "p", |
| 38 | + Address: "testIP", |
| 39 | + Tags: []string{"CLOUD"}, |
| 40 | + ID: "host-test-id", |
| 41 | + State: "COMPLETED", |
| 42 | + }, |
| 43 | + }, |
| 44 | + NextPageLink: "/fake-next-page-link", |
| 45 | + PreviousPageLink: "", |
| 46 | + } |
| 47 | + |
| 48 | + response, err := json.Marshal(hostList) |
| 49 | + if err != nil { |
| 50 | + t.Error("Not expecting error serializing host list") |
| 51 | + } |
| 52 | + |
| 53 | + server := mocks.NewTestServer() |
| 54 | + mocks.RegisterResponder( |
| 55 | + "GET", |
| 56 | + server.URL+rootUrl+"/infrastructure/hosts", |
| 57 | + mocks.CreateResponder(200, string(response[:]))) |
| 58 | + |
| 59 | + hostList = MockHostsPage{ |
| 60 | + Items: []photon.Host{}, |
| 61 | + NextPageLink: "", |
| 62 | + PreviousPageLink: "", |
| 63 | + } |
| 64 | + response, err = json.Marshal(hostList) |
| 65 | + if err != nil { |
| 66 | + t.Error("Not expecting error serializing expected taskLists") |
| 67 | + } |
| 68 | + |
| 69 | + mocks.RegisterResponder( |
| 70 | + "GET", |
| 71 | + server.URL+"/fake-next-page-link", |
| 72 | + mocks.CreateResponder(200, string(response[:]))) |
| 73 | + |
| 74 | + defer server.Close() |
| 75 | + |
| 76 | + mocks.Activate(true) |
| 77 | + httpClient := &http.Client{Transport: mocks.DefaultMockTransport} |
| 78 | + client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient) |
| 79 | + |
| 80 | + set := flag.NewFlagSet("test", 0) |
| 81 | + err = set.Parse([]string{"1"}) |
| 82 | + if err != nil { |
| 83 | + t.Error("Not expecting arguments parsing to fail") |
| 84 | + } |
| 85 | + cxt := cli.NewContext(nil, set, nil) |
| 86 | + err = listInfrastructureHosts(cxt, os.Stdout) |
| 87 | + if err != nil { |
| 88 | + t.Error("Not expecting deployment list hosts to fail") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestUpdateInfrastructureImageDatastores(t *testing.T) { |
| 93 | + deploymentId := "deployment1" |
| 94 | + queuedTask := &photon.Task{ |
| 95 | + Operation: "UPDATE_IMAGE_DATASTORES", |
| 96 | + State: "QUEUED", |
| 97 | + Entity: photon.Entity{ID: "1"}, |
| 98 | + } |
| 99 | + completedTask := photon.Task{ |
| 100 | + ID: "task1", |
| 101 | + Operation: "UPDATE_IMAGE_DATASTORES", |
| 102 | + State: "COMPLETED", |
| 103 | + Entity: photon.Entity{ID: deploymentId}, |
| 104 | + } |
| 105 | + response, err := json.Marshal(queuedTask) |
| 106 | + if err != nil { |
| 107 | + t.Error("Not expecting error when serializing tasks") |
| 108 | + } |
| 109 | + taskResponse, err := json.Marshal(completedTask) |
| 110 | + if err != nil { |
| 111 | + t.Error("Not expecting error during serializing expected completedTask") |
| 112 | + } |
| 113 | + |
| 114 | + server := mocks.NewTestServer() |
| 115 | + mocks.RegisterResponder( |
| 116 | + "POST", |
| 117 | + server.URL+rootUrl+"/infrastructure/image-datastores", |
| 118 | + mocks.CreateResponder(200, string(response[:]))) |
| 119 | + mocks.RegisterResponder( |
| 120 | + "GET", |
| 121 | + server.URL+rootUrl+"/tasks/"+queuedTask.ID, |
| 122 | + mocks.CreateResponder(200, string(taskResponse[:]))) |
| 123 | + defer server.Close() |
| 124 | + |
| 125 | + mocks.Activate(true) |
| 126 | + httpClient := &http.Client{Transport: mocks.DefaultMockTransport} |
| 127 | + client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient) |
| 128 | + |
| 129 | + set := flag.NewFlagSet("test", 0) |
| 130 | + err = set.Parse([]string{deploymentId}) |
| 131 | + if err != nil { |
| 132 | + t.Error(err) |
| 133 | + } |
| 134 | + set.String("datastores", "ds1,ds2", "blob") |
| 135 | + ctx := cli.NewContext(nil, set, nil) |
| 136 | + |
| 137 | + err = updateInfrastructureImageDatastores(ctx) |
| 138 | + if err != nil { |
| 139 | + t.Error(err) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestSyncInfrastructureHostsConfig(t *testing.T) { |
| 144 | + queuedTask := &photon.Task{ |
| 145 | + Operation: "SYNC_HOSTS_CONFIG", |
| 146 | + State: "QUEUED", |
| 147 | + Entity: photon.Entity{ID: "1"}, |
| 148 | + } |
| 149 | + completedTask := &photon.Task{ |
| 150 | + Operation: "SYNC_HOSTS_CONFIG", |
| 151 | + State: "COMPLETED", |
| 152 | + Entity: photon.Entity{ID: "1"}, |
| 153 | + } |
| 154 | + |
| 155 | + response, err := json.Marshal(queuedTask) |
| 156 | + if err != nil { |
| 157 | + t.Error("Not expecting error during serializing expected queuedTask") |
| 158 | + } |
| 159 | + taskResponse, err := json.Marshal(completedTask) |
| 160 | + if err != nil { |
| 161 | + t.Error("Not expecting error during serializing expected completedTask") |
| 162 | + } |
| 163 | + |
| 164 | + server := mocks.NewTestServer() |
| 165 | + mocks.RegisterResponder( |
| 166 | + "POST", |
| 167 | + server.URL+rootUrl+"/infrastructure/sync-hosts-config", |
| 168 | + mocks.CreateResponder(200, string(response[:]))) |
| 169 | + mocks.RegisterResponder( |
| 170 | + "GET", |
| 171 | + server.URL+rootUrl+"/tasks/"+queuedTask.ID, |
| 172 | + mocks.CreateResponder(200, string(taskResponse[:]))) |
| 173 | + defer server.Close() |
| 174 | + |
| 175 | + mocks.Activate(true) |
| 176 | + httpClient := &http.Client{Transport: mocks.DefaultMockTransport} |
| 177 | + client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient) |
| 178 | + |
| 179 | + set := flag.NewFlagSet("test", 0) |
| 180 | + err = set.Parse([]string{queuedTask.Entity.ID}) |
| 181 | + if err != nil { |
| 182 | + t.Error("Not expecting arguments parsing to fail") |
| 183 | + } |
| 184 | + cxt := cli.NewContext(nil, set, nil) |
| 185 | + |
| 186 | + err = syncInfrastructureHostsConfig(cxt) |
| 187 | + if err != nil { |
| 188 | + t.Error(err) |
| 189 | + t.Error("Not expecting syncHostsConfig to fail") |
| 190 | + } |
| 191 | +} |
0 commit comments