Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #144 from Netflix/change-environment
Browse files Browse the repository at this point in the history
Change Environment to New Netflix Style
  • Loading branch information
sargun committed Jun 21, 2018
2 parents 5c65544 + 8981120 commit cd5e6f8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- run: ./build.sh no-entrypoint
- run: ./build.sh ignore-signals
- run: ./build.sh pty
- run: ./build.sh ubuntu-env-label
ci-builder:
docker:
- image: docker:17.05.0-ce-git
Expand Down
19 changes: 19 additions & 0 deletions executor/mock/jobrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func (jobRunResponse *JobRunResponse) WaitForSuccess() bool {
return status == "TASK_FINISHED"
}

// WaitForFailure blocks on the jobs completion and returns true
// if it exits with a non-zero (user) error
func (jobRunResponse *JobRunResponse) WaitForFailure() bool {
status, err := jobRunResponse.WaitForCompletion()
if err != nil {
return false
}
return status == "TASK_FAILED"
}

// WaitForCompletion blocks on the jobs completion and returns its status
// if it completed successfully.
func (jobRunResponse *JobRunResponse) WaitForCompletion() (string, error) {
Expand Down Expand Up @@ -239,6 +249,15 @@ func RunJobExpectingSuccess(jobInput *JobInput) bool {
return jobResult.WaitForSuccess()
}

// RunJobExpectingFailure is similar to RunJob but returns true when the task completes successfully.
func RunJobExpectingFailure(jobInput *JobInput) bool {
jobRunner := NewJobRunner()
defer jobRunner.StopExecutor()

jobResult := jobRunner.StartJob(jobInput)
return jobResult.WaitForFailure()
}

// RunJob runs a single Titus task based on provided JobInput
func RunJob(jobInput *JobInput) (string, error) {
jobRunner := NewJobRunner()
Expand Down
56 changes: 56 additions & 0 deletions executor/mock/standalone/standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ var (
name: "titusoss/pty",
tag: "20180507-1525733149",
}
envLabel = testImage{
name: "titusoss/ubuntu-env-label",
tag: "20180621-1529540359",
}
)

// This file still uses log as opposed to using the testing library's built-in logging framework.
Expand Down Expand Up @@ -99,6 +103,10 @@ func TestStandalone(t *testing.T) {
testSchedBatch,
testSchedNormal,
testSchedIdle,
testNewEnvironmentLocationPositive,
testNewEnvironmentLocationNegative,
testOldEnvironmentLocationPositive,
testOldEnvironmentLocationNegative,
}
for _, fun := range testFunctions {
fullName := runtime.FuncForPC(reflect.ValueOf(fun).Pointer()).Name()
Expand Down Expand Up @@ -615,3 +623,51 @@ func testSchedIdle(t *testing.T, jobID string) {
t.Fail()
}
}

func testNewEnvironmentLocationPositive(t *testing.T, jobID string) {
ji := &mock.JobInput{
ImageName: envLabel.name,
Version: envLabel.tag,
Entrypoint: `cat /etc/nflx/base-environment.d/titus`,
JobID: jobID,
}
if !mock.RunJobExpectingSuccess(ji) {
t.Fail()
}
}

func testNewEnvironmentLocationNegative(t *testing.T, jobID string) {
ji := &mock.JobInput{
ImageName: envLabel.name,
Version: envLabel.tag,
Entrypoint: `cat /etc/profile.d/netflix_environment.sh`,
JobID: jobID,
}
if !mock.RunJobExpectingFailure(ji) {
t.Fail()
}
}

func testOldEnvironmentLocationPositive(t *testing.T, jobID string) {
ji := &mock.JobInput{
ImageName: ubuntu.name,
Version: ubuntu.tag,
Entrypoint: `cat /etc/profile.d/netflix_environment.sh`,
JobID: jobID,
}
if !mock.RunJobExpectingSuccess(ji) {
t.Fail()
}
}
func testOldEnvironmentLocationNegative(t *testing.T, jobID string) {

ji := &mock.JobInput{
ImageName: ubuntu.name,
Version: ubuntu.tag,
Entrypoint: `cat /etc/nflx/base-environment.d/titus`,
JobID: jobID,
}
if !mock.RunJobExpectingFailure(ji) {
t.Fail()
}
}
14 changes: 11 additions & 3 deletions executor/runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ func (r *DockerRuntime) Prepare(parentCtx context.Context, c *runtimeTypes.Conta
defer cancel()
var containerCreateBody container.ContainerCreateCreatedBody
dockerCreateStartTime := time.Now()
var myImageInfo *types.ImageInspect
var dockerCfg *container.Config
var hostCfg *container.HostConfig
var size int64
Expand Down Expand Up @@ -815,6 +816,8 @@ func (r *DockerRuntime) Prepare(parentCtx context.Context, c *runtimeTypes.Conta
if !r.hasEntrypoint(imageInfo, c) {
return NoEntrypointError
}

myImageInfo = &imageInfo
return nil
})

Expand Down Expand Up @@ -884,7 +887,7 @@ func (r *DockerRuntime) Prepare(parentCtx context.Context, c *runtimeTypes.Conta
}
l.Info("Titus Configuration pushed")

err = r.pushEnvironment(c)
err = r.pushEnvironment(c, myImageInfo)
l.Info("Titus environment pushed")

error:
Expand Down Expand Up @@ -1046,7 +1049,7 @@ func (r *DockerRuntime) pushMetatron(parentCtx context.Context, c *runtimeTypes.
return r.client.CopyToContainer(ctx, c.ID, "/", bytes.NewReader(tarBuf.Bytes()), cco)
}

func (r *DockerRuntime) pushEnvironment(c *runtimeTypes.Container) error {
func (r *DockerRuntime) pushEnvironment(c *runtimeTypes.Container, imageInfo *types.ImageInspect) error { // nolint: gocyclo
var envTemplateBuf, tarBuf bytes.Buffer

if err := envFileTemplate.Execute(&envTemplateBuf, c.Env); err != nil {
Expand Down Expand Up @@ -1084,8 +1087,13 @@ func (r *DockerRuntime) pushEnvironment(c *runtimeTypes.Container) error {
}
}

path := "etc/profile.d/netflix_environment.sh"
if version, ok := imageInfo.Config.Labels["nflxenv"]; ok && strings.HasPrefix(version, "1.") {
path = "etc/nflx/base-environment.d/titus"
}

hdr := &tar.Header{
Name: "etc/profile.d/netflix_environment.sh",
Name: path,
Mode: 0644,
Size: int64(envTemplateBuf.Len()),
}
Expand Down
2 changes: 2 additions & 0 deletions hack/test-images/ubuntu-env-label/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu:xenial
LABEL nflxenv=1.0

0 comments on commit cd5e6f8

Please sign in to comment.