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