diff --git a/internal/mockapi/model.go b/internal/mockapi/model.go index e5dc8b7..969b322 100644 --- a/internal/mockapi/model.go +++ b/internal/mockapi/model.go @@ -86,11 +86,18 @@ func (e *Environment) SetCurrentDeployment(d *Deployment) { e.currentDeployment = d } +type Mount struct { + Source string `json:"source"` + SourcePath string `json:"source_path"` +} + type App struct { Name string `json:"name"` Type string `json:"type"` Size string `json:"size"` - Disk string `json:"disk"` + Disk int `json:"disk"` + + Mounts map[string]Mount `json:"mounts"` } type Commands struct { diff --git a/tests/app_config_test.go b/tests/app_config_test.go new file mode 100644 index 0000000..113e7c1 --- /dev/null +++ b/tests/app_config_test.go @@ -0,0 +1,96 @@ +package tests + +import ( + "encoding/base64" + "encoding/json" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/platformsh/cli/internal/mockapi" +) + +func TestAppConfig(t *testing.T) { + authServer := mockapi.NewAuthServer(t) + defer authServer.Close() + + apiHandler := mockapi.NewHandler(t) + + projectID := "aht1iegh3nei9" + + apiHandler.SetProjects([]*mockapi.Project{{ + ID: projectID, + Links: mockapi.MakeHALLinks("self=/projects/"+projectID, + "environments=/projects/"+projectID+"/environments"), + DefaultBranch: "main", + }}) + + main := makeEnv(projectID, "main", "production", "active", nil) + main.SetCurrentDeployment(&mockapi.Deployment{ + WebApps: map[string]mockapi.App{ + "app": {Name: "app", Type: "golang:1.23", Size: "M", Disk: 2048, Mounts: map[string]mockapi.Mount{}}, + }, + Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"), + }) + envs := []*mockapi.Environment{main} + + apiHandler.SetEnvironments(envs) + + apiServer := httptest.NewServer(apiHandler) + defer apiServer.Close() + + run := runnerWithAuth(t, apiServer.URL, authServer.URL) + + assert.Equal(t, strings.TrimLeft(` +name: app +type: 'golang:1.23' +size: M +disk: 2048 +mounts: { } +`, "\n"), run("app:config", "-p", projectID, "-e", ".", "--refresh")) + + assert.Equal(t, "golang:1.23\n", run("app:config", "-p", projectID, "-e", ".", "--refresh", "-P", "type")) +} + +func TestAppConfigLocal(t *testing.T) { + run := runWithLocalApp(t, &mockapi.App{ + Name: "local-app", + Type: "golang:1.24", + Size: "L", + Disk: 1024, + Mounts: map[string]mockapi.Mount{ + "example": { + Source: "local", + SourcePath: "example", + }, + }, + }) + + assert.Equal(t, strings.TrimLeft(` +name: local-app +type: 'golang:1.24' +size: L +disk: 1024 +mounts: + example: + source: local + source_path: example +`, "\n"), run("app:config")) + + assert.Equal(t, "local\n", run("app:config", "--property", "mounts.example.source")) +} + +func runWithLocalApp(t *testing.T, app *mockapi.App) func(args ...string) string { + return func(args ...string) string { + j, err := json.Marshal(app) + require.NoError(t, err) + cmd := command(t, args...) + cmd.Env = append(cmd.Env, "PLATFORM_APPLICATION="+base64.StdEncoding.EncodeToString(j)) + b, err := cmd.Output() + require.NoError(t, err) + return string(b) + } +} diff --git a/tests/mount_list_test.go b/tests/mount_list_test.go new file mode 100644 index 0000000..4e685c0 --- /dev/null +++ b/tests/mount_list_test.go @@ -0,0 +1,80 @@ +package tests + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/platformsh/cli/internal/mockapi" +) + +func TestMountList(t *testing.T) { + authServer := mockapi.NewAuthServer(t) + defer authServer.Close() + + apiHandler := mockapi.NewHandler(t) + + apiServer := httptest.NewServer(apiHandler) + defer apiServer.Close() + + projectID := "oa3chu0foot4s" + + apiHandler.SetProjects([]*mockapi.Project{{ + ID: projectID, + Links: mockapi.MakeHALLinks("self=/projects/"+projectID, + "environments=/projects/"+projectID+"/environments"), + DefaultBranch: "main", + }}) + + main := makeEnv(projectID, "main", "production", "active", nil) + main.Links["pf:ssh:app"] = mockapi.HALLink{HREF: "ssh://" + projectID + "--app@ssh.example.com"} + main.SetCurrentDeployment(&mockapi.Deployment{ + WebApps: map[string]mockapi.App{ + "app": { + Name: "app", + Type: "golang:1.23", + Size: "AUTO", + Mounts: map[string]mockapi.Mount{ + "/public/sites/default/files": {Source: "local", SourcePath: "files"}, + }}, + }, + Services: map[string]mockapi.App{}, + Routes: map[string]any{}, + Workers: map[string]mockapi.Worker{}, + Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"), + }) + + apiHandler.SetEnvironments([]*mockapi.Environment{main}) + + run := runnerWithAuth(t, apiServer.URL, authServer.URL) + + assert.Equal(t, strings.TrimLeft(` +Mount path Definition +public/sites/default/files "source: local +source_path: files" +`, "\n"), run("mounts", "-p", projectID, "-e", "main", "--refresh", "--format", "tsv")) + + assert.Equal(t, "public/sites/default/files\n", run("mounts", "-p", projectID, "-e", "main", "--paths")) +} + +func TestMountListLocal(t *testing.T) { + run := runWithLocalApp(t, &mockapi.App{ + Name: "local-app", + Type: "golang:1.24", + Size: "L", + Mounts: map[string]mockapi.Mount{ + "/tmp": {Source: "local", SourcePath: "tmp"}, + }, + }) + + assert.Equal(t, strings.TrimLeft(` ++------------+------------------+ +| Mount path | Definition | ++------------+------------------+ +| tmp | source: local | +| | source_path: tmp | ++------------+------------------+ +`, "\n"), run("mounts")) +}