Skip to content

Commit

Permalink
enhancement:
Browse files Browse the repository at this point in the history
- Update testify error handling code
- Add sample serviceAccount.json inside testdata
  • Loading branch information
scaletech-milan committed Dec 17, 2024
1 parent 62ed760 commit 2ec6d25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
34 changes: 11 additions & 23 deletions receiver/googlecloudmonitoringreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package googlecloudmonitoringreceiver

import (
"context"
"fmt"
"net"
"os"
"testing"
Expand Down Expand Up @@ -105,7 +104,7 @@ func TestInitializeClient_Success(t *testing.T) {
ctx := context.Background()

// Set up fake credentials for testing
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./../googlecloudspannerreceiver/testdata/serviceAccount.json")
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./testdata/serviceAccount.json")
defer os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")

logger := zap.NewNop()
Expand Down Expand Up @@ -185,14 +184,11 @@ func TestStart_Failure_NoClient(t *testing.T) {
},
}

receiver := newGoogleCloudMonitoringReceiver(cfg, logger)
// Don't initialize the client to test failure case

// Test the Start function
err := receiver.Start(ctx, nil)
errMsg := fmt.Errorf("%s", "failed to find default credentials: google: could not find default credentials. See https://cloud.google.com/docs/authentication/external/set-up-adc for more information")
assert.Error(t, err)
assert.Contains(t, err.Error(), errMsg.Error())
receiver := newGoogleCloudMonitoringReceiver(cfg, logger)
err := receiver.Start(ctx, nil) // Test the Start function
assert.Error(t, err) // Assert error presence
assert.Contains(t, err.Error(), "credentials") // Assert error contains a specific substring
}

