Skip to content

Commit

Permalink
Send tests results to Sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jun 7, 2023
1 parent 029c538 commit 5b3a2a9
Show file tree
Hide file tree
Showing 1,100 changed files with 443,654 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .ibm/pipelines/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,21 @@ skip() {
echo " - $change not in $SKIP_IF_ONLY"
return 0
}

save_results() {
(
cd .ibm/tools/tests-results
echo -n $RESULTS_SERVICE_ACCOUNT > sa.json
JUNIT="$1"
LOGFILE="$2"
NAME="$3"
BASE_URL="https://s3.${IBM_REGION}.cloud-object-storage.appdomain.cloud/${IBM_BUCKET}"

GOOGLE_APPLICATION_CREDENTIALS=$PWD/sa.json go run main.go \
--sheetId "${RESULTS_SHEET_ID}" \
--junit "${JUNIT}" \
--pr "${GIT_PR_NUMBER}" \
--test "${NAME}" \
--logfile "${BASE_URL}/${LOGFILE}.txt"
)
}
2 changes: 1 addition & 1 deletion .ibm/pipelines/kubernetes-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export SKIP_USER_LOGIN_TESTS=true
RESULT=${PIPESTATUS[0]}

save_logs "${LOGFILE}" "Kubernetes Tests" ${RESULT}

save_results test-integration.xml "${LOGFILE}" "Kubernetes Tests"
exit ${RESULT}
1 change: 1 addition & 0 deletions .ibm/pipelines/openshift-unauth-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ ibmcloud oc cluster config -c "${CLUSTER_ID}"
RESULT=${PIPESTATUS[0]}

save_logs "${LOGFILE}" "OpenShift Unauthenticated Tests" ${RESULT}
save_results "${PWD}/test-integration-unauth.xml" "${LOGFILE}" "OpenShift Unauthenticated Tests"

exit ${RESULT}
29 changes: 29 additions & 0 deletions .ibm/tools/tests-results/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/feloy/junit-collector

go 1.19

require (
github.com/joshdk/go-junit v1.0.0
golang.org/x/oauth2 v0.8.0
google.golang.org/api v0.125.0
)

require (
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
199 changes: 199 additions & 0 deletions .ibm/tools/tests-results/go.sum

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions .ibm/tools/tests-results/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"time"

"github.com/feloy/junit-collector/pkg/db"
"github.com/joshdk/go-junit"
)

func usage(msg string) {
fmt.Printf("%s\n\nUsage: %s --sheetId <spreadsheetId> --junit <junit-file> --pr <pr-number> --test <test title> --logfile <logfile>\n", msg, os.Args[0])
os.Exit(1)
}

/*
GOOGLE_APPLICATION_CREDENTIALS must point to an existing GCP JSON Service account file.
The service account does not need any extra role.
The service account must have an Editor access to the Sheet (use the Share button on the Sheet UI to add this permission)
*/
func main() {
var (
fSheetId = flag.String("sheetId", "", "spreadsheetId")
fJunitFile = flag.String("junit", "", "junit file")
fPrNumber = flag.String("pr", "", "PR number")
fTestTile = flag.String("test", "", "Test title")
fLogFile = flag.String("logfile", "", "Log file including base")
)

flag.Parse()

if *fSheetId == "" {
usage("--sheetId is missing")
}
spreadsheetId := *fSheetId

if *fJunitFile == "" {
usage("--junit is missing")
}
junitFile := *fJunitFile

if *fPrNumber == "" {
usage("--pr is missing")
}
prNumber := *fPrNumber

if *fTestTile == "" {
usage("--test is missing")
}
testTitle := *fTestTile

if *fLogFile == "" {
usage("--logfile is missing")
}
logFile := *fLogFile

ctx := context.Background()

oldDate := time.Now().Add(-15 * 24 * time.Hour)
fmt.Printf("Deleting entries before: %s\n", oldDate.Format("2006-01-02"))
err := db.Clean(ctx, spreadsheetId, oldDate.Format("2006-01-02"))
if err != nil {
panic(err)
}

suites, err := junit.IngestFile(junitFile)
if err != nil {
panic(err)
}

for _, suite := range suites {
for _, test := range suite.Tests {
if test.Error == nil {
continue
}
currentTime := time.Now()
data := []interface{}{
// Date must be set in this format, to be correctly comparable as strings
currentTime.Format("2006-01-02"),
test.Name,
test.Error.Error(),
prNumber,
testTitle,
logFile,
}
err = db.SaveToSheet(ctx, spreadsheetId, data)
if err != nil {
log.Fatalf("Unable to add data to sheet: %v", err)
}
}
}
}
52 changes: 52 additions & 0 deletions .ibm/tools/tests-results/pkg/db/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package db

import (
"context"
"fmt"

"google.golang.org/api/sheets/v4"
)

func Clean(ctx context.Context, sheetId string, beforeDate string) error {
srv, err := getService(ctx)
if err != nil {
return err
}
range_ := "Template!A2:A"

result, err := srv.Spreadsheets.Values.Get(sheetId, range_).Do()
if err != nil {
return err
}
var rowNumber int64 = 2
for _, row := range result.Values {
value := row[0].(string)
if value >= beforeDate {
if rowNumber > 2 {
fmt.Printf("Delete rows from 2 to %d\n", rowNumber-1)
}
break
}
rowNumber++
}

if rowNumber < 3 {
return nil
}

var requests []*sheets.Request
requests = append(requests, &sheets.Request{
DeleteDimension: &sheets.DeleteDimensionRequest{
Range: &sheets.DimensionRange{
Dimension: "ROWS",
StartIndex: 1, // 0-index, inclusive
EndIndex: rowNumber - 1, // 0-index, exclusive
},
},
})
req := sheets.BatchUpdateSpreadsheetRequest{
Requests: requests,
}
_, err = srv.Spreadsheets.BatchUpdate(sheetId, &req).Do()
return err
}
22 changes: 22 additions & 0 deletions .ibm/tools/tests-results/pkg/db/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package db

import (
"context"

"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)

func getService(ctx context.Context) (*sheets.Service, error) {
client, err := google.DefaultClient(ctx, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return nil, err
}

srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, err
}
return srv, nil
}
26 changes: 26 additions & 0 deletions .ibm/tools/tests-results/pkg/db/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package db

import (
"context"

"google.golang.org/api/sheets/v4"
)

func SaveToSheet(ctx context.Context, sheetId string, data []interface{}) error {
srv, err := getService(ctx)
if err != nil {
return err
}
readRange := "Template"
valueRange := sheets.ValueRange{
MajorDimension: "ROWS",
Range: "Template",
Values: [][]interface{}{
data,
},
}
_, err = srv.Spreadsheets.Values.
Append(sheetId, readRange, &valueRange).
ValueInputOption("USER_ENTERED").Do()
return err
}
Loading

0 comments on commit 5b3a2a9

Please sign in to comment.