Skip to content

Commit 7780b6a

Browse files
authored
Add Golang integration tests (#1510)
1 parent b31b3b2 commit 7780b6a

19 files changed

+1408
-13
lines changed

.github/workflows/ci.yml

+6-13
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,23 @@ jobs:
4646
CI_COMMIT_SHORT_SHA=$(echo $CI_COMMIT_SHA | head -c8)
4747
./bin/platform self:build --no-composer-rebuild --yes --replace-version "$CI_COMMIT_REF_NAME"-"$CI_COMMIT_SHORT_SHA" --output platform.phar
4848
49-
- name: Clone main CLI repository
50-
run: git clone https://github.com/platformsh/cli.git ./cli
51-
5249
- name: Set up Go
5350
uses: actions/setup-go@v4
5451
with:
5552
go-version: 1.22
5653
cache-dependency-path: cli/go.sum
5754

58-
- uses: actions/upload-artifact@v4
59-
with:
60-
name: cli-phar
61-
path: platform.phar
62-
6355
- name: Run integration tests
6456
run: |
6557
export TEST_CLI_PATH=$(realpath "./platform.phar")
6658
chmod +x "$TEST_CLI_PATH"
67-
cd cli
59+
cd go-tests
60+
go test ./... -v
6861
69-
# Temporary workaround for new test (https://github.com/platformsh/cli/pull/216).
70-
git grep -q can-create || git merge af8078a84dd0ade724e88577773b098bd2527344
71-
72-
go test ./tests -v
62+
- uses: actions/upload-artifact@v4
63+
with:
64+
name: cli-phar
65+
path: platform.phar
7366

7467
# TODO run these when upgraded for PHP 8+ compatibility
7568
# - name: Run unit tests

go-tests/app_config_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tests
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/platformsh/cli/pkg/mockapi"
14+
)
15+
16+
func TestAppConfig(t *testing.T) {
17+
authServer := mockapi.NewAuthServer(t)
18+
defer authServer.Close()
19+
20+
apiHandler := mockapi.NewHandler(t)
21+
22+
projectID := "aht1iegh3nei9"
23+
24+
apiHandler.SetProjects([]*mockapi.Project{{
25+
ID: projectID,
26+
Links: mockapi.MakeHALLinks("self=/projects/"+projectID,
27+
"environments=/projects/"+projectID+"/environments"),
28+
DefaultBranch: "main",
29+
}})
30+
31+
main := makeEnv(projectID, "main", "production", "active", nil)
32+
main.SetCurrentDeployment(&mockapi.Deployment{
33+
WebApps: map[string]mockapi.App{
34+
"app": {Name: "app", Type: "golang:1.23", Size: "M", Disk: 2048, Mounts: map[string]mockapi.Mount{}},
35+
},
36+
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
37+
})
38+
envs := []*mockapi.Environment{main}
39+
40+
apiHandler.SetEnvironments(envs)
41+
42+
apiServer := httptest.NewServer(apiHandler)
43+
defer apiServer.Close()
44+
45+
run := runnerWithAuth(t, apiServer.URL, authServer.URL)
46+
47+
assert.Equal(t, strings.TrimLeft(`
48+
name: app
49+
type: 'golang:1.23'
50+
size: M
51+
disk: 2048
52+
mounts: { }
53+
`, "\n"), run("app:config", "-p", projectID, "-e", ".", "--refresh"))
54+
55+
assert.Equal(t, "golang:1.23\n", run("app:config", "-p", projectID, "-e", ".", "--refresh", "-P", "type"))
56+
}
57+
58+
func TestAppConfigLocal(t *testing.T) {
59+
run := runWithLocalApp(t, &mockapi.App{
60+
Name: "local-app",
61+
Type: "golang:1.24",
62+
Size: "L",
63+
Disk: 1024,
64+
Mounts: map[string]mockapi.Mount{
65+
"example": {
66+
Source: "local",
67+
SourcePath: "example",
68+
},
69+
},
70+
})
71+
72+
assert.Equal(t, strings.TrimLeft(`
73+
name: local-app
74+
type: 'golang:1.24'
75+
size: L
76+
disk: 1024
77+
mounts:
78+
example:
79+
source: local
80+
source_path: example
81+
`, "\n"), run("app:config"))
82+
83+
assert.Equal(t, "local\n", run("app:config", "--property", "mounts.example.source"))
84+
}
85+
86+
func runWithLocalApp(t *testing.T, app *mockapi.App) func(args ...string) string {
87+
return func(args ...string) string {
88+
j, err := json.Marshal(app)
89+
require.NoError(t, err)
90+
cmd := command(t, args...)
91+
cmd.Env = append(cmd.Env, "PLATFORM_APPLICATION="+base64.StdEncoding.EncodeToString(j))
92+
b, err := cmd.Output()
93+
require.NoError(t, err)
94+
return string(b)
95+
}
96+
}

