github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/resources/security_group_resource_test.go (about)

     1  package resources_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	. "code.cloudfoundry.org/cli/resources"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Security group resource", func() {
    13  	var (
    14  		portsString       = "some-Ports"
    15  		descriptionString = "some-Description"
    16  		typeInt           = 1
    17  		codeInt           = 0
    18  		logBool           = false
    19  		trueBool          = true
    20  		falseBool         = false
    21  	)
    22  
    23  	DescribeTable("UnmarshalJSON",
    24  		func(givenBytes []byte, expectedStruct SecurityGroup) {
    25  			var actualStruct SecurityGroup
    26  			err := json.Unmarshal(givenBytes, &actualStruct)
    27  			Expect(err).ToNot(HaveOccurred())
    28  			Expect(actualStruct).To(Equal(expectedStruct))
    29  		},
    30  		Entry(
    31  			"name, guid, and rules",
    32  			[]byte(`{
    33  				"name": "security-group-name",
    34  				"guid": "security-group-guid",
    35  				"rules":[
    36  					{
    37  						"protocol":"all",
    38  						"destination":"some-Destination",
    39  						"ports":"some-Ports",
    40  						"type":1,
    41  						"code":0,
    42  						"description":"some-Description",
    43  						"log":false
    44  					}
    45  				]
    46  			}`),
    47  			SecurityGroup{
    48  				Name: "security-group-name",
    49  				GUID: "security-group-guid",
    50  				Rules: []Rule{
    51  					{
    52  						Protocol:    "all",
    53  						Destination: "some-Destination",
    54  						Ports:       &portsString,
    55  						Type:        &typeInt,
    56  						Code:        &codeInt,
    57  						Description: &descriptionString,
    58  						Log:         &logBool,
    59  					},
    60  				},
    61  			},
    62  		),
    63  		Entry(
    64  			"globally enabled",
    65  			[]byte(`{
    66  				"name": "security-group-name",
    67  				"guid": "security-group-guid",
    68  				"globally_enabled": {
    69  					"running": true,
    70  					"staging": false
    71  				}
    72  			}`),
    73  			SecurityGroup{
    74  				Name:                   "security-group-name",
    75  				GUID:                   "security-group-guid",
    76  				StagingGloballyEnabled: &falseBool,
    77  				RunningGloballyEnabled: &trueBool,
    78  			},
    79  		),
    80  		Entry(
    81  			"relationships",
    82  			[]byte(`{
    83  				"name": "security-group-name",
    84  				"guid": "security-group-guid",
    85  				"relationships": {
    86  					"staging_spaces": {
    87  					  "data": [
    88  						{ "guid": "space-guid-1" },
    89  						{ "guid": "space-guid-2" }
    90  					  ]
    91  					},
    92  					"running_spaces": {
    93  					  "data": [
    94  						{ "guid": "space-guid-3" }
    95  					  ]
    96  					}
    97  				}
    98  			}`),
    99  			SecurityGroup{
   100  				Name:              "security-group-name",
   101  				GUID:              "security-group-guid",
   102  				StagingSpaceGUIDs: []string{"space-guid-1", "space-guid-2"},
   103  				RunningSpaceGUIDs: []string{"space-guid-3"},
   104  			},
   105  		),
   106  	)
   107  
   108  	DescribeTable("MarshalJSON",
   109  		func(resource SecurityGroup, expected string) {
   110  			actualBytes, err := json.Marshal(resource)
   111  			Expect(err).ToNot(HaveOccurred())
   112  			Expect(actualBytes).To(MatchJSON(expected))
   113  		},
   114  		Entry(
   115  			"name and empty rules",
   116  			SecurityGroup{
   117  				Name:                   "security-group-name",
   118  				GUID:                   "security-group-guid",
   119  				RunningGloballyEnabled: &trueBool,
   120  				StagingGloballyEnabled: &falseBool,
   121  				Rules: []Rule{
   122  					{
   123  						Protocol:    "udp",
   124  						Destination: "another-destination",
   125  					},
   126  				},
   127  			},
   128  			`{"globally_enabled":{"running":true,"staging":false},"guid":"security-group-guid","name":"security-group-name","rules":[{"protocol":"udp","destination":"another-destination"}]}`,
   129  		),
   130  		Entry(
   131  			"only rules",
   132  			SecurityGroup{
   133  				Rules: []Rule{
   134  					{
   135  						Protocol:    "udp",
   136  						Destination: "another-destination",
   137  					},
   138  				},
   139  			},
   140  			`{"rules":[{"protocol":"udp","destination":"another-destination"}]}`,
   141  		),
   142  		Entry(
   143  			"name and rules",
   144  			SecurityGroup{
   145  				Name:                   "security-group-name",
   146  				GUID:                   "security-group-guid",
   147  				RunningGloballyEnabled: &trueBool,
   148  				StagingGloballyEnabled: &falseBool,
   149  				Rules: []Rule{
   150  					{
   151  						Protocol:    "all",
   152  						Destination: "some-Destination",
   153  						Ports:       &portsString,
   154  						Type:        &typeInt,
   155  						Code:        &codeInt,
   156  						Description: &descriptionString,
   157  						Log:         &logBool,
   158  					},
   159  				},
   160  			},
   161  			`{"globally_enabled":{"running":true,"staging":false},"guid":"security-group-guid","name":"security-group-name","rules":[{"protocol":"all","destination":"some-Destination","ports":"some-Ports","type":1,"code":0,"description":"some-Description","log":false}]}`,
   162  		),
   163  		Entry(
   164  			"globally enabled",
   165  			SecurityGroup{
   166  				Name:                   "security-group-name",
   167  				GUID:                   "security-group-guid",
   168  				RunningGloballyEnabled: &trueBool,
   169  				StagingGloballyEnabled: &falseBool,
   170  				StagingSpaceGUIDs:      []string{"space-guid-1", "space-guid-2"},
   171  				RunningSpaceGUIDs:      []string{"space-guid-3"},
   172  			},
   173  			`{"globally_enabled":{"running":true,"staging":false},"guid":"security-group-guid","name":"security-group-name","relationships":{"running_spaces":{"data":[{"guid":"space-guid-3"}]},"staging_spaces":{"data":[{"guid":"space-guid-1"},{"guid":"space-guid-2"}]}}}`,
   174  		),
   175  		Entry(
   176  			"relationships",
   177  			SecurityGroup{
   178  				Name:                   "security-group-name",
   179  				GUID:                   "security-group-guid",
   180  				RunningGloballyEnabled: &trueBool,
   181  				StagingGloballyEnabled: &falseBool,
   182  				StagingSpaceGUIDs:      []string{"space-guid-1", "space-guid-2"},
   183  				RunningSpaceGUIDs:      []string{"space-guid-3"},
   184  			},
   185  			`{"globally_enabled":{"running":true,"staging":false},"guid":"security-group-guid","name":"security-group-name","relationships":{"running_spaces":{"data":[{"guid":"space-guid-3"}]},"staging_spaces":{"data":[{"guid":"space-guid-1"},{"guid":"space-guid-2"}]}}}`,
   186  		),
   187  	)
   188  })