github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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 TestGetTokenWhenMissing(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 TestGetTokenWhenInvalid(t *testing.T) {
    51  	// setup
    52  	opts := testConfig.AuthOptions
    53  	provider, err := openstack.AuthenticatedClient(opts)
    54  	assert.NoError(t, err)
    55  	client := openstack.NewIdentityV3(provider)
    56  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: invalidToken, monascaURL: nil}
    57  
    58  	// do
    59  	token, err := ksClient.GetToken()
    60  
    61  	// assert
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, token, testToken)
    64  }
    65  
    66  func TestGetTokenWhenValid(t *testing.T) {
    67  	// setup
    68  	opts := testConfig.AuthOptions
    69  	provider, err := openstack.AuthenticatedClient(opts)
    70  	assert.NoError(t, err)
    71  	client := openstack.NewIdentityV3(provider)
    72  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}
    73  
    74  	// do
    75  	token, err := ksClient.GetToken()
    76  
    77  	// assert
    78  	assert.NoError(t, err)
    79  	assert.Equal(t, token, testToken)
    80  }
    81  
    82  func TestKeystoneClientReauthenticate(t *testing.T) {
    83  	// setup
    84  	opts := testConfig.AuthOptions
    85  	provider, err := openstack.AuthenticatedClient(opts)
    86  	assert.NoError(t, err)
    87  	client := openstack.NewIdentityV3(provider)
    88  	client.TokenID = "someinvalidtoken"
    89  	client.ReauthFunc = func() error { return openstack.AuthenticateV3(client.ProviderClient, opts) }
    90  	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}
    91  
    92  	// do
    93  	token, err := ksClient.GetToken()
    94  
    95  	// assert
    96  	assert.NoError(t, err)
    97  	assert.Equal(t, token, testToken)
    98  }