Skip to content

Commit

Permalink
Allow to skip SonarQube steps in tests via -skip-sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Jan 18, 2023
1 parent 75f247b commit a899140
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 111 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/opendevstack/pipeline
go 1.18

require (
github.com/google/go-cmp v0.5.6
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v42 v42.0.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/sonatype-nexus-community/gonexus v0.59.0
Expand Down Expand Up @@ -66,13 +66,13 @@ require (
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/api v0.36.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-containerregistry v0.4.1-0.20210128200529-19c2b639fab1 h1:o2ykCuuhHeUwtzNg89pH2hi+821aqjLWkaREVR3ziTQ=
github.com/google/go-containerregistry v0.4.1-0.20210128200529-19c2b639fab1/go.mod h1:GU9FUA/X9rd2cV3ZoUNaWihp27tki6/38EsVzL2Dyzc=
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20210129212729-5c4818de4025/go.mod h1:n9wRxRfKkHy6ZFyj0jJQHw11P+mGLnED4sqegwrXxDk=
Expand Down Expand Up @@ -698,6 +699,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a h1:/YWeLOBWYV5WAQORVPkZF3Pq9IppkcT72GKnWjNf5W8=
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -721,8 +724,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -862,8 +865,8 @@ golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down Expand Up @@ -946,8 +949,8 @@ golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
23 changes: 23 additions & 0 deletions test/tasks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,40 @@ import (
"github.com/opendevstack/pipeline/pkg/pipelinectxt"
"github.com/opendevstack/pipeline/pkg/sonar"
"github.com/opendevstack/pipeline/pkg/tasktesting"
"golang.org/x/exp/slices"
kclient "k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
)

var alwaysKeepTmpWorkspacesFlag = flag.Bool("always-keep-tmp-workspaces", false, "Whether to keep temporary workspaces from taskruns even when test is successful")
var outsideKindFlag = flag.Bool("outside-kind", false, "Whether to continue if not in KinD cluster")
var skipSonarQubeFlag = flag.Bool("skip-sonar", false, "Whether to skip SonarQube steps")

const (
taskKindRef = "Task"
)

// buildTaskParams forces all SonarQube params to be "falsy"
// if the skipSonarQubeFlag is set.
func buildTaskParams(p map[string]string) map[string]string {
if *skipSonarQubeFlag {
p["sonar-skip"] = "true"
p["sonar-quality-gate"] = "false"
}
return p
}

// requiredServices takes a variable amount of services and removes
// SonarQube from the resulting slice if the skipSonarQubeFlag is set.
func requiredServices(s ...tasktesting.Service) []tasktesting.Service {
requiredServices := []tasktesting.Service{tasktesting.Nexus}
sqIndex := slices.Index(requiredServices, tasktesting.SonarQube)
if sqIndex != -1 && *skipSonarQubeFlag {
requiredServices = slices.Delete(requiredServices, sqIndex, sqIndex+1)
}
return requiredServices
}

func checkODSContext(t *testing.T, repoDir string, want *pipelinectxt.ODSContext) {
checkODSFileContent(t, repoDir, "component", want.Component)
checkODSFileContent(t, repoDir, "git-commit-sha", want.GitCommitSHA)
Expand Down
79 changes: 46 additions & 33 deletions test/tasks/ods-build-go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ func TestTaskODSBuildGo(t *testing.T) {
goProverb := "Don't communicate by sharing memory, share memory by communicating."
runTaskTestCases(t,
"ods-build-go",
[]tasktesting.Service{
tasktesting.SonarQube,
},
requiredServices(tasktesting.SonarQube),
map[string]tasktesting.TestCase{
"build go app": {
WorkspaceDirMapping: map[string]string{"source": "go-sample-app"},
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
wsDir := ctxt.Workspaces["source"]
ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
"sonar-quality-gate": "true",
"cache-build": "false",
}
})
},
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
Expand All @@ -45,13 +43,17 @@ func TestTaskODSBuildGo(t *testing.T) {
filepath.Join(pipelinectxt.LintReportsPath, "report.txt"),
filepath.Join(pipelinectxt.XUnitReportsPath, "report.xml"),
filepath.Join(pipelinectxt.CodeCoveragesPath, "coverage.out"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "analysis-report.md"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "issues-report.csv"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "quality-gate.json"),
)

sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
if !*skipSonarQubeFlag {
checkFilesExist(t, wsDir,
filepath.Join(pipelinectxt.SonarAnalysisPath, "analysis-report.md"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "issues-report.csv"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "quality-gate.json"),
)
sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
}

wantLogMsg := "No sonar-project.properties present, using default:"
if !strings.Contains(string(ctxt.CollectedLogs), wantLogMsg) {
Expand All @@ -76,12 +78,11 @@ func TestTaskODSBuildGo(t *testing.T) {
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
wsDir := ctxt.Workspaces["source"]
ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
"sonar-quality-gate": "true",
// "cache-build": "true", expected to be default
}
})
},
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
Expand All @@ -93,13 +94,18 @@ func TestTaskODSBuildGo(t *testing.T) {
filepath.Join(pipelinectxt.LintReportsPath, "report.txt"),
filepath.Join(pipelinectxt.XUnitReportsPath, "report.xml"),
filepath.Join(pipelinectxt.CodeCoveragesPath, "coverage.out"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "analysis-report.md"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "issues-report.csv"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "quality-gate.json"),
)

sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
if !*skipSonarQubeFlag {
checkFilesExist(t, wsDir,
filepath.Join(pipelinectxt.SonarAnalysisPath, "analysis-report.md"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "issues-report.csv"),
filepath.Join(pipelinectxt.SonarAnalysisPath, "quality-gate.json"),
)

sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
}

// This is not available when build skipping as the default is
// supplied on the second repeat.
Expand Down Expand Up @@ -134,12 +140,12 @@ func TestTaskODSBuildGo(t *testing.T) {
createAppInSubDirectory(t, wsDir, subdir, "go-sample-app")

ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
"sonar-quality-gate": "true",
"working-dir": subdir,
}
})
},
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
Expand All @@ -154,12 +160,17 @@ func TestTaskODSBuildGo(t *testing.T) {
filepath.Join(pipelinectxt.LintReportsPath, fmt.Sprintf("%s-report.txt", subdir)),
filepath.Join(pipelinectxt.XUnitReportsPath, fmt.Sprintf("%s-report.xml", subdir)),
filepath.Join(pipelinectxt.CodeCoveragesPath, fmt.Sprintf("%s-coverage.out", subdir)),
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-analysis-report.md", subdir)),
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-issues-report.csv", subdir)),
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-quality-gate.json", subdir)))
)

