github.com/gophercloud/gophercloud@v1.11.0/openstack/testing/endpoint_location_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/openstack"
     8  	tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
     9  	tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  )
    12  
    13  // Service catalog fixtures take too much vertical space!
    14  var catalog2 = tokens2.ServiceCatalog{
    15  	Entries: []tokens2.CatalogEntry{
    16  		{
    17  			Type: "same",
    18  			Name: "same",
    19  			Endpoints: []tokens2.Endpoint{
    20  				{
    21  					Region:      "same",
    22  					PublicURL:   "https://public.correct.com/",
    23  					InternalURL: "https://internal.correct.com/",
    24  					AdminURL:    "https://admin.correct.com/",
    25  				},
    26  				{
    27  					Region:    "different",
    28  					PublicURL: "https://badregion.com/",
    29  				},
    30  			},
    31  		},
    32  		{
    33  			Type: "same",
    34  			Name: "different",
    35  			Endpoints: []tokens2.Endpoint{
    36  				{
    37  					Region:    "same",
    38  					PublicURL: "https://badname.com/",
    39  				},
    40  				{
    41  					Region:    "different",
    42  					PublicURL: "https://badname.com/+badregion",
    43  				},
    44  			},
    45  		},
    46  		{
    47  			Type: "different",
    48  			Name: "different",
    49  			Endpoints: []tokens2.Endpoint{
    50  				{
    51  					Region:    "same",
    52  					PublicURL: "https://badtype.com/+badname",
    53  				},
    54  				{
    55  					Region:    "different",
    56  					PublicURL: "https://badtype.com/+badregion+badname",
    57  				},
    58  			},
    59  		},
    60  	},
    61  }
    62  
    63  func TestV2EndpointExact(t *testing.T) {
    64  	expectedURLs := map[gophercloud.Availability]string{
    65  		gophercloud.AvailabilityPublic:   "https://public.correct.com/",
    66  		gophercloud.AvailabilityAdmin:    "https://admin.correct.com/",
    67  		gophercloud.AvailabilityInternal: "https://internal.correct.com/",
    68  	}
    69  
    70  	for availability, expected := range expectedURLs {
    71  		actual, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
    72  			Type:         "same",
    73  			Name:         "same",
    74  			Region:       "same",
    75  			Availability: availability,
    76  		})
    77  		th.AssertNoErr(t, err)
    78  		th.CheckEquals(t, expected, actual)
    79  	}
    80  }
    81  
    82  func TestV2EndpointNone(t *testing.T) {
    83  	_, actual := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
    84  		Type:         "nope",
    85  		Availability: gophercloud.AvailabilityPublic,
    86  	})
    87  	expected := &gophercloud.ErrEndpointNotFound{}
    88  	th.CheckEquals(t, expected.Error(), actual.Error())
    89  }
    90  
    91  func TestV2EndpointMultiple(t *testing.T) {
    92  	actual, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
    93  		Type:         "same",
    94  		Region:       "same",
    95  		Availability: gophercloud.AvailabilityPublic,
    96  	})
    97  
    98  	th.AssertNoErr(t, err)
    99  	th.AssertEquals(t, "https://public.correct.com/", actual)
   100  }
   101  
   102  func TestV2EndpointBadAvailability(t *testing.T) {
   103  	_, err := openstack.V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
   104  		Type:         "same",
   105  		Name:         "same",
   106  		Region:       "same",
   107  		Availability: "wat",
   108  	})
   109  	th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
   110  }
   111  
   112  var catalog3 = tokens3.ServiceCatalog{
   113  	Entries: []tokens3.CatalogEntry{
   114  		{
   115  			Type: "same",
   116  			Name: "same",
   117  			Endpoints: []tokens3.Endpoint{
   118  				{
   119  					ID:        "1",
   120  					Region:    "same",
   121  					Interface: "public",
   122  					URL:       "https://public.correct.com/",
   123  				},
   124  				{
   125  					ID:        "2",
   126  					Region:    "same",
   127  					Interface: "admin",
   128  					URL:       "https://admin.correct.com/",
   129  				},
   130  				{
   131  					ID:        "3",
   132  					Region:    "same",
   133  					Interface: "internal",
   134  					URL:       "https://internal.correct.com/",
   135  				},
   136  				{
   137  					ID:        "4",
   138  					Region:    "different",
   139  					Interface: "public",
   140  					URL:       "https://badregion.com/",
   141  				},
   142  			},
   143  		},
   144  		{
   145  			Type: "same",
   146  			Name: "different",
   147  			Endpoints: []tokens3.Endpoint{
   148  				{
   149  					ID:        "5",
   150  					Region:    "same",
   151  					Interface: "public",
   152  					URL:       "https://badname.com/",
   153  				},
   154  				{
   155  					ID:        "6",
   156  					Region:    "different",
   157  					Interface: "public",
   158  					URL:       "https://badname.com/+badregion",
   159  				},
   160  			},
   161  		},
   162  		{
   163  			Type: "different",
   164  			Name: "different",
   165  			Endpoints: []tokens3.Endpoint{
   166  				{
   167  					ID:        "7",
   168  					Region:    "same",
   169  					Interface: "public",
   170  					URL:       "https://badtype.com/+badname",
   171  				},
   172  				{
   173  					ID:        "8",
   174  					Region:    "different",
   175  					Interface: "public",
   176  					URL:       "https://badtype.com/+badregion+badname",
   177  				},
   178  			},
   179  		},
   180  		{
   181  			Type: "someother",
   182  			Name: "someother",
   183  			Endpoints: []tokens3.Endpoint{
   184  				{
   185  					ID:        "1",
   186  					Region:    "someother",
   187  					Interface: "public",
   188  					URL:       "https://public.correct.com/",
   189  				},
   190  				{
   191  					ID:        "2",
   192  					RegionID:  "someother",
   193  					Interface: "admin",
   194  					URL:       "https://admin.correct.com/",
   195  				},
   196  				{
   197  					ID:        "3",
   198  					RegionID:  "someother",
   199  					Interface: "internal",
   200  					URL:       "https://internal.correct.com/",
   201  				},
   202  			},
   203  		},
   204  	},
   205  }
   206  
   207  func TestV3EndpointExact(t *testing.T) {
   208  	expectedURLs := map[gophercloud.Availability]string{
   209  		gophercloud.AvailabilityPublic:   "https://public.correct.com/",
   210  		gophercloud.AvailabilityAdmin:    "https://admin.correct.com/",
   211  		gophercloud.AvailabilityInternal: "https://internal.correct.com/",
   212  	}
   213  
   214  	for availability, expected := range expectedURLs {
   215  		actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
   216  			Type:         "same",
   217  			Name:         "same",
   218  			Region:       "same",
   219  			Availability: availability,
   220  		})
   221  		th.AssertNoErr(t, err)
   222  		th.CheckEquals(t, expected, actual)
   223  	}
   224  }
   225  
   226  func TestV3EndpointNone(t *testing.T) {
   227  	_, actual := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
   228  		Type:         "nope",
   229  		Availability: gophercloud.AvailabilityPublic,
   230  	})
   231  	expected := &gophercloud.ErrEndpointNotFound{}
   232  	th.CheckEquals(t, expected.Error(), actual.Error())
   233  }
   234  
   235  func TestV3EndpointMultiple(t *testing.T) {
   236  	actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
   237  		Type:         "same",
   238  		Region:       "same",
   239  		Availability: gophercloud.AvailabilityPublic,
   240  	})
   241  
   242  	th.AssertNoErr(t, err)
   243  	th.AssertEquals(t, "https://public.correct.com/", actual)
   244  }
   245  
   246  func TestV3EndpointBadAvailability(t *testing.T) {
   247  	_, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
   248  		Type:         "same",
   249  		Name:         "same",
   250  		Region:       "same",
   251  		Availability: "wat",
   252  	})
   253  	th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
   254  }
   255  
   256  func TestV3EndpointWithRegionID(t *testing.T) {
   257  	expectedURLs := map[gophercloud.Availability]string{
   258  		gophercloud.AvailabilityPublic:   "https://public.correct.com/",
   259  		gophercloud.AvailabilityAdmin:    "https://admin.correct.com/",
   260  		gophercloud.AvailabilityInternal: "https://internal.correct.com/",
   261  	}
   262  
   263  	for availability, expected := range expectedURLs {
   264  		actual, err := openstack.V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
   265  			Type:         "someother",
   266  			Name:         "someother",
   267  			Region:       "someother",
   268  			Availability: availability,
   269  		})
   270  		th.AssertNoErr(t, err)
   271  		th.CheckEquals(t, expected, actual)
   272  	}
   273  }