Exemple #1
0
func TestPreparerWillLaunchPreparerAsRoot(t *testing.T) {
	builder := pods.NewManifestBuilder()
	builder.SetID(POD_ID)
	builder.SetRunAsUser("root")
	illegalManifest := builder.GetManifest()
	newPair := ManifestPair{
		ID:     illegalManifest.ID(),
		Intent: illegalManifest,
	}
	testPod := &TestPod{
		launchSuccess:   true,
		currentManifest: illegalManifest,
	}

	p, hooks, fakePodRoot := testPreparer(t, &FakeStore{})
	defer p.Close()
	defer os.RemoveAll(fakePodRoot)

	success := p.resolvePair(newPair, testPod, logging.DefaultLogger)

	Assert(t).IsTrue(success, "Running preparer as root should succeed")
	Assert(t).IsTrue(hooks.ranBeforeInstall, "Should have run hooks prior to install")
	Assert(t).IsTrue(testPod.installed, "Should have installed")
	Assert(t).IsTrue(testPod.launched, "Should have attempted to launch")
	Assert(t).IsTrue(hooks.ranAfterLaunch, "Should have run after_launch hooks")
	Assert(t).IsFalse(hooks.ranAfterAuthFail, "Should not have run after_auth_fail hooks")
}
func setup(t *testing.T) (
	rcStore rcstore.Store,
	kpStore fakeKpStore,
	applicator labels.Applicator,
	rc ReplicationController) {

	rcStore = rcstore.NewFake()

	manifestBuilder := pods.NewManifestBuilder()
	manifestBuilder.SetID("testPod")
	manifest := manifestBuilder.GetManifest()

	nodeSelector := klabels.Everything().Add("nodeQuality", klabels.EqualsOperator, []string{"good"})
	podLabels := map[string]string{"podTest": "successful"}

	rcData, err := rcStore.Create(manifest, nodeSelector, podLabels)
	Assert(t).IsNil(err, "expected no error creating request")

	kpStore = fakeKpStore{manifests: make(map[string]pods.Manifest)}
	applicator = labels.NewFakeApplicator()

	rc = New(
		rcData,
		&kpStore,
		rcStore,
		NewApplicatorScheduler(applicator),
		applicator,
		logging.DefaultLogger,
	)

	return
}
Exemple #3
0
func TestPreparerLaunchesPodsThatHaveDifferentSHAs(t *testing.T) {
	builder := pods.NewManifestBuilder()
	builder.SetID("hello")
	existing := builder.GetManifest()

	testPod := &TestPod{
		launchSuccess:   true,
		haltSuccess:     true,
		currentManifest: existing,
	}
	newManifest := testManifest(t)
	newPair := ManifestPair{
		ID:      newManifest.ID(),
		Reality: existing,
		Intent:  newManifest,
	}

	p, hooks, fakePodRoot := testPreparer(t, &FakeStore{})
	defer p.Close()
	defer os.RemoveAll(fakePodRoot)
	success := p.resolvePair(newPair, testPod, logging.DefaultLogger)

	Assert(t).IsTrue(success, "should have succeeded")
	Assert(t).IsTrue(testPod.installed, "should have installed")
	Assert(t).IsTrue(testPod.launched, "should have launched")
	Assert(t).IsTrue(hooks.ranBeforeInstall, "before install should have ran")
	Assert(t).IsTrue(hooks.ranAfterLaunch, "after launch should have ran")
	Assert(t).AreEqual(newManifest, testPod.currentManifest, "the current manifest should now be the new manifest")
}
Exemple #4
0
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()
}
Exemple #5
0
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()
}
Exemple #6
0
func newManifestResult(id string) kp.ManifestResult {
	builder := pods.NewManifestBuilder()
	builder.SetID(id)
	builder.SetStatusPort(1) // StatusPort must != 0 for updatePods to use it
	return kp.ManifestResult{
		Manifest: builder.GetManifest(),
	}
}
Exemple #7
0
func main() {
	bin2pod.Version(version.VERSION)
	bin2pod.Parse(os.Args[1:])

	res := Result{}
	manifestBuilder := pods.NewManifestBuilder()
	manifestBuilder.SetID(podId())

	stanza := pods.LaunchableStanza{}
	stanza.LaunchableId = podId()
	stanza.LaunchableType = "hoist"

	workingDir := activeDir()

	err := addManifestConfig(manifestBuilder)
	tarLocation, err := makeTar(workingDir)
	if err != nil {
		log.Fatalln(err.Error())
	}
	res.TarPath = tarLocation

	if *location != "" {
		stanza.Location = *location
		res.FinalLocation = *location
	} else {
		stanza.Location = tarLocation
		res.FinalLocation = tarLocation
	}

	manifestBuilder.SetLaunchables(map[string]pods.LaunchableStanza{
		podId(): stanza,
	})

	if err != nil {
		log.Fatalln(err.Error())
	}

	manifest := manifestBuilder.GetManifest()
	res.ManifestPath, err = writeManifest(workingDir, manifest)
	if err != nil {
		log.Fatalf("Couldn't write manifest: %s", err)
	}

	b, err := json.MarshalIndent(res, "", "  ")
	if err != nil {
		log.Fatalf("Couldn't marshal output: %s", err)
	}
	_, err = os.Stdout.Write(b)
	if err != nil {
		log.Fatalf("Couldn't write to stdout: %s", err)
	}
}
Exemple #8
0
func TestJSONMarshal(t *testing.T) {
	mb := pods.NewManifestBuilder()
	mb.SetID("hello")
	m := mb.GetManifest()

	rc1 := fields.RC{
		ID:       "hello",
		Manifest: m,
	}

	b, err := json.Marshal(&rc1)
	Assert(t).IsNil(err, "should have marshaled")
	t.Log("serialized format:", string(b))

	var rc2 fields.RC
	err = json.Unmarshal(b, &rc2)
	Assert(t).IsNil(err, "should have unmarshaled")
	Assert(t).AreEqual(rc1.ID, rc2.ID, "RC ID changed when serialized")
	Assert(t).AreEqual(rc1.Manifest.ID(), rc2.Manifest.ID(), "Manifest ID changed when serialized")
}
Exemple #9
0
func basicManifest() pods.Manifest {
	builder := pods.NewManifestBuilder()
	builder.SetID(testPodId)
	return builder.GetManifest()
}
Exemple #10
0
func testManifest() pods.Manifest {
	builder := pods.NewManifestBuilder()
	builder.SetID(podId)
	return builder.GetManifest()
}
Exemple #11
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")
}
Exemple #12
0
func podWithID(id string) pods.Manifest {
	builder := pods.NewManifestBuilder()
	builder.SetID(id)
	return builder.GetManifest()
}