Skip to content

Commit

Permalink
use manifest deploy section in deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeodor committed Jun 14, 2024
1 parent d73810e commit f939895
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
31 changes: 22 additions & 9 deletions cli/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,39 @@ var (
)

func Deploy(ctx context.Context, apps AppService, appDir, projectDir, slug string, appName string, verbose bool) error {
task := output.StartTask("Loading app configuration")
manifest, err := manifest.LoadManifest(filepath.Join(appDir, manifest.ManifestPath))
if err != nil {
task.Error()
output.PrintErrorAppNotInitialized(appDir)

return err
}

secrets := loadSecretsFromEnv(appDir)

if slug == "" && manifest.Deployment != nil {
slug = manifest.Deployment.OrganizationSlug
}

if !validate.IsValidIdentifier(slug) {
task.Error()
output.PrintError("Error: Invalid organization %q.", "Must contain only lower-case alphanumerical characters and dashes.", slug)

return ErrInvalidSlug
}

if !validate.IsValidIdentifier(appName) {
output.PrintError("Error: Invalid app name %q.", "Must contain only lower-case alphanumerical characters and dashes.", appName)
return ErrInvalidAppName
if appName == "" && manifest.Deployment != nil {
appName = manifest.Deployment.AppName
}

task := output.StartTask("Loading app configuration")
manifest, err := manifest.LoadManifest(filepath.Join(appDir, manifest.ManifestPath))
if err != nil {
if !validate.IsValidIdentifier(appName) {
task.Error()
output.PrintErrorAppNotInitialized(appDir)
output.PrintError("Error: Invalid app name %q.", "Must contain only lower-case alphanumerical characters and dashes.", appName)

return err
return ErrInvalidAppName
}

secrets := loadSecretsFromEnv(appDir)
task.Done()

task = output.StartTask("Registering new version")
Expand Down
52 changes: 50 additions & 2 deletions cli/cmd/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,64 @@ func TestDeploy(t *testing.T) {
})

t.Run("given invalid slug then it returns error", func(t *testing.T) {
err := Deploy(context.TODO(), nil, ".", "", "Some Invalid Organization Slug", appName, false)
appDir := t.TempDir()
copyTo(t, "../../testdata/streamlit_app", appDir)

err := Deploy(context.TODO(), nil, appDir, "", "Some Invalid Organization Slug", appName, false)

assert.ErrorIs(t, err, ErrInvalidSlug)
})

t.Run("given invalid app name then it returns error", func(t *testing.T) {
err := Deploy(context.TODO(), nil, ".", "", slug, "Some Invalid App Name", false)
appDir := t.TempDir()
copyTo(t, "../../testdata/streamlit_app", appDir)

err := Deploy(context.TODO(), nil, appDir, "", slug, "Some Invalid App Name", false)

assert.ErrorIs(t, err, ErrInvalidAppName)
})

t.Run("given no slug or app name arguments and manifest with deployment and then it uses manifest deployment", func(t *testing.T) {
appDir := t.TempDir()
copyTo(t, "../../testdata/streamlit_app", appDir)

apps := &mockAppService{}
apps.On("ReadApp", mock.Anything, mock.Anything).Return(app.ReadAppOutput{}, app.ErrAppNotFound)
apps.On("Create", mock.Anything, mock.Anything).Return(app.CreateAppOutput{AppID: appID}, nil)
apps.On("CreateVersion", mock.Anything, mock.Anything).Return(app.CreateAppVersionOutput{AppVersionID: appVersionID}, nil)
apps.On("AppVersionUploadURL", mock.Anything, mock.Anything).Return(app.AppVersionUploadURLOutput{UploadURL: uploadURL}, nil)
apps.On("UploadAppSource", mock.Anything, mock.Anything).Return(nil)
apps.On("DeployApp", mock.Anything, mock.Anything).Return(app.DeployAppOutput{DeploymentVersionID: deployVersionID}, nil)
apps.On("DeployEvents", mock.Anything, mock.Anything).Return(nil)

err := Deploy(context.TODO(), apps, appDir, "", "", "", false)

if assert.NoError(t, err) {
expectedInput := app.CreateAppInput{OrganizationSlug: "organization-slug-in-manifest", Name: "app-name-in-manifest", DisplayName: "Streamlit App With Deploy"}
apps.AssertCalled(t, "Create", mock.Anything, expectedInput)
}
})

t.Run("given slug or app name arguments and manifest with deployment and then arguments override manifest deployment", func(t *testing.T) {
appDir := t.TempDir()
copyTo(t, "../../testdata/streamlit_app", appDir)

apps := &mockAppService{}
apps.On("ReadApp", mock.Anything, mock.Anything).Return(app.ReadAppOutput{}, app.ErrAppNotFound)
apps.On("Create", mock.Anything, mock.Anything).Return(app.CreateAppOutput{AppID: appID}, nil)
apps.On("CreateVersion", mock.Anything, mock.Anything).Return(app.CreateAppVersionOutput{AppVersionID: appVersionID}, nil)
apps.On("AppVersionUploadURL", mock.Anything, mock.Anything).Return(app.AppVersionUploadURLOutput{UploadURL: uploadURL}, nil)
apps.On("UploadAppSource", mock.Anything, mock.Anything).Return(nil)
apps.On("DeployApp", mock.Anything, mock.Anything).Return(app.DeployAppOutput{DeploymentVersionID: deployVersionID}, nil)
apps.On("DeployEvents", mock.Anything, mock.Anything).Return(nil)

err := Deploy(context.TODO(), apps, appDir, "", "organization-slug-in-argument", "app-name-in-argument", false)

if assert.NoError(t, err) {
expectedInput := app.CreateAppInput{OrganizationSlug: "organization-slug-in-argument", Name: "app-name-in-argument", DisplayName: "Streamlit App With Deploy"}
apps.AssertCalled(t, "Create", mock.Anything, expectedInput)
}
})
}

func copyTo(t *testing.T, src string, dest string) {
Expand Down
6 changes: 5 additions & 1 deletion cli/testdata/streamlit_app/numerous.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "streamlit_example"
name = "Streamlit App With Deploy"
description = ""
library = "streamlit"
python = "3.11"
Expand All @@ -7,3 +7,7 @@ requirements_file = "requirements.txt"
port = 80
cover_image = "app_cover.jpg"
exclude = ["*venv", "venv*", ".git"]

[deploy]
organization = "organization-slug-in-manifest"
name = "app-name-in-manifest"
10 changes: 10 additions & 0 deletions cli/testdata/streamlit_app_without_deploy/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import streamlit as st

st.title("Counter Example")
count = 0

increment = st.button("Increment")
if increment:
count += 1

st.write("Count = ", count)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions cli/testdata/streamlit_app_without_deploy/numerous.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "Streamlit App Without Deploy"
description = ""
library = "streamlit"
python = "3.11"
app_file = "app.py"
requirements_file = "requirements.txt"
port = 80
cover_image = "app_cover.jpg"
exclude = ["*venv", "venv*", ".git"]
1 change: 1 addition & 0 deletions cli/testdata/streamlit_app_without_deploy/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
streamlit

0 comments on commit f939895

Please sign in to comment.