Skip to content

Commit

Permalink
Merge pull request #414 from astronomer/hotfix/astro-dev-init-set-air…
Browse files Browse the repository at this point in the history
…flow-version-improved-logging

improve error logging
  • Loading branch information
aliotta committed Jun 24, 2021
2 parents e261c0b + cf5b456 commit d0c7fa3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 32 deletions.
16 changes: 8 additions & 8 deletions airflow/airflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func Init(path string, airflowImageTag string) error {

// Map of files to create
files := map[string]string{
".dockerignore": include.Dockerignore,
"Dockerfile": fmt.Sprintf(include.Dockerfile, airflowImageTag),
".gitignore": include.Gitignore,
"packages.txt": "",
"requirements.txt": "",
".env": "",
"airflow_settings.yaml": include.Settingsyml,
"dags/example-dag.py": include.Exampledag,
".dockerignore": include.Dockerignore,
"Dockerfile": fmt.Sprintf(include.Dockerfile, airflowImageTag),
".gitignore": include.Gitignore,
"packages.txt": "",
"requirements.txt": "",
".env": "",
"airflow_settings.yaml": include.Settingsyml,
"dags/example-dag.py": include.Exampledag,
}

// Initailize directories
Expand Down
7 changes: 6 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func prepareDefaultAirflowImageTag(airflowVersion string, httpClient *airflowver
defaultImageTag = fmt.Sprintf("%s-buster-onbuild", airflowVersion)
}
} else if airflowVersion != "" {
return "", errors.New("An Error occurred: Are you authenticated? If not - you can't use the --airflow-version option")
switch t := err; t {
default:
return "", err
case houston.PermissionsErrorVerbose:
return "", errors.New("The --airflow-version flag is not supported if you're not authenticated to Astronomer. Please authenticate and try again.")
}
}

if len(defaultImageTag) == 0 {
Expand Down
116 changes: 95 additions & 21 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
Expand Down Expand Up @@ -96,26 +97,27 @@ func Test_prepareDefaultAirflowImageTag(t *testing.T) {

func Test_prepareDefaultAirflowImageTagHoustonBadRequest(t *testing.T) {
testUtil.InitTestConfig()

mockErrorResponse := `An error occurred`
// prepare fake response from updates.astronomer.io
okResponse := `{
"version": "1.0",
"available_releases": [
{
"version": "1.10.5",
"level": "new_feature",
"url": "https://github.com/astronomer/airflow/releases/tag/1.10.5-11",
"release_date": "2020-10-05T20:03:00+00:00",
"tags": [
"1.10.5-alpine3.10-onbuild",
"1.10.5-buster-onbuild",
"1.10.5-alpine3.10",
"1.10.5-buster"
],
"channel": "stable"
}
]
}`
"version": "1.0",
"available_releases": [
{
"version": "1.10.5",
"level": "new_feature",
"url": "https://github.com/astronomer/airflow/releases/tag/1.10.5-11",
"release_date": "2020-10-05T20:03:00+00:00",
"tags": [
"1.10.5-alpine3.10-onbuild",
"1.10.5-buster-onbuild",
"1.10.5-alpine3.10",
"1.10.5-buster"
],
"channel": "stable"
}
]
}`

client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Expand All @@ -126,11 +128,83 @@ func Test_prepareDefaultAirflowImageTagHoustonBadRequest(t *testing.T) {
httpClient := airflowversions.NewClient(client)

// prepare fake response from houston
emptyResp := ``
houstonClient := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(emptyResp)),
Body: ioutil.NopCloser(bytes.NewBufferString(mockErrorResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(houstonClient)

output := new(bytes.Buffer)

defaultTag, err := prepareDefaultAirflowImageTag("2.0.2", httpClient, api, output)
assert.Equal(t, err.Error(), fmt.Sprintf("API error (400): %s", mockErrorResponse))
assert.Equal(t, "", defaultTag)
}

func Test_prepareDefaultAirflowImageTagHoustonUnauthedRequest(t *testing.T) {
testUtil.InitTestConfig()
mockErrorResponse := `{
"errors": [
{
"message": "You do not have the appropriate permissions for that",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"deploymentConfig"
],
"extensions": {
"code": "FORBIDDEN",
"exception": {
"message": "You do not have the appropriate permissions for that"
}
}
}
],
"data": {
"deploymentConfig": null
}
}`
// prepare fake response from updates.astronomer.io
okResponse := `{
"version": "1.0",
"available_releases": [
{
"version": "1.10.5",
"level": "new_feature",
"url": "https://github.com/astronomer/airflow/releases/tag/1.10.5-11",
"release_date": "2020-10-05T20:03:00+00:00",
"tags": [
"1.10.5-alpine3.10-onbuild",
"1.10.5-buster-onbuild",
"1.10.5-alpine3.10",
"1.10.5-buster"
],
"channel": "stable"
}
]
}`

client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(okResponse)),
Header: make(http.Header),
}
})
httpClient := airflowversions.NewClient(client)

// prepare fake response from houston
houstonClient := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(mockErrorResponse)),
Header: make(http.Header),
}
})
Expand All @@ -139,6 +213,6 @@ func Test_prepareDefaultAirflowImageTagHoustonBadRequest(t *testing.T) {
output := new(bytes.Buffer)

defaultTag, err := prepareDefaultAirflowImageTag("2.0.2", httpClient, api, output)
assert.Error(t, err, "An Error occurred: Are you authenticated? If not - you can't use the --airflow-version option")
assert.Equal(t, "The --airflow-version flag is not supported if you're not authenticated to Astronomer. Please authenticate and try again.", err.Error())
assert.Equal(t, "", defaultTag)
}
4 changes: 2 additions & 2 deletions houston/houston.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var PermissionsError = errors.New("You do not have the appropriate permissions for that")
var PermissionsErrorVerbose = errors.New("You do not have the appropriate permissions for that: Your token has expired. Please log in again.")

// Client containers the logger and HTTPClient used to communicate with the HoustonAPI
type Client struct {
Expand Down Expand Up @@ -85,12 +86,11 @@ func (c *Client) Do(doOpts httputil.DoOptions) (*Response, error) {
if err != nil {
return nil, errors.Wrap(err, "Failed to JSON decode Houston response")
}

// Houston Specific Errors
if decode.Errors != nil {
err = errors.New(decode.Errors[0].Message)
if err.Error() == PermissionsError.Error() {
return nil, errors.Wrap(errors.New("Your token has expired. Please log in again."), err.Error())
return nil, PermissionsErrorVerbose
}
return nil, err
}
Expand Down

0 comments on commit d0c7fa3

Please sign in to comment.