ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 1 | package v2 |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/gophercloud/gophercloud/acceptance/clients" |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharenetworks" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
ehdou | fd6d86c | 2016-11-03 21:35:45 +0200 | [diff] [blame] | 9 | th "github.com/gophercloud/gophercloud/testhelper" |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 10 | ) |
| 11 | |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 12 | func TestShareNetworkCreateDestroy(t *testing.T) { |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 13 | client, err := clients.NewSharedFileSystemV2Client() |
| 14 | if err != nil { |
| 15 | t.Fatalf("Unable to create shared file system client: %v", err) |
| 16 | } |
| 17 | |
| 18 | shareNetwork, err := CreateShareNetwork(t, client) |
| 19 | if err != nil { |
| 20 | t.Fatalf("Unable to create share network: %v", err) |
| 21 | } |
| 22 | |
ehdou | a088cec | 2016-10-25 21:02:05 +0300 | [diff] [blame] | 23 | newShareNetwork, err := sharenetworks.Get(client, shareNetwork.ID).Extract() |
| 24 | if err != nil { |
| 25 | t.Errorf("Unable to retrieve shareNetwork: %v", err) |
| 26 | } |
| 27 | |
| 28 | if newShareNetwork.Name != shareNetwork.Name { |
| 29 | t.Fatalf("Share network name was expeted to be: %s", shareNetwork.Name) |
| 30 | } |
| 31 | |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 32 | PrintShareNetwork(t, shareNetwork) |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 33 | |
| 34 | defer DeleteShareNetwork(t, client, shareNetwork) |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 35 | } |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 36 | |
ehdou | fd6d86c | 2016-11-03 21:35:45 +0200 | [diff] [blame] | 37 | // Create a share network and update the name and description. Get the share |
| 38 | // network and verify that the name and description have been updated |
| 39 | func TestShareNetworkUpdate(t *testing.T) { |
| 40 | client, err := clients.NewSharedFileSystemV2Client() |
| 41 | if err != nil { |
| 42 | t.Fatalf("Unable to create shared file system client: %v", err) |
| 43 | } |
| 44 | |
| 45 | shareNetwork, err := CreateShareNetwork(t, client) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Unable to create share network: %v", err) |
| 48 | } |
| 49 | |
| 50 | expectedShareNetwork, err := sharenetworks.Get(client, shareNetwork.ID).Extract() |
| 51 | if err != nil { |
| 52 | t.Errorf("Unable to retrieve shareNetwork: %v", err) |
| 53 | } |
| 54 | |
| 55 | options := sharenetworks.UpdateOpts{ |
| 56 | Name: "NewName", |
| 57 | Description: "New share network description", |
| 58 | NovaNetID: "New_nova_network_id", |
| 59 | } |
| 60 | |
| 61 | expectedShareNetwork.Name = options.Name |
| 62 | expectedShareNetwork.Description = options.Description |
| 63 | expectedShareNetwork.NovaNetID = options.NovaNetID |
| 64 | |
| 65 | _, err = sharenetworks.Update(client, shareNetwork.ID, options).Extract() |
| 66 | if err != nil { |
| 67 | t.Errorf("Unable to update shareNetwork: %v", err) |
| 68 | } |
| 69 | |
| 70 | updatedShareNetwork, err := sharenetworks.Get(client, shareNetwork.ID).Extract() |
| 71 | if err != nil { |
| 72 | t.Errorf("Unable to retrieve shareNetwork: %v", err) |
| 73 | } |
| 74 | |
| 75 | // Update time has to be set in order to get the assert equal to pass |
| 76 | expectedShareNetwork.UpdatedAt = updatedShareNetwork.UpdatedAt |
| 77 | |
| 78 | th.CheckDeepEquals(t, expectedShareNetwork, updatedShareNetwork) |
| 79 | |
| 80 | PrintShareNetwork(t, shareNetwork) |
| 81 | |
| 82 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 83 | } |
| 84 | |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 85 | func TestShareNetworkListDetail(t *testing.T) { |
| 86 | client, err := clients.NewSharedFileSystemV2Client() |
| 87 | if err != nil { |
| 88 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 89 | } |
| 90 | |
| 91 | allPages, err := sharenetworks.ListDetail(client, sharenetworks.ListOpts{}).AllPages() |
| 92 | if err != nil { |
| 93 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 94 | } |
| 95 | |
| 96 | allShareNetworks, err := sharenetworks.ExtractShareNetworks(allPages) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Unable to extract share networks: %v", err) |
| 99 | } |
| 100 | |
| 101 | for _, shareNetwork := range allShareNetworks { |
| 102 | PrintShareNetwork(t, &shareNetwork) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // The test creates 2 shared networks and verifies that only the one(s) with |
| 107 | // a particular name are being listed |
| 108 | func TestShareNetworkListFiltering(t *testing.T) { |
| 109 | client, err := clients.NewSharedFileSystemV2Client() |
| 110 | if err != nil { |
| 111 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 112 | } |
| 113 | |
| 114 | shareNetwork, err := CreateShareNetwork(t, client) |
| 115 | if err != nil { |
| 116 | t.Fatalf("Unable to create share network: %v", err) |
| 117 | } |
| 118 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 119 | |
| 120 | shareNetwork, err = CreateShareNetwork(t, client) |
| 121 | if err != nil { |
| 122 | t.Fatalf("Unable to create share network: %v", err) |
| 123 | } |
| 124 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 125 | |
| 126 | options := sharenetworks.ListOpts{ |
| 127 | Name: shareNetwork.Name, |
| 128 | } |
| 129 | |
| 130 | allPages, err := sharenetworks.ListDetail(client, options).AllPages() |
| 131 | if err != nil { |
| 132 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 133 | } |
| 134 | |
| 135 | allShareNetworks, err := sharenetworks.ExtractShareNetworks(allPages) |
| 136 | if err != nil { |
| 137 | t.Fatalf("Unable to extract share networks: %v", err) |
| 138 | } |
| 139 | |
| 140 | for _, listedShareNetwork := range allShareNetworks { |
| 141 | if listedShareNetwork.Name != shareNetwork.Name { |
| 142 | t.Fatalf("The name of the share network was expected to be %s", shareNetwork.Name) |
| 143 | } |
| 144 | PrintShareNetwork(t, &listedShareNetwork) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestShareNetworkListPagination(t *testing.T) { |
| 149 | client, err := clients.NewSharedFileSystemV2Client() |
| 150 | if err != nil { |
| 151 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 152 | } |
| 153 | |
| 154 | shareNetwork, err := CreateShareNetwork(t, client) |
| 155 | if err != nil { |
| 156 | t.Fatalf("Unable to create share network: %v", err) |
| 157 | } |
| 158 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 159 | |
| 160 | shareNetwork, err = CreateShareNetwork(t, client) |
| 161 | if err != nil { |
| 162 | t.Fatalf("Unable to create share network: %v", err) |
| 163 | } |
| 164 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 165 | |
| 166 | count := 0 |
| 167 | |
| 168 | err = sharenetworks.ListDetail(client, sharenetworks.ListOpts{Offset: 0, Limit: 1}).EachPage(func(page pagination.Page) (bool, error) { |
| 169 | count++ |
| 170 | _, err := sharenetworks.ExtractShareNetworks(page) |
| 171 | if err != nil { |
| 172 | t.Fatalf("Failed to extract share networks: %v", err) |
| 173 | return false, err |
| 174 | } |
| 175 | |
| 176 | return true, nil |
| 177 | }) |
| 178 | if err != nil { |
| 179 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 180 | } |
| 181 | |
| 182 | if count < 2 { |
| 183 | t.Fatal("Expected to get at least 2 pages") |
| 184 | } |
| 185 | |
| 186 | } |