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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk"
     9  	"github.com/huaweicloud/golangsdk/openstack/utils"
    10  	"github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func setupVersionHandler() {
    14  	testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    15  		fmt.Fprintf(w, `
    16  			{
    17  				"versions": {
    18  					"values": [
    19  						{
    20  							"status": "stable",
    21  							"id": "v3.0",
    22  							"links": [
    23  								{ "href": "%s/v3.0", "rel": "self" }
    24  							]
    25  						},
    26  						{
    27  							"status": "stable",
    28  							"id": "v2.0",
    29  							"links": [
    30  								{ "href": "%s/v2.0", "rel": "self" }
    31  							]
    32  						}
    33  					]
    34  				}
    35  			}
    36  		`, testhelper.Server.URL, testhelper.Server.URL)
    37  	})
    38  }
    39  
    40  func TestChooseVersion(t *testing.T) {
    41  	testhelper.SetupHTTP()
    42  	defer testhelper.TeardownHTTP()
    43  	setupVersionHandler()
    44  
    45  	v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
    46  	v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
    47  
    48  	c := &golangsdk.ProviderClient{
    49  		IdentityBase:     testhelper.Endpoint(),
    50  		IdentityEndpoint: "",
    51  	}
    52  	v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
    53  
    54  	if err != nil {
    55  		t.Fatalf("Unexpected error from ChooseVersion: %v", err)
    56  	}
    57  
    58  	if v != v3 {
    59  		t.Errorf("Expected %#v to win, but %#v did instead", v3, v)
    60  	}
    61  
    62  	expected := testhelper.Endpoint() + "v3.0/"
    63  	if endpoint != expected {
    64  		t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
    65  	}
    66  }
    67  
    68  func TestChooseVersionOpinionatedLink(t *testing.T) {
    69  	testhelper.SetupHTTP()
    70  	defer testhelper.TeardownHTTP()
    71  	setupVersionHandler()
    72  
    73  	v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
    74  	v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "northis"}
    75  
    76  	c := &golangsdk.ProviderClient{
    77  		IdentityBase:     testhelper.Endpoint(),
    78  		IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
    79  	}
    80  	v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
    81  	if err != nil {
    82  		t.Fatalf("Unexpected error from ChooseVersion: %v", err)
    83  	}
    84  
    85  	if v != v2 {
    86  		t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
    87  	}
    88  
    89  	expected := testhelper.Endpoint() + "v2.0/"
    90  	if endpoint != expected {
    91  		t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
    92  	}
    93  }
    94  
    95  func TestChooseVersionFromSuffix(t *testing.T) {
    96  	testhelper.SetupHTTP()
    97  	defer testhelper.TeardownHTTP()
    98  
    99  	v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
   100  	v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
   101  
   102  	c := &golangsdk.ProviderClient{
   103  		IdentityBase:     testhelper.Endpoint(),
   104  		IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
   105  	}
   106  	v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
   107  	if err != nil {
   108  		t.Fatalf("Unexpected error from ChooseVersion: %v", err)
   109  	}
   110  
   111  	if v != v2 {
   112  		t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
   113  	}
   114  
   115  	expected := testhelper.Endpoint() + "v2.0/"
   116  	if endpoint != expected {
   117  		t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
   118  	}
   119  }