From 03dc2ca8191be6ac290464daf219214a58e6d0d3 Mon Sep 17 00:00:00 2001 From: xin <3235773541@qq.com> Date: Wed, 20 Mar 2024 14:00:50 +0800 Subject: [PATCH] add Distributed Client Test --- cli/actions/generation_test.go | 37 +++++++++++++++++++ cli/generate_test.go | 14 +++++++ internal/pkg/clients/clients_test.go | 12 ++++++ internal/pkg/clients/types_test.go | 29 +++++++++++++++ internal/pkg/generate/errors.go | 3 ++ internal/pkg/generate/generate_scripts.go | 32 ++++++++-------- .../pkg/generate/generate_scripts_test.go | 37 +++++++++++++++++++ 7 files changed, 148 insertions(+), 16 deletions(-) diff --git a/cli/actions/generation_test.go b/cli/actions/generation_test.go index af6896b9b..fa3cdeefa 100644 --- a/cli/actions/generation_test.go +++ b/cli/actions/generation_test.go @@ -95,6 +95,13 @@ func TestGenerateDockerCompose(t *testing.T) { if err != nil { t.Errorf("SupportedClients(\"validator\") failed: %v", err) } + var distributedValidatorClients []string + if network == "goerli" { + distributedValidatorClients, err = c.SupportedClients("distributedValidator") + if err != nil { + t.Errorf("SupportedClients(\"distributedValidator\") failed: %v", err) + } + } rNum, err := rand.Int(rand.Reader, big.NewInt(int64(100))) if err != nil { @@ -265,6 +272,25 @@ func TestGenerateDockerCompose(t *testing.T) { }, ) } + + // For distributedValidator + if utils.Contains(distributedValidatorClients, "charon") { + tests = append(tests, + genTestData{ + name: fmt.Sprintf("execution: %s, consensus: %s, validator: %s,distributedValidator: %s, network: %s, all, with distributedValidator", executionCl, consensusCl, consensusCl, distributedValidatorClients, network), + genData: generate.GenData{ + Distributed: true, + DistributedValidatorClient: &clients.Client{Name: "charon", Type: "distributedValidator"}, + ExecutionClient: &clients.Client{Name: executionCl, Type: "execution"}, + ConsensusClient: &clients.Client{Name: consensusCl, Type: "consensus"}, + ValidatorClient: &clients.Client{Name: consensusCl, Type: "validator"}, + Services: []string{"execution", "consensus", "validator", "distributedValidator"}, + Network: network, + }, + }, + ) + } + } } } @@ -287,6 +313,9 @@ func TestGenerateDockerCompose(t *testing.T) { if tc.genData.ValidatorClient != nil { tc.genData.ValidatorClient.SetImageOrDefault("") } + if tc.genData.DistributedValidatorClient != nil { + tc.genData.DistributedValidatorClient.SetImageOrDefault("") + } _, err := sedgeAction.Generate(actions.GenerateOptions{ GenerationData: tc.genData, @@ -470,6 +499,14 @@ func TestGenerateDockerCompose(t *testing.T) { } } + // Validate that Distributed Validator Client info matches the sample data + if tc.genData.DistributedValidatorClient != nil { + // Check that the distributed-validator service is set. + assert.NotNil(t, cmpData.Services.DistributedValidator) + // Check that the distributed-validator container Volume is set. + assert.Equal(t, "${DC_DATA_DIR}"+":/opt/charon/.charon", cmpData.Services.DistributedValidator.Volumes[0]) + } + if tc.genData.ValidatorClient == nil { // Check validator blocker is not set if validator is not set assert.Nil(t, cmpData.Services.ValidatorBlocker) diff --git a/cli/generate_test.go b/cli/generate_test.go index ce2b6c767..1ccc40592 100644 --- a/cli/generate_test.go +++ b/cli/generate_test.go @@ -1310,6 +1310,20 @@ func TestGenerateCmd(t *testing.T) { globalFlags{}, nil, }, + { + "full-node random client Distributed", + subCmd{ + name: "distributed", + args: []string{}, + }, + GenCmdFlags{ + distributed: true, + }, + globalFlags{ + network: "goerli", + }, + nil, + }, } // TODO: Add test cases for Execution fallback urls diff --git a/internal/pkg/clients/clients_test.go b/internal/pkg/clients/clients_test.go index ddb40428b..41680fcf6 100644 --- a/internal/pkg/clients/clients_test.go +++ b/internal/pkg/clients/clients_test.go @@ -35,6 +35,7 @@ func TestSupportedClients(t *testing.T) { {"execution", "mainnet", AllClients["execution"], false}, {"consensus", "mainnet", AllClients["consensus"], false}, {"validator", "mainnet", AllClients["validator"], false}, + {"distributedValidator", "goerli", AllClients["distributedValidator"], false}, {"random", "mainnet", []string{}, true}, } @@ -138,6 +139,17 @@ func TestClients(t *testing.T) { "gnosis", false, }, + { + map[string][]string{ + "validator": {"lighthouse", "prysm", "teku", "lodestar"}, + "consensus": {"lighthouse", "prysm", "teku", "lodestar"}, + "execution": {"nethermind", "geth", "besu", "erigon"}, + "distributedValidator": {"charon"}, + }, + []string{"consensus", "execution", "validator", "distributedValidator"}, + "goerli", + false, + }, } for i, input := range inputs { diff --git a/internal/pkg/clients/types_test.go b/internal/pkg/clients/types_test.go index 2a48505d3..1b784d0ed 100644 --- a/internal/pkg/clients/types_test.go +++ b/internal/pkg/clients/types_test.go @@ -148,6 +148,28 @@ func TestSetImageOrDefault_Validator(t *testing.T) { } } +func TestSetImageOrDefault_DistributedValidator(t *testing.T) { + tests := []struct { + client Client + expectedImage regexp.Regexp + }{ + { + client: Client{ + Name: "charon", + Type: "distributedValidator", + }, + expectedImage: *regexp.MustCompile(`^ghcr.io/obolnetwork/charon:v\d+\.\d+\.\d+$`), + }, + } + for _, test := range tests { + t.Run(test.client.Name, func(t *testing.T) { + test.client.SetImageOrDefault("") + t.Logf("print %s", test.client.Image) + assert.True(t, test.expectedImage.Match([]byte(test.client.Image))) + }) + } +} + func TestSetImageOrDefault_CustomImage(t *testing.T) { tests := []struct { client Client @@ -174,6 +196,13 @@ func TestSetImageOrDefault_CustomImage(t *testing.T) { }, customImage: "my/prysm-image:v1.0.0", }, + { + client: Client{ + Name: "charon", + Type: "distributedValidator", + }, + customImage: "my/charon-image:v1.0.0", + }, } for _, test := range tests { t.Run(test.client.Name, func(t *testing.T) { diff --git a/internal/pkg/generate/errors.go b/internal/pkg/generate/errors.go index 30b2f0cd9..cf22755bf 100644 --- a/internal/pkg/generate/errors.go +++ b/internal/pkg/generate/errors.go @@ -34,3 +34,6 @@ var ErrExecutionClientNotValid = errors.New("invalid execution client") // ErrValidatorClientNotValid is returned when the validator client is not valid var ErrValidatorClientNotValid = errors.New("invalid validator client") + +// ErrDistributedValidatorClientNotValid is returned when the distributed validator client is not valid +var ErrDistributedValidatorClientNotValid = errors.New("invalid distributed validator client") diff --git a/internal/pkg/generate/generate_scripts.go b/internal/pkg/generate/generate_scripts.go index 7aa7bda37..de5e346a1 100644 --- a/internal/pkg/generate/generate_scripts.go +++ b/internal/pkg/generate/generate_scripts.go @@ -56,9 +56,9 @@ func validateClients(gd *GenData) error { if err := validateValidator(gd, &c); err != nil { return err } - // if err := validateDistributedValidator(gd, &c); err != nil { - // return err - // } + if err := validateDistributedValidator(gd, &c); err != nil { + return err + } return nil } @@ -78,19 +78,19 @@ func validateValidator(gd *GenData, c *clients.ClientInfo) error { } // validateDistributedValidator validates the validator client in GenData -// func validateDistributedValidator(gd *GenData, c *clients.ClientInfo) error { -// if gd.DistributedValidatorClient == nil { -// return nil -// } -// validatorClients, err := c.SupportedClients(distributedValidator) -// if err != nil { -// return ErrUnableToGetClientsInfo -// } -// if !utils.Contains(validatorClients, gd.ValidatorClient.Name) { -// return ErrValidatorClientNotValid -// } -// return nil -// } +func validateDistributedValidator(gd *GenData, c *clients.ClientInfo) error { + if gd.DistributedValidatorClient == nil { + return nil + } + distributedValidatorClients, err := c.SupportedClients(distributedValidator) + if err != nil { + return ErrUnableToGetClientsInfo + } + if !utils.Contains(distributedValidatorClients, gd.DistributedValidatorClient.Name) { + return ErrDistributedValidatorClientNotValid + } + return nil +} // validateExecution validates the execution client in GenData func validateExecution(gd *GenData, c *clients.ClientInfo) error { diff --git a/internal/pkg/generate/generate_scripts_test.go b/internal/pkg/generate/generate_scripts_test.go index 38ad4355b..1a07610f1 100644 --- a/internal/pkg/generate/generate_scripts_test.go +++ b/internal/pkg/generate/generate_scripts_test.go @@ -379,6 +379,19 @@ func TestGenerateComposeServices(t *testing.T) { }, CheckFunctions: []CheckFunc{checkExtraFlagsOnExecution, checkValidatorBlocker}, }, + { + Description: "Test Distributed Validator", + GenerationData: &GenData{ + ExecutionClient: &clients.Client{Name: "nethermind"}, + ConsensusClient: &clients.Client{Name: "teku"}, + ValidatorClient: &clients.Client{Name: "teku"}, + DistributedValidatorClient: &clients.Client{Name: "charon"}, + Distributed: true, + Network: "goerli", + Services: []string{execution, consensus, validator, distributedValidator}, + }, + CheckFunctions: []CheckFunc{defaultFunc, checkValidatorBlocker}, + }, } tests = append(tests, generateTestCases(t)...) @@ -536,6 +549,16 @@ func TestValidateClients(t *testing.T) { }, Error: ErrUnableToGetClientsInfo, }, + { + Description: "Wrong network, good distributed validator", + Data: &GenData{ + Distributed: true, + DistributedValidatorClient: &clients.Client{Name: "charon"}, + ValidatorClient: &clients.Client{Name: "teku"}, + Network: wrongDep, + }, + Error: ErrUnableToGetClientsInfo, + }, } for _, tt := range tests { t.Run(tt.Description, func(t *testing.T) { @@ -609,6 +632,17 @@ func TestEnvFileAndFlags(t *testing.T) { }, Error: nil, }, + { + Description: "Distributed validator charon with ConsensusApiUrl", + Data: &GenData{ + ConsensusClient: &clients.Client{Name: "teku"}, + ValidatorClient: &clients.Client{Name: "teku"}, + DistributedValidatorClient: &clients.Client{Name: "charon"}, + Distributed: true, + Network: "goerli", + ConsensusApiUrl: "http://localhost:8080", + }, + }, } for _, tt := range tests { @@ -631,6 +665,9 @@ func TestEnvFileAndFlags(t *testing.T) { } } } + if tt.Data.Distributed { + assert.Contains(t, buffer.String(), "DC_API_URL="+endpointOrEmpty(tt.Data.ConsensusClient)) + } }) } }