Skip to content

Commit

Permalink
Add CISystem and BuildURL tracking
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente Zepeda Mas <[email protected]>
  • Loading branch information
chentex committed Jul 4, 2024
1 parent 80de096 commit e5d3ecb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/elastic/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

type doc struct {
Uuid string `json:"uuid"`
Version string `json:"version"`
Attack string `json:"attack"`
Metrics vegeta.Metrics `json:"metrics"`
Uuid string `json:"uuid"`
Version string `json:"version"`
Attack string `json:"attack"`
BuildURL string `json:"buildUrl"`
CiSystem string `json:"ciSystem"`
Metrics vegeta.Metrics `json:"metrics"`
}
25 changes: 25 additions & 0 deletions pkg/elastic/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package elastic
import (
"context"
"fmt"
"os"
"strings"

"github.com/cloud-bulldozer/go-commons/indexers"
Expand Down Expand Up @@ -48,12 +49,16 @@ func IndexFile(ctx context.Context, testID string, version string, attack string
logger.Error(ctx, "obtaining indexer: %s", err)
}

env := extractENVContent()

var errors string
_doc := doc{}
_doc.Metrics = metrics
_doc.Uuid = testID
_doc.Version = version
_doc.Attack = attack
_doc.BuildURL = env["buildUrl"]
_doc.CiSystem = env["CiSystem"]

resp, err := (*indexer).Index([]interface{}{_doc}, indexers.IndexingOpts{
MetricName: strings.Join([]string{testID, attack}, "-"),
Expand All @@ -68,3 +73,23 @@ func IndexFile(ctx context.Context, testID string, version string, attack string
}
return nil
}

func extractENVContent() map[string]string {
result := make(map[string]string)
buildURL := os.Getenv("BUILD_URL")
if buildURL != "" {
result["buildUrl"] = buildURL
} else {
result["buildUrl"] = "Manual run"
}
dagID := os.Getenv("AIRFLOW_CTX_DAG_ID")
jenkinsURL := os.Getenv("JENKINS_URL")
if jenkinsURL != "" {
result["CiSystem"] = "Jenkins"
} else if dagID != "" {
result["CiSystem"] = "Airflow"
} else {
result["CiSystem"] = "Local"
}
return result
}
43 changes: 43 additions & 0 deletions pkg/elastic/elastic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package elastic

import (
"os"
"reflect"
"testing"
)

func Test_extractENVContent(t *testing.T) {
tests := []struct {
name string
envs map[string]string
want map[string]string
}{
{"manual_run", map[string]string{}, map[string]string{"buildUrl": "Manual run", "CiSystem": "Local"}},
{"has_build_url", map[string]string{"BUILD_URL": "https://test.com"}, map[string]string{"buildUrl": "https://test.com", "CiSystem": "Local"}},
{"jenkins_run", map[string]string{"BUILD_URL": "https://test.com", "JENKINS_URL": "http://jenkins.dev"}, map[string]string{"buildUrl": "https://test.com", "CiSystem": "Jenkins"}},
{"airflow_run", map[string]string{"BUILD_URL": "https://test.com", "AIRFLOW_CTX_DAG_ID": "ocm-dag"}, map[string]string{"buildUrl": "https://test.com", "CiSystem": "Airflow"}},
{"jenkins_run", map[string]string{"BUILD_URL": "https://test.com", "JENKINS_URL": "http://jenkins.dev", "AIRFLOW_CTX_DAG_ID": "ocm-dag"}, map[string]string{"buildUrl": "https://test.com", "CiSystem": "Jenkins"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for key, v := range tt.envs {
os.Setenv(key, v)
}
if got := extractENVContent(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractENVContent() = %v, want %v", got, tt.want)
}
for key := range tt.envs {
os.Unsetenv(key)
}
})
}
}

// buildURL := os.Getenv("BUILD_URL")
// if buildURL != "" {
// result["buildUrl"] = buildURL
// } else {
// result["buildUrl"] = "Manual run"
// }
// dagID := os.Getenv("AIRFLOW_CTX_DAG_ID")
// jenkinsURL := os.Getenv("JENKINS_URL")

0 comments on commit e5d3ecb

Please sign in to comment.