func testDeployment(t testing.TB, img string, test func(k *kernel)) {

	requiresImage(t, img)

	imgName := randSeq(10)
	containerName := randSeq(10)

	tempDir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Errorf("could not create tempdir: %v", err)
		return
	}
	defer os.RemoveAll(tempDir)
	bundleFile := filepath.Join(tempDir, "bundle.json")

	var meta *ModelInfo
	var bundle []byte
	var deployErr error
	hf := func(w http.ResponseWriter, r *http.Request) {

		if r.URL.Path == "/verify" {
			handleVerify(w, r)
			return
		}
		meta, bundle, deployErr = ReadBundle(r)
		w.WriteHeader(http.StatusOK)
	}

	var s *httptest.Server

	if runtime.GOOS == "linux" {
		s = httptest.NewServer(http.HandlerFunc(hf))
	} else {
		// for macs using Boot2Docker, we need to extract the computer's local IP
		// create a listener accordingly and then create a test server
		// because otherwise Boot2Docker can't access the local IP
		s, err = NewMacTestServer(http.HandlerFunc(hf))
		if err != nil {
			t.Errorf("could not create test server for Mac: %v", err)
			return
		}
	}

	err = integration.Run(img, "bob", "apikey", s.URL+"/")
	s.Close()
	if err != nil {
		t.Errorf("could not make request: %v", err)
		return
	}
	if deployErr != nil {
		t.Errorf("could not do deployment: %v", deployErr)
		return
	}
	if err = ioutil.WriteFile(bundleFile, bundle, 0644); err != nil {
		t.Errorf("could not create bundle file: %v", err)
		return
	}
	d := DeployInfo{
		Username:         "******",
		Modelname:        meta.Modelname,
		Version:          1,
		Lang:             meta.Lang,
		LanguagePackages: meta.LanguagePackages,
		UbuntuPackages:   meta.UbuntuPackages,
	}

	run := func(cmd string, args ...string) {
		out, err := exec.Command(cmd, args...).CombinedOutput()
		if err != nil {
			t.Errorf("command %s %s failed %s", cmd, args, out)
		}
	}

	var stderr bytes.Buffer

	dockerFile, err := createDockerfile(&d)
	if err != nil {
		t.Errorf("failed to create dockerfile: %v", err)
		return
	}

	if err = buildImage(dockerFile, bundleFile, imgName, &stderr); err != nil {
		t.Errorf("could not build image %v: %s", err, stderr.String())
		return
	}
	defer run("docker", "rmi", "-f", imgName)

	cid, conn, err := startContainer(imgName, containerName)
	if err != nil {
		t.Errorf("could not create container: %v", err)
		return
	}
	defer run("docker", "rm", "-f", cid)

	stderr.Reset()
	k, err := newKernel(conn, &stderr)
	if err != nil {
		t.Errorf("could not begin kernel %v: %s", err, stderr.String())
		return
	}
	if err = k.heartbeat(); err != nil {
		t.Errorf("kernel failed heartbeat: %v", err)
		return
	}
	test(k)
}
示例#2
0
func testReadBundle(t *testing.T, reader bundleReader, imgs []string) {
	for _, img := range imgs {
		requiresImage(t, img)
	}
	var bundle []byte
	var meta *ModelInfo
	var err error

	hf := func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/verify" {
			handleVerify(w, r)
			return
		}
		meta, bundle, err = reader(r)
		if err != nil {
			t.Errorf("failed to read bundle: %v", err)
		}
		w.WriteHeader(http.StatusOK)
	}

	var s *httptest.Server

	if runtime.GOOS == "linux" {
		s = httptest.NewServer(http.HandlerFunc(hf))
	} else {
		// for Macs using Boot2Docker, we need to extract the computer's local IP
		// create a listener accordingly and then create a test server that Boot2Docker can find
		s, err = NewMacTestServer(http.HandlerFunc(hf))
		if err != nil {
			t.Errorf("could not create test server for Mac: %v", err)
			return
		}
	}

	defer s.Close()

	for _, img := range imgs {

		bundle = nil
		meta = nil

		err := integration.Run(img, "bob", "apikey", s.URL+"/")
		if err != nil {
			t.Errorf("could not make request: %v", err)
			continue
		}
		if bundle == nil {
			t.Errorf("bundle not set")
			continue
		}
		expMeta, ok := metaExp[img]
		if !ok {
			t.Errorf("no expected meta found for: %v", img)
			continue
		}
		// R doesn't do verisoning very well. Don't bother comparing
		compareMeta(t, img, meta, expMeta, expMeta.Lang == Python2)
		if meta.SourceCode == "" {
			t.Errorf("no source code detected")
		}
	}
}