Example #1
0
// NewClient initializes CIPD client object.
func NewClient(opts ClientOptions) Client {
	if opts.ServiceURL == "" {
		opts.ServiceURL = ServiceURL
	}
	if opts.Logger == nil {
		opts.Logger = logging.Null()
	}
	if opts.AnonymousClientFactory == nil {
		opts.AnonymousClientFactory = func() (*http.Client, error) { return http.DefaultClient, nil }
	}
	if opts.AuthenticatedClientFactory == nil {
		opts.AuthenticatedClientFactory = opts.AnonymousClientFactory
	}
	if opts.UserAgent == "" {
		opts.UserAgent = UserAgent
	}
	c := &clientImpl{
		ClientOptions: opts,
		clock:         &clockImpl{},
	}
	c.remote = &remoteImpl{c}
	c.storage = &storageImpl{c, uploadChunkSize}
	c.deployer = local.NewDeployer(opts.Root, opts.Logger)
	return c
}
Example #2
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 #3
0
func TestEnsurePackages(t *testing.T) {
	Convey("Mocking temp dir", t, func() {
		tempDir, err := ioutil.TempDir("", "cipd_test")
		So(err, ShouldBeNil)
		Reset(func() { os.RemoveAll(tempDir) })

		assertFile := func(relPath, data string) {
			body, err := ioutil.ReadFile(filepath.Join(tempDir, relPath))
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, data)
		}

		Convey("EnsurePackages full flow", func(c C) {
			// Prepare a bunch of packages.
			a1 := buildInstanceInMemory("pkg/a", []local.File{local.NewTestFile("file a 1", "test data", false)})
			defer a1.Close()
			a2 := buildInstanceInMemory("pkg/a", []local.File{local.NewTestFile("file a 2", "test data", false)})
			defer a2.Close()
			b := buildInstanceInMemory("pkg/b", []local.File{local.NewTestFile("file b", "test data", false)})
			defer b.Close()

			// Calls EnsurePackages, mocking fetch backend first. Backend will be mocked
			// to serve only 'fetched' packages.
			callEnsure := func(instances []local.PackageInstance, fetched []local.PackageInstance) error {
				client := mockClientForFetch(c, tempDir, fetched)
				pins := []common.Pin{}
				for _, i := range instances {
					pins = append(pins, i.Pin())
				}
				return client.EnsurePackages(pins)
			}

			findDeployed := func(root string) []common.Pin {
				deployer := local.NewDeployer(root, nil)
				pins, err := deployer.FindDeployed()
				So(err, ShouldBeNil)
				return pins
			}

			// Noop run on top of empty directory.
			err := callEnsure(nil, nil)
			So(err, ShouldBeNil)

			// Specify same package twice. Fails.
			err = callEnsure([]local.PackageInstance{a1, a2}, nil)
			So(err, ShouldNotBeNil)

			// Install a1 into a site root.
			err = callEnsure([]local.PackageInstance{a1}, []local.PackageInstance{a1})
			So(err, ShouldBeNil)
			assertFile("file a 1", "test data")
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{a1.Pin()})

			// Noop run. Nothing is fetched.
			err = callEnsure([]local.PackageInstance{a1}, nil)
			So(err, ShouldBeNil)
			assertFile("file a 1", "test data")
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{a1.Pin()})

			// Upgrade a1 to a2.
			err = callEnsure([]local.PackageInstance{a2}, []local.PackageInstance{a2})
			So(err, ShouldBeNil)
			assertFile("file a 2", "test data")
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{a2.Pin()})

			// Remove a2 and install b.
			err = callEnsure([]local.PackageInstance{b}, []local.PackageInstance{b})
			So(err, ShouldBeNil)
			assertFile("file b", "test data")
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{b.Pin()})

			// Remove b.
			err = callEnsure(nil, nil)
			So(err, ShouldBeNil)
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{})

			// Install a1 and b.
			err = callEnsure([]local.PackageInstance{a1, b}, []local.PackageInstance{a1, b})
			So(err, ShouldBeNil)
			assertFile("file a 1", "test data")
			assertFile("file b", "test data")
			So(findDeployed(tempDir), ShouldResemble, []common.Pin{a1.Pin(), b.Pin()})
		})
	})
}