github.com/chenbh/concourse/v6@v6.4.2/atc/resource/resource_put_test.go (about) 1 package resource_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "io" 8 "strings" 9 10 "github.com/chenbh/concourse/v6/atc" 11 "github.com/chenbh/concourse/v6/atc/resource" 12 "github.com/chenbh/concourse/v6/atc/runtime" 13 "github.com/chenbh/concourse/v6/atc/runtime/runtimefakes" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("Resource Put", func() { 21 var ( 22 ctx context.Context 23 someProcessSpec runtime.ProcessSpec 24 fakeRunnable runtimefakes.FakeRunner 25 26 putVersionResult runtime.VersionResult 27 28 source atc.Source 29 params atc.Params 30 version atc.Version 31 32 resource resource.Resource 33 34 putErr error 35 ) 36 37 BeforeEach(func() { 38 ctx = context.Background() 39 40 source = atc.Source{"some": "source"} 41 version = atc.Version{"some": "version"} 42 params = atc.Params{"some": "params"} 43 44 someProcessSpec.Path = "some/fake/path" 45 someProcessSpec.Args = []string{"some/foo-dir"} 46 someProcessSpec.StderrWriter = gbytes.NewBuffer() 47 48 resource = resourceFactory.NewResource(source, params, version) 49 50 }) 51 52 JustBeforeEach(func() { 53 putVersionResult, putErr = resource.Put(ctx, someProcessSpec, &fakeRunnable) 54 }) 55 56 Context("when Runnable -> RunScript succeeds and returns a Version", func() { 57 BeforeEach(func() { 58 fakeRunnable.RunScriptStub = func(i context.Context, s string, strings []string, bytes []byte, versionResult interface{}, writer io.Writer, b bool) error { 59 err := json.Unmarshal([]byte(`{"version": {"ref":"v1"}}`), &versionResult) 60 if err != nil { 61 return err 62 } 63 64 return nil 65 } 66 }) 67 68 It("Invokes Runnable -> RunScript with the correct arguments", func() { 69 actualCtx, actualSpecPath, actualArgs, actualInput, 70 actualVersionResultRef, actualSpecStdErrWriter, 71 actualRecoverableBool := fakeRunnable.RunScriptArgsForCall(0) 72 73 signature, err := resource.Signature() 74 Expect(err).ToNot(HaveOccurred()) 75 76 Expect(actualCtx).To(Equal(ctx)) 77 Expect(actualSpecPath).To(Equal(someProcessSpec.Path)) 78 Expect(actualArgs).To(Equal(someProcessSpec.Args)) 79 Expect(actualInput).To(Equal(signature)) 80 Expect(actualVersionResultRef).To(Equal(&putVersionResult)) 81 Expect(actualSpecStdErrWriter).To(Equal(someProcessSpec.StderrWriter)) 82 Expect(actualRecoverableBool).To(BeTrue()) 83 }) 84 85 It("doesnt return an error", func() { 86 Expect(putErr).To(BeNil()) 87 }) 88 }) 89 Context("when Runnable -> RunScript succeeds and does NOT return a Version", func() { 90 BeforeEach(func() { 91 fakeRunnable.RunScriptReturns(nil) 92 }) 93 It("returns a corresponding error", func() { 94 Expect(putErr).To(MatchError("resource script (" + someProcessSpec.Path + " " + strings.Join(someProcessSpec.Args, " ") + ") output a null version")) 95 }) 96 }) 97 98 Context("when Runnable -> RunScript returns an error", func() { 99 var disasterErr = errors.New("there was an issue") 100 BeforeEach(func() { 101 fakeRunnable.RunScriptReturns(disasterErr) 102 }) 103 It("returns the error", func() { 104 Expect(putErr).To(Equal(disasterErr)) 105 }) 106 }) 107 108 })