From 83c81db7de76b9932c54117be8511bd5687714d1 Mon Sep 17 00:00:00 2001 From: Niko Virtala Date: Sun, 7 Apr 2019 13:33:58 +0300 Subject: [PATCH] tests with or without project name declaration. --- server/events/runtime/run_step_runner.go | 14 +-- server/events/runtime/run_step_runner_test.go | 110 +++++++++++++----- 2 files changed, 90 insertions(+), 34 deletions(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index 7622fda11b..ae87c668d2 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -24,20 +24,20 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa } baseEnvVars := os.Environ() customEnvVars := map[string]string{ - "WORKSPACE": ctx.Workspace, "ATLANTIS_TERRAFORM_VERSION": tfVersion, - "DIR": path, - "PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)), + "BASE_BRANCH_NAME": ctx.Pull.BaseBranch, "BASE_REPO_NAME": ctx.BaseRepo.Name, "BASE_REPO_OWNER": ctx.BaseRepo.Owner, + "DIR": path, + "HEAD_BRANCH_NAME": ctx.Pull.HeadBranch, "HEAD_REPO_NAME": ctx.HeadRepo.Name, "HEAD_REPO_OWNER": ctx.HeadRepo.Owner, - "HEAD_BRANCH_NAME": ctx.Pull.HeadBranch, - "BASE_BRANCH_NAME": ctx.Pull.BaseBranch, - "PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num), + "PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)), + "PROJECT_NAME": ctx.ProjectName, "PULL_AUTHOR": ctx.Pull.Author, + "PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num), "USER_NAME": ctx.User.Username, - "PROJECT_NAME": ctx.ProjectName, + "WORKSPACE": ctx.Workspace, } finalEnvVars := baseEnvVars diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go index a7b9a606f3..ff1758c2da 100644 --- a/server/events/runtime/run_step_runner_test.go +++ b/server/events/runtime/run_step_runner_test.go @@ -47,17 +47,32 @@ func TestRunStepRunner_Run(t *testing.T) { }, { Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE", - ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myproject-myworkspace.tfplan\n", + ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan\n", }, { - Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR project_name=$PROJECT_NAME", - ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat base_branch_name=master pull_num=2 pull_author=acme project_name=myproject\n", + Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR", + ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat base_branch_name=master pull_num=2 pull_author=acme\n", }, { Command: "echo user_name=$USER_NAME", ExpOut: "user_name=acme-user\n", }, } + casesWithProject := []struct { + Command string + ProjectName string + ExpOut string + ExpErr string + }{ + { + Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME", + ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myproject-myworkspace.tfplan project=myproject\n", + }, + { + Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR project_name=$PROJECT_NAME", + ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat base_branch_name=master pull_num=2 pull_author=acme project_name=myproject\n", + }, + } projVersion, err := version.NewVersion("v0.11.0") Ok(t, err) @@ -65,34 +80,75 @@ func TestRunStepRunner_Run(t *testing.T) { r := runtime.RunStepRunner{ DefaultTFVersion: defaultVersion, } - ctx := models.ProjectCommandContext{ - BaseRepo: models.Repo{ - Name: "basename", - Owner: "baseowner", - }, - HeadRepo: models.Repo{ - Name: "headname", - Owner: "headowner", - }, - Pull: models.PullRequest{ - Num: 2, - HeadBranch: "add-feat", - BaseBranch: "master", - Author: "acme", - }, - User: models.User{ - Username: "acme-user", - }, - Log: logging.NewNoopLogger(), - Workspace: "myworkspace", - RepoRelDir: "mydir", - TerraformVersion: projVersion, - ProjectName: "myproject", - } for _, c := range cases { t.Run(c.Command, func(t *testing.T) { tmpDir, cleanup := TempDir(t) defer cleanup() + ctx := models.ProjectCommandContext{ + BaseRepo: models.Repo{ + Name: "basename", + Owner: "baseowner", + }, + HeadRepo: models.Repo{ + Name: "headname", + Owner: "headowner", + }, + Pull: models.PullRequest{ + Num: 2, + HeadBranch: "add-feat", + BaseBranch: "master", + Author: "acme", + }, + User: models.User{ + Username: "acme-user", + }, + Log: logging.NewNoopLogger(), + Workspace: "myworkspace", + RepoRelDir: "mydir", + TerraformVersion: projVersion, + // ProjectName: "myproject", + } + out, err := r.Run(ctx, c.Command, tmpDir) + if c.ExpErr != "" { + ErrContains(t, c.ExpErr, err) + return + } + Ok(t, err) + // Replace $DIR in the exp with the actual temp dir. We do this + // here because when constructing the cases we don't yet know the + // temp dir. + expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1) + Equals(t, expOut, out) + }) + } + for _, c := range casesWithProject { + t.Run(c.Command, func(t *testing.T) { + tmpDir, cleanup := TempDir(t) + defer cleanup() + ctx := models.ProjectCommandContext{ + BaseRepo: models.Repo{ + Name: "basename", + Owner: "baseowner", + }, + HeadRepo: models.Repo{ + Name: "headname", + Owner: "headowner", + }, + Pull: models.PullRequest{ + Num: 2, + HeadBranch: "add-feat", + BaseBranch: "master", + Author: "acme", + }, + User: models.User{ + Username: "acme-user", + }, + Log: logging.NewNoopLogger(), + Workspace: "myworkspace", + RepoRelDir: "mydir", + TerraformVersion: projVersion, + ProjectName: "myproject", + } out, err := r.Run(ctx, c.Command, tmpDir) if c.ExpErr != "" { ErrContains(t, c.ExpErr, err)