github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/cloud/keystone_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package cloud
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	"github.com/onsi/gomega/ghttp"
    22  )
    23  
    24  var _ = Describe("Keystone client", func() {
    25  	var (
    26  		server     *ghttp.Server
    27  		client     KeystoneClient
    28  		username   string = "admin"
    29  		password   string = "password"
    30  		domainName string = "domain"
    31  		tenantName string = "admin"
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		server = ghttp.NewServer()
    36  	})
    37  
    38  	AfterEach(func() {
    39  		server.Close()
    40  	})
    41  
    42  	Describe("Match verion from auth URL", func() {
    43  		It("Should match v2 version successfully", func() {
    44  			res := matchVersionFromAuthURL("http://example.com:5000/v2.0")
    45  			Expect(res).To(Equal("v2.0"))
    46  			res = matchVersionFromAuthURL("http://example.com:5000/v2.0/")
    47  			Expect(res).To(Equal("v2.0"))
    48  		})
    49  
    50  		It("Should match v3 version successfully", func() {
    51  			res := matchVersionFromAuthURL("http://example.com:5000/v3")
    52  			Expect(res).To(Equal("v3"))
    53  			res = matchVersionFromAuthURL("http://example.com:5000/v3/")
    54  			Expect(res).To(Equal("v3"))
    55  		})
    56  
    57  		It("Should should match no version", func() {
    58  			res := matchVersionFromAuthURL("http://example.com:5000/nonsense")
    59  			Expect(res).To(Equal(""))
    60  		})
    61  	})
    62  
    63  	Describe("Tenant ID <-> Tenant Name Mapper", func() {
    64  		Context("Keystone v2", func() {
    65  			BeforeEach(func() {
    66  				server.AppendHandlers(
    67  					ghttp.RespondWithJSONEncoded(200, getV2TokensResponse()),
    68  				)
    69  				client, _ = NewKeystoneV2Client(server.URL()+"/v2.0", username, password, tenantName)
    70  			})
    71  
    72  			It("Should map Tenant Name to Tenant ID successfully", func() {
    73  				server.AppendHandlers(
    74  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
    75  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
    76  				)
    77  				tenantID, err := client.GetTenantID("admin")
    78  				Expect(err).ToNot(HaveOccurred())
    79  				Expect(tenantID).To(Equal("1234"))
    80  
    81  				tenantID, err = client.GetTenantID("demo")
    82  				Expect(err).ToNot(HaveOccurred())
    83  				Expect(tenantID).To(Equal("3456"))
    84  			})
    85  
    86  			It("Should map Tenant ID to Tenant Name successfully", func() {
    87  				server.AppendHandlers(
    88  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
    89  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
    90  				)
    91  				tenantName, err := client.GetTenantName("1234")
    92  				Expect(err).ToNot(HaveOccurred())
    93  				Expect(tenantName).To(Equal("admin"))
    94  
    95  				tenantName, err = client.GetTenantName("3456")
    96  				Expect(err).ToNot(HaveOccurred())
    97  				Expect(tenantName).To(Equal("demo"))
    98  			})
    99  
   100  			It("Should show error - tenant with provided id not found", func() {
   101  				server.AppendHandlers(
   102  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
   103  				)
   104  				tenantID, err := client.GetTenantID("santa")
   105  				Expect(tenantID).To(Equal(""))
   106  				Expect(err).To(MatchError("Tenant with name 'santa' not found"))
   107  			})
   108  
   109  			It("Should show error - tenant with provided name not found", func() {
   110  				server.AppendHandlers(
   111  					ghttp.RespondWithJSONEncoded(200, getV2TenantsResponse()),
   112  				)
   113  				tenantName, err := client.GetTenantName("santa")
   114  				Expect(tenantName).To(Equal(""))
   115  				Expect(err).To(MatchError("Tenant with ID 'santa' not found"))
   116  			})
   117  		})
   118  
   119  		Context("Keystone v3", func() {
   120  			BeforeEach(func() {
   121  				server.AppendHandlers(
   122  					ghttp.RespondWithJSONEncoded(201, getV3TokensResponse()),
   123  				)
   124  				client, _ = NewKeystoneV3Client(server.URL()+"/v3", username, password, domainName, tenantName)
   125  			})
   126  
   127  			It("Should map Tenant Name to Tenant ID successfully", func() {
   128  				server.AppendHandlers(
   129  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   130  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   131  				)
   132  				tenantID, err := client.GetTenantID("admin")
   133  				Expect(err).ToNot(HaveOccurred())
   134  				Expect(tenantID).To(Equal("1234"))
   135  
   136  				tenantID, err = client.GetTenantID("demo")
   137  				Expect(err).ToNot(HaveOccurred())
   138  				Expect(tenantID).To(Equal("3456"))
   139  			})
   140  
   141  			It("Should map Tenant ID to Tenant Name successfully", func() {
   142  				server.AppendHandlers(
   143  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   144  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   145  				)
   146  				tenantName, err := client.GetTenantName("1234")
   147  				Expect(err).ToNot(HaveOccurred())
   148  				Expect(tenantName).To(Equal("admin"))
   149  
   150  				tenantName, err = client.GetTenantName("3456")
   151  				Expect(err).ToNot(HaveOccurred())
   152  				Expect(tenantName).To(Equal("demo"))
   153  			})
   154  
   155  			It("Should show error - tenant with provided id not found", func() {
   156  				server.AppendHandlers(
   157  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   158  				)
   159  				tenantID, err := client.GetTenantID("santa")
   160  				Expect(tenantID).To(Equal(""))
   161  				Expect(err).To(MatchError("Tenant with name 'santa' not found"))
   162  			})
   163  
   164  			It("Should show error - tenant with provided name not found", func() {
   165  				server.AppendHandlers(
   166  					ghttp.RespondWithJSONEncoded(200, getV3TenantsResponse()),
   167  				)
   168  				tenantName, err := client.GetTenantName("santa")
   169  				Expect(tenantName).To(Equal(""))
   170  				Expect(err).To(MatchError("Tenant with ID 'santa' not found"))
   171  			})
   172  		})
   173  	})
   174  })