go-tests/app_list_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tests
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/platformsh/cli/pkg/mockapi"
12+
)
13+
14+
func TestAppList(t *testing.T) {
15+
authServer := mockapi.NewAuthServer(t)
16+
defer authServer.Close()
17+
18+
apiHandler := mockapi.NewHandler(t)
19+
20+
apiServer := httptest.NewServer(apiHandler)
21+
defer apiServer.Close()
22+
23+
projectID := "nu8ohgeizah1a"
24+
25+
apiHandler.SetProjects([]*mockapi.Project{{
26+
ID: projectID,
27+
Links: mockapi.MakeHALLinks("self=/projects/"+projectID,
28+
"environments=/projects/"+projectID+"/environments"),
29+
DefaultBranch: "main",
30+
}})
31+
32+
main := makeEnv(projectID, "main", "production", "active", nil)
33+
main.SetCurrentDeployment(&mockapi.Deployment{
34+
WebApps: map[string]mockapi.App{
35+
"app": {Name: "app", Type: "golang:1.23", Size: "AUTO"},
36+
},
37+
Services: map[string]mockapi.App{},
38+
Routes: map[string]any{},
39+
Workers: map[string]mockapi.Worker{
40+
"app--worker1": {
41+
App: mockapi.App{Name: "app--worker1", Type: "golang:1.23", Size: "AUTO"},
42+
Worker: mockapi.WorkerInfo{Commands: mockapi.Commands{Start: "sleep 60"}},
43+
},
44+
},
45+
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
46+
})
47+
48+
envs := []*mockapi.Environment{
49+
main,
50+
makeEnv(projectID, "staging", "staging", "active", "main"),
51+
makeEnv(projectID, "dev", "development", "active", "staging"),
52+
makeEnv(projectID, "fix", "development", "inactive", "dev"),
53+
}
54+
55+
apiHandler.SetEnvironments(envs)
56+
57+
run := runnerWithAuth(t, apiServer.URL, authServer.URL)
58+
59+
assert.Equal(t, strings.TrimLeft(`
60+
Name Type
61+
app golang:1.23
62+
`, "\n"), run("apps", "-p", projectID, "-e", ".", "--refresh", "--format", "tsv"))
63+
64+
assert.Equal(t, strings.TrimLeft(`
65+
+--------------+-------------+-------------------+
66+
| Name | Type | Commands |
67+
+--------------+-------------+-------------------+
68+
| app--worker1 | golang:1.23 | start: 'sleep 60' |
69+
+--------------+-------------+-------------------+
70+
`, "\n"), run("workers", "-v", "-p", projectID, "-e", "."))
71+
72+
runCombinedOutput := runnerCombinedOutput(t, apiServer.URL, authServer.URL)
73+
co, err := runCombinedOutput("services", "-p", projectID, "-e", "main")
74+
require.NoError(t, err)
75+
assert.Contains(t, co, "No services found")
76+
}

go-tests/auth_info_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package tests
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/platformsh/cli/pkg/mockapi"
11+
)
12+
13+
func TestAuthInfo(t *testing.T) {
14+
authServer := mockapi.NewAuthServer(t)
15+
defer authServer.Close()
16+
17+
apiHandler := mockapi.NewHandler(t)
18+
apiHandler.SetMyUser(&mockapi.User{
19+
ID: "my-user-id",
20+
Deactivated: false,
21+
Namespace: "ns",
22+
Username: "my-username",
23+
FirstName: "Foo",
24+
LastName: "Bar",
25+
26+
EmailVerified: true,
27+
Picture: "https://example.com/profile.png",
28+
Country: "NO",
29+
PhoneNumberVerified: true,
30+
MFAEnabled: true,
31+
})
32+
33+
apiServer := httptest.NewServer(apiHandler)
34+
defer apiServer.Close()
35+
36+
run := runnerWithAuth(t, apiServer.URL, authServer.URL)
37+
38+
assert.Equal(t, strings.TrimLeft(`
39+
+-----------------------+---------------------+
40+
| Property | Value |
41+
+-----------------------+---------------------+
42+
| id | my-user-id |
43+
| first_name | Foo |
44+
| last_name | Bar |
45+
| username | my-username |
46+
| email | [email protected] |
47+
| phone_number_verified | true |
48+
+-----------------------+---------------------+
49+
`, "\n"), run("auth:info", "-v", "--refresh"))
50+
51+
assert.Equal(t, "my-user-id\n", run("auth:info", "-P", "id"))
52+
}