sonarProject := sonar.ProjectKey(ctxt.ODS, subdir+"-")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
if !*skipSonarQubeFlag {
checkFilesExist(t, wsDir,
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-analysis-report.md", subdir)),
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-issues-report.csv", subdir)),
filepath.Join(pipelinectxt.SonarAnalysisPath, fmt.Sprintf("%s-quality-gate.json", subdir)),
)
sonarProject := sonar.ProjectKey(ctxt.ODS, subdir+"-")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
}

b, _, err := command.RunBuffered(filepath.Join(wsDir, binary), []string{})
if err != nil {
Expand All @@ -179,10 +190,10 @@ func TestTaskODSBuildGo(t *testing.T) {
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
wsDir := ctxt.Workspaces["source"]
ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
}
})
},
WantRunSuccess: false,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
Expand All @@ -205,10 +216,10 @@ func TestTaskODSBuildGo(t *testing.T) {
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
wsDir := ctxt.Workspaces["source"]
ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"sonar-skip": "true",
"pre-test-script": "pre-test-script.sh",
}
})
},
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
Expand All @@ -229,16 +240,18 @@ func TestTaskODSBuildGo(t *testing.T) {
ctxt.ODS = tasktesting.SetupGitRepo(t, ctxt.Namespace, wsDir)
writeContextFile(t, wsDir, "pr-key", "3")
writeContextFile(t, wsDir, "pr-key", "master")
ctxt.Params = map[string]string{
ctxt.Params = buildTaskParams(map[string]string{
"go-os": runtime.GOOS,
"go-arch": runtime.GOARCH,
"sonar-quality-gate": "true",
}
})
},
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
if !*skipSonarQubeFlag {
sonarProject := sonar.ProjectKey(ctxt.ODS, "")
checkSonarQualityGate(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace, sonarProject, true, "OK")
}
},
CleanupFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
wsDir := ctxt.Workspaces["source"]
Expand Down
Loading

0 comments on commit a899140

Please sign in to comment.