github.com/chenbh/concourse/v6@v6.4.2/testflight/task_vars_test.go (about) 1 package testflight_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gbytes" 12 "github.com/onsi/gomega/gexec" 13 ) 14 15 var _ = Describe("External Tasks", func() { 16 var fixture string 17 18 BeforeEach(func() { 19 var err error 20 21 fixture = filepath.Join(tmp, "fixture") 22 23 err = os.MkdirAll(fixture, 0755) 24 Expect(err).NotTo(HaveOccurred()) 25 }) 26 27 Context("when external task relies on template variables", func() { 28 var taskFileContents string 29 30 BeforeEach(func(){ 31 // we are testing an external task with two external variables - ((image_resource_type)) and ((echo_text)) 32 taskFileContents = `--- 33 platform: linux 34 35 image_resource: 36 type: ((image_resource_type)) 37 source: {mirror_self: true} 38 39 run: 40 path: echo 41 args: [((echo_text))] 42 ` 43 }) 44 45 JustBeforeEach(func() { 46 47 48 err := ioutil.WriteFile( 49 filepath.Join(fixture, "task.yml"), 50 []byte(taskFileContents), 51 0755, 52 ) 53 Expect(err).NotTo(HaveOccurred()) 54 55 taskUnwrapContents := `--- 56 platform: linux 57 58 image_resource: 59 type: mock 60 source: {mirror_self: true} 61 62 inputs: 63 - name: some-resource 64 65 outputs: 66 - name: unwrapped-task-resource 67 68 run: 69 path: sh 70 args: ["-c", "cat some-resource/task.yml | sed -e 's/START_VAR/((/' | sed -e 's/END_VAR/))/' > unwrapped-task-resource/task.yml"] 71 ` 72 73 // since we are using create_files() in mock resource and we don't want pipeline resource to 74 // contain unresolved variables in "task.yml":((task_content)), we will pre-process task 75 // contents and temporarily replace "((" with "START_VAR" and "))" with "END_VAR" 76 taskFileContents = strings.Replace(taskFileContents, "((", "START_VAR", -1) 77 taskFileContents = strings.Replace(taskFileContents, "))", "END_VAR", -1) 78 79 // then when we run the pipeline itself, it will contain an additional step called 'process-task-definition' 80 // to do a backwards conversion to "((" and "))" using taskUnwrapContents 81 fly("set-pipeline", "-n", "-p", pipelineName, "-c", "fixtures/task_vars.yml", "-v", "task_content="+taskFileContents+"", "-v", "task_unwrap_content="+taskUnwrapContents+"") 82 83 fly("unpause-pipeline", "-p", pipelineName) 84 }) 85 86 Context("when required vars are passed from the pipeline", func() { 87 It("successfully runs pipeline job with external task", func() { 88 execS := fly("trigger-job", "-w", "-j", pipelineName+"/external-task-success") 89 Expect(execS).To(gbytes.Say("Hello World")) 90 }) 91 }) 92 93 Context("when not all required vars are passed from the pipeline", func() { 94 It("fails pipeline job with external task due to an uninterpolated variable", func() { 95 execS := spawnFly("trigger-job", "-w", "-j", pipelineName+"/external-task-failure") 96 <-execS.Exited 97 Expect(execS).To(gexec.Exit(2)) 98 Expect(execS.Out).To(gbytes.Say("undefined vars: echo_text")) 99 }) 100 }) 101 102 Context("when required vars are passed from from command line using -v", func() { 103 It("successfully runs external task via fly execute", func() { 104 execS := flyIn(fixture, "execute", "-c", "task.yml", "-v", "image_resource_type=mock", "-v", "echo_text=Hello World From Command Line") 105 Expect(execS).To(gbytes.Say("Hello World From Command Line")) 106 }) 107 }) 108 109 Context("when required vars are passed from from command line using -v", func() { 110 It("successfully runs external task via fly execute", func() { 111 varsContents := ` 112 image_resource_type: mock 113 echo_text: Hello World From Command Line 114 ` 115 err := ioutil.WriteFile( 116 filepath.Join(fixture, "vars.yml"), 117 []byte(varsContents), 118 0755, 119 ) 120 Expect(err).NotTo(HaveOccurred()) 121 execS := flyIn(fixture, "execute", "-c", "task.yml", "-l", "vars.yml") 122 Expect(execS).To(gbytes.Say("Hello World From Command Line")) 123 }) 124 }) 125 126 Context("when not all required vars are passed from from command line", func() { 127 It("fails external task via fly execute due to an uninterpolated variable", func() { 128 execS := spawnFlyIn(fixture, "execute", "-c", "task.yml", "-v", "image_resource_type=mock") 129 <-execS.Exited 130 Expect(execS).To(gexec.Exit(2)) 131 Expect(execS.Out).To(gbytes.Say("undefined vars: echo_text")) 132 }) 133 }) 134 135 Context("when vars are from load_var", func() { 136 It("successfully runs pipeline job with external task", func() { 137 execS := fly("trigger-job", "-w", "-j", pipelineName+"/external-task-vars-from-load-var") 138 Expect(execS).To(gbytes.Say("bar")) 139 }) 140 }) 141 142 Context("when task vars are not used, task should get vars from var_sources", func() { 143 BeforeEach(func(){ 144 taskFileContents = `--- 145 platform: linux 146 147 image_resource: 148 type: ((image_resource_type)) 149 source: {mirror_self: true} 150 151 run: 152 path: echo 153 args: [((vs:echo_text))] 154 ` 155 }) 156 It("successfully runs pipeline job with external task", func() { 157 execS := fly("trigger-job", "-w", "-j", pipelineName+"/task-var-is-defined-but-task-also-needs-vars-from-var-sources") 158 Expect(execS).To(gbytes.Say("text-from-var-source")) 159 }) 160 }) 161 }) 162 163 })