about summary refs log tree commit diff
path: root/main/civisibility/utils/ci_providers_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'main/civisibility/utils/ci_providers_test.go')
-rw-r--r--main/civisibility/utils/ci_providers_test.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/main/civisibility/utils/ci_providers_test.go b/main/civisibility/utils/ci_providers_test.go
new file mode 100644
index 0000000..b85acc1
--- /dev/null
+++ b/main/civisibility/utils/ci_providers_test.go
@@ -0,0 +1,116 @@
+// Unless explicitly stated otherwise all files in this repository are licensed
+// under the Apache License Version 2.0.
+// This product includes software developed at Datadog (https://www.datadoghq.com/).
+// Copyright 2024 Datadog, Inc.
+
+package utils
+
+import (
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+	"testing"
+)
+
+func setEnvs(t *testing.T, env map[string]string) {
+	for key, value := range env {
+		t.Setenv(key, value)
+	}
+}
+
+func sortJSONKeys(jsonStr string) string {
+	tmp := map[string]string{}
+	_ = json.Unmarshal([]byte(jsonStr), &tmp)
+	jsonBytes, _ := json.Marshal(tmp)
+	return string(jsonBytes)
+}
+
+// TestTags asserts that all tags are extracted from environment variables.
+func TestTags(t *testing.T) {
+	// Reset provider env key when running in CI
+	resetProviders := map[string]string{}
+	for key := range providers {
+		if value, ok := os.LookupEnv(key); ok {
+			resetProviders[key] = value
+			_ = os.Unsetenv(key)
+		}
+	}
+	defer func() {
+		for key, value := range resetProviders {
+			_ = os.Setenv(key, value)
+		}
+	}()
+
+	paths, err := filepath.Glob("testdata/fixtures/providers/*.json")
+	if err != nil {
+		t.Fatal(err)
+	}
+	for _, path := range paths {
+		providerName := strings.TrimSuffix(filepath.Base(path), ".json")
+
+		t.Run(providerName, func(t *testing.T) {
+			fp, err := os.Open(path)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			data, err := io.ReadAll(fp)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			var examples [][]map[string]string
+			if err := json.Unmarshal(data, &examples); err != nil {
+				t.Fatal(err)
+			}
+
+			for i, line := range examples {
+				name := fmt.Sprintf("%d", i)
+				env := line[0]
+				tags := line[1]
+
+				// Because we have a fallback algorithm for some variables
+				// we need to initialize some of them to not use the one set by the github action running this test.
+				if providerName == "github" {
+					// We initialize GITHUB_RUN_ATTEMPT if it doesn't exist to avoid using the one set in the GitHub action.
+					if _, ok := env["GITHUB_RUN_ATTEMPT"]; !ok {
+						env["GITHUB_RUN_ATTEMPT"] = ""
+					}
+					// We initialize GITHUB_HEAD_REF if it doesn't exist to avoid using the one set in the GitHub action.
+					if _, ok := env["GITHUB_HEAD_REF"]; !ok {
+						env["GITHUB_HEAD_REF"] = ""
+					}
+					// We initialize GITHUB_REF if it doesn't exist to avoid using the one set in the GitHub action.
+					if _, ok := env["GITHUB_REF"]; !ok {
+						env["GITHUB_REF"] = ""
+					}
+				}
+
+				t.Run(name, func(t *testing.T) {
+					setEnvs(t, env)
+					providerTags := getProviderTags()
+
+					for expectedKey, expectedValue := range tags {
+						if actualValue, ok := providerTags[expectedKey]; ok {
+							if expectedKey == "_dd.ci.env_vars" {
+								expectedValue = sortJSONKeys(expectedValue)
+							}
+							if expectedValue != actualValue {
+								if expectedValue == strings.ReplaceAll(actualValue, "\\", "/") {
+									continue
+								}
+
+								t.Fatalf("Key: %s, the actual value (%s) is different to the expected value (%s)", expectedKey, actualValue, expectedValue)
+							}
+						} else {
+							t.Fatalf("Key: %s, doesn't exist.", expectedKey)
+						}
+					}
+				})
+			}
+		})
+	}
+}