github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/resources/events_test.go (about)

     1  package resources_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"time"
     7  
     8  	. "code.cloudfoundry.org/cli/cf/api/resources"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Event resources", func() {
    14  	var resource EventResource
    15  
    16  	Describe("New V2 resources", func() {
    17  		BeforeEach(func() {
    18  			resource = new(EventResourceNewV2)
    19  		})
    20  
    21  		It("unmarshals app crash events", func() {
    22  			err := json.Unmarshal([]byte(`
    23  			{
    24  			  "metadata": {
    25  				"guid":"event-1-guid"
    26  			  },
    27  			  "entity": {
    28  				"timestamp": "2013-10-07T16:51:07+00:00",
    29  				"type": "app.crash",
    30  				"metadata": {
    31  				  "instance": "50dd66d3f8874b35988d23a25d19bfa0",
    32  				  "index": 3,
    33  					"cell_id": "some-cell",
    34  				  "exit_status": -1,
    35  				  "exit_description": "unknown",
    36  				  "reason": "CRASHED"
    37  				}
    38  			  }
    39  			}`), &resource)
    40  
    41  			Expect(err).NotTo(HaveOccurred())
    42  
    43  			timestamp, err := time.Parse(eventTimestampFormat, "2013-10-07T16:51:07+00:00")
    44  			Expect(err).NotTo(HaveOccurred())
    45  			eventFields := resource.ToFields()
    46  			Expect(eventFields.GUID).To(Equal("event-1-guid"))
    47  			Expect(eventFields.Name).To(Equal("app.crash"))
    48  			Expect(eventFields.Timestamp).To(Equal(timestamp))
    49  			Expect(eventFields.Description).To(Equal(`index: 3, reason: CRASHED, cell_id: some-cell, instance: 50dd66d3f8874b35988d23a25d19bfa0, exit_description: unknown, exit_status: -1`))
    50  		})
    51  
    52  		It("unmarshals app update events", func() {
    53  			err := json.Unmarshal([]byte(`
    54  			{
    55  			  "metadata": {
    56  				"guid": "event-1-guid"
    57  			  },
    58  			  "entity": {
    59  				"type": "audit.app.update",
    60  				"timestamp": "2014-01-21T00:20:11+00:00",
    61  				"metadata": {
    62  				  "request": {
    63  				  	"state": "STOPPED",
    64  					"command": "PRIVATE DATA HIDDEN",
    65  					"instances": 1,
    66  					"memory": 256,
    67  					"environment_json": "PRIVATE DATA HIDDEN"
    68  				  }
    69  				}
    70  			  }
    71  			}`), &resource)
    72  
    73  			Expect(err).NotTo(HaveOccurred())
    74  
    75  			timestamp, err := time.Parse(eventTimestampFormat, "2014-01-21T00:20:11+00:00")
    76  			Expect(err).NotTo(HaveOccurred())
    77  			eventFields := resource.ToFields()
    78  			Expect(eventFields.GUID).To(Equal("event-1-guid"))
    79  			Expect(eventFields.Name).To(Equal("audit.app.update"))
    80  			Expect(eventFields.Timestamp).To(Equal(timestamp))
    81  			Expect(eventFields.Description).To(Equal("instances: 1, memory: 256, state: STOPPED, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN"))
    82  		})
    83  
    84  		It("unmarshals app delete events", func() {
    85  			resource := new(EventResourceNewV2)
    86  			err := json.Unmarshal([]byte(`
    87  			{
    88  			  "metadata": {
    89  				"guid": "event-2-guid"
    90  			  },
    91  			  "entity": {
    92  				"type": "audit.app.delete-request",
    93  				"timestamp": "2014-01-21T18:39:09+00:00",
    94  				"metadata": {
    95  				  "request": {
    96  					"recursive": true
    97  				  }
    98  				}
    99  			  }
   100  			}`), &resource)
   101  
   102  			Expect(err).NotTo(HaveOccurred())
   103  
   104  			timestamp, err := time.Parse(eventTimestampFormat, "2014-01-21T18:39:09+00:00")
   105  			Expect(err).NotTo(HaveOccurred())
   106  			eventFields := resource.ToFields()
   107  			Expect(eventFields.GUID).To(Equal("event-2-guid"))
   108  			Expect(eventFields.Name).To(Equal("audit.app.delete-request"))
   109  			Expect(eventFields.Timestamp).To(Equal(timestamp))
   110  			Expect(eventFields.Description).To(Equal("recursive: true"))
   111  		})
   112  
   113  		It("unmarshals the new v2 app create event", func() {
   114  			resource := new(EventResourceNewV2)
   115  			err := json.Unmarshal([]byte(`
   116  			{
   117  			  "metadata": {
   118  				"guid": "event-1-guid"
   119  			  },
   120  			  "entity": {
   121  				"type": "audit.app.create",
   122  				"timestamp": "2014-01-22T19:34:16+00:00",
   123  				"metadata": {
   124  				  "request": {
   125  					"name": "java-warz",
   126  					"space_guid": "6cc20fec-0dee-4843-b875-b124bfee791a",
   127  					"production": false,
   128  					"environment_json": "PRIVATE DATA HIDDEN",
   129  					"instances": 1,
   130  					"disk_quota": 1024,
   131  					"state": "STOPPED",
   132  					"console": false
   133  				  }
   134  				}
   135  			  }
   136  			}`), &resource)
   137  
   138  			Expect(err).NotTo(HaveOccurred())
   139  
   140  			timestamp, err := time.Parse(eventTimestampFormat, "2014-01-22T19:34:16+00:00")
   141  			Expect(err).NotTo(HaveOccurred())
   142  			eventFields := resource.ToFields()
   143  			Expect(eventFields.GUID).To(Equal("event-1-guid"))
   144  			Expect(eventFields.Name).To(Equal("audit.app.create"))
   145  			Expect(eventFields.Timestamp).To(Equal(timestamp))
   146  			Expect(eventFields.Description).To(Equal("disk_quota: 1024, instances: 1, state: STOPPED, environment_json: PRIVATE DATA HIDDEN"))
   147  		})
   148  	})
   149  
   150  	Describe("Old V2 Resources", func() {
   151  		BeforeEach(func() {
   152  			resource = new(EventResourceOldV2)
   153  		})
   154  
   155  		It("unmarshals app crashed events", func() {
   156  			err := json.Unmarshal([]byte(`
   157  			{
   158  			  "metadata": {
   159  				"guid": "event-1-guid"
   160  			  },
   161  			  "entity": {
   162  				"timestamp": "2014-01-22T19:34:16+00:00",
   163  				"exit_status": 3,
   164  				"instance_index": 4,
   165  				"exit_description": "the exit description"
   166  			  }
   167  			}`), &resource)
   168  
   169  			Expect(err).NotTo(HaveOccurred())
   170  
   171  			timestamp, err := time.Parse(eventTimestampFormat, "2014-01-22T19:34:16+00:00")
   172  			Expect(err).NotTo(HaveOccurred())
   173  			eventFields := resource.ToFields()
   174  			Expect(eventFields.GUID).To(Equal("event-1-guid"))
   175  			Expect(eventFields.Name).To(Equal("app crashed"))
   176  			Expect(eventFields.Timestamp).To(Equal(timestamp))
   177  			Expect(eventFields.Description).To(Equal("instance: 4, reason: the exit description, exit_status: 3"))
   178  		})
   179  	})
   180  })
   181  
   182  const eventTimestampFormat = "2006-01-02T15:04:05-07:00"