diff --git a/receiver/googlecloudmonitoringreceiver/receiver_test.go b/receiver/googlecloudmonitoringreceiver/receiver_test.go index dca2a5bf487a..23fe3fd8c55b 100644 --- a/receiver/googlecloudmonitoringreceiver/receiver_test.go +++ b/receiver/googlecloudmonitoringreceiver/receiver_test.go @@ -5,7 +5,6 @@ package googlecloudmonitoringreceiver import ( "context" - "fmt" "net" "os" "testing" @@ -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() @@ -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) { @@ -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) { @@ -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) { @@ -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) } @@ -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()) @@ -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) { @@ -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) } diff --git a/receiver/googlecloudmonitoringreceiver/testdata/serviceAccount.json b/receiver/googlecloudmonitoringreceiver/testdata/serviceAccount.json new file mode 100644 index 000000000000..1e9a93e54193 --- /dev/null +++ b/receiver/googlecloudmonitoringreceiver/testdata/serviceAccount.json @@ -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": "qwerty@qwerty.iam.gserviceaccount.com", + "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" +}