-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some Go tests for variables, activities, help and list commands
- Loading branch information
1 parent
d0108b8
commit 988263d
Showing
7 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |