mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 18:27:19 +00:00
Fix: Refactor teests to use cupaloy
This commit is contained in:
+77
-96
@@ -1,137 +1,118 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/Infisical/infisical-merge/packages/cmd"
|
"github.com/bradleyjkemp/cupaloy/v2"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunCmd(t *testing.T, authToken string, projectId string, envSlug string) {
|
func TestServiceToken_RunCmdRecursiveAndImports(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand := cmd.NewRootCmd()
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world")
|
||||||
|
|
||||||
commandOutput := new(bytes.Buffer)
|
fmt.Printf("output: %v\n", output)
|
||||||
errorOutput := new(bytes.Buffer)
|
|
||||||
rootCommand.SetOut(commandOutput)
|
|
||||||
rootCommand.SetErr(errorOutput)
|
|
||||||
|
|
||||||
args := []string{
|
if err != nil {
|
||||||
"run",
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, fmt.Sprintf("--token=%s", authToken))
|
// Use cupaloy to snapshot test the output
|
||||||
args = append(args, fmt.Sprintf("--projectId=%s", projectId))
|
err = cupaloy.Snapshot(output)
|
||||||
args = append(args, fmt.Sprintf("--env=%s", envSlug))
|
if err != nil {
|
||||||
args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED")
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestServiceToken_RunCmdWithImports(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand.SetArgs(args)
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world")
|
||||||
rootCommand.Execute()
|
|
||||||
|
|
||||||
var secrets []Secret
|
fmt.Printf("output: %v\n", output)
|
||||||
|
|
||||||
stringSecrets := commandOutput.String()
|
if err != nil {
|
||||||
arraySecrets := strings.Split(stringSecrets, "\n")
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
|
|
||||||
for idx, secret := range arraySecrets {
|
|
||||||
if idx == len(arraySecrets)-1 && secret == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
secretParts := strings.Split(secret, "=")
|
|
||||||
|
|
||||||
if len(secretParts) != 2 {
|
|
||||||
t.Errorf("Error: secret at index %d is not formatted correctly", idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
newSecret := Secret{
|
|
||||||
Key: secretParts[0],
|
|
||||||
Value: secretParts[1],
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the new secret key is at least one of the expected keys
|
|
||||||
if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
secrets = append(secrets, newSecret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedLength := len(DEV_SECRETS) + len(STAGING_SECRETS)
|
// Use cupaloy to snapshot test the output
|
||||||
|
err = cupaloy.Snapshot(output)
|
||||||
assert.Len(t, secrets, expectedLength)
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
for _, secret := range secrets {
|
|
||||||
assert.Contains(t, ALL_SECRET_KEYS, secret.Key)
|
|
||||||
assert.Contains(t, ALL_SECRET_VALUES, secret.Value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunCmdWithoutImportsAndWithRecursive(t *testing.T, authToken string, projectId string, envSlug string) {
|
func TestUniversalAuth_RunCmdRecursiveAndImports(t *testing.T) {
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand := cmd.NewRootCmd()
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world")
|
||||||
|
|
||||||
commandOutput := new(bytes.Buffer)
|
fmt.Printf("output: %v\n", output)
|
||||||
errorOutput := new(bytes.Buffer)
|
|
||||||
rootCommand.SetOut(commandOutput)
|
|
||||||
rootCommand.SetErr(errorOutput)
|
|
||||||
|
|
||||||
args := []string{
|
if err != nil {
|
||||||
"run",
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, fmt.Sprintf("--token=%s", authToken))
|
// Use cupaloy to snapshot test the output
|
||||||
args = append(args, fmt.Sprintf("--projectId=%s", projectId))
|
err = cupaloy.Snapshot(output)
|
||||||
args = append(args, fmt.Sprintf("--env=%s", envSlug))
|
if err != nil {
|
||||||
args = append(args, "--include-imports=false")
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
args = append(args, "--recursive")
|
}
|
||||||
args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED_RECURSIVE")
|
}
|
||||||
|
|
||||||
|
func TestUniversalAuth_RunCmdWithImports(t *testing.T) {
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
rootCommand.SetArgs(args)
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world")
|
||||||
rootCommand.Execute()
|
|
||||||
|
|
||||||
var secrets []Secret
|
fmt.Printf("output: %v\n", output)
|
||||||
|
|
||||||
stringSecrets := commandOutput.String()
|
if err != nil {
|
||||||
arraySecrets := strings.Split(stringSecrets, "\n")
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
for idx, secret := range arraySecrets {
|
// Use cupaloy to snapshot test the output
|
||||||
if idx == len(arraySecrets)-1 && secret == "" {
|
err = cupaloy.Snapshot(output)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
secretParts := strings.Split(secret, "=")
|
func TestUniversalAuth_RunCmdWithoutImports(t *testing.T) {
|
||||||
|
MachineIdentityLoginCmd(t)
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
if len(secretParts) != 2 {
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world")
|
||||||
t.Errorf("Error: secret at index %d is not formatted correctly", idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
newSecret := Secret{
|
fmt.Printf("output: %v\n", output)
|
||||||
Key: secretParts[0],
|
|
||||||
Value: secretParts[1],
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the new secret key is at least one of the expected keys
|
if err != nil {
|
||||||
if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) {
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
|
|
||||||
secrets = append(secrets, newSecret)
|
// Use cupaloy to snapshot test the output
|
||||||
|
err = cupaloy.Snapshot(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceToken_RunCmdWithoutImports(t *testing.T) {
|
||||||
|
SetupCli(t)
|
||||||
|
|
||||||
nestedDevSecrets := append(DEV_FOLDER_SECRETS, DEV_SECRETS...)
|
output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world")
|
||||||
nestedDevSecretsKeys := Map(nestedDevSecrets, func(secret Secret) string { return secret.Key })
|
|
||||||
nestedDevSecretsValues := Map(nestedDevSecrets, func(secret Secret) string { return secret.Value })
|
|
||||||
|
|
||||||
expectedLength := len(nestedDevSecrets)
|
fmt.Printf("output: %v\n", output)
|
||||||
assert.Len(t, secrets, expectedLength)
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error running CLI command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
for _, secret := range secrets {
|
// Use cupaloy to snapshot test the output
|
||||||
assert.Contains(t, nestedDevSecretsKeys, secret.Key)
|
err = cupaloy.Snapshot(output)
|
||||||
assert.Contains(t, nestedDevSecretsValues, secret.Value)
|
if err != nil {
|
||||||
|
t.Fatalf("snapshot failed: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user