github.com/kotalco/kotal@v0.3.0/apis/shared/resources_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  	"k8s.io/apimachinery/pkg/util/validation/field"
     9  )
    10  
    11  var _ = Describe("Resource validation", func() {
    12  	createCases := []struct {
    13  		Title     string
    14  		Resources *Resources
    15  		Errors    field.ErrorList
    16  	}{
    17  		{
    18  			Title: "invalid cpu limit value",
    19  			Resources: &Resources{
    20  				CPU:         "2",
    21  				CPULimit:    "1",
    22  				Memory:      "1Gi",
    23  				MemoryLimit: "2Gi",
    24  			},
    25  			Errors: field.ErrorList{
    26  				{
    27  					Type:     field.ErrorTypeInvalid,
    28  					Field:    "spec.resources.cpuLimit",
    29  					BadValue: "1",
    30  					Detail:   "must be greater than or equal to cpu 2",
    31  				},
    32  			},
    33  		},
    34  		{
    35  			Title: "invalid memory limit value",
    36  			Resources: &Resources{
    37  				CPU:         "1",
    38  				CPULimit:    "2",
    39  				Memory:      "2Gi",
    40  				MemoryLimit: "1Gi",
    41  			},
    42  			Errors: field.ErrorList{
    43  				{
    44  					Type:     field.ErrorTypeInvalid,
    45  					Field:    "spec.resources.memoryLimit",
    46  					BadValue: "1Gi",
    47  					Detail:   "must be greater than memory 2Gi",
    48  				},
    49  			},
    50  		},
    51  	}
    52  
    53  	storageClass := "standard"
    54  	newStorageClass := "custom"
    55  
    56  	updateCases := []struct {
    57  		Title        string
    58  		OldResources *Resources
    59  		NewResources *Resources
    60  		Errors       field.ErrorList
    61  	}{
    62  		{
    63  			Title: "invalid new cpu limit value",
    64  			OldResources: &Resources{
    65  				CPU:         "1",
    66  				CPULimit:    "2",
    67  				Memory:      "1Gi",
    68  				MemoryLimit: "2Gi",
    69  			},
    70  			NewResources: &Resources{
    71  				CPU:         "2",
    72  				CPULimit:    "1",
    73  				Memory:      "1Gi",
    74  				MemoryLimit: "2Gi",
    75  			},
    76  			Errors: field.ErrorList{
    77  				{
    78  					Type:     field.ErrorTypeInvalid,
    79  					Field:    "spec.resources.cpuLimit",
    80  					BadValue: "1",
    81  					Detail:   "must be greater than or equal to cpu 2",
    82  				},
    83  			},
    84  		},
    85  		{
    86  			Title: "invalid new memory limit value",
    87  			OldResources: &Resources{
    88  				CPU:         "1",
    89  				CPULimit:    "2",
    90  				Memory:      "1Gi",
    91  				MemoryLimit: "2Gi",
    92  			},
    93  			NewResources: &Resources{
    94  				CPU:         "1",
    95  				CPULimit:    "2",
    96  				Memory:      "2Gi",
    97  				MemoryLimit: "1Gi",
    98  			},
    99  			Errors: field.ErrorList{
   100  				{
   101  					Type:     field.ErrorTypeInvalid,
   102  					Field:    "spec.resources.memoryLimit",
   103  					BadValue: "1Gi",
   104  					Detail:   "must be greater than memory 2Gi",
   105  				},
   106  			},
   107  		},
   108  		{
   109  			Title: "invalid new storage class value",
   110  			OldResources: &Resources{
   111  				CPU:          "1",
   112  				CPULimit:     "2",
   113  				Memory:       "1Gi",
   114  				MemoryLimit:  "2Gi",
   115  				StorageClass: &storageClass,
   116  			},
   117  			NewResources: &Resources{
   118  				CPU:          "1",
   119  				CPULimit:     "2",
   120  				Memory:       "1Gi",
   121  				MemoryLimit:  "2Gi",
   122  				StorageClass: &newStorageClass,
   123  			},
   124  			Errors: field.ErrorList{
   125  				{
   126  					Type:     field.ErrorTypeInvalid,
   127  					Field:    "spec.resources.storageClass",
   128  					BadValue: "custom",
   129  					Detail:   "field is immutable",
   130  				},
   131  			},
   132  		},
   133  	}
   134  
   135  	Context("While creating node", func() {
   136  		for _, c := range createCases {
   137  			func() {
   138  				cc := c
   139  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
   140  					errorList := cc.Resources.ValidateCreate()
   141  					Expect(errorList).To(ContainElements(cc.Errors))
   142  				})
   143  			}()
   144  		}
   145  	})
   146  
   147  	Context("While updating node", func() {
   148  		for _, c := range updateCases {
   149  			func() {
   150  				cc := c
   151  				It(fmt.Sprintf("Should validate %s", cc.Title), func() {
   152  					errorList := cc.NewResources.ValidateUpdate(cc.OldResources)
   153  					Expect(errorList).To(ContainElements(cc.Errors))
   154  				})
   155  			}()
   156  		}
   157  	})
   158  
   159  })