github.com/aavshr/aws-sdk-go@v1.41.3/aws/endpoints/endpoints_test.go (about)

     1  package endpoints
     2  
     3  import "testing"
     4  
     5  func TestEnumDefaultPartitions(t *testing.T) {
     6  	resolver := DefaultResolver()
     7  	enum, ok := resolver.(EnumPartitions)
     8  
     9  	if ok != true {
    10  		t.Fatalf("resolver must satisfy EnumPartition interface")
    11  	}
    12  
    13  	ps := enum.Partitions()
    14  
    15  	if a, e := len(ps), len(defaultPartitions); a != e {
    16  		t.Errorf("expected %d partitions, got %d", e, a)
    17  	}
    18  }
    19  
    20  func TestEnumDefaultRegions(t *testing.T) {
    21  	expectPart := defaultPartitions[0]
    22  	partEnum := defaultPartitions[0].Partition()
    23  
    24  	regEnum := partEnum.Regions()
    25  
    26  	if a, e := len(regEnum), len(expectPart.Regions); a != e {
    27  		t.Errorf("expected %d regions, got %d", e, a)
    28  	}
    29  }
    30  
    31  func TestEnumPartitionServices(t *testing.T) {
    32  	expectPart := testPartitions[0]
    33  	partEnum := testPartitions[0].Partition()
    34  
    35  	if a, e := partEnum.ID(), "part-id"; a != e {
    36  		t.Errorf("expect %q partition ID, got %q", e, a)
    37  	}
    38  
    39  	svcEnum := partEnum.Services()
    40  
    41  	// Expect the number of services in the partition + ec2metadata
    42  	if a, e := len(svcEnum), len(expectPart.Services)+1; a != e {
    43  		t.Errorf("expected %d regions, got %d", e, a)
    44  	}
    45  }
    46  
    47  func TestEnumRegionServices(t *testing.T) {
    48  	p := testPartitions[0].Partition()
    49  
    50  	rs := p.Regions()
    51  
    52  	if a, e := len(rs), 2; a != e {
    53  		t.Errorf("expect %d regions got %d", e, a)
    54  	}
    55  
    56  	if _, ok := rs["us-east-1"]; !ok {
    57  		t.Errorf("expect us-east-1 region to be found, was not")
    58  	}
    59  	if _, ok := rs["us-west-2"]; !ok {
    60  		t.Errorf("expect us-west-2 region to be found, was not")
    61  	}
    62  
    63  	r := rs["us-east-1"]
    64  
    65  	if a, e := r.ID(), "us-east-1"; a != e {
    66  		t.Errorf("expect %q region ID, got %q", e, a)
    67  	}
    68  
    69  	if a, e := r.Description(), "region description"; a != e {
    70  		t.Errorf("expect %q region Description, got %q", e, a)
    71  	}
    72  
    73  	ss := r.Services()
    74  	if a, e := len(ss), 1; a != e {
    75  		t.Errorf("expect %d services for us-east-1, got %d", e, a)
    76  	}
    77  
    78  	if _, ok := ss["service1"]; !ok {
    79  		t.Errorf("expect service1 service to be found, was not")
    80  	}
    81  
    82  	resolved, err := r.ResolveEndpoint("service1")
    83  	if err != nil {
    84  		t.Fatalf("expect no error, got %v", err)
    85  	}
    86  
    87  	if a, e := resolved.URL, "https://service1.us-east-1.amazonaws.com"; a != e {
    88  		t.Errorf("expect %q resolved URL, got %q", e, a)
    89  	}
    90  }
    91  
    92  func TestEnumServiceRegions(t *testing.T) {
    93  	p := testPartitions[0].Partition()
    94  
    95  	rs := p.Services()["service1"].Regions()
    96  	if e, a := 2, len(rs); e != a {
    97  		t.Errorf("expect %d regions, got %d", e, a)
    98  	}
    99  
   100  	if _, ok := rs["us-east-1"]; !ok {
   101  		t.Errorf("expect region to be found")
   102  	}
   103  	if _, ok := rs["us-west-2"]; !ok {
   104  		t.Errorf("expect region to be found")
   105  	}
   106  }
   107  
   108  func TestEnumServicesEndpoints(t *testing.T) {
   109  	p := testPartitions[0].Partition()
   110  
   111  	ss := p.Services()
   112  
   113  	// Expect the number of services in the partition + ec2metadata
   114  	if a, e := len(ss), 6; a != e {
   115  		t.Errorf("expect %d regions got %d", e, a)
   116  	}
   117  
   118  	if _, ok := ss["service1"]; !ok {
   119  		t.Errorf("expect service1 region to be found, was not")
   120  	}
   121  	if _, ok := ss["service2"]; !ok {
   122  		t.Errorf("expect service2 region to be found, was not")
   123  	}
   124  
   125  	s := ss["service1"]
   126  	if a, e := s.ID(), "service1"; a != e {
   127  		t.Errorf("expect %q service ID, got %q", e, a)
   128  	}
   129  
   130  	resolved, err := s.ResolveEndpoint("us-west-2")
   131  	if err != nil {
   132  		t.Fatalf("expect no error, got %v", err)
   133  	}
   134  
   135  	if a, e := resolved.URL, "https://service1.us-west-2.amazonaws.com"; a != e {
   136  		t.Errorf("expect %q resolved URL, got %q", e, a)
   137  	}
   138  }
   139  
   140  func TestEnumEndpoints(t *testing.T) {
   141  	p := testPartitions[0].Partition()
   142  	s := p.Services()["service1"]
   143  
   144  	es := s.Endpoints()
   145  	if a, e := len(es), 2; a != e {
   146  		t.Errorf("expect %d endpoints for service2, got %d", e, a)
   147  	}
   148  	if _, ok := es["us-east-1"]; !ok {
   149  		t.Errorf("expect us-east-1 to be found, was not")
   150  	}
   151  
   152  	e := es["us-east-1"]
   153  	if a, e := e.ID(), "us-east-1"; a != e {
   154  		t.Errorf("expect %q endpoint ID, got %q", e, a)
   155  	}
   156  	if a, e := e.ServiceID(), "service1"; a != e {
   157  		t.Errorf("expect %q service ID, got %q", e, a)
   158  	}
   159  
   160  	resolved, err := e.ResolveEndpoint()
   161  	if err != nil {
   162  		t.Fatalf("expect no error, got %v", err)
   163  	}
   164  
   165  	if a, e := resolved.URL, "https://service1.us-east-1.amazonaws.com"; a != e {
   166  		t.Errorf("expect %q resolved URL, got %q", e, a)
   167  	}
   168  }
   169  
   170  func TestResolveEndpointForPartition(t *testing.T) {
   171  	enum := testPartitions.Partitions()[0]
   172  
   173  	expected, err := testPartitions.EndpointFor("service1", "us-east-1")
   174  	if err != nil {
   175  		t.Fatalf("unexpected error, %v", err)
   176  	}
   177  
   178  	actual, err := enum.EndpointFor("service1", "us-east-1")
   179  	if err != nil {
   180  		t.Fatalf("unexpected error, %v", err)
   181  	}
   182  
   183  	if expected != actual {
   184  		t.Errorf("expect resolved endpoint to be %v, but got %v", expected, actual)
   185  	}
   186  }
   187  
   188  func TestAddScheme(t *testing.T) {
   189  	cases := []struct {
   190  		In         string
   191  		Expect     string
   192  		DisableSSL bool
   193  	}{
   194  		{
   195  			In:     "https://example.com",
   196  			Expect: "https://example.com",
   197  		},
   198  		{
   199  			In:     "example.com",
   200  			Expect: "https://example.com",
   201  		},
   202  		{
   203  			In:     "http://example.com",
   204  			Expect: "http://example.com",
   205  		},
   206  		{
   207  			In:         "example.com",
   208  			Expect:     "http://example.com",
   209  			DisableSSL: true,
   210  		},
   211  		{
   212  			In:         "https://example.com",
   213  			Expect:     "https://example.com",
   214  			DisableSSL: true,
   215  		},
   216  	}
   217  
   218  	for i, c := range cases {
   219  		actual := AddScheme(c.In, c.DisableSSL)
   220  		if actual != c.Expect {
   221  			t.Errorf("%d, expect URL to be %q, got %q", i, c.Expect, actual)
   222  		}
   223  	}
   224  }
   225  
   226  func TestResolverFunc(t *testing.T) {
   227  	var resolver Resolver
   228  
   229  	resolver = ResolverFunc(func(s, r string, opts ...func(*Options)) (ResolvedEndpoint, error) {
   230  		return ResolvedEndpoint{
   231  			URL:           "https://service.region.dnssuffix.com",
   232  			SigningRegion: "region",
   233  			SigningName:   "service",
   234  		}, nil
   235  	})
   236  
   237  	resolved, err := resolver.EndpointFor("service", "region", func(o *Options) {
   238  		o.DisableSSL = true
   239  	})
   240  	if err != nil {
   241  		t.Fatalf("expect no error, got %v", err)
   242  	}
   243  
   244  	if a, e := resolved.URL, "https://service.region.dnssuffix.com"; a != e {
   245  		t.Errorf("expect %q endpoint URL, got %q", e, a)
   246  	}
   247  
   248  	if a, e := resolved.SigningRegion, "region"; a != e {
   249  		t.Errorf("expect %q region, got %q", e, a)
   250  	}
   251  	if a, e := resolved.SigningName, "service"; a != e {
   252  		t.Errorf("expect %q signing name, got %q", e, a)
   253  	}
   254  }
   255  
   256  func TestOptionsSet(t *testing.T) {
   257  	var actual Options
   258  	actual.Set(DisableSSLOption, UseDualStackOption, StrictMatchingOption)
   259  
   260  	expect := Options{
   261  		DisableSSL:     true,
   262  		UseDualStack:   true,
   263  		StrictMatching: true,
   264  	}
   265  
   266  	if actual != expect {
   267  		t.Errorf("expect %v options got %v", expect, actual)
   268  	}
   269  }
   270  
   271  func TestRegionsForService(t *testing.T) {
   272  	ps := DefaultPartitions()
   273  
   274  	var expect map[string]Region
   275  	var serviceID string
   276  	for _, s := range ps[0].Services() {
   277  		expect = s.Regions()
   278  		serviceID = s.ID()
   279  		if len(expect) > 0 {
   280  			break
   281  		}
   282  	}
   283  
   284  	actual, ok := RegionsForService(ps, ps[0].ID(), serviceID)
   285  	if !ok {
   286  		t.Fatalf("expect regions to be found, was not")
   287  	}
   288  
   289  	if len(actual) == 0 {
   290  		t.Fatalf("expect service %s to have regions", serviceID)
   291  	}
   292  	if e, a := len(expect), len(actual); e != a {
   293  		t.Fatalf("expect %d regions, got %d", e, a)
   294  	}
   295  
   296  	for id, r := range actual {
   297  		if e, a := id, r.ID(); e != a {
   298  			t.Errorf("expect %s region id, got %s", e, a)
   299  		}
   300  		if _, ok := expect[id]; !ok {
   301  			t.Errorf("expect %s region to be found", id)
   302  		}
   303  		if a, e := r.Description(), expect[id].desc; a != e {
   304  			t.Errorf("expect %q region Description, got %q", e, a)
   305  		}
   306  	}
   307  }
   308  
   309  func TestRegionsForService_NotFound(t *testing.T) {
   310  	ps := testPartitions.Partitions()
   311  
   312  	actual, ok := RegionsForService(ps, ps[0].ID(), "service-not-exists")
   313  	if ok {
   314  		t.Fatalf("expect no regions to be found, but were")
   315  	}
   316  	if len(actual) != 0 {
   317  		t.Errorf("expect no regions, got %v", actual)
   318  	}
   319  }
   320  
   321  func TestPartitionForRegion(t *testing.T) {
   322  	ps := DefaultPartitions()
   323  	expect := ps[len(ps)%2]
   324  
   325  	var regionID string
   326  	for id := range expect.Regions() {
   327  		regionID = id
   328  		break
   329  	}
   330  
   331  	actual, ok := PartitionForRegion(ps, regionID)
   332  	if !ok {
   333  		t.Fatalf("expect partition to be found")
   334  	}
   335  	if e, a := expect.DNSSuffix(), actual.DNSSuffix(); e != a {
   336  		t.Errorf("expect %s partition DNSSuffix, got %s", e, a)
   337  	}
   338  	if e, a := expect.ID(), actual.ID(); e != a {
   339  		t.Errorf("expect %s partition ID, got %s", e, a)
   340  	}
   341  }
   342  
   343  func TestPartitionForRegion_NotFound(t *testing.T) {
   344  	ps := DefaultPartitions()
   345  
   346  	actual, ok := PartitionForRegion(ps, "regionNotExists")
   347  	if ok {
   348  		t.Errorf("expect no partition to be found, got %v", actual)
   349  	}
   350  }
   351  
   352  func TestEC2MetadataEndpoint(t *testing.T) {
   353  	cases := []struct {
   354  		Options  Options
   355  		Expected string
   356  	}{
   357  		{
   358  			Expected: ec2MetadataEndpointIPv4,
   359  		},
   360  		{
   361  			Options: Options{
   362  				EC2MetadataEndpointMode: EC2IMDSEndpointModeStateIPv4,
   363  			},
   364  			Expected: ec2MetadataEndpointIPv4,
   365  		},
   366  		{
   367  			Options: Options{
   368  				EC2MetadataEndpointMode: EC2IMDSEndpointModeStateIPv6,
   369  			},
   370  			Expected: ec2MetadataEndpointIPv6,
   371  		},
   372  	}
   373  
   374  	for _, p := range DefaultPartitions() {
   375  		var region string
   376  		for r := range p.Regions() {
   377  			region = r
   378  			break
   379  		}
   380  
   381  		for _, c := range cases {
   382  			endpoint, err := p.EndpointFor("ec2metadata", region, func(options *Options) {
   383  				*options = c.Options
   384  			})
   385  			if err != nil {
   386  				t.Fatalf("expect no error, got %v", err)
   387  			}
   388  			if e, a := c.Expected, endpoint.URL; e != a {
   389  				t.Errorf("exect %v, got %v", e, a)
   390  			}
   391  		}
   392  	}
   393  }