Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Distributed Client Test #7

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cli/actions/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
},
)
}

}
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions cli/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions internal/pkg/clients/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/generate/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion internal/pkg/generate/generate_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func validateDistributedValidator(gd *GenData, c *clients.ClientInfo) error {
return ErrUnableToGetClientsInfo
}
if !utils.Contains(distributedValidatorClients, gd.DistributedValidatorClient.Name) {
return ErrValidatorClientNotValid
return ErrDistributedValidatorClientNotValid
}
return nil
}
Expand Down
37 changes: 37 additions & 0 deletions internal/pkg/generate/generate_scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
})
}
}
Expand Down