Skip to content

Commit

Permalink
Add some Go tests for variables, activities, help and list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Dec 25, 2024
1 parent d0108b8 commit 988263d
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 3 deletions.
90 changes: 90 additions & 0 deletions go-tests/activity_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tests

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

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

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

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

projectID := "oth9aidoo4zio"

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["#activities"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/activities"}
envs := []*mockapi.Environment{main}
apiHandler.SetEnvironments(envs)

aprilFoolsDay9am, _ := time.Parse(time.RFC3339, "2014-04-01T9:00:00Z")
aprilFoolsDay10am, _ := time.Parse(time.RFC3339, "2014-04-01T10:00:00Z")
aprilFoolsDay11am, _ := time.Parse(time.RFC3339, "2014-04-01T11:00:00Z")

apiHandler.SetProjectActivities(projectID, []*mockapi.Activity{
{
ID: "act1",
Type: "environment.variable.create",
State: "complete",
Result: "success",
CompletionPercent: 100,
Project: projectID,
Environments: []string{"main"},
Description: "<user>Mock User</user> created variable <variable>X</variable> on environment <environment>main</environment>",
Text: "Mock User created variable X on environment main",
CreatedAt: aprilFoolsDay10am,
UpdatedAt: aprilFoolsDay11am,
},
{
ID: "act2",
Type: "project.variable.create",
State: "complete",
Result: "success",
CompletionPercent: 100,
Project: projectID,
Environments: []string{},
Description: "<user>Mock User</user> created variable <variable>X</variable>",
Text: "Mock User created variable X",
CreatedAt: aprilFoolsDay9am,
UpdatedAt: aprilFoolsDay9am,
},
})

f := newCommandFactory(t, apiServer.URL, authServer.URL)

f.Run("cc")

assertTrimmed(t, `
+------+---------------------------+--------------------------------------------------+----------+----------+---------+
| ID | Created | Description | Progress | State | Result |
+------+---------------------------+--------------------------------------------------+----------+----------+---------+
| act1 | 2014-04-01T10:00:00+00:00 | Mock User created variable X on environment main | 100% | complete | success |
+------+---------------------------+--------------------------------------------------+----------+----------+---------+
`, f.Run("act", "-p", projectID, "-e", "."))

assertTrimmed(t, `
+------+----------------------+---------------------------------+----------+----------+---------+----------------+
| ID | Created | Description | Progress | State | Result | Environment(s) |
+------+----------------------+---------------------------------+----------+----------+---------+----------------+
| act1 | 2014-04-01T10:00:00+ | Mock User created variable X on | 100% | complete | success | main |
| | 00:00 | environment main | | | | |
| act2 | 2014-04-01T09:00:00+ | Mock User created variable X | 100% | complete | success | |
| | 00:00 | | | | | |
+------+----------------------+---------------------------------+----------+----------+---------+----------------+
`, f.Run("act", "-p", projectID, "--all", "--limit", "20"))

assertTrimmed(t, "complete", f.Run("act:get", "-p", projectID, "-e", ".", "act1", "-P", "state"))
assertTrimmed(t, "2014-04-01T10:00:00+00:00", f.Run("act:get", "-p", projectID, "-e", ".", "act1", "-P", "created_at"))
}
2 changes: 2 additions & 0 deletions go-tests/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ api:
base_url: 'http://127.0.0.1'
auth_url: 'http://127.0.0.1'

disable_credential_helpers: true

organizations: true
centralized_permissions: true
teams: true
Expand Down
2 changes: 1 addition & 1 deletion go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/platformsh/legacy-cli/tests
go 1.22.9

