// buildInstanceInMemory makes fully functional PackageInstance object that uses // memory buffer as a backing store. func buildInstanceInMemory(pkgName string, files []local.File) local.PackageInstance { out := bytes.Buffer{} err := local.BuildInstance(local.BuildInstanceOptions{ Input: files, Output: &out, PackageName: pkgName, }) So(err, ShouldBeNil) inst, err := local.OpenInstance(bytes.NewReader(out.Bytes()), "") So(err, ShouldBeNil) return inst }
func buildInstanceFile(instanceFile string, inputOpts InputOptions) error { // Read the list of files to add to the package. buildOpts, err := inputOpts.prepareInput() if err != nil { return err } // Prepare the destination, update build options with io.Writer to it. out, err := os.OpenFile(instanceFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { return err } buildOpts.Output = out // Build the package. err = local.BuildInstance(buildOpts) out.Close() if err != nil { os.Remove(instanceFile) return err } return nil }
func TestRegisterInstance(t *testing.T) { Convey("Mocking a package instance", t, func() { // Build an empty package to be uploaded. out := bytes.Buffer{} err := local.BuildInstance(local.BuildInstanceOptions{ Input: []local.File{}, Output: &out, PackageName: "testing", }) So(err, ShouldBeNil) // Open it for reading. inst, err := local.OpenInstance(bytes.NewReader(out.Bytes()), "") So(err, ShouldBeNil) Reset(func() { inst.Close() }) Convey("RegisterInstance full flow", func(c C) { client := mockClient(c, "", []expectedHTTPCall{ { Method: "POST", Path: "/_ah/api/repo/v1/instance", Query: url.Values{ "instance_id": []string{inst.Pin().InstanceID}, "package_name": []string{inst.Pin().PackageName}, }, Reply: `{ "status": "UPLOAD_FIRST", "upload_session_id": "12345", "upload_url": "http://localhost" }`, }, { Method: "POST", Path: "/_ah/api/cas/v1/finalize/12345", Reply: `{"status":"PUBLISHED"}`, }, { Method: "POST", Path: "/_ah/api/repo/v1/instance", Query: url.Values{ "instance_id": []string{inst.Pin().InstanceID}, "package_name": []string{inst.Pin().PackageName}, }, Reply: `{ "status": "REGISTERED", "instance": { "registered_by": "user:[email protected]", "registered_ts": "0" } }`, }, }) client.storage = &mockedStorage{c, nil} err = client.RegisterInstance(inst) So(err, ShouldBeNil) }) Convey("RegisterInstance already registered", func(c C) { client := mockClient(c, "", []expectedHTTPCall{ { Method: "POST", Path: "/_ah/api/repo/v1/instance", Query: url.Values{ "instance_id": []string{inst.Pin().InstanceID}, "package_name": []string{inst.Pin().PackageName}, }, Reply: `{ "status": "ALREADY_REGISTERED", "instance": { "registered_by": "user:[email protected]", "registered_ts": "0" } }`, }, }) client.storage = &mockedStorage{c, nil} err = client.RegisterInstance(inst) So(err, ShouldBeNil) }) }) }