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 | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 9 | ) |
| 10 | |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 11 | func TestShareNetworkCreateDestroy(t *testing.T) { |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 12 | client, err := clients.NewSharedFileSystemV2Client() |
| 13 | if err != nil { |
| 14 | t.Fatalf("Unable to create shared file system client: %v", err) |
| 15 | } |
| 16 | |
| 17 | shareNetwork, err := CreateShareNetwork(t, client) |
| 18 | if err != nil { |
| 19 | t.Fatalf("Unable to create share network: %v", err) |
| 20 | } |
| 21 | |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 22 | PrintShareNetwork(t, shareNetwork) |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 23 | |
| 24 | defer DeleteShareNetwork(t, client, shareNetwork) |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 25 | } |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 26 | |
| 27 | func TestShareNetworkListDetail(t *testing.T) { |
| 28 | client, err := clients.NewSharedFileSystemV2Client() |
| 29 | if err != nil { |
| 30 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 31 | } |
| 32 | |
| 33 | allPages, err := sharenetworks.ListDetail(client, sharenetworks.ListOpts{}).AllPages() |
| 34 | if err != nil { |
| 35 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 36 | } |
| 37 | |
| 38 | allShareNetworks, err := sharenetworks.ExtractShareNetworks(allPages) |
| 39 | if err != nil { |
| 40 | t.Fatalf("Unable to extract share networks: %v", err) |
| 41 | } |
| 42 | |
| 43 | for _, shareNetwork := range allShareNetworks { |
| 44 | PrintShareNetwork(t, &shareNetwork) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // The test creates 2 shared networks and verifies that only the one(s) with |
| 49 | // a particular name are being listed |
| 50 | func TestShareNetworkListFiltering(t *testing.T) { |
| 51 | client, err := clients.NewSharedFileSystemV2Client() |
| 52 | if err != nil { |
| 53 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 54 | } |
| 55 | |
| 56 | shareNetwork, err := CreateShareNetwork(t, client) |
| 57 | if err != nil { |
| 58 | t.Fatalf("Unable to create share network: %v", err) |
| 59 | } |
| 60 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 61 | |
| 62 | shareNetwork, err = CreateShareNetwork(t, client) |
| 63 | if err != nil { |
| 64 | t.Fatalf("Unable to create share network: %v", err) |
| 65 | } |
| 66 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 67 | |
| 68 | options := sharenetworks.ListOpts{ |
| 69 | Name: shareNetwork.Name, |
| 70 | } |
| 71 | |
| 72 | allPages, err := sharenetworks.ListDetail(client, options).AllPages() |
| 73 | if err != nil { |
| 74 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 75 | } |
| 76 | |
| 77 | allShareNetworks, err := sharenetworks.ExtractShareNetworks(allPages) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Unable to extract share networks: %v", err) |
| 80 | } |
| 81 | |
| 82 | for _, listedShareNetwork := range allShareNetworks { |
| 83 | if listedShareNetwork.Name != shareNetwork.Name { |
| 84 | t.Fatalf("The name of the share network was expected to be %s", shareNetwork.Name) |
| 85 | } |
| 86 | PrintShareNetwork(t, &listedShareNetwork) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestShareNetworkListPagination(t *testing.T) { |
| 91 | client, err := clients.NewSharedFileSystemV2Client() |
| 92 | if err != nil { |
| 93 | t.Fatalf("Unable to create a shared file system client: %v", err) |
| 94 | } |
| 95 | |
| 96 | shareNetwork, err := CreateShareNetwork(t, client) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Unable to create share network: %v", err) |
| 99 | } |
| 100 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 101 | |
| 102 | shareNetwork, err = CreateShareNetwork(t, client) |
| 103 | if err != nil { |
| 104 | t.Fatalf("Unable to create share network: %v", err) |
| 105 | } |
| 106 | defer DeleteShareNetwork(t, client, shareNetwork) |
| 107 | |
| 108 | count := 0 |
| 109 | |
| 110 | err = sharenetworks.ListDetail(client, sharenetworks.ListOpts{Offset: 0, Limit: 1}).EachPage(func(page pagination.Page) (bool, error) { |
| 111 | count++ |
| 112 | _, err := sharenetworks.ExtractShareNetworks(page) |
| 113 | if err != nil { |
| 114 | t.Fatalf("Failed to extract share networks: %v", err) |
| 115 | return false, err |
| 116 | } |
| 117 | |
| 118 | return true, nil |
| 119 | }) |
| 120 | if err != nil { |
| 121 | t.Fatalf("Unable to retrieve share networks: %v", err) |
| 122 | } |
| 123 | |
| 124 | if count < 2 { |
| 125 | t.Fatal("Expected to get at least 2 pages") |
| 126 | } |
| 127 | |
| 128 | } |