chore: embed trivial rand string function (#22177)

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
This commit is contained in:
Michael Crenshaw 2025-03-05 16:34:16 -05:00 committed by GitHub
parent 29c69b3601
commit 74582e9965
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 9 deletions

View file

@ -3,6 +3,8 @@ package plugin
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@ -12,7 +14,6 @@ import (
"strings"
"time"
"github.com/argoproj/pkg/rand"
"github.com/golang/protobuf/ptypes/empty"
"github.com/argoproj/argo-cd/v3/cmpserver/apiclient"
@ -61,6 +62,16 @@ func (s *Service) Init(workDir string) error {
return nil
}
const execIDLen = 5
func randExecID() (string, error) {
execIDBytes := make([]byte, execIDLen/2+1) // we need one extra letter to discard
if _, err := rand.Read(execIDBytes); err != nil {
return "", err
}
return hex.EncodeToString(execIDBytes)[0:execIDLen], nil
}
func runCommand(ctx context.Context, command Command, path string, env []string) (string, error) {
if len(command.Command) == 0 {
return "", errors.New("Command is empty")
@ -70,7 +81,7 @@ func runCommand(ctx context.Context, command Command, path string, env []string)
cmd.Env = env
cmd.Dir = path
execId, err := rand.RandString(5)
execId, err := randExecID()
if err != nil {
return "", err
}

View file

@ -2,12 +2,14 @@ package e2e
import (
"context"
"crypto/rand"
"encoding/hex"
"strings"
"testing"
"time"
"github.com/argoproj/pkg/rand"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -20,6 +22,16 @@ import (
"github.com/argoproj/argo-cd/v3/test/e2e/fixture/applicationsets/utils"
)
func randStr(t *testing.T) string {
t.Helper()
bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil {
require.NoError(t, err)
return ""
}
return hex.EncodeToString(bytes)
}
func TestSimpleGitDirectoryGenerator(t *testing.T) {
generateExpectedApp := func(name string) v1alpha1.Application {
return v1alpha1.Application{
@ -393,13 +405,11 @@ func TestSimpleGitDirectoryGeneratorGPGEnabledWithoutKnownKeys(t *testing.T) {
project := "gpg"
str, _ := rand.RandString(1)
Given(t).
Project(project).
Path(guestbookPath).
When().
AddSignedFile("test.yaml", str).IgnoreErrors().
AddSignedFile("test.yaml", randStr(t)).IgnoreErrors().
IgnoreErrors().
// Create a GitGenerator-based ApplicationSet
Create(v1alpha1.ApplicationSet{
@ -705,8 +715,6 @@ func TestSimpleGitFilesGeneratorGPGEnabledWithoutKnownKeys(t *testing.T) {
}
}
str, _ := rand.RandString(1)
expectedApps := []v1alpha1.Application{
generateExpectedApp("engineering-dev-guestbook"),
generateExpectedApp("engineering-prod-guestbook"),
@ -716,7 +724,7 @@ func TestSimpleGitFilesGeneratorGPGEnabledWithoutKnownKeys(t *testing.T) {
Project(project).
Path(guestbookPath).
When().
AddSignedFile("test.yaml", str).IgnoreErrors().
AddSignedFile("test.yaml", randStr(t)).IgnoreErrors().
IgnoreErrors().
// Create a GitGenerator-based ApplicationSet
Create(v1alpha1.ApplicationSet{