func TestStart_Failure_InvalidMetricDescriptor_ProjectNotFound(t *testing.T) {
Expand Down Expand Up @@ -222,9 +218,9 @@ func TestStart_Failure_InvalidMetricDescriptor_ProjectNotFound(t *testing.T) {

// Test the Start function
err = receiver.Start(ctx, nil)
errMsg := fmt.Errorf("%s", "failed to retrieve metric descriptors data: rpc error: code = NotFound desc = project not found")
assert.Error(t, err)
assert.Contains(t, err.Error(), errMsg.Error())
assert.Contains(t, err.Error(), "not found")
assert.Equal(t, status.Code(err), codes.NotFound)
}

func TestStart_Failure_InvalidMetricDescriptor_Unauthenticated_User(t *testing.T) {
Expand Down Expand Up @@ -252,9 +248,9 @@ func TestStart_Failure_InvalidMetricDescriptor_Unauthenticated_User(t *testing.T

// Test the Start function
err = receiver.Start(ctx, nil)
errMsg := fmt.Errorf("%s", "failed to retrieve metric descriptors data: rpc error: code = Unauthenticated desc = transport: per-RPC creds failed due to error: auth: cannot fetch token: 400")
assert.Error(t, err)
assert.Contains(t, err.Error(), errMsg.Error())
assert.Contains(t, err.Error(), "Unauthenticated")
assert.Equal(t, status.Code(err), codes.Unauthenticated)
}

func TestScrape_Success(t *testing.T) {
Expand Down Expand Up @@ -301,8 +297,6 @@ func TestScrape_Success(t *testing.T) {

// Execute Scrape
metrics, err := receiver.Scrape(ctx)

// Assert
assert.NoError(t, err)
assert.NotNil(t, metrics)
}
Expand Down Expand Up @@ -342,8 +336,6 @@ func TestScrape_Failure_MetricDescriptorNotFound(t *testing.T) {

// Execute Scrape
metrics, err := receiver.Scrape(ctx)

// Assert
assert.NoError(t, err) // Should not return error as it just show logs warning
assert.NotNil(t, metrics)
assert.Equal(t, 0, metrics.ResourceMetrics().Len())
Expand Down Expand Up @@ -393,12 +385,10 @@ func TestScrape_Failure_InvalidProjectID_WithMaxInterval(t *testing.T) {

// Execute Scrape
metrics, err := receiver.Scrape(ctx)

// Assert
errMsg := fmt.Errorf("%s", "failed to retrieve time series data: rpc error: code = NotFound desc = project not found")
assert.Error(t, err)
assert.NotNil(t, metrics)
assert.Contains(t, err.Error(), errMsg.Error())
assert.Contains(t, err.Error(), "not found")
assert.Equal(t, status.Code(err), codes.NotFound)
}

func TestConvertGCPTimeSeriesToMetrics(t *testing.T) {
Expand Down Expand Up @@ -770,8 +760,6 @@ func TestScrape_WithDefaultCollectionInterval(t *testing.T) {

// Execute Scrape
metrics, err := receiver.Scrape(ctx)

// Assert
assert.NoError(t, err)
assert.NotNil(t, metrics)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "qwerty",
"private_key_id": "qwerty",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDKusrw23c7AsN\nQCxprTTyywmH1L0f5kJhqVrG+5KxlAO5ivX3NG4XimkzaI5zz3ROb9X2zNNIJ//n\nudEL4ke8oM87RPPAgyAQ7hIevFew2r8FHBToQki7Pl6+UcmAU67Lsqlv4UVNBbzv\nai89POKurAp5S6qTo3tV9OilnMiBirIIbx1l103BLG+7ZgDEkrxJZ/ysF+xSy/AC\n/Rs1YTIp8kyxLisHJq8SShKxLtywuOeJerVAJD0s1pVVORAOK6OJVCB23SDzMZZM\no1rIrrd5NkqNtP9YniceDUSk5s1ePC6UWZdMUpIXuW396+EuyaqMT2j1J7i4agce\nPgwrXbtVAgMBAAECggEAWkJuEoZGN+a+Ubl9EL52/1ZhKP19HdSanJn7Do1oUVH3\nywZ2LIaF58MuVgUj5HFsA557ILGngPZmxl9MS6exr43baTjBViYOXWYM4UQPdg4k\nS5OqI6QMCPSWUoR2oS+SZkhRThmruCxQLZU5FaQj3c5Y5bHVyIPR1XX3Zig1Hpsb\nQHwYC/Nkl60u2GPJfutoyZiBuYQVkwIEWTDBbPbrR2amw6YR2HhOOzXlgM9OmC12\nE11q1IQE6r7vc4BK4jrRT7zgxpjeshLjI82jxzGM7R1ZidDD9LnO2s9oBkwE1bJ2\nJvTkzGHPaEYIEie8VL8MePxmDXSLEospu6NEOUwsvQKBgQD4pY9J693JBvVu/5xX\ndasI0XYpp77Yx8L8O8xxySTxPbZGIzPkZ5yGlMefvIYKkB5eSCeWZhVvbL/Jdbkt\ni8tVVtTxUGRgqeHlkGbRDXTdvGKo2qlGsXObIaux1ISop2vEfq21VabIh66YIRFL\nhiGnmnORgLJI5YBgOWfCdsyF5wKBgQDI8HuQRuvbLNEPQ81coFzC5DljK6ACBk3j\nY/QFNiRh2Ao1LQPd4lLWUeiTfCpmrWul/bdPLqqCHYpiAMNtSV/YQ8sWSeg03PlB\n2JE9zNnH6QP9Zl4MtxkVVb/nUEtYe+6lJXc2oJbb7sf60FixV4JWyGWTYPFSvg6E\nkdW9dSYVYwKBgChJ0zTcFfyrtvr8Sd3WABeWsPnA52iCvbJXEiJhwC2inTUyIQdi\nTnd0BgB48JMnlPQb5uY0tkZurYi8HXwmyZSTVD+hkhIjlKm4wyAeeRAwpR/NBl6h\nBCVfyE0xLSmSryCQkh7uuO6HJaAaw42kNzHMEevCmaC8JxfwVUKqMyuDAoGBAMIU\npQRhvAVicDrSPlSs/2uujE9hH5dVB34OWO9/r/xhctqRtB5oL6KaUo3BbCLodgVJ\n3fg5Fq++YJ7wJdI1AMIeGNZaZFJK7OXQi9ipN+CDDDuA2G5nm26j+EsvntbEPWh9\nm3vD+HJNXBLBkikYYOf5f0Kua+iDcfpR8aSgtjwJAoGBAOQLi1i7crOEvj+g5k23\nOdCXrFapkmzyW+HHPXLm2Sz/4UGJjGfUKAEelfLFFJBs9FLRHbwSndvRjcf/oXcl\nLT+4/eBI8eOQRAbRNg86BGTn+iTLPF2qFZqTQQtq0BHOXO/7jOI4H36i1uTcvf6+\n4w0afAT+jEpv2LBZWNT2XYdK\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "123",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/qwerty%40qwerty.iam.gserviceaccount.com"
}

0 comments on commit 2ec6d25

Please sign in to comment.