Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tests from platformsh/cli to here #1510

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,23 @@ jobs:
CI_COMMIT_SHORT_SHA=$(echo $CI_COMMIT_SHA | head -c8)
./bin/platform self:build --no-composer-rebuild --yes --replace-version "$CI_COMMIT_REF_NAME"-"$CI_COMMIT_SHORT_SHA" --output platform.phar

- name: Clone main CLI repository
run: git clone https://github.com/platformsh/cli.git ./cli

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22
cache-dependency-path: cli/go.sum

- uses: actions/upload-artifact@v4
with:
name: cli-phar
path: platform.phar

- name: Run integration tests
run: |
export TEST_CLI_PATH=$(realpath "./platform.phar")
chmod +x "$TEST_CLI_PATH"
cd cli
cd go-tests
go test ./... -v

# Temporary workaround for new test (https://github.com/platformsh/cli/pull/216).
git grep -q can-create || git merge af8078a84dd0ade724e88577773b098bd2527344

go test ./tests -v
- uses: actions/upload-artifact@v4
with:
name: cli-phar
path: platform.phar

# TODO run these when upgraded for PHP 8+ compatibility
# - name: Run unit tests
Expand Down
96 changes: 96 additions & 0 deletions go-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/pkg/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)
}
}
76 changes: 76 additions & 0 deletions go-tests/app_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tests

import (
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/platformsh/cli/pkg/mockapi"
)

func TestAppList(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)

apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

projectID := "nu8ohgeizah1a"

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: "AUTO"},
},
Services: map[string]mockapi.App{},
Routes: map[string]any{},
Workers: map[string]mockapi.Worker{
"app--worker1": {
App: mockapi.App{Name: "app--worker1", Type: "golang:1.23", Size: "AUTO"},
Worker: mockapi.WorkerInfo{Commands: mockapi.Commands{Start: "sleep 60"}},
},
},
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
})

envs := []*mockapi.Environment{
main,
makeEnv(projectID, "staging", "staging", "active", "main"),
makeEnv(projectID, "dev", "development", "active", "staging"),
makeEnv(projectID, "fix", "development", "inactive", "dev"),
}

apiHandler.SetEnvironments(envs)

run := runnerWithAuth(t, apiServer.URL, authServer.URL)

assert.Equal(t, strings.TrimLeft(`
Name Type
app golang:1.23
`, "\n"), run("apps", "-p", projectID, "-e", ".", "--refresh", "--format", "tsv"))

assert.Equal(t, strings.TrimLeft(`
+--------------+-------------+-------------------+
| Name | Type | Commands |
+--------------+-------------+-------------------+
| app--worker1 | golang:1.23 | start: 'sleep 60' |
+--------------+-------------+-------------------+
`, "\n"), run("workers", "-v", "-p", projectID, "-e", "."))

runCombinedOutput := runnerCombinedOutput(t, apiServer.URL, authServer.URL)
co, err := runCombinedOutput("services", "-p", projectID, "-e", "main")
require.NoError(t, err)
assert.Contains(t, co, "No services found")
}
52 changes: 52 additions & 0 deletions go-tests/auth_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tests

import (
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/platformsh/cli/pkg/mockapi"
)

func TestAuthInfo(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)
apiHandler.SetMyUser(&mockapi.User{
ID: "my-user-id",
Deactivated: false,
Namespace: "ns",
Username: "my-username",
FirstName: "Foo",
LastName: "Bar",
Email: "[email protected]",
EmailVerified: true,
Picture: "https://example.com/profile.png",
Country: "NO",
PhoneNumberVerified: true,
MFAEnabled: true,
})

apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

run := runnerWithAuth(t, apiServer.URL, authServer.URL)

assert.Equal(t, strings.TrimLeft(`
+-----------------------+---------------------+
| Property | Value |
+-----------------------+---------------------+
| id | my-user-id |
| first_name | Foo |
| last_name | Bar |
| username | my-username |
| email | [email protected] |
| phone_number_verified | true |
+-----------------------+---------------------+
`, "\n"), run("auth:info", "-v", "--refresh"))

assert.Equal(t, "my-user-id\n", run("auth:info", "-P", "id"))
}
118 changes: 118 additions & 0 deletions go-tests/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package tests

import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/platformsh/cli/pkg/mockapi"
)

func TestBackupList(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)
apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

projectID := "rai7quieroohu"

apiHandler.SetProjects([]*mockapi.Project{
{
ID: projectID,
DefaultBranch: "main",
Links: mockapi.MakeHALLinks(
"self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments",
),
},
})
main := makeEnv(projectID, "main", "production", "active", nil)
main.Links["backups"] = mockapi.HALLink{HREF: "/projects/" + projectID + "//environments/main/backups"}
apiHandler.SetEnvironments([]*mockapi.Environment{main})

created1, err := time.Parse(time.RFC3339, "2014-04-01T10:00:00+01:00")
require.NoError(t, err)
created2, err := time.Parse(time.RFC3339, "2015-04-01T10:00:00+01:00")
require.NoError(t, err)

apiHandler.SetProjectBackups(projectID, []*mockapi.Backup{
{
ID: "123",
EnvironmentID: "main",
Status: "CREATED",
Safe: true,
Restorable: true,
Automated: false,
CommitID: "foo",
CreatedAt: created1,
},
{
ID: "456",
EnvironmentID: "main",
Status: "CREATED",
Safe: false,
Restorable: true,
Automated: true,
CommitID: "bar",
CreatedAt: created2,
},
})

run := runnerWithAuth(t, apiServer.URL, authServer.URL)

assert.Equal(t, strings.TrimLeft(`
+---------------------------+-----------+------------+
| Created | Backup ID | Restorable |
+---------------------------+-----------+------------+
| 2015-04-01T09:00:00+00:00 | 456 | true |
| 2014-04-01T09:00:00+00:00 | 123 | true |
+---------------------------+-----------+------------+
`, "\n"), run("backups", "-p", projectID, "-e", "."))

assert.Equal(t, strings.TrimLeft(`
+---------------------------+-----------+------------+-----------+-----------+
| Created | Backup ID | Restorable | Automated | Commit ID |
+---------------------------+-----------+------------+-----------+-----------+
| 2015-04-01T09:00:00+00:00 | 456 | true | true | bar |
| 2014-04-01T09:00:00+00:00 | 123 | true | false | foo |
+---------------------------+-----------+------------+-----------+-----------+
`, "\n"), run("backups", "-p", projectID, "-e", ".", "--columns", "+automated,commit_id"))
}

func TestBackupCreate(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)
apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

projectID := "vei8wah5Ohl2e"

apiHandler.SetProjects([]*mockapi.Project{
{
ID: projectID,
DefaultBranch: "main",
Links: mockapi.MakeHALLinks(
"self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments",
),
},
})
main := makeEnv(projectID, "main", "production", "active", nil)
main.Links["backups"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main//backups"}
main.Links["#backup"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/backups"}
apiHandler.SetEnvironments([]*mockapi.Environment{main})

run := runnerWithAuth(t, apiServer.URL, authServer.URL)

run("backup", "-p", projectID, "-e", ".")

assert.NotEmpty(t, run("backups", "-p", projectID, "-e", "."))
}
Loading
Loading