github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/resources/quota_resource_test.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/types"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("quota limits", func() {
    13  	Describe("AppLimit", func() {
    14  		DescribeTable("MarshalJSON",
    15  			func(appLimit AppLimit, expectedBytes []byte) {
    16  				bytes, err := json.Marshal(appLimit)
    17  				Expect(err).ToNot(HaveOccurred())
    18  				Expect(bytes).To(Equal(expectedBytes))
    19  			},
    20  			Entry("total memory", AppLimit{TotalMemory: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_memory_in_mb":1}`)),
    21  			Entry("total memory", AppLimit{TotalMemory: nil}, []byte(`{}`)),
    22  			Entry("total memory", AppLimit{TotalMemory: &types.NullInt{IsSet: false}}, []byte(`{"total_memory_in_mb":null}`)),
    23  			Entry("total log volume", AppLimit{TotalLogVolume: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"log_rate_limit_in_bytes_per_second":1}`)),
    24  			Entry("total log volume", AppLimit{TotalLogVolume: nil}, []byte(`{}`)),
    25  			Entry("total log volume", AppLimit{TotalLogVolume: &types.NullInt{IsSet: false}}, []byte(`{"log_rate_limit_in_bytes_per_second":null}`)),
    26  			Entry("instance memory", AppLimit{InstanceMemory: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"per_process_memory_in_mb":1}`)),
    27  			Entry("instance memory", AppLimit{InstanceMemory: nil}, []byte(`{}`)),
    28  			Entry("instance memory", AppLimit{InstanceMemory: &types.NullInt{IsSet: false}}, []byte(`{"per_process_memory_in_mb":null}`)),
    29  			Entry("total app instances", AppLimit{TotalAppInstances: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_instances":1}`)),
    30  			Entry("total app instances", AppLimit{TotalAppInstances: nil}, []byte(`{}`)),
    31  			Entry("total app instances", AppLimit{TotalAppInstances: &types.NullInt{IsSet: false}}, []byte(`{"total_instances":null}`)),
    32  		)
    33  
    34  		DescribeTable("UnmarshalJSON",
    35  			func(givenBytes []byte, expectedStruct AppLimit) {
    36  				var actualStruct AppLimit
    37  				err := json.Unmarshal(givenBytes, &actualStruct)
    38  				Expect(err).ToNot(HaveOccurred())
    39  				Expect(actualStruct).To(Equal(expectedStruct))
    40  			},
    41  			Entry(
    42  				"no null values",
    43  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":2,"total_instances":3,"log_rate_limit_in_bytes_per_second":4}`),
    44  				AppLimit{
    45  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    46  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    47  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    48  					TotalLogVolume:    &types.NullInt{IsSet: true, Value: 4},
    49  				}),
    50  			Entry(
    51  				"total memory is null",
    52  				[]byte(`{"total_memory_in_mb":null,"per_process_memory_in_mb":2,"total_instances":3,"log_rate_limit_in_bytes_per_second":4}`),
    53  				AppLimit{
    54  					TotalMemory:       &types.NullInt{IsSet: false, Value: 0},
    55  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    56  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    57  					TotalLogVolume:    &types.NullInt{IsSet: true, Value: 4},
    58  				}),
    59  			Entry(
    60  				"per process memory is null",
    61  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":null,"total_instances":3,"log_rate_limit_in_bytes_per_second":4}`),
    62  				AppLimit{
    63  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    64  					InstanceMemory:    &types.NullInt{IsSet: false, Value: 0},
    65  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    66  					TotalLogVolume:    &types.NullInt{IsSet: true, Value: 4},
    67  				}),
    68  			Entry(
    69  				"total instances is null",
    70  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":2,"total_instances":null,"log_rate_limit_in_bytes_per_second":4}`),
    71  				AppLimit{
    72  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    73  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    74  					TotalAppInstances: &types.NullInt{IsSet: false, Value: 0},
    75  					TotalLogVolume:    &types.NullInt{IsSet: true, Value: 4},
    76  				}),
    77  			Entry(
    78  				"total log volume is null",
    79  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":2,"total_instances":3,"log_rate_limit_in_bytes_per_second":null}`),
    80  				AppLimit{
    81  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    82  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    83  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    84  					TotalLogVolume:    &types.NullInt{IsSet: false, Value: 0},
    85  				}),
    86  		)
    87  	})
    88  
    89  	Describe("ServiceLimit", func() {
    90  		var (
    91  			trueValue  = true
    92  			falseValue = false
    93  		)
    94  
    95  		DescribeTable("MarshalJSON",
    96  			func(serviceLimit ServiceLimit, expectedBytes []byte) {
    97  				bytes, err := json.Marshal(serviceLimit)
    98  				Expect(err).ToNot(HaveOccurred())
    99  				Expect(bytes).To(Equal(expectedBytes))
   100  			},
   101  			Entry("total service instances", ServiceLimit{TotalServiceInstances: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_service_instances":1}`)),
   102  			Entry("total service instances", ServiceLimit{TotalServiceInstances: nil}, []byte(`{}`)),
   103  			Entry("total service instances", ServiceLimit{TotalServiceInstances: &types.NullInt{IsSet: false}}, []byte(`{"total_service_instances":null}`)),
   104  			Entry("paid service plans", ServiceLimit{PaidServicePlans: &trueValue}, []byte(`{"paid_services_allowed":true}`)),
   105  			Entry("paid service plans", ServiceLimit{PaidServicePlans: nil}, []byte(`{}`)),
   106  			Entry("paid service plans", ServiceLimit{PaidServicePlans: &falseValue}, []byte(`{"paid_services_allowed":false}`)),
   107  		)
   108  
   109  		DescribeTable("UnmarshalJSON",
   110  			func(givenBytes []byte, expectedStruct ServiceLimit) {
   111  				var actualStruct ServiceLimit
   112  				err := json.Unmarshal(givenBytes, &actualStruct)
   113  				Expect(err).ToNot(HaveOccurred())
   114  				Expect(actualStruct).To(Equal(expectedStruct))
   115  			},
   116  			Entry(
   117  				"no null values",
   118  				[]byte(`{"total_service_instances":1,"paid_services_allowed":true}`),
   119  				ServiceLimit{
   120  					TotalServiceInstances: &types.NullInt{IsSet: true, Value: 1},
   121  					PaidServicePlans:      &trueValue,
   122  				}),
   123  			Entry(
   124  				"total service instances is null and paid services allowed is false",
   125  				[]byte(`{"total_service_instances":null,"paid_services_allowed":false}`),
   126  				ServiceLimit{
   127  					TotalServiceInstances: &types.NullInt{IsSet: false, Value: 0},
   128  					PaidServicePlans:      &falseValue,
   129  				}),
   130  		)
   131  	})
   132  
   133  	Describe("RouteLimit", func() {
   134  		DescribeTable("MarshalJSON",
   135  			func(routeLimit RouteLimit, expectedBytes []byte) {
   136  				bytes, err := json.Marshal(routeLimit)
   137  				Expect(err).ToNot(HaveOccurred())
   138  				Expect(bytes).To(Equal(expectedBytes))
   139  			},
   140  			Entry("total routes", RouteLimit{TotalRoutes: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_routes":1}`)),
   141  			Entry("total routes", RouteLimit{TotalRoutes: nil}, []byte(`{}`)),
   142  			Entry("total routes", RouteLimit{TotalRoutes: &types.NullInt{IsSet: false}}, []byte(`{"total_routes":null}`)),
   143  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_reserved_ports":1}`)),
   144  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: nil}, []byte(`{}`)),
   145  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: &types.NullInt{IsSet: false}}, []byte(`{"total_reserved_ports":null}`)),
   146  		)
   147  
   148  		DescribeTable("UnmarshalJSON",
   149  			func(givenBytes []byte, expectedStruct RouteLimit) {
   150  				var actualStruct RouteLimit
   151  				err := json.Unmarshal(givenBytes, &actualStruct)
   152  				Expect(err).ToNot(HaveOccurred())
   153  				Expect(actualStruct).To(Equal(expectedStruct))
   154  			},
   155  			Entry(
   156  				"no null values",
   157  				[]byte(`{"total_routes":1,"total_reserved_ports":2}`),
   158  				RouteLimit{
   159  					TotalRoutes:        &types.NullInt{IsSet: true, Value: 1},
   160  					TotalReservedPorts: &types.NullInt{IsSet: true, Value: 2},
   161  				}),
   162  			Entry(
   163  				"total routes is null",
   164  				[]byte(`{"total_routes":null,"total_reserved_ports":3}`),
   165  				RouteLimit{
   166  					TotalRoutes:        &types.NullInt{IsSet: false, Value: 0},
   167  					TotalReservedPorts: &types.NullInt{IsSet: true, Value: 3},
   168  				}),
   169  			Entry(
   170  				"total reserved ports is null",
   171  				[]byte(`{"total_routes":4,"total_reserved_ports":null}`),
   172  				RouteLimit{
   173  					TotalRoutes:        &types.NullInt{IsSet: true, Value: 4},
   174  					TotalReservedPorts: &types.NullInt{IsSet: false, Value: 0},
   175  				}),
   176  		)
   177  
   178  	})
   179  })