Example #1
0
func registerInstanceFile(instanceFile string, refsOpts RefsOptions, tagsOpts TagsOptions, serviceOpts ServiceOptions) (common.Pin, error) {
	inst, err := local.OpenInstanceFile(instanceFile, "")
	if err != nil {
		return common.Pin{}, err
	}
	defer inst.Close()
	client, err := serviceOpts.makeCipdClient("")
	if err != nil {
		return common.Pin{}, err
	}
	inspectInstance(inst, false)
	err = client.RegisterInstance(inst)
	if err != nil {
		return common.Pin{}, err
	}
	err = client.AttachTagsWhenReady(inst.Pin(), tagsOpts.tags)
	if err != nil {
		return common.Pin{}, err
	}
	for _, ref := range refsOpts.refs {
		err = client.SetRefWhenReady(ref, inst.Pin())
		if err != nil {
			return common.Pin{}, err
		}
	}
	return inst.Pin(), nil
}
Example #2
0
func inspectInstanceFile(instanceFile string, listFiles bool) (common.Pin, error) {
	inst, err := local.OpenInstanceFile(instanceFile, "")
	if err != nil {
		return common.Pin{}, err
	}
	defer inst.Close()
	inspectInstance(inst, listFiles)
	return inst.Pin(), nil
}
Example #3
0
func deployInstanceFile(root string, instanceFile string) error {
	inst, err := local.OpenInstanceFile(instanceFile, "")
	if err != nil {
		return err
	}
	defer inst.Close()
	inspectInstance(inst, false)
	_, err = local.NewDeployer(root, log).DeployInstance(inst)
	return err
}
Example #4
0
func TestFetch(t *testing.T) {
	Convey("Mocking remote services", t, func() {
		tempDir, err := ioutil.TempDir("", "cipd_test")
		So(err, ShouldBeNil)
		Reset(func() { os.RemoveAll(tempDir) })
		tempFile := filepath.Join(tempDir, "pkg")

		Convey("FetchInstance works", func(c C) {
			inst := buildInstanceInMemory("pkgname", nil)
			defer inst.Close()

			out, err := os.OpenFile(tempFile, os.O_WRONLY|os.O_CREATE, 0666)
			So(err, ShouldBeNil)
			closed := false
			defer func() {
				if !closed {
					out.Close()
				}
			}()

			client := mockClientForFetch(c, "", []local.PackageInstance{inst})
			err = client.FetchInstance(inst.Pin(), out)
			So(err, ShouldBeNil)
			out.Close()
			closed = true

			fetched, err := local.OpenInstanceFile(tempFile, "")
			So(err, ShouldBeNil)
			So(fetched.Pin(), ShouldResemble, inst.Pin())
		})

		Convey("FetchAndDeployInstance works", func(c C) {
			// Build a package instance with some file.
			inst := buildInstanceInMemory("testing/package", []local.File{
				local.NewTestFile("file", "test data", false),
			})
			defer inst.Close()

			// Install the package, fetching it from the fake server.
			client := mockClientForFetch(c, tempDir, []local.PackageInstance{inst})
			err = client.FetchAndDeployInstance(inst.Pin())
			So(err, ShouldBeNil)

			// The file from the package should be installed.
			data, err := ioutil.ReadFile(filepath.Join(tempDir, "file"))
			So(err, ShouldBeNil)
			So(data, ShouldResemble, []byte("test data"))
		})
	})
}
Example #5
0
func fetchInstanceFile(packageName, version, instanceFile string, serviceOpts ServiceOptions) error {
	client, err := serviceOpts.makeCipdClient("")
	if err != nil {
		return err
	}
	pin, err := client.ResolveVersion(packageName, version)
	if err != nil {
		return err
	}

	out, err := os.OpenFile(instanceFile, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		return err
	}
	ok := false
	defer func() {
		if !ok {
			out.Close()
			os.Remove(instanceFile)
		}
	}()

	err = client.FetchInstance(pin, out)
	if err != nil {
		return err
	}

	// Verify it (by checking that instanceID matches the file content).
	out.Close()
	ok = true
	inst, err := local.OpenInstanceFile(instanceFile, pin.InstanceID)
	if err != nil {
		os.Remove(instanceFile)
		return err
	}
	defer inst.Close()
	inspectInstance(inst, false)
	return nil
}