github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sinks/monasca/keystone_client_test.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     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 implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package monasca
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/rackspace/gophercloud/openstack"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestMonascaURLDiscovery(t *testing.T) {
    25  	// setup
    26  	ksClient, err := NewKeystoneClient(testConfig)
    27  	assert.NoError(t, err)
    28  
    29  	// do
    30  	monURL, err := ksClient.MonascaURL()
    31  
    32  	// assert
    33  	assert.NoError(t, err)
    34  	assert.Equal(t, monURL.String(), monascaAPIStub.URL)
    35  }
    36  
    37  func TestCreateUnscopedToken(t *testing.T) {
    38  	// setup
    39  	ksClient, err := NewKeystoneClient(testConfig)
    40  	assert.NoError(t, err)
    41  
    42  	// do
    43  	token, err := ksClient.GetToken()
    44  
    45  	// assert
    46  	assert.NoError(t, err)
    47  	assert.Equal(t, token, testToken)
    48  }
    49  
    50  func TestCreateScopedToken(t *testing.T) {
    51  	// setup
    52  	scopedConfig := testConfig
    53  	scopedConfig.TenantID = testTenantID
    54  	ksClient, err := NewKeystoneClient(scopedConfig)
    55  
    56  	// do
    57  	token, err := ksClient.GetToken()
    58  
    59  	// assert
    60  	assert.NoError(t, err)
    61  	assert.Equal(t, token, testScopedToken)
    62  }
    63  
    64  func TestGetTokenWhenInvalid(t *testing.T) {
    65  	// setup
    66  	opts := testConfig.AuthOptions
    67  	provider, err := openstack.AuthenticatedClient(opts)
    68  	assert.NoError(t, err)
    69  	client := openstack.NewIdentityV3(provider)
    70  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: invalidToken, monascaURL: nil}
    71  
    72  	// do
    73  	token, err := ksClient.GetToken()
    74  
    75  	// assert
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, token, testToken)
    78  }
    79  
    80  func TestGetTokenWhenValid(t *testing.T) {
    81  	// setup
    82  	opts := testConfig.AuthOptions
    83  	provider, err := openstack.AuthenticatedClient(opts)
    84  	assert.NoError(t, err)
    85  	client := openstack.NewIdentityV3(provider)
    86  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}
    87  
    88  	// do
    89  	token, err := ksClient.GetToken()
    90  
    91  	// assert
    92  	assert.NoError(t, err)
    93  	assert.Equal(t, token, testToken)
    94  }
    95  
    96  func TestKeystoneClientReauthenticate(t *testing.T) {
    97  	// setup
    98  	opts := testConfig.AuthOptions
    99  	provider, err := openstack.AuthenticatedClient(opts)
   100  	assert.NoError(t, err)
   101  	client := openstack.NewIdentityV3(provider)
   102  	client.TokenID = "someinvalidtoken"
   103  	client.ReauthFunc = func() error { return openstack.AuthenticateV3(client.ProviderClient, opts) }
   104  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}
   105  
   106  	// do
   107  	token, err := ksClient.GetToken()
   108  
   109  	// assert
   110  	assert.NoError(t, err)
   111  	assert.Equal(t, token, testToken)
   112  }