github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/resources/applications_test.go (about)

     1  package resources_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/resources"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Application resources", func() {
    14  	var resource *resources.ApplicationResource
    15  
    16  	Describe("New Application", func() {
    17  		BeforeEach(func() {
    18  			resource = new(resources.ApplicationResource)
    19  		})
    20  
    21  		It("Adds a packageUpdatedAt timestamp", func() {
    22  			err := json.Unmarshal([]byte(`
    23  			{
    24  				"metadata": {
    25  					"guid":"application-1-guid"
    26  				},
    27  				"entity": {
    28  					"package_updated_at": "2013-10-07T16:51:07+00:00"
    29  				}
    30  			}`), &resource)
    31  
    32  			Expect(err).NotTo(HaveOccurred())
    33  
    34  			applicationModel := resource.ToModel()
    35  			timestamp, err := time.Parse(eventTimestampFormat, "2013-10-07T16:51:07+00:00")
    36  			Expect(err).ToNot(HaveOccurred())
    37  			Expect(*applicationModel.PackageUpdatedAt).To(Equal(timestamp))
    38  		})
    39  	})
    40  
    41  	Describe("NewApplicationEntityFromAppParams", func() {
    42  		var (
    43  			appParams models.AppParams
    44  
    45  			diskQuota, memory                 int64
    46  			healthCheckTimeout, instanceCount int
    47  			healthCheckHTTPEndpoint           string
    48  			diego, enableSSH                  bool
    49  			packageUpdatedAt                  time.Time
    50  			appPorts                          []int
    51  			environmentVars                   map[string]interface{}
    52  
    53  			buildpackURL,
    54  			command,
    55  			healthCheckType,
    56  			dockerImage,
    57  			dockerUsername,
    58  			dockerPassword,
    59  			name,
    60  			spaceGUID,
    61  			stackGUID,
    62  			state string
    63  		)
    64  
    65  		BeforeEach(func() {
    66  			buildpackURL = "buildpack-url"
    67  			command = "command"
    68  			diskQuota = int64(1024)
    69  			environmentVars = map[string]interface{}{
    70  				"foo": "bar",
    71  				"baz": "quux",
    72  			}
    73  			healthCheckType = "none"
    74  			healthCheckTimeout = 5
    75  			healthCheckHTTPEndpoint = "/some-endpoint"
    76  			dockerImage = "docker-image"
    77  			dockerUsername = "docker-user"
    78  			dockerPassword = "docker-pass"
    79  			diego = true
    80  			enableSSH = true
    81  			instanceCount = 5
    82  			memory = int64(2048)
    83  			name = "app-name"
    84  			spaceGUID = "space-guid"
    85  			stackGUID = "stack-guid"
    86  			state = "state"
    87  			packageUpdatedAt = time.Now()
    88  			appPorts = []int{9090, 123}
    89  
    90  			appParams = models.AppParams{
    91  				BuildpackURL:            &buildpackURL,
    92  				Command:                 &command,
    93  				DiskQuota:               &diskQuota,
    94  				EnvironmentVars:         &environmentVars,
    95  				HealthCheckType:         &healthCheckType,
    96  				HealthCheckTimeout:      &healthCheckTimeout,
    97  				HealthCheckHTTPEndpoint: &healthCheckHTTPEndpoint,
    98  				DockerImage:             &dockerImage,
    99  				DockerUsername:          &dockerUsername,
   100  				DockerPassword:          &dockerPassword,
   101  				Diego:                   &diego,
   102  				EnableSSH:               &enableSSH,
   103  				InstanceCount:           &instanceCount,
   104  				Memory:                  &memory,
   105  				Name:                    &name,
   106  				SpaceGUID:               &spaceGUID,
   107  				StackGUID:               &stackGUID,
   108  				State:                   &state,
   109  				PackageUpdatedAt:        &packageUpdatedAt,
   110  				AppPorts:                &appPorts,
   111  			}
   112  		})
   113  
   114  		It("directly assigns some attributes", func() {
   115  			entity := resources.NewApplicationEntityFromAppParams(appParams)
   116  			Expect(*entity.Buildpack).To(Equal(buildpackURL))
   117  			Expect(*entity.Name).To(Equal(name))
   118  			Expect(*entity.SpaceGUID).To(Equal(spaceGUID))
   119  			Expect(*entity.Instances).To(Equal(instanceCount))
   120  			Expect(*entity.Memory).To(Equal(memory))
   121  			Expect(*entity.DiskQuota).To(Equal(diskQuota))
   122  			Expect(*entity.StackGUID).To(Equal(stackGUID))
   123  			Expect(*entity.Command).To(Equal(command))
   124  			Expect(*entity.HealthCheckType).To(Equal(healthCheckType))
   125  			Expect(*entity.HealthCheckTimeout).To(Equal(healthCheckTimeout))
   126  			Expect(*entity.HealthCheckHTTPEndpoint).To(Equal(healthCheckHTTPEndpoint))
   127  			Expect(*entity.DockerImage).To(Equal(dockerImage))
   128  			Expect(entity.DockerCredentials.Username).To(Equal(dockerUsername))
   129  			Expect(entity.DockerCredentials.Password).To(Equal(dockerPassword))
   130  			Expect(*entity.Diego).To(Equal(diego))
   131  			Expect(*entity.EnableSSH).To(Equal(enableSSH))
   132  			Expect(*entity.PackageUpdatedAt).To(Equal(packageUpdatedAt))
   133  			Expect(*entity.AppPorts).To(Equal(appPorts))
   134  		})
   135  
   136  		It("upcases the state", func() {
   137  			entity := resources.NewApplicationEntityFromAppParams(appParams)
   138  			Expect(*entity.State).To(Equal("STATE"))
   139  		})
   140  
   141  		It("does not include environment vars when they do not exist in the params", func() {
   142  			appParams.EnvironmentVars = nil
   143  			entity := resources.NewApplicationEntityFromAppParams(appParams)
   144  			Expect(entity.EnvironmentJSON).To(BeNil())
   145  		})
   146  	})
   147  })