Esempio n. 1
0
func FakeHoistLaunchableForDir(dirName string) (*Launchable, *runit.ServiceBuilder) {
	tempDir, _ := ioutil.TempDir("", "fakeenv")
	launchableInstallDir := util.From(runtime.Caller(0)).ExpandPath(dirName)

	launchable := &Launchable{
		Location:  "testLaunchable.tar.gz",
		Id:        "testPod__testLaunchable",
		RunAs:     "testPod",
		PodEnvDir: tempDir,
		Fetcher:   uri.DefaultFetcher,
		RootDir:   launchableInstallDir,
		P2Exec:    util.From(runtime.Caller(0)).ExpandPath("fake_p2-exec"),
	}

	curUser, err := user.Current()
	if err == nil {
		launchable.RunAs = curUser.Username
	}

	sbTemp, _ := ioutil.TempDir("", "fakesvdir")
	sb := &runit.ServiceBuilder{
		RunitRoot: sbTemp,
	}

	executables, _ := launchable.Executables(sb)
	for _, exe := range executables {
		os.MkdirAll(exe.Service.Path, 0644)
	}

	return launchable, sb
}
Esempio n. 2
0
File: check.go Progetto: rudle/p2
func signBuild(artifactPath string) error {
	sigLoc := fmt.Sprintf("%s.sig", artifactPath)
	return exec.Command("gpg", "--no-default-keyring",
		"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
		"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
		"-u", "p2universe",
		"--out", sigLoc,
		"--detach-sign", artifactPath).Run()
}
Esempio n. 3
0
func signManifest(manifestPath string, workdir string) (string, error) {
	signedManifestPath := fmt.Sprintf("%s.asc", manifestPath)
	return signedManifestPath,
		exec.Command("gpg", "--no-default-keyring",
			"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
			"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
			"-u", "p2universe",
			"--output", signedManifestPath,
			"--clearsign", manifestPath).Run()
}
Esempio n. 4
0
func signBuild(artifactPath string) error {
	sigLoc := fmt.Sprintf("%s.sig", artifactPath)
	output, err := exec.Command("gpg", "--no-default-keyring",
		"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
		"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
		"-u", "p2universe",
		"--out", sigLoc,
		"--detach-sign", artifactPath).CombinedOutput()
	if err != nil {
		fmt.Println(string(output))
		return err
	}

	return nil
}
Esempio n. 5
0
// FakeServiceBuilder constructs a testServiceBuilder for use in unit tests. It is the
// caller's responsibility to always call Cleanup() on the return value to ensure that
// file system changes are removed when this test ends.
func FakeServiceBuilder() (s *testServiceBuilder) {
	root, err := ioutil.TempDir("", "runit_test")
	if err != nil {
		panic(err)
	}
	defer func() {
		// If the method exits abnormally, try to clean up the file system.
		if s == nil {
			os.RemoveAll(root)
		}
	}()
	config := filepath.Join(root, "config")
	mustMkdirAll(config)
	staging := filepath.Join(root, "staging")
	mustMkdirAll(staging)
	install := filepath.Join(root, "service")
	mustMkdirAll(install)

	bin := util.From(runtime.Caller(0)).ExpandPath("fake_servicebuilder")

	return &testServiceBuilder{
		root: root,
		ServiceBuilder: ServiceBuilder{
			ConfigRoot:     config,
			StagingRoot:    staging,
			RunitRoot:      install,
			Bin:            bin,
			testingNoChown: true,
		},
	}
}
Esempio n. 6
0
func postHelloManifest(dir string) error {
	hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
	manifest := &pods.Manifest{}
	manifest.Id = "hello"
	stanza := pods.LaunchableStanza{
		LaunchableId:   "hello",
		LaunchableType: "hoist",
		Location:       hello,
	}
	manifest.LaunchableStanzas = map[string]pods.LaunchableStanza{
		"hello": stanza,
	}
	manifestPath := path.Join(dir, "hello.yaml")

	f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	err = manifest.Write(f)
	if err != nil {
		return err
	}
	f.Close()

	manifestPath, err = signManifest(manifestPath, dir)
	if err != nil {
		return err
	}

	return exec.Command("p2-schedule", manifestPath).Run()
}
Esempio n. 7
0
File: check.go Progetto: tomzhang/p2
func postHelloManifest(dir string) error {
	hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
	builder := pods.NewManifestBuilder()
	builder.SetID("hello")
	builder.SetStatusPort(43770)
	stanzas := map[string]pods.LaunchableStanza{
		"hello": {
			LaunchableId:   "hello",
			LaunchableType: "hoist",
			Location:       hello,
		},
	}
	builder.SetLaunchables(stanzas)
	manifest := builder.GetManifest()
	manifestPath := path.Join(dir, "hello.yaml")

	f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	err = manifest.Write(f)
	if err != nil {
		return err
	}
	f.Close()

	manifestPath, err = signManifest(manifestPath, dir)
	if err != nil {
		return err
	}

	return exec.Command("p2-schedule", manifestPath).Run()
}
Esempio n. 8
0
File: check.go Progetto: tomzhang/p2
func getConsulManifest(dir string) (string, error) {
	consulTar := fmt.Sprintf(
		"file://%s",
		util.From(runtime.Caller(0)).ExpandPath("../hoisted-consul_052.tar.gz"),
	)
	builder := pods.NewManifestBuilder()
	builder.SetID("consul")
	stanzas := map[string]pods.LaunchableStanza{
		"consul": {
			LaunchableId:   "consul",
			LaunchableType: "hoist",
			Location:       consulTar,
		},
	}
	builder.SetLaunchables(stanzas)
	manifest := builder.GetManifest()

	consulPath := path.Join(dir, "consul.yaml")
	f, err := os.OpenFile(consulPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return "", err
	}
	defer f.Close()

	err = manifest.Write(f)
	if err != nil {
		return "", err
	}
	return consulPath, f.Close()
}
Esempio n. 9
0
// Writes a pod manifest for the hello pod at with the specified name in the
// specified dir, configured to run on the specified port. Returns the path to
// the signed manifest
func writeHelloManifest(dir string, manifestName string, port int) (string, error) {
	hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
	builder := manifest.NewBuilder()
	builder.SetID("hello")
	builder.SetStatusPort(port)
	builder.SetStatusHTTP(true)
	stanzas := map[launch.LaunchableID]launch.LaunchableStanza{
		"hello": {
			LaunchableType: "hoist",
			Location:       hello,
		},
	}
	builder.SetLaunchables(stanzas)
	builder.SetConfig(map[interface{}]interface{}{
		"port": port,
	})
	manifest := builder.GetManifest()

	manifestPath := filepath.Join(dir, manifestName)
	f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return "", err
	}
	defer f.Close()
	err = manifest.Write(f)
	if err != nil {
		return "", err
	}

	return signManifest(manifestPath, dir)
}
Esempio n. 10
0
func testManifest(t *testing.T) pods.Manifest {
	manifestPath := util.From(runtime.Caller(0)).ExpandPath("test_manifest.yaml")
	manifest, err := pods.ManifestFromPath(manifestPath)
	if err != nil {
		t.Fatal("No test manifest found, failing\n")
	}
	return manifest
}
Esempio n. 11
0
func testHookListener(t *testing.T) (HookListener, string, <-chan struct{}) {
	hookPrefix := kp.HOOK_TREE
	destDir, _ := ioutil.TempDir("", "pods")
	defer os.RemoveAll(destDir)
	execDir, err := ioutil.TempDir("", "exec")
	defer os.RemoveAll(execDir)
	Assert(t).IsNil(err, "should not have erred creating a tempdir")

	current, err := user.Current()
	Assert(t).IsNil(err, "test setup: could not get the current user")
	builder := manifest.NewBuilder()
	builder.SetID("users")
	builder.SetRunAsUser(current.Username)
	builder.SetLaunchables(map[launch.LaunchableID]launch.LaunchableStanza{
		"create": {
			Location:       util.From(runtime.Caller(0)).ExpandPath("hoisted-hello_def456.tar.gz"),
			LaunchableType: "hoist",
			LaunchableId:   "create",
		},
	})
	podManifest := builder.GetManifest()
	manifestBytes, err := podManifest.Marshal()
	Assert(t).IsNil(err, "manifest bytes error should have been nil")

	fakeSigner, err := openpgp.NewEntity("p2", "p2-test", "*****@*****.**", nil)
	Assert(t).IsNil(err, "NewEntity error should have been nil")

	var buf bytes.Buffer
	sigWriter, err := clearsign.Encode(&buf, fakeSigner.PrivateKey, nil)
	Assert(t).IsNil(err, "clearsign encode error should have been nil")

	sigWriter.Write(manifestBytes)
	sigWriter.Close()

	podManifest, err = manifest.FromBytes(buf.Bytes())
	Assert(t).IsNil(err, "should have generated manifest from signed bytes")

	fakeIntent := fakeStoreWithManifests(kp.ManifestResult{
		Manifest: podManifest,
	})

	hookFactory := pods.NewHookFactory(destDir, "testNode")

	listener := HookListener{
		Intent:           fakeIntent,
		HookPrefix:       hookPrefix,
		ExecDir:          execDir,
		HookFactory:      hookFactory,
		Logger:           logging.DefaultLogger,
		authPolicy:       auth.FixedKeyringPolicy{openpgp.EntityList{fakeSigner}, nil},
		artifactVerifier: auth.NopVerifier(),
		artifactRegistry: artifact.NewRegistry(nil, uri.DefaultFetcher, osversion.DefaultDetector),
	}

	return listener, destDir, fakeIntent.quit
}
Esempio n. 12
0
File: check.go Progetto: tomzhang/p2
func generatePreparerPod(workdir string) (string, error) {
	// build the artifact from HEAD
	err := exec.Command("go", "build", "github.com/square/p2/bin/p2-preparer").Run()
	if err != nil {
		return "", util.Errorf("Couldn't build preparer: %s", err)
	}
	wd, _ := os.Getwd()
	hostname, err := os.Hostname()
	if err != nil {
		return "", util.Errorf("Couldn't get hostname: %s", err)
	}
	// the test number forces the pod manifest to change every test run.
	testNumber := fmt.Sprintf("test=%d", rand.Intn(2000000000))
	cmd := exec.Command("p2-bin2pod", "--work-dir", workdir, "--id", "p2-preparer", "--config", fmt.Sprintf("node_name=%s", hostname), "--config", testNumber, wd+"/p2-preparer")
	manifestPath, err := executeBin2Pod(cmd)
	if err != nil {
		return "", err
	}

	manifest, err := pods.ManifestFromPath(manifestPath)
	if err != nil {
		return "", err
	}
	builder := manifest.GetBuilder()
	builder.SetID("p2-preparer")
	builder.SetConfig(map[interface{}]interface{}{
		"preparer": map[interface{}]interface{}{
			"auth": map[string]string{
				"type":    "keyring",
				"keyring": util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
			},
			"ca_file":     filepath.Join(certpath, "cert.pem"),
			"cert_file":   filepath.Join(certpath, "cert.pem"),
			"key_file":    filepath.Join(certpath, "key.pem"),
			"status_port": preparerStatusPort,
		},
	})
	builder.SetRunAsUser("root")
	builder.SetStatusPort(preparerStatusPort)
	builder.SetStatusHTTP(true)

	manifest = builder.GetManifest()

	manifestBytes, err := manifest.Marshal()
	if err != nil {
		return "", err
	}

	err = ioutil.WriteFile(manifestPath, manifestBytes, 0644)
	if err != nil {
		return "", err
	}

	return manifestPath, err
}
Esempio n. 13
0
File: pod_test.go Progetto: rudle/p2
func TestInstall(t *testing.T) {
	fetcher := uri.NewLoggedFetcher(nil)
	testContext := util.From(runtime.Caller(0))

	currentUser, err := user.Current()
	Assert(t).IsNil(err, "test setup: couldn't get current user")

	testLocation := testContext.ExpandPath("hoisted-hello_3c021aff048ca8117593f9c71e03b87cf72fd440.tar.gz")

	launchables := map[launch.LaunchableID]launch.LaunchableStanza{
		"hello": {
			LaunchableId:   "hello",
			Location:       testLocation,
			LaunchableType: "hoist",
		},
	}

	builder := manifest.NewBuilder()
	builder.SetID("hello")
	builder.SetLaunchables(launchables)
	builder.SetRunAsUser(currentUser.Username)
	manifest := builder.GetManifest()

	testPodDir, err := ioutil.TempDir("", "testPodDir")
	Assert(t).IsNil(err, "Got an unexpected error creating a temp directory")
	defer os.RemoveAll(testPodDir)

	pod := Pod{
		Id:      "testPod",
		home:    testPodDir,
		logger:  Log.SubLogger(logrus.Fields{"pod": "testPod"}),
		Fetcher: fetcher,
	}

	err = pod.Install(manifest, auth.NopVerifier(), artifact.NewRegistry(nil, uri.DefaultFetcher, osversion.DefaultDetector))
	Assert(t).IsNil(err, "there should not have been an error when installing")

	Assert(t).AreEqual(
		fetcher.SrcUri.String(),
		testLocation,
		"The correct url wasn't set for the curl library",
	)

	hoistedHelloUnpacked := filepath.Join(testPodDir, "hello", "installs", "hello_3c021aff048ca8117593f9c71e03b87cf72fd440")
	if info, err := os.Stat(hoistedHelloUnpacked); err != nil || !info.IsDir() {
		t.Fatalf("Expected %s to be the unpacked artifact location", hoistedHelloUnpacked)
	}
	helloLaunch := filepath.Join(hoistedHelloUnpacked, "bin", "launch")
	if info, err := os.Stat(helloLaunch); err != nil || info.IsDir() {
		t.Fatalf("Expected %s to be a the launch script for hello", helloLaunch)
	}
}
Esempio n. 14
0
func TestURIWithNoProtocolTreatedLikeLocalPath(t *testing.T) {
	tempdir, err := ioutil.TempDir("", "cp-dest")
	Assert(t).IsNil(err, "Couldn't create temp dir")
	defer os.RemoveAll(tempdir)
	thisFile := util.From(runtime.Caller(0)).Filename
	copied := filepath.Join(tempdir, "copied")
	err = URICopy(thisFile, copied)
	Assert(t).IsNil(err, "The file should have been copied")
	copiedContents, err := ioutil.ReadFile(copied)
	thisContents, err := ioutil.ReadFile(thisFile)
	Assert(t).IsNil(err, "The original file could not be read")
	Assert(t).AreEqual(string(thisContents), string(copiedContents), "The contents of the files do not match")
}
Esempio n. 15
0
File: check.go Progetto: rudle/p2
func createHelloReplicationController(dir string) (fields.ID, error) {
	hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
	builder := manifest.NewBuilder()
	builder.SetID("hello")
	builder.SetStatusPort(43770)
	stanzas := map[launch.LaunchableID]launch.LaunchableStanza{
		"hello": {
			LaunchableId:   "hello",
			LaunchableType: "hoist",
			Location:       hello,
		},
	}
	builder.SetLaunchables(stanzas)
	manifest := builder.GetManifest()
	manifestPath := path.Join(dir, "hello.yaml")

	f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return fields.ID(""), err
	}
	defer f.Close()
	err = manifest.Write(f)
	if err != nil {
		return fields.ID(""), err
	}
	f.Close()

	manifestPath, err = signManifest(manifestPath, dir)
	if err != nil {
		return fields.ID(""), err
	}

	cmd := exec.Command("p2-rctl", "--log-json", "create", "--manifest", manifestPath, "--node-selector", "test=yes")
	out := bytes.Buffer{}
	cmd.Stdout = &out
	cmd.Stderr = &out
	err = cmd.Run()
	if err != nil {
		return fields.ID(""), fmt.Errorf("Couldn't create replication controller for hello: %s %s", out.String(), err)
	}
	var rctlOut struct {
		ID string `json:"id"`
	}

	err = json.Unmarshal(out.Bytes(), &rctlOut)
	if err != nil {
		return fields.ID(""), fmt.Errorf("Couldn't read RC ID out of p2-rctl invocation result: %v", err)
	}

	return fields.ID(rctlOut.ID), exec.Command("p2-rctl", "set-replicas", rctlOut.ID, "1").Run()
}
Esempio n. 16
0
func buildTestFileTree(t *testing.T, files []testFile) string {
	tempDir, err := ioutil.TempDir("", "test-artifact-verifier")
	if err != nil {
		t.Fatalf("Could not make tempdir for verification: %v", err)
	}
	artifactDir := util.From(runtime.Caller(0)).ExpandPath(testdata)
	for _, file := range files {
		err = os.Link(filepath.Join(artifactDir, string(file)), filepath.Join(tempDir, string(file)))
		if err != nil {
			t.Fatal(err)
		}
	}
	return tempDir
}
Esempio n. 17
0
func TestLoadConfigWillMarshalYaml(t *testing.T) {
	configPath := util.From(runtime.Caller(0)).ExpandPath("test_preparer_config.yaml")
	preparerConfig, err := LoadPreparerConfig(configPath)
	Assert(t).IsNil(err, "should have read config correctly")

	Assert(t).AreEqual("foohost", preparerConfig.NodeName, "did not read the node name correctly")
	Assert(t).AreEqual("0.0.0.0", preparerConfig.ConsulAddress, "did not read the consul address correctly")
	Assert(t).AreEqual("/etc/p2/hooks", preparerConfig.HooksDirectory, "did not read the hooks directory correctly")
	Assert(t).AreEqual("/etc/p2.keyring", preparerConfig.Auth["keyring"], "did not read the keyring path correctly")
	Assert(t).AreEqual(1, len(preparerConfig.ExtraLogDestinations), "should have picked up 1 log destination")
	destination := preparerConfig.ExtraLogDestinations[0]
	Assert(t).AreEqual(logging.OUT_SOCKET, destination.Type, "should have been the socket type")
	Assert(t).AreEqual("/var/log/p2-socket.out", destination.Path, "should have parsed path correctly")
}
Esempio n. 18
0
func testPreparer(t *testing.T, f *FakeStore) (*Preparer, *fakeHooks, string) {
	podRoot, _ := ioutil.TempDir("", "pod_root")
	cfg := &PreparerConfig{
		NodeName:       "hostname",
		ConsulAddress:  "0.0.0.0",
		HooksDirectory: util.From(runtime.Caller(0)).ExpandPath("test_hooks"),
		PodRoot:        podRoot,
		Auth:           map[string]interface{}{"type": "none"},
	}
	p, err := New(cfg, logging.DefaultLogger)
	Assert(t).IsNil(err, "Test setup error: should not have erred when trying to load a fake preparer")
	hooks := &fakeHooks{}
	p.hooks = hooks
	p.store = f
	return p, hooks, podRoot
}
Esempio n. 19
0
func TestInstallHooks(t *testing.T) {
	destDir, _ := ioutil.TempDir("", "pods")
	defer os.RemoveAll(destDir)
	execDir, err := ioutil.TempDir("", "exec")
	defer os.RemoveAll(execDir)
	Assert(t).IsNil(err, "should not have erred creating a tempdir")

	current, err := user.Current()
	Assert(t).IsNil(err, "test setup: could not get the current user")
	builder := manifest.NewBuilder()
	builder.SetID("users")
	builder.SetRunAsUser(current.Username)
	builder.SetLaunchables(map[launch.LaunchableID]launch.LaunchableStanza{
		"create": {
			Location:       util.From(runtime.Caller(0)).ExpandPath("testdata/hoisted-hello_def456.tar.gz"),
			LaunchableType: "hoist",
		},
	})

	podManifest := builder.GetManifest()

	hookFactory := pods.NewHookFactory(destDir, "testNode")
	hooksPod := hookFactory.NewHookPod(podManifest.ID())

	preparer := Preparer{
		hooksManifest:    podManifest,
		hooksPod:         hooksPod,
		hooksExecDir:     execDir,
		Logger:           logging.DefaultLogger,
		artifactRegistry: artifact.NewRegistry(nil, uri.DefaultFetcher, osversion.DefaultDetector),
		artifactVerifier: auth.NopVerifier(),
	}

	err = preparer.InstallHooks()
	Assert(t).IsNil(err, "There should not have been an error in the call to SyncOnce()")
	currentAlias := filepath.Join(destDir, "users", "create", "current", "bin", "launch")
	_, err = os.Stat(currentAlias)
	Assert(t).IsNil(err, fmt.Sprintf("%s should have been created", currentAlias))

	hookFile := filepath.Join(execDir, "users__create__launch")
	_, err = os.Stat(hookFile)
	Assert(t).IsNil(err, "should have created the user launch script")
}
Esempio n. 20
0
func FakeServiceBuilder() *ServiceBuilder {
	testDir := os.TempDir()
	fakeSBBinPath := util.From(runtime.Caller(0)).ExpandPath("fake_servicebuilder")
	configRoot := filepath.Join(testDir, "/etc/servicebuilder.d")
	os.MkdirAll(configRoot, 0755)
	_, err := os.Stat(configRoot)
	if err != nil {
		panic("unable to create test dir")
	}
	stagingRoot := filepath.Join(testDir, "/var/service-stage")
	os.MkdirAll(stagingRoot, 0755)
	runitRoot := filepath.Join(testDir, "/var/service")
	os.MkdirAll(runitRoot, 0755)

	return &ServiceBuilder{
		ConfigRoot:  configRoot,
		StagingRoot: stagingRoot,
		RunitRoot:   runitRoot,
		Bin:         fakeSBBinPath,
	}
}
Esempio n. 21
0
func testExtraction(t *testing.T, tarfile string,
	check func(error, string),
) {
	tarPath := util.From(runtime.Caller(0)).ExpandPath(tarfile)
	file, err := os.Open(tarPath)
	Assert(t).IsNil(err, "expected no error opening file")

	tmpdir, err := ioutil.TempDir("", "gziptest")
	defer os.RemoveAll(tmpdir)
	Assert(t).IsNil(err, "expected no error creating tempdir")

	dest := filepath.Join(tmpdir, "dest")
	err = os.Mkdir(dest, 0755)
	Assert(t).IsNil(err, "expected no error creating destdir")

	user, err := user.Current()
	Assert(t).IsNil(err, "expected no error getting current user")

	err = ExtractTarGz(user.Username, file, dest)

	check(err, dest)
}
Esempio n. 22
0
func TestInstall(t *testing.T) {
	fetcher := uri.NewLoggedFetcher(nil)
	testContext := util.From(runtime.Caller(0))

	currentUser, err := user.Current()
	Assert(t).IsNil(err, "test setup: couldn't get current user")

	testLocation := testContext.ExpandPath("hoisted-hello_def456.tar.gz")
	launchableHome, err := ioutil.TempDir("", "launchable_home")
	defer os.RemoveAll(launchableHome)

	launchable := &Launchable{
		Location:  testLocation,
		Id:        "hello",
		RunAs:     currentUser.Username,
		PodEnvDir: launchableHome,
		Fetcher:   fetcher,
		RootDir:   launchableHome,
	}

	err = launchable.Install()
	Assert(t).IsNil(err, "there should not have been an error when installing")

	Assert(t).AreEqual(
		fetcher.SrcUri,
		testLocation,
		"The correct url wasn't set for the curl library",
	)

	hoistedHelloUnpacked := path.Join(launchableHome, "installs", "hoisted-hello_def456")
	if info, err := os.Stat(hoistedHelloUnpacked); err != nil || !info.IsDir() {
		t.Fatalf("Expected %s to be the unpacked artifact location", hoistedHelloUnpacked)
	}
	helloLaunch := path.Join(hoistedHelloUnpacked, "bin", "launch")
	if info, err := os.Stat(helloLaunch); err != nil || info.IsDir() {
		t.Fatalf("Expected %s to be a the launch script for hello", helloLaunch)
	}
}
Esempio n. 23
0
func TestCorrectlyPullsFilesOverHTTP(t *testing.T) {
	tempdir, err := ioutil.TempDir("", "cp-dest")
	Assert(t).IsNil(err, "Couldn't create temp dir")
	defer os.RemoveAll(tempdir)

	copied := filepath.Join(tempdir, "copied")

	caller := util.From(runtime.Caller(0))

	ts := httptest.NewServer(http.FileServer(http.Dir(caller.Dirname())))
	defer ts.Close()
	serverURL, err := url.Parse(ts.URL)
	Assert(t).IsNil(err, "should have parsed server URL")

	serverURL.Path = filepath.Base(caller.Filename)
	err = URICopy(serverURL.String(), copied)
	Assert(t).IsNil(err, "the file should have been downloaded")

	copiedContents, err := ioutil.ReadFile(copied)
	thisContents, err := ioutil.ReadFile(caller.Filename)

	Assert(t).AreEqual(string(thisContents), string(copiedContents), "Should have downloaded the file correctly")
}
Esempio n. 24
0
func getConsulManifest(dir string) (string, error) {
	consulTar := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-consul_abc123.tar.gz"))
	manifest := &pods.Manifest{}
	manifest.Id = "consul"
	stanza := pods.LaunchableStanza{
		LaunchableId:   "consul",
		LaunchableType: "hoist",
		Location:       consulTar,
	}
	manifest.LaunchableStanzas = map[string]pods.LaunchableStanza{
		"consul": stanza,
	}
	consulPath := path.Join(dir, "consul.yaml")
	f, err := os.OpenFile(consulPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return "", err
	}
	defer f.Close()
	err = manifest.Write(f)
	if err != nil {
		return "", err
	}
	return consulPath, f.Close()
}
Esempio n. 25
0
func getTestArtifact(t *testing.T) *Artifact {
	artifactPath := util.From(runtime.Caller(0)).ExpandPath("testdata/myapp_123.tar.gz")
	art, err := NewArtifact(artifactPath)
	Assert(t).IsNil(err, "the test artifact wasn't present")
	return art
}
Esempio n. 26
0
func getUpdatedManifest(t *testing.T) Manifest {
	podPath := util.From(runtime.Caller(0)).ExpandPath("updated_manifest.yaml")
	pod, err := ManifestFromPath(podPath)
	Assert(t).IsNil(err, "couldn't read test manifest")
	return pod
}
Esempio n. 27
0
func TestHookPodsInstallAndLinkCorrectly(t *testing.T) {
	hookPrefix := "hooks"
	destDir, _ := ioutil.TempDir("", "pods")
	defer os.RemoveAll(destDir)
	execDir, err := ioutil.TempDir("", "exec")
	defer os.RemoveAll(execDir)
	Assert(t).IsNil(err, "should not have erred creating a tempdir")

	current, err := user.Current()
	Assert(t).IsNil(err, "test setup: could not get the current user")
	builder := pods.NewManifestBuilder()
	builder.SetID("users")
	builder.SetRunAsUser(current.Username)
	builder.SetLaunchables(map[string]pods.LaunchableStanza{
		"create": {
			Location:       util.From(runtime.Caller(0)).ExpandPath("hoisted-hello_def456.tar.gz"),
			LaunchableType: "hoist",
			LaunchableId:   "create",
		},
	})
	manifest := builder.GetManifest()
	manifestBytes, err := manifest.Marshal()
	Assert(t).IsNil(err, "manifest bytes error should have been nil")

	fakeSigner, err := openpgp.NewEntity("p2", "p2-test", "*****@*****.**", nil)
	Assert(t).IsNil(err, "NewEntity error should have been nil")

	var buf bytes.Buffer
	sigWriter, err := clearsign.Encode(&buf, fakeSigner.PrivateKey, nil)
	Assert(t).IsNil(err, "clearsign encode error should have been nil")

	sigWriter.Write(manifestBytes)
	sigWriter.Close()

	manifest, err = pods.ManifestFromBytes(buf.Bytes())
	Assert(t).IsNil(err, "should have generated manifest from signed bytes")

	fakeIntent := fakeStoreWithManifests(kp.ManifestResult{
		Path:     path.Join(hookPrefix, "users"),
		Manifest: manifest,
	})

	listener := HookListener{
		Intent:         fakeIntent,
		HookPrefix:     hookPrefix,
		ExecDir:        execDir,
		DestinationDir: destDir,
		Logger:         logging.DefaultLogger,
		authPolicy:     auth.FixedKeyringPolicy{openpgp.EntityList{fakeSigner}, nil},
	}

	errCh := make(chan error, 1)
	listener.Sync(fakeIntent.quit, errCh)
	select {
	case err := <-errCh:
		Assert(t).IsNil(err, "There should not have been an error in the call to sync")
	default:
	}

	currentAlias := path.Join(destDir, "users", "create", "current", "bin", "launch")
	_, err = os.Stat(currentAlias)
	Assert(t).IsNil(err, fmt.Sprintf("%s should have been created", currentAlias))

	hookFile := path.Join(execDir, "users__create__launch")
	_, err = os.Stat(hookFile)
	Assert(t).IsNil(err, "should have created the user launch script")
}
Esempio n. 28
0
func FakeChpst() string {
	return util.From(runtime.Caller(0)).ExpandPath("fake_chpst")
}
Esempio n. 29
0
func ErringSV() SV {
	return &sv{util.From(runtime.Caller(0)).ExpandPath("erring_sv")}
}
Esempio n. 30
0
func FakeSV() SV {
	return &sv{util.From(runtime.Caller(0)).ExpandPath("fake_sv")}
}