Skip to content

Commit

Permalink
Test the app:config and mount:list commands
Browse files Browse the repository at this point in the history
Test the mount:list command
  • Loading branch information
pjcdawkins committed Oct 15, 2024
1 parent 31c7381 commit 52f22e4
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/mockapi/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
96 changes: 96 additions & 0 deletions tests/app_config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
80 changes: 80 additions & 0 deletions tests/mount_list_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}

0 comments on commit 52f22e4

Please sign in to comment.