github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sinks/monasca/driver_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  	"net/url"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  // test the transformation of timeseries to monasca metrics
    25  func TestTimeseriesTransform(t *testing.T) {
    26  	// setup
    27  	sut := monascaSink{}
    28  
    29  	// do
    30  	metrics := sut.processMetrics(testInput)
    31  
    32  	// assert
    33  	set1 := map[string]metric{}
    34  	set2 := map[string]metric{}
    35  	for _, m := range expectedTransformed {
    36  		set1[m.Name] = m
    37  	}
    38  	for _, m := range metrics {
    39  		set2[m.Name] = m
    40  	}
    41  	assert.Equal(t, set1, set2)
    42  }
    43  
    44  // test if the sink creation fails when password is not provided
    45  func TestMissingPasswordError(t *testing.T) {
    46  	// setup
    47  	uri, _ := url.Parse("monasca:?keystone-url=" + testConfig.IdentityEndpoint + "&user_id=" + testUserID)
    48  
    49  	// do
    50  	_, err := CreateMonascaSink(uri)
    51  
    52  	// assert
    53  	assert.Error(t, err)
    54  }
    55  
    56  // test if the sink creation fails when keystone-url is not provided
    57  func TestMissingKeystoneURLError(t *testing.T) {
    58  	// setup
    59  	uri, _ := url.Parse("monasca:?user_id=" + testUserID + "&password=" + testPassword)
    60  
    61  	// do
    62  	_, err := CreateMonascaSink(uri)
    63  
    64  	// assert
    65  	assert.Error(t, err)
    66  }
    67  
    68  // test if the sink creation fails when neither user-id nor username are provided
    69  func TestMissingUserError(t *testing.T) {
    70  	// setup
    71  	uri, _ := url.Parse("monasca:?keystone-url=" + testConfig.IdentityEndpoint + "&password=" + testPassword)
    72  
    73  	// do
    74  	_, err := CreateMonascaSink(uri)
    75  
    76  	// assert
    77  	assert.Error(t, err)
    78  }
    79  
    80  // test if the sink creation fails when domain_id and domainname are missing
    81  // and username is provided
    82  func TestMissingDomainWhenUsernameError(t *testing.T) {
    83  	// setup
    84  	uri, _ := url.Parse("monasca:?keystone-url=" + testConfig.IdentityEndpoint + "&password=" +
    85  		testPassword + "&username=" + testUsername)
    86  
    87  	// do
    88  	_, err := CreateMonascaSink(uri)
    89  
    90  	// assert
    91  	assert.Error(t, err)
    92  }
    93  
    94  // test if the sink creation fails when password is not provided
    95  func TestWrongMonascaURLError(t *testing.T) {
    96  	// setup
    97  	uri, _ := url.Parse("monasca:?keystone-url=" + testConfig.IdentityEndpoint + "&password=" +
    98  		testConfig.Password + "&user-id=" + testConfig.UserID + "&monasca-url=_malformed")
    99  
   100  	// do
   101  	_, err := CreateMonascaSink(uri)
   102  
   103  	// assert
   104  	assert.Error(t, err)
   105  }
   106  
   107  // test the successful creation of the monasca sink
   108  func TestMonascaSinkCreation(t *testing.T) {
   109  	// setup
   110  	uri, _ := url.Parse("monasca:?keystone-url=" + testConfig.IdentityEndpoint + "&password=" +
   111  		testConfig.Password + "&user-id=" + testConfig.UserID)
   112  
   113  	// do
   114  	_, err := CreateMonascaSink(uri)
   115  
   116  	// assert
   117  	assert.NoError(t, err)
   118  }
   119  
   120  // integration test of storing metrics
   121  func TestStoreMetrics(t *testing.T) {
   122  	// setup
   123  	ks, _ := NewKeystoneClient(testConfig)
   124  	monURL, err := ks.MonascaURL()
   125  	assert.NoError(t, err)
   126  	sut := monascaSink{client: &ClientImpl{ksClient: ks, monascaURL: monURL}}
   127  
   128  	// do
   129  	sut.ExportData(testInput)
   130  
   131  	// assert
   132  	assert.Equal(t, 0, sut.numberOfFailures)
   133  }
   134  
   135  // integration test of failure to create metrics
   136  func TestStoreMetricsFailure(t *testing.T) {
   137  	// setup
   138  	ks, _ := NewKeystoneClient(testConfig)
   139  	monURL, _ := url.Parse("http://unexisting.monasca.com")
   140  	sut := monascaSink{client: &ClientImpl{ksClient: ks, monascaURL: monURL}}
   141  
   142  	// do
   143  	sut.ExportData(testInput)
   144  
   145  	// assert
   146  	assert.Equal(t, 1, sut.numberOfFailures)
   147  }