github.com/kotalco/kotal@v0.3.0/apis/chainlink/v1alpha1/node_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  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/util/validation/field"
    12  )
    13  
    14  var _ = Describe("Chainlink node validation", func() {
    15  	createCases := []struct {
    16  		Title  string
    17  		Node   *Node
    18  		Errors field.ErrorList
    19  	}{}
    20  
    21  	updateCases := []struct {
    22  		Title   string
    23  		OldNode *Node
    24  		NewNode *Node
    25  		Errors  field.ErrorList
    26  	}{
    27  		{
    28  			Title: "updating ethereum chain ID",
    29  			OldNode: &Node{
    30  				ObjectMeta: v1.ObjectMeta{
    31  					Name: "my-node",
    32  				},
    33  				Spec: NodeSpec{
    34  					EthereumChainId: 111,
    35  				},
    36  			},
    37  			NewNode: &Node{
    38  				ObjectMeta: v1.ObjectMeta{
    39  					Name: "my-node",
    40  				},
    41  				Spec: NodeSpec{
    42  					EthereumChainId: 222,
    43  				},
    44  			},
    45  			Errors: field.ErrorList{
    46  				{
    47  					Type:     field.ErrorTypeInvalid,
    48  					Field:    "spec.ethereumChainId",
    49  					BadValue: "222",
    50  					Detail:   "field is immutable",
    51  				},
    52  			},
    53  		},
    54  		{
    55  			Title: "updating LINK contract address",
    56  			OldNode: &Node{
    57  				ObjectMeta: v1.ObjectMeta{
    58  					Name: "my-node",
    59  				},
    60  				Spec: NodeSpec{
    61  					LinkContractAddress: "0x514910771af9ca656af840dff83e8264ecf986ca",
    62  				},
    63  			},
    64  			NewNode: &Node{
    65  				ObjectMeta: v1.ObjectMeta{
    66  					Name: "my-node",
    67  				},
    68  				Spec: NodeSpec{
    69  					LinkContractAddress: "0x326c977e6efc84e512bb9c30f76e30c160ed06fb",
    70  				},
    71  			},
    72  			Errors: field.ErrorList{
    73  				{
    74  					Type:     field.ErrorTypeInvalid,
    75  					Field:    "spec.linkContractAddress",
    76  					BadValue: "0x326c977e6efc84e512bb9c30f76e30c160ed06fb",
    77  					Detail:   "field is immutable",
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	Context("While creating node", func() {
    84  		for _, c := range createCases {
    85  			func() {
    86  				cc := c
    87  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
    88  					cc.Node.Default()
    89  					_, err := cc.Node.ValidateCreate()
    90  
    91  					errStatus := err.(*errors.StatusError)
    92  
    93  					causes := shared.ErrorsToCauses(cc.Errors)
    94  
    95  					Expect(errStatus.ErrStatus.Details.Causes).To(ContainElements(causes))
    96  				})
    97  			}()
    98  		}
    99  	})
   100  
   101  	Context("While updating node", func() {
   102  		for _, c := range updateCases {
   103  			func() {
   104  				cc := c
   105  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
   106  					cc.OldNode.Default()
   107  					cc.NewNode.Default()
   108  					_, err := cc.NewNode.ValidateUpdate(cc.OldNode)
   109  
   110  					errStatus := err.(*errors.StatusError)
   111  
   112  					causes := shared.ErrorsToCauses(cc.Errors)
   113  
   114  					Expect(errStatus.ErrStatus.Details.Causes).To(ContainElements(causes))
   115  				})
   116  			}()
   117  		}
   118  	})
   119  
   120  })