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 3231ca4
Show file tree
Hide file tree
Showing 1,103 changed files with 443,668 additions and 7 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"
)
}
4 changes: 3 additions & 1 deletion .ibm/pipelines/kubernetes-docs-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

LOGFILE="pr-${GIT_PR_NUMBER}-kubernetes-docs-tests-${BUILD_NUMBER}"
TEST_NAME="Kubernetes Docs Tests"

source .ibm/pipelines/functions.sh

Expand All @@ -17,6 +18,7 @@ cleanup_namespaces

RESULT=${PIPESTATUS[0]}

save_logs "${LOGFILE}" "Kubernetes Docs Tests" ${RESULT}
save_logs "${LOGFILE}" "${TEST_NAME}" ${RESULT}
save_results "${PWD}/test-doc-automation.xml" "${LOGFILE}" "${TEST_NAME}"

exit ${RESULT}
5 changes: 3 additions & 2 deletions .ibm/pipelines/kubernetes-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

LOGFILE="pr-${GIT_PR_NUMBER}-kubernetes-tests-${BUILD_NUMBER}"
TEST_NAME="Kubernetes Tests"

source .ibm/pipelines/functions.sh

Expand All @@ -22,6 +23,6 @@ export SKIP_USER_LOGIN_TESTS=true

RESULT=${PIPESTATUS[0]}

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

save_logs "${LOGFILE}" "${TEST_NAME}" ${RESULT}
save_results "${PWD}/test-integration.xml" "${LOGFILE}" "${TEST_NAME}"
exit ${RESULT}
4 changes: 3 additions & 1 deletion .ibm/pipelines/nocluster-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

LOGFILE="pr-${GIT_PR_NUMBER}-nocluster-tests-${BUILD_NUMBER}"
TEST_NAME="NoCluster Tests"

source .ibm/pipelines/functions.sh

Expand All @@ -18,6 +19,7 @@ ibmcloud target -r "${IBM_REGION}"

RESULT=${PIPESTATUS[0]}

save_logs "${LOGFILE}" "NoCluster Tests" ${RESULT}
save_logs "${LOGFILE}" "${TEST_NAME}" ${RESULT}
save_results "${PWD}/test-integration-nc.xml" "${LOGFILE}" "${TEST_NAME}"

exit ${RESULT}
5 changes: 4 additions & 1 deletion .ibm/pipelines/openshift-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

LOGFILE="pr-${GIT_PR_NUMBER}-openshift-tests-${BUILD_NUMBER}"
TEST_NAME="OpenShift Tests"

source .ibm/pipelines/functions.sh

Expand All @@ -24,6 +25,8 @@ cleanup_namespaces

RESULT=${PIPESTATUS[0]}

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

exit ${RESULT}
4 changes: 3 additions & 1 deletion .ibm/pipelines/openshift-unauth-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

LOGFILE="pr-${GIT_PR_NUMBER}-openshift-unauth-tests-${BUILD_NUMBER}"
TEST_NAME="OpenShift Unauthenticated Tests"

source .ibm/pipelines/functions.sh

Expand All @@ -18,6 +19,7 @@ ibmcloud oc cluster config -c "${CLUSTER_ID}"

RESULT=${PIPESTATUS[0]}

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

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 3231ca4

Please sign in to comment.