github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/integration/push/interpolate_manifest_test.go (about)

     1  package push
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Push with manifest variable interpolation", func() {
    18  	var (
    19  		appName   string
    20  		instances int
    21  
    22  		manifestPath string
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		appName = helpers.NewAppName()
    27  		instances = 4
    28  
    29  		tmp, err := ioutil.TempFile("", "manifest-interpolation")
    30  		Expect(err).ToNot(HaveOccurred())
    31  		Expect(tmp.Close()).ToNot(HaveOccurred())
    32  		manifestPath = tmp.Name()
    33  
    34  		helpers.WriteManifest(manifestPath, map[string]interface{}{
    35  			"applications": []map[string]interface{}{
    36  				{
    37  					"name":      "((vars1))",
    38  					"instances": "((vars2))",
    39  				},
    40  			},
    41  		})
    42  	})
    43  
    44  	AfterEach(func() {
    45  		Expect(os.RemoveAll(manifestPath)).ToNot(HaveOccurred())
    46  	})
    47  
    48  	Context("when only `--vars-file` flags are provided", func() {
    49  		var (
    50  			tmpDir string
    51  
    52  			firstVarsFilePath  string
    53  			secondVarsFilePath string
    54  		)
    55  
    56  		BeforeEach(func() {
    57  			var err error
    58  			tmpDir, err = ioutil.TempDir("", "vars-files")
    59  			Expect(err).ToNot(HaveOccurred())
    60  		})
    61  
    62  		AfterEach(func() {
    63  			Expect(os.RemoveAll(tmpDir)).ToNot(HaveOccurred())
    64  		})
    65  
    66  		Context("when there are no duplicate variables", func() {
    67  			BeforeEach(func() {
    68  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
    69  				vars1 := fmt.Sprintf("vars1: %s", appName)
    70  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
    71  				Expect(err).ToNot(HaveOccurred())
    72  
    73  				secondVarsFilePath = filepath.Join(tmpDir, "vars2")
    74  				vars2 := fmt.Sprintf("vars2: %d", instances)
    75  				err = ioutil.WriteFile(secondVarsFilePath, []byte(vars2), 0666)
    76  				Expect(err).ToNot(HaveOccurred())
    77  			})
    78  
    79  			It("pushes the app with the interpolated values in the manifest", func() {
    80  				helpers.WithHelloWorldApp(func(dir string) {
    81  
    82  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath, "--vars-file", secondVarsFilePath)
    83  					Eventually(session).Should(Say("Getting app info\\.\\.\\."))
    84  					Eventually(session).Should(Say("Creating app with these attributes\\.\\.\\."))
    85  					Eventually(session).Should(Say("\\+\\s+name:\\s+%s", appName))
    86  					Eventually(session).Should(Say("\\+\\s+instances:\\s+%d", instances))
    87  					Eventually(session).Should(Say("Mapping routes\\.\\.\\."))
    88  					Eventually(session).Should(Say("Waiting for app to start\\.\\.\\."))
    89  					Eventually(session).Should(Say("requested state:\\s+started"))
    90  					Eventually(session).Should(Exit(0))
    91  				})
    92  				session := helpers.CF("app", appName)
    93  				Eventually(session).Should(Exit(0))
    94  			})
    95  		})
    96  
    97  		Context("when a variable in manifest is not found in var_file", func() {
    98  			BeforeEach(func() {
    99  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
   100  				vars1 := fmt.Sprintf("vars1: %s", appName)
   101  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
   102  				Expect(err).ToNot(HaveOccurred())
   103  
   104  				helpers.WriteManifest(manifestPath, map[string]interface{}{
   105  					"applications": []map[string]interface{}{
   106  						{
   107  							"name": "((not_vars))",
   108  						},
   109  					},
   110  				})
   111  			})
   112  
   113  			It("fails with error saying that variable is missing", func() {
   114  				helpers.WithHelloWorldApp(func(dir string) {
   115  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath)
   116  					Eventually(session.Err).Should(Say("Expected to find variables: not_vars"))
   117  					Eventually(session).Should(Exit(1))
   118  				})
   119  			})
   120  		})
   121  
   122  		Context("when there are duplicate variables", func() {
   123  			BeforeEach(func() {
   124  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
   125  				vars1 := fmt.Sprintf("vars1: %s\nvars2: %d", "some-garbage-appname", instances)
   126  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
   127  				Expect(err).ToNot(HaveOccurred())
   128  
   129  				secondVarsFilePath = filepath.Join(tmpDir, "vars2")
   130  				vars2 := fmt.Sprintf("vars1: %s", appName)
   131  				err = ioutil.WriteFile(secondVarsFilePath, []byte(vars2), 0666)
   132  				Expect(err).ToNot(HaveOccurred())
   133  			})
   134  
   135  			It("pushes the app using the values from the latter vars-file interpolated in the manifest", func() {
   136  				helpers.WithHelloWorldApp(func(dir string) {
   137  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath, "--vars-file", secondVarsFilePath)
   138  					Eventually(session).Should(Say("Getting app info\\.\\.\\."))
   139  					Eventually(session).Should(Say("Creating app with these attributes\\.\\.\\."))
   140  					Eventually(session).Should(Say("\\+\\s+name:\\s+%s", appName))
   141  					Eventually(session).Should(Say("\\+\\s+instances:\\s+%d", instances))
   142  					Eventually(session).Should(Say("Mapping routes\\.\\.\\."))
   143  					Eventually(session).Should(Say("Waiting for app to start\\.\\.\\."))
   144  					Eventually(session).Should(Say("requested state:\\s+started"))
   145  					Eventually(session).Should(Exit(0))
   146  				})
   147  				session := helpers.CF("app", appName)
   148  				Eventually(session).Should(Exit(0))
   149  			})
   150  		})
   151  	})
   152  
   153  	Context("when only `--var` flag vars are provided", func() {
   154  		It("replaces the variables with the provided values", func() {
   155  			helpers.WithHelloWorldApp(func(dir string) {
   156  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--var", fmt.Sprintf("vars1=%s", appName), "--var", fmt.Sprintf("vars2=%d", instances))
   157  				Eventually(session).Should(Say("Getting app info\\.\\.\\."))
   158  				Eventually(session).Should(Say("Creating app with these attributes\\.\\.\\."))
   159  				Eventually(session).Should(Say("\\+\\s+name:\\s+%s", appName))
   160  				Eventually(session).Should(Say("\\+\\s+instances:\\s+%d", instances))
   161  				Eventually(session).Should(Say("Mapping routes\\.\\.\\."))
   162  				Eventually(session).Should(Say("Waiting for app to start\\.\\.\\."))
   163  				Eventually(session).Should(Say("requested state:\\s+started"))
   164  				Eventually(session).Should(Exit(0))
   165  			})
   166  			session := helpers.CF("app", appName)
   167  			Eventually(session).Should(Exit(0))
   168  		})
   169  	})
   170  
   171  	Context("when `--vars-file` and `--var` flag vars are provided", func() {
   172  		var varsFilePath string
   173  		BeforeEach(func() {
   174  			tmp, err := ioutil.TempFile("", "varsfile-interpolation")
   175  			Expect(err).ToNot(HaveOccurred())
   176  			Expect(tmp.Close()).ToNot(HaveOccurred())
   177  
   178  			varsFilePath = tmp.Name()
   179  			vars1 := fmt.Sprintf("vars1: %s\nvars2: %d", "some-garbage-appname", instances)
   180  			Expect(ioutil.WriteFile(varsFilePath, []byte(vars1), 0666)).ToNot(HaveOccurred())
   181  		})
   182  
   183  		AfterEach(func() {
   184  			Expect(os.RemoveAll(varsFilePath)).ToNot(HaveOccurred())
   185  		})
   186  
   187  		It("overwrites the vars-file with the provided var key value pair", func() {
   188  			helpers.WithHelloWorldApp(func(dir string) {
   189  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--var", fmt.Sprintf("vars1=%s", appName), "--vars-file", varsFilePath)
   190  				Eventually(session).Should(Say("Getting app info\\.\\.\\."))
   191  				Eventually(session).Should(Say("Creating app with these attributes\\.\\.\\."))
   192  				Eventually(session).Should(Say("\\+\\s+name:\\s+%s", appName))
   193  				Eventually(session).Should(Say("\\+\\s+instances:\\s+%d", instances))
   194  				Eventually(session).Should(Say("Mapping routes\\.\\.\\."))
   195  				Eventually(session).Should(Say("Waiting for app to start\\.\\.\\."))
   196  				Eventually(session).Should(Say("requested state:\\s+started"))
   197  				Eventually(session).Should(Exit(0))
   198  			})
   199  			session := helpers.CF("app", appName)
   200  			Eventually(session).Should(Exit(0))
   201  		})
   202  	})
   203  })