gogreen/apps_test.go

335 lines
8.9 KiB
Go

package godo
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testAppSpec = &AppSpec{
Name: "app-name",
Region: "ams3",
Services: []*AppServiceSpec{{
Name: "service-name",
Routes: []*AppRouteSpec{{
Path: "/",
}},
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/service",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
Workers: []*AppWorkerSpec{{
Name: "worker-name",
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/worker",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
StaticSites: []*AppStaticSiteSpec{{
Name: "static-name",
BuildCommand: "build-command",
Git: &GitSourceSpec{
RepoCloneURL: "git@githost.com/owner/static.git",
Branch: "branch",
},
OutputDir: "out",
}},
Jobs: []*AppJobSpec{{
Name: "job-name",
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/job",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
Databases: []*AppDatabaseSpec{{
Name: "db",
Engine: AppDatabaseSpecEngine_MySQL,
Version: "8",
Size: "size",
NumNodes: 1,
Production: true,
ClusterName: "cluster-name",
DBName: "app",
DBUser: "appuser",
}},
Domains: []*AppDomainSpec{
{
Domain: "example.com",
Type: AppDomainSpecType_Primary,
},
},
}
testDeployment = Deployment{
ID: "08f10d33-94c3-4492-b9a3-1603e9ab7fe4",
Spec: testAppSpec,
Services: []*DeploymentService{{
Name: "service-name",
SourceCommitHash: "service-hash",
}},
Workers: []*DeploymentWorker{{
Name: "worker-name",
SourceCommitHash: "worker-hash",
}},
StaticSites: []*DeploymentStaticSite{{
Name: "static-name",
SourceCommitHash: "static-hash",
}},
Jobs: []*DeploymentJob{{
Name: "job-name",
SourceCommitHash: "job-hash",
}},
CreatedAt: time.Unix(1595959200, 0).UTC(),
UpdatedAt: time.Unix(1595959200, 0).UTC(),
PhaseLastUpdatedAt: time.Unix(1595959200, 0).UTC(),
Phase: DeploymentPhase_Active,
Progress: &DeploymentProgress{
SuccessSteps: 1,
TotalSteps: 1,
Steps: []*DeploymentProgressStep{{
Name: "step",
Status: DeploymentProgressStepStatus_Success,
StartedAt: time.Unix(1595959200, 0).UTC(),
EndedAt: time.Unix(1595959200, 0).UTC(),
Steps: []*DeploymentProgressStep{{
Name: "sub",
Status: DeploymentProgressStepStatus_Success,
StartedAt: time.Unix(1595959200, 0).UTC(),
EndedAt: time.Unix(1595959200, 0).UTC(),
}},
}},
},
}
testApp = App{
ID: "1c70f8f3-106e-428b-ae6d-bfc693c77536",
Spec: testAppSpec,
DefaultIngress: "example.com",
LiveURL: "https://example.com",
ActiveDeployment: &testDeployment,
InProgressDeployment: &testDeployment,
LastDeploymentCreatedAt: time.Unix(1595959200, 0).UTC(),
CreatedAt: time.Unix(1595959200, 0).UTC(),
UpdatedAt: time.Unix(1595959200, 0).UTC(),
Region: &AppRegion{
Slug: "ams",
Label: "Amsterdam",
Continent: "Europe",
Disabled: true,
Reason: "Scheduled maintenance",
DataCenters: []string{"ams3"},
Flag: "nl",
},
}
)
func TestApps_CreateApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var req AppCreateRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, testAppSpec, req.Spec)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Create(ctx, &AppCreateRequest{Spec: testAppSpec})
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_GetApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Get(ctx, testApp.ID)
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_ListApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appsRoot{Apps: []*App{&testApp}})
})
apps, _, err := client.Apps.List(ctx, nil)
require.NoError(t, err)
assert.Equal(t, []*App{&testApp}, apps)
}
func TestApps_UpdateApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
updatedSpec := *testAppSpec
updatedSpec.Name = "new-name"
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
var req AppUpdateRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, &updatedSpec, req.Spec)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Update(ctx, testApp.ID, &AppUpdateRequest{Spec: &updatedSpec})
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_DeleteApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Apps.Delete(ctx, testApp.ID)
require.NoError(t, err)
}
func TestApps_CreateDeployment(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
json.NewEncoder(w).Encode(&deploymentRoot{Deployment: &testDeployment})
})
deployment, _, err := client.Apps.CreateDeployment(ctx, testApp.ID)
require.NoError(t, err)
assert.Equal(t, &testDeployment, deployment)
}
func TestApps_GetDeployment(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&deploymentRoot{Deployment: &testDeployment})
})
deployment, _, err := client.Apps.GetDeployment(ctx, testApp.ID, testDeployment.ID)
require.NoError(t, err)
assert.Equal(t, &testDeployment, deployment)
}
func TestApps_ListDeployment(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&deploymentsRoot{Deployments: []*Deployment{&testDeployment}})
})
deployments, _, err := client.Apps.ListDeployments(ctx, testApp.ID, nil)
require.NoError(t, err)
assert.Equal(t, []*Deployment{&testDeployment}, deployments)
}
func TestApps_GetLogs(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s/logs", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "RUN", r.URL.Query().Get("type"))
assert.Equal(t, "true", r.URL.Query().Get("follow"))
_, hasComponent := r.URL.Query()["component_name"]
assert.False(t, hasComponent)
json.NewEncoder(w).Encode(&AppLogs{LiveURL: "https://live.logs.url"})
})
logs, _, err := client.Apps.GetLogs(ctx, testApp.ID, testDeployment.ID, "", AppLogTypeRun, true)
require.NoError(t, err)
assert.NotEmpty(t, logs.LiveURL)
}
func TestApps_GetLogs_component(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s/logs", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "RUN", r.URL.Query().Get("type"))
assert.Equal(t, "true", r.URL.Query().Get("follow"))
assert.Equal(t, "service-name", r.URL.Query().Get("component_name"))
json.NewEncoder(w).Encode(&AppLogs{LiveURL: "https://live.logs.url"})
})
logs, _, err := client.Apps.GetLogs(ctx, testApp.ID, testDeployment.ID, "service-name", AppLogTypeRun, true)
require.NoError(t, err)
assert.NotEmpty(t, logs.LiveURL)
}