-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package usage_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/testsutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateMetric(t *testing.T) { | ||
// Set environment variables for the test using SetEnvWithCallbackAndAssert | ||
envVars := map[string]string{ | ||
"JFROG_CLI_USAGE_OIDC_USED": "true", | ||
"JFROG_CLI_USAGE_JOB_ID": "job123", | ||
"JFROG_CLI_USAGE_RUN_ID": "run456", | ||
"JFROG_CLI_USAGE_GIT_REPO": "test-repo", | ||
"JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED": "true", | ||
} | ||
cleanupFuncs := []func(){} | ||
for key, value := range envVars { | ||
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, key, value) | ||
cleanupFuncs = append(cleanupFuncs, cleanup) | ||
} | ||
defer func() { | ||
for _, cleanup := range cleanupFuncs { | ||
cleanup() | ||
} | ||
}() | ||
|
||
commandName := "testCommand" | ||
metric, err := createMetric(commandName) | ||
assert.NoError(t, err) | ||
|
||
// Define the expected JSON structure | ||
expectedJSON := `{ | ||
"value": 1, | ||
"metrics_name": "jfcli_commands_countaa", | ||
"labels": { | ||
"product_id": "` + coreutils.GetCliUserAgentName() + `", | ||
"feature_id": "testCommand", | ||
"oidc_used": "true", | ||
"job_id": "job123", | ||
"run_id": "run456", | ||
"git_repo": "https://github.com/jfrog/test-repo", | ||
"gh_token_for_code_scanning_alerts_provided": "true" | ||
} | ||
}` | ||
|
||
// Compare the generated JSON to the expected JSON | ||
assert.JSONEq(t, expectedJSON, string(metric)) | ||
} |