github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/resources/process_resource_test.go (about) 1 package resources_test 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 7 "code.cloudfoundry.org/cli/resources" 8 "code.cloudfoundry.org/cli/types" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gstruct" 12 ) 13 14 var _ = Describe("Process", func() { 15 Describe("MarshalJSON", func() { 16 var ( 17 process resources.Process 18 processBytes []byte 19 err error 20 ) 21 22 BeforeEach(func() { 23 process = resources.Process{} 24 }) 25 26 JustBeforeEach(func() { 27 processBytes, err = process.MarshalJSON() 28 Expect(err).ToNot(HaveOccurred()) 29 }) 30 31 When("instances is provided", func() { 32 BeforeEach(func() { 33 process = resources.Process{ 34 Instances: types.NullInt{Value: 0, IsSet: true}, 35 } 36 }) 37 38 It("sets the instances to be set", func() { 39 Expect(string(processBytes)).To(MatchJSON(`{"instances": 0}`)) 40 }) 41 }) 42 43 When("memory is provided", func() { 44 BeforeEach(func() { 45 process = resources.Process{ 46 MemoryInMB: types.NullUint64{Value: 0, IsSet: true}, 47 } 48 }) 49 50 It("sets the memory to be set", func() { 51 Expect(string(processBytes)).To(MatchJSON(`{"memory_in_mb": 0}`)) 52 }) 53 }) 54 55 When("disk is provided", func() { 56 BeforeEach(func() { 57 process = resources.Process{ 58 DiskInMB: types.NullUint64{Value: 0, IsSet: true}, 59 } 60 }) 61 62 It("sets the disk to be set", func() { 63 Expect(string(processBytes)).To(MatchJSON(`{"disk_in_mb": 0}`)) 64 }) 65 }) 66 67 When("log rate limit is provided", func() { 68 BeforeEach(func() { 69 process = resources.Process{ 70 LogRateLimitInBPS: types.NullInt{Value: 1024, IsSet: true}, 71 } 72 }) 73 74 It("sets the log rate limit to be set", func() { 75 Expect(string(processBytes)).To(MatchJSON(`{"log_rate_limit_in_bytes_per_second": 1024}`)) 76 }) 77 }) 78 When("health check type http is provided", func() { 79 BeforeEach(func() { 80 process = resources.Process{ 81 HealthCheckType: constant.HTTP, 82 HealthCheckEndpoint: "some-endpoint", 83 } 84 }) 85 86 It("sets the health check type to http and has an endpoint", func() { 87 Expect(string(processBytes)).To(MatchJSON(`{"health_check":{"type":"http", "data": {"endpoint": "some-endpoint"}}}`)) 88 }) 89 }) 90 91 When("health check type port is provided", func() { 92 BeforeEach(func() { 93 process = resources.Process{ 94 HealthCheckType: constant.Port, 95 } 96 }) 97 98 It("sets the health check type to port", func() { 99 Expect(string(processBytes)).To(MatchJSON(`{"health_check":{"type":"port", "data": {}}}`)) 100 }) 101 }) 102 103 When("health check type process is provided", func() { 104 BeforeEach(func() { 105 process = resources.Process{ 106 HealthCheckType: constant.Process, 107 } 108 }) 109 110 It("sets the health check type to process", func() { 111 Expect(string(processBytes)).To(MatchJSON(`{"health_check":{"type":"process", "data": {}}}`)) 112 }) 113 }) 114 115 When("process has no fields provided", func() { 116 BeforeEach(func() { 117 process = resources.Process{} 118 }) 119 120 It("sets the health check type to process", func() { 121 Expect(string(processBytes)).To(MatchJSON(`{}`)) 122 }) 123 }) 124 }) 125 126 Describe("UnmarshalJSON", func() { 127 var ( 128 process resources.Process 129 processBytes []byte 130 err error 131 ) 132 BeforeEach(func() { 133 processBytes = []byte("{}") 134 }) 135 136 JustBeforeEach(func() { 137 err = json.Unmarshal(processBytes, &process) 138 Expect(err).ToNot(HaveOccurred()) 139 }) 140 When("log rate limit is provided", func() { 141 BeforeEach(func() { 142 processBytes = []byte(`{"log_rate_limit_in_bytes_per_second": 512}`) 143 }) 144 145 It("sets the log rate limit", func() { 146 Expect(process).To(MatchFields(IgnoreExtras, Fields{ 147 "LogRateLimitInBPS": Equal(types.NullInt{Value: 512, IsSet: true}), 148 })) 149 }) 150 }) 151 When("health check type http is provided", func() { 152 BeforeEach(func() { 153 processBytes = []byte(`{"health_check":{"type":"http", "data": {"endpoint": "some-endpoint"}}}`) 154 }) 155 156 It("sets the health check type to http and has an endpoint", func() { 157 Expect(process).To(MatchFields(IgnoreExtras, Fields{ 158 "HealthCheckType": Equal(constant.HTTP), 159 "HealthCheckEndpoint": Equal("some-endpoint"), 160 })) 161 }) 162 }) 163 164 When("health check type port is provided", func() { 165 BeforeEach(func() { 166 processBytes = []byte(`{"health_check":{"type":"port", "data": {"endpoint": null}}}`) 167 }) 168 169 It("sets the health check type to port", func() { 170 Expect(process).To(MatchFields(IgnoreExtras, Fields{ 171 "HealthCheckType": Equal(constant.Port), 172 })) 173 }) 174 }) 175 176 When("health check type process is provided", func() { 177 BeforeEach(func() { 178 processBytes = []byte(`{"health_check":{"type":"process", "data": {"endpoint": null}}}`) 179 }) 180 181 It("sets the health check type to process", func() { 182 Expect(process).To(MatchFields(IgnoreExtras, Fields{ 183 "HealthCheckType": Equal(constant.Process), 184 })) 185 }) 186 }) 187 }) 188 })