go-tests/backup_test.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package tests
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/platformsh/cli/pkg/mockapi"
13+
)
14+
15+
func TestBackupList(t *testing.T) {
16+
authServer := mockapi.NewAuthServer(t)
17+
defer authServer.Close()
18+
19+
apiHandler := mockapi.NewHandler(t)
20+
apiServer := httptest.NewServer(apiHandler)
21+
defer apiServer.Close()
22+
23+
projectID := "rai7quieroohu"
24+
25+
apiHandler.SetProjects([]*mockapi.Project{
26+
{
27+
ID: projectID,
28+
DefaultBranch: "main",
29+
Links: mockapi.MakeHALLinks(
30+
"self=/projects/"+projectID,
31+
"environments=/projects/"+projectID+"/environments",
32+
),
33+
},
34+
})
35+
main := makeEnv(projectID, "main", "production", "active", nil)
36+
main.Links["backups"] = mockapi.HALLink{HREF: "/projects/" + projectID + "//environments/main/backups"}
37+
apiHandler.SetEnvironments([]*mockapi.Environment{main})
38+
39+
created1, err := time.Parse(time.RFC3339, "2014-04-01T10:00:00+01:00")
40+
require.NoError(t, err)
41+
created2, err := time.Parse(time.RFC3339, "2015-04-01T10:00:00+01:00")
42+
require.NoError(t, err)
43+
44+
apiHandler.SetProjectBackups(projectID, []*mockapi.Backup{
45+
{
46+
ID: "123",
47+
EnvironmentID: "main",
48+
Status: "CREATED",
49+
Safe: true,
50+
Restorable: true,
51+
Automated: false,
52+
CommitID: "foo",
53+
CreatedAt: created1,
54+
},
55+
{
56+
ID: "456",
57+
EnvironmentID: "main",
58+
Status: "CREATED",
59+
Safe: false,
60+
Restorable: true,
61+
Automated: true,
62+
CommitID: "bar",
63+
CreatedAt: created2,
64+
},
65+
})
66+
67+
run := runnerWithAuth(t, apiServer.URL, authServer.URL)
68+
69+
assert.Equal(t, strings.TrimLeft(`
70+
+---------------------------+-----------+------------+
71+
| Created | Backup ID | Restorable |
72+
+---------------------------+-----------+------------+
73+
| 2015-04-01T09:00:00+00:00 | 456 | true |
74+
| 2014-04-01T09:00:00+00:00 | 123 | true |
75+
+---------------------------+-----------+------------+
76+
`, "\n"), run("backups", "-p", projectID, "-e", "."))
77+
78+
assert.Equal(t, strings.TrimLeft(`
79+
+---------------------------+-----------+------------+-----------+-----------+
80+
| Created | Backup ID | Restorable | Automated | Commit ID |
81+
+---------------------------+-----------+------------+-----------+-----------+
82+
| 2015-04-01T09:00:00+00:00 | 456 | true | true | bar |
83+
| 2014-04-01T09:00:00+00:00 | 123 | true | false | foo |
84+
+---------------------------+-----------+------------+-----------+-----------+
85+
`, "\n"), run("backups", "-p", projectID, "-e", ".", "--columns", "+automated,commit_id"))
86+
}
87+
88+
func TestBackupCreate(t *testing.T) {
89+
authServer := mockapi.NewAuthServer(t)
90+
defer authServer.Close()
91+
92+
apiHandler := mockapi.NewHandler(t)
93+
apiServer := httptest.NewServer(apiHandler)
94+
defer apiServer.Close()
95+
96+
projectID := "vei8wah5Ohl2e"
97+
98+
apiHandler.SetProjects([]*mockapi.Project{
99+
{
100+
ID: projectID,
101+
DefaultBranch: "main",
102+
Links: mockapi.MakeHALLinks(
103+
"self=/projects/"+projectID,
104+
"environments=/projects/"+projectID+"/environments",
105+
),
106+
},
107+
})
108+
main := makeEnv(projectID, "main", "production", "active", nil)
109+
main.Links["backups"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main//backups"}
110+
main.Links["#backup"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/backups"}
111+
apiHandler.SetEnvironments([]*mockapi.Environment{main})
112+
113+
run := runnerWithAuth(t, apiServer.URL, authServer.URL)
114+
115+
run("backup", "-p", projectID, "-e", ".")
116+
117+
assert.NotEmpty(t, run("backups", "-p", projectID, "-e", "."))
118+
}

0 commit comments

Comments
 (0)