require (
github.com/platformsh/cli v0.0.0-20241216143533-ba63495052eb
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/platformsh/cli v0.0.0-20241216143533-ba63495052eb h1:C6PzzI/enoZM05y/ifsfjoXNefnJbvhMV8syA/RwXGo=
github.com/platformsh/cli v0.0.0-20241216143533-ba63495052eb/go.mod h1:92H+miZV5F6iXLr/2698VLSZI8X7h+l/xd+i+1JaBAY=
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5 h1:k3nAT1td76kAXtyriBFkP9eYsFl7PFRk6p1zIKJiwXI=
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5/go.mod h1:92H+miZV5F6iXLr/2698VLSZI8X7h+l/xd+i+1JaBAY=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down
27 changes: 27 additions & 0 deletions go-tests/help_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tests

import (
"encoding/json"
"github.com/stretchr/testify/require"
"testing"

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

func TestHelp(t *testing.T) {
f := newCommandFactory(t, "", "")

assert.Contains(t, f.Run("help", "pro"),
"platform-test projects [--pipe] [--region REGION] [--title TITLE] [--my] [--refresh REFRESH] [--sort SORT] [--reverse] [--page PAGE] [-c|--count COUNT] [-o|--org ORG] [--format FORMAT] [--columns COLUMNS] [--no-header] [--date-fmt DATE-FMT]")

actListHelp := f.Run("help", "act", "--format", "json")
var helpData struct {
Examples []struct {
CommandLine string `json:"commandLine"`
Description string `json:"description"`
} `json:"examples"`
}
err := json.Unmarshal([]byte(actListHelp), &helpData)
require.NoError(t, err)
assert.NotEmpty(t, helpData.Examples)
}
24 changes: 24 additions & 0 deletions go-tests/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tests

import (
"testing"

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

func TestList(t *testing.T) {
f := newCommandFactory(t, "", "")

output := f.Run("list")

assert.NotEmpty(t, output)
assert.Contains(t, output, "Available commands:")
assert.Contains(t, output, "activity:list (activities, act)")
assert.NotContains(t, output, "mount:size")

output = f.Run("list", "--all")

assert.NotEmpty(t, output)
assert.Contains(t, output, "Available commands:")
assert.Contains(t, output, "mount:size")
}
78 changes: 78 additions & 0 deletions go-tests/variable_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package tests

import (
"github.com/platformsh/cli/pkg/mockapi"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"testing"
)

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

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

projectID := "su0heinehei7i"

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["#variables"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/variables"}
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{}},
},
Routes: make(map[string]any),
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
})
envs := []*mockapi.Environment{main}
apiHandler.SetEnvironments(envs)

apiHandler.SetProjectVariables(projectID, []*mockapi.Variable{
{
Name: "bar",
IsSensitive: true,
VisibleBuild: true,
},
})

apiHandler.SetEnvLevelVariables(projectID, "main", []*mockapi.EnvLevelVariable{
{
Variable: mockapi.Variable{
Name: "env:FOO",
Value: "bar",
VisibleRuntime: true,
},
IsEnabled: true,
Inherited: false,
IsInheritable: false,
},
})

f := newCommandFactory(t, apiServer.URL, authServer.URL)

f.Run("cc")
assertTrimmed(t, `
+---------+-------------+---------------------------+---------+
| Name | Level | Value | Enabled |
+---------+-------------+---------------------------+---------+
| bar | project | [Hidden: sensitive value] | |
| env:FOO | environment | bar | true |
+---------+-------------+---------------------------+---------+
`, f.Run("var", "-p", projectID, "-e", "."))

assertTrimmed(t, "false", f.Run("var:get", "-p", projectID, "-e", ".", "env:FOO", "-P", "is_sensitive"))
assertTrimmed(t, "true", f.Run("var:get", "-p", projectID, "-e", ".", "env:FOO", "-P", "visible_runtime"))

assertTrimmed(t, "false", f.Run("var:get", "-p", projectID, "-l", "p", "bar", "-P", "visible_runtime"))
_, stdErr, err := f.RunCombinedOutput("var:get", "-p", projectID, "-e", ".", "bar", "-P", "value")
assert.Error(t, err)
assert.Contains(t, stdErr, "The variable is sensitive")
}

0 comments on commit 988263d

Please sign in to comment.