github.com/kotalco/kotal@v0.3.0/apis/ethereum2/v1alpha1/validator_validation_webhook_test.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/kotalco/kotal/apis/shared"
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	"k8s.io/apimachinery/pkg/api/errors"
    10  	"k8s.io/apimachinery/pkg/util/validation/field"
    11  )
    12  
    13  var _ = Describe("Ethereum 2.0 validator client validation", func() {
    14  
    15  	createCases := []struct {
    16  		Title     string
    17  		Validator *Validator
    18  		Errors    field.ErrorList
    19  	}{
    20  		{
    21  			Title: "Validator #1",
    22  			Validator: &Validator{
    23  				Spec: ValidatorSpec{
    24  					Network:  "mainnet",
    25  					Client:   PrysmClient,
    26  					Graffiti: "Kotal is amazing",
    27  					Keystores: []Keystore{
    28  						{
    29  							SecretName: "my-validator",
    30  						},
    31  					},
    32  				},
    33  			},
    34  			Errors: field.ErrorList{
    35  				{
    36  					Type:     field.ErrorTypeInvalid,
    37  					Field:    "spec.walletPasswordSecret",
    38  					BadValue: "",
    39  					Detail:   "must provide walletPasswordSecret if client is prysm",
    40  				},
    41  			},
    42  		},
    43  		{
    44  			Title: "Validator #2",
    45  			Validator: &Validator{
    46  				Spec: ValidatorSpec{
    47  					Network:  "mainnet",
    48  					Client:   TekuClient,
    49  					Graffiti: "Kotal is amazing",
    50  					BeaconEndpoints: []string{
    51  						"http://10.96.130.88:9999",
    52  						"http://10.96.130.88:9988",
    53  					},
    54  				},
    55  			},
    56  			Errors: field.ErrorList{
    57  				{
    58  					Type:     field.ErrorTypeInvalid,
    59  					Field:    "spec.beaconEndpoints",
    60  					BadValue: "http://10.96.130.88:9999,http://10.96.130.88:9988",
    61  					Detail:   "multiple beacon node endpoints not supported by teku client",
    62  				},
    63  			},
    64  		},
    65  		{
    66  			Title: "Validator #3",
    67  			Validator: &Validator{
    68  				Spec: ValidatorSpec{
    69  					Network:  "mainnet",
    70  					Client:   LighthouseClient,
    71  					Graffiti: "Kotal is amazing",
    72  					BeaconEndpoints: []string{
    73  						"http://10.96.130.88:9999",
    74  					},
    75  					Keystores: []Keystore{
    76  						{
    77  							SecretName: "my-validator",
    78  						},
    79  					},
    80  				},
    81  			},
    82  			Errors: field.ErrorList{
    83  				{
    84  					Type:     field.ErrorTypeInvalid,
    85  					Field:    "spec.keystores[0].publicKey",
    86  					BadValue: "",
    87  					Detail:   "keystore public key is required if client is lighthouse",
    88  				},
    89  			},
    90  		},
    91  		{
    92  			Title: "Validator #4",
    93  			Validator: &Validator{
    94  				Spec: ValidatorSpec{
    95  					Network:        "mainnet",
    96  					Client:         LighthouseClient,
    97  					CertSecretName: "my-cert",
    98  				},
    99  			},
   100  			Errors: field.ErrorList{
   101  				{
   102  					Type:     field.ErrorTypeInvalid,
   103  					Field:    "spec.certSecretName",
   104  					BadValue: "my-cert",
   105  					Detail:   "not supported by lighthouse client",
   106  				},
   107  			},
   108  		},
   109  	}
   110  
   111  	updateCases := []struct {
   112  		Title        string
   113  		OldValidator *Validator
   114  		NewValidator *Validator
   115  		Errors       field.ErrorList
   116  	}{
   117  		{
   118  			Title: "Validator #1",
   119  			OldValidator: &Validator{
   120  				Spec: ValidatorSpec{
   121  					Network:  "mainnet",
   122  					Client:   TekuClient,
   123  					Graffiti: "Kotal is amazing",
   124  					Keystores: []Keystore{
   125  						{
   126  							SecretName: "my-validator",
   127  						},
   128  					},
   129  				},
   130  			},
   131  			NewValidator: &Validator{
   132  				Spec: ValidatorSpec{
   133  					Network:  "mainnet",
   134  					Client:   PrysmClient,
   135  					Graffiti: "Kotal is amazing",
   136  					Keystores: []Keystore{
   137  						{
   138  							SecretName: "my-validator",
   139  						},
   140  					},
   141  				},
   142  			},
   143  			Errors: field.ErrorList{
   144  				{
   145  					Type:     field.ErrorTypeInvalid,
   146  					Field:    "spec.walletPasswordSecret",
   147  					BadValue: "",
   148  					Detail:   "must provide walletPasswordSecret if client is prysm",
   149  				},
   150  			},
   151  		},
   152  		{
   153  			Title: "Validator #2",
   154  			OldValidator: &Validator{
   155  				Spec: ValidatorSpec{
   156  					Network:  "mainnet",
   157  					Client:   TekuClient,
   158  					Graffiti: "Kotal is amazing",
   159  					Keystores: []Keystore{
   160  						{
   161  							SecretName: "my-validator",
   162  						},
   163  					},
   164  				},
   165  			},
   166  			NewValidator: &Validator{
   167  				Spec: ValidatorSpec{
   168  					Network:  "goerli",
   169  					Client:   TekuClient,
   170  					Graffiti: "Kotal is amazing",
   171  					Keystores: []Keystore{
   172  						{
   173  							SecretName: "my-validator",
   174  						},
   175  					},
   176  				},
   177  			},
   178  			Errors: field.ErrorList{
   179  				{
   180  					Type:     field.ErrorTypeInvalid,
   181  					Field:    "spec.network",
   182  					BadValue: "goerli",
   183  					Detail:   "field is immutable",
   184  				},
   185  			},
   186  		},
   187  		{
   188  			Title: "Validator #3",
   189  			OldValidator: &Validator{
   190  				Spec: ValidatorSpec{
   191  					Network:  "mainnet",
   192  					Client:   TekuClient,
   193  					Graffiti: "Kotal is amazing",
   194  					Keystores: []Keystore{
   195  						{
   196  							SecretName: "my-validator",
   197  						},
   198  					},
   199  				},
   200  			},
   201  			NewValidator: &Validator{
   202  				Spec: ValidatorSpec{
   203  					Network:  "mainnet",
   204  					Client:   PrysmClient,
   205  					Graffiti: "Kotal is amazing",
   206  					Keystores: []Keystore{
   207  						{
   208  							SecretName: "my-validator",
   209  						},
   210  					},
   211  				},
   212  			},
   213  			Errors: field.ErrorList{
   214  				{
   215  					Type:     field.ErrorTypeInvalid,
   216  					Field:    "spec.client",
   217  					BadValue: "prysm",
   218  					Detail:   "field is immutable",
   219  				},
   220  			},
   221  		},
   222  	}
   223  
   224  	Context("While creating validator client", func() {
   225  		for _, c := range createCases {
   226  			func() {
   227  				cc := c
   228  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
   229  					cc.Validator.Default()
   230  					_, err := cc.Validator.ValidateCreate()
   231  
   232  					errStatus := err.(*errors.StatusError)
   233  
   234  					causes := shared.ErrorsToCauses(cc.Errors)
   235  
   236  					Expect(errStatus.ErrStatus.Details.Causes).To(ContainElements(causes))
   237  				})
   238  			}()
   239  		}
   240  	})
   241  
   242  	Context("While updating validator client", func() {
   243  		for _, c := range updateCases {
   244  			func() {
   245  				cc := c
   246  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
   247  					cc.OldValidator.Default()
   248  					cc.NewValidator.Default()
   249  					_, err := cc.NewValidator.ValidateUpdate(cc.OldValidator)
   250  
   251  					errStatus := err.(*errors.StatusError)
   252  
   253  					causes := shared.ErrorsToCauses(cc.Errors)
   254  
   255  					Expect(errStatus.ErrStatus.Details.Causes).To(ContainElements(causes))
   256  				})
   257  			}()
   258  		}
   259  	})
   260  
   261  })