github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_sonar_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be executed with 5 // go test -v -tags integration -run TestSonarIntegration ./integration/... 6 7 package main 8 9 import ( 10 "os" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 16 piperhttp "github.com/SAP/jenkins-library/pkg/http" 17 "github.com/SAP/jenkins-library/pkg/sonar" 18 ) 19 20 func TestSonarIntegrationIssueSearch(t *testing.T) { 21 t.Parallel() 22 // init 23 token := os.Getenv("PIPER_INTEGRATION_SONAR_TOKEN") 24 require.NotEmpty(t, token, "SonarQube API Token is missing") 25 host := os.Getenv("PIPER_INTEGRATION_SONAR_HOST") 26 if len(host) == 0 { 27 host = "https://sonarcloud.io" 28 } 29 organization := os.Getenv("PIPER_INTEGRATION_SONAR_ORGANIZATION") 30 if len(organization) == 0 { 31 organization = "sap-1" 32 } 33 componentKey := os.Getenv("PIPER_INTEGRATION_SONAR_PROJECT") 34 if len(componentKey) == 0 { 35 componentKey = "SAP_jenkins-library" 36 } 37 options := &sonar.IssuesSearchOption{ 38 ComponentKeys: componentKey, 39 Severities: "MINOR", 40 Resolved: "false", 41 Ps: "1", 42 Organization: organization, 43 } 44 issueService := sonar.NewIssuesService(host, token, componentKey, organization, "", "", &piperhttp.Client{}) 45 // test 46 result, _, err := issueService.SearchIssues(options) 47 // assert 48 assert.NoError(t, err) 49 assert.NotEmpty(t, result.Components) 50 //FIXME: include once implememnted 51 // assert.NotEmpty(t, result.Organizations) 52 } 53 54 func TestSonarIntegrationMeasuresComponentSearch(t *testing.T) { 55 t.Parallel() 56 // init 57 token := os.Getenv("PIPER_INTEGRATION_SONAR_TOKEN") 58 require.NotEmpty(t, token, "SonarQube API Token is missing") 59 host := os.Getenv("PIPER_INTEGRATION_SONAR_HOST") 60 if len(host) == 0 { 61 host = "https://sonarcloud.io" 62 } 63 organization := os.Getenv("PIPER_INTEGRATION_SONAR_ORGANIZATION") 64 if len(organization) == 0 { 65 organization = "sap-1" 66 } 67 componentKey := os.Getenv("PIPER_INTEGRATION_SONAR_PROJECT") 68 if len(componentKey) == 0 { 69 componentKey = "SAP_jenkins-library" 70 } 71 72 componentService := sonar.NewMeasuresComponentService(host, token, componentKey, organization, "", "", &piperhttp.Client{}) 73 // test 74 _, err := componentService.GetCoverage() 75 // assert 76 assert.NoError(t, err) 77 } 78 79 func TestSonarIntegrationGetLinesOfCode(t *testing.T) { 80 t.Parallel() 81 // init 82 token := os.Getenv("PIPER_INTEGRATION_SONAR_TOKEN") 83 require.NotEmpty(t, token, "SonarQube API Token is missing") 84 host := os.Getenv("PIPER_INTEGRATION_SONAR_HOST") 85 if len(host) == 0 { 86 host = "https://sonarcloud.io" 87 } 88 organization := os.Getenv("PIPER_INTEGRATION_SONAR_ORGANIZATION") 89 if len(organization) == 0 { 90 organization = "sap-1" 91 } 92 componentKey := os.Getenv("PIPER_INTEGRATION_SONAR_PROJECT") 93 if len(componentKey) == 0 { 94 componentKey = "SAP_jenkins-library" 95 } 96 97 componentService := sonar.NewMeasuresComponentService(host, token, componentKey, organization, "", "", &piperhttp.Client{}) 98 // test 99 _, err := componentService.GetLinesOfCode() 100 // assert 101 assert.NoError(t, err) 102 }