Skip to content

Commit

Permalink
Check env file path on astro deploy (#881)
Browse files Browse the repository at this point in the history
* do not enable/disable dag deploy if already enabled/disabled

* Check env file on astro deploy

* Adding test env file

* Updating test cases to delete exported files

* Fixing tests

* wrapping error message
  • Loading branch information
kushalmalani committed Nov 17, 2022
1 parent ad9ae67 commit 5496fd5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
10 changes: 9 additions & 1 deletion cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ var (
azureUploader = azure.Upload
)

var errDagsParseFailed = errors.New("your local DAGs did not parse. Fix the listed errors or use `astro deploy [deployment-id] -f` to force deploy") //nolint:revive
var (
errDagsParseFailed = errors.New("your local DAGs did not parse. Fix the listed errors or use `astro deploy [deployment-id] -f` to force deploy") //nolint:revive
envFileMissing = errors.New("Env file path is incorrect: ") //nolint:revive
)

type deploymentInfo struct {
deploymentID string
Expand Down Expand Up @@ -248,6 +251,11 @@ func Deploy(deployInput InputDeploy, client astro.Client) error { //nolint
fmt.Sprintf("\n Deployment View: %s", ansi.Bold(deploymentURL)) +
fmt.Sprintf("\n Airflow UI: %s", ansi.Bold(deployInfo.webserverURL)))
} else {
envFileExists, _ := fileutil.Exists(deployInput.EnvFile, nil)
if !envFileExists {
return fmt.Errorf("%w %s", envFileMissing, deployInput.EnvFile)
}

if deployInfo.dagDeployEnabled && len(dagFiles) == 0 {
fmt.Println("No DAGs found. Skipping DAG deploy.")
}
Expand Down
17 changes: 12 additions & 5 deletions cloud/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDeployWithoutDagsDeploySuccess(t *testing.T) {
RuntimeID: "",
WsID: ws,
Pytest: "parse",
EnvFile: "",
EnvFile: "./testfiles/.env",
ImageName: "",
DeploymentName: "",
Prompt: true,
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestDeployWithDagsDeploySuccess(t *testing.T) {
RuntimeID: "",
WsID: ws,
Pytest: "parse",
EnvFile: "",
EnvFile: "./testfiles/.env",
ImageName: "",
DeploymentName: "",
Prompt: true,
Expand Down Expand Up @@ -257,12 +257,13 @@ func TestDeployWithDagsDeploySuccess(t *testing.T) {

os.Mkdir("./testfiles1/", os.ModePerm)
fileutil.WriteStringToFile("./testfiles1/Dockerfile", "FROM quay.io/astronomer/astro-runtime:4.2.5")
fileutil.WriteStringToFile("./testfiles1/.env", "")
deployInput = InputDeploy{
Path: "./testfiles1/",
RuntimeID: "",
WsID: ws,
Pytest: "parse",
EnvFile: "",
EnvFile: "./testfiles/.env",
ImageName: "",
DeploymentName: "",
Prompt: true,
Expand Down Expand Up @@ -594,7 +595,7 @@ func TestDeployFailure(t *testing.T) {
RuntimeID: "test-id",
WsID: ws,
Pytest: "parse",
EnvFile: "",
EnvFile: "./testfiles/.env",
ImageName: "",
DeploymentName: "",
Prompt: true,
Expand All @@ -619,7 +620,7 @@ func TestDeployFailure(t *testing.T) {
}
testUtil.InitTestConfig(testUtil.CloudPlatform)
mockClient := new(astro_mocks.Client)
mockClient.On("ListDeployments", org, ws).Return(mockDeplyResp, nil).Once()
mockClient.On("ListDeployments", org, ws).Return(mockDeplyResp, nil).Times(2)
mockClient.On("GetDeploymentConfig").Return(astro.DeploymentConfig{RuntimeReleases: []astro.RuntimeRelease{{Version: "4.2.5"}}}, nil).Once()

mockImageHandler := new(mocks.ImageHandler)
Expand Down Expand Up @@ -663,6 +664,12 @@ func TestDeployFailure(t *testing.T) {
err = Deploy(deployInput, mockClient)
assert.NoError(t, err)

defer testUtil.MockUserInput(t, "y")()
deployInput.WsID = ws
deployInput.EnvFile = "invalid-path"
err = Deploy(deployInput, mockClient)
assert.ErrorIs(t, err, envFileMissing)

mockClient.AssertExpectations(t)
mockImageHandler.AssertExpectations(t)
mockContainerHandler.AssertExpectations(t)
Expand Down
Empty file added cloud/deploy/testfiles/.env
Empty file.
11 changes: 11 additions & 0 deletions cmd/cloud/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"os"
"strings"
"testing"

astro "github.com/astronomer/astro-cli/astro-client"
Expand Down Expand Up @@ -73,4 +74,14 @@ func TestOrganizationExportAuditLogs(t *testing.T) {
_, err := execOrganizationCmd(cmdArgs...)
assert.NoError(t, err)
})

// Delete audit logs exports
currentDir, _ := os.Getwd()
files, _ := os.ReadDir(currentDir)
for _, file := range files {
if strings.HasPrefix(file.Name(), "audit-logs-") {
os.Remove(file.Name())
}
}
os.Remove("test.json")
}

0 comments on commit 5496fd5

Please sign in to comment.