github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/testing/endpoint_location_test.go (about)

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