Ejemplo n.º 1
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"))
		})
	})
}
Ejemplo n.º 2
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()})
		})
	})
}