Skip to content

Commit d7d7850

Browse files
committed
Integration test for release ls
1 parent d39df83 commit d7d7850

File tree

8 files changed

+128
-12
lines changed

8 files changed

+128
-12
lines changed

cli/cmd/release_ls.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cmd
22

33
import (
4+
"context"
5+
46
"github.com/replicatedhq/replicated/cli/print"
7+
"github.com/replicatedhq/replicated/pkg/integration"
58
"github.com/spf13/cobra"
69
)
710

@@ -10,16 +13,25 @@ func (r *runners) IniReleaseList(parent *cobra.Command) {
1013
Use: "ls",
1114
Short: "List all of an app's releases",
1215
Long: "List all of an app's releases",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
ctx := context.Background()
18+
if integrationTest != "" {
19+
ctx = context.WithValue(ctx, integration.IntegrationTestContextKey, integrationTest)
20+
}
21+
if logAPICalls != "" {
22+
ctx = context.WithValue(ctx, integration.APICallLogContextKey, logAPICalls)
23+
}
24+
25+
return r.releaseList(ctx, cmd, args)
26+
},
1327
}
1428

1529
parent.AddCommand(cmd)
1630
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
17-
18-
cmd.RunE = r.releaseList
1931
}
2032

21-
func (r *runners) releaseList(cmd *cobra.Command, args []string) error {
22-
releases, err := r.api.ListReleases(r.appID, r.appType)
33+
func (r *runners) releaseList(ctx context.Context, cmd *cobra.Command, args []string) error {
34+
releases, err := r.api.ListReleases(ctx, r.appID, r.appType)
2335
if err != nil {
2436
return err
2537
}

client/release.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package client
22

33
import (
4+
"context"
45
"errors"
56

67
"github.com/replicatedhq/replicated/pkg/types"
78
)
89

9-
func (c *Client) ListReleases(appID string, appType string) ([]types.ReleaseInfo, error) {
10+
func (c *Client) ListReleases(ctx context.Context, appID string, appType string) ([]types.ReleaseInfo, error) {
1011
if appType == "platform" {
11-
platformReleases, err := c.PlatformClient.ListReleases(appID)
12+
platformReleases, err := c.PlatformClient.ListReleases(ctx, appID)
1213
if err != nil {
1314
return nil, err
1415
}
@@ -41,7 +42,7 @@ func (c *Client) ListReleases(appID string, appType string) ([]types.ReleaseInfo
4142
return releaseInfos, nil
4243

4344
} else if appType == "kots" {
44-
return c.KotsClient.ListReleases(appID)
45+
return c.KotsClient.ListReleases(ctx, appID)
4546
}
4647

4748
return nil, errors.New("unknown app type")
File renamed without changes.

pkg/integration/integration.go

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package integration
33
import (
44
"fmt"
55
"path"
6+
"time"
67

78
kotsclienttypes "github.com/replicatedhq/replicated/pkg/kotsclient/types"
89
"github.com/replicatedhq/replicated/pkg/types"
@@ -33,6 +34,25 @@ var (
3334
Name: "name",
3435
IsFoundation: true,
3536
}
37+
38+
typesKOTSAppRelease = types.KotsAppRelease{
39+
AppID: "app-id",
40+
Sequence: 1,
41+
CreatedAt: time.Now(),
42+
IsArchived: false,
43+
Spec: "spec",
44+
ReleaseNotes: "release-notes",
45+
IsReleaseNotEditable: false,
46+
Channels: []*types.Channel{
47+
{
48+
ID: "channel-id",
49+
Name: "channel-name",
50+
},
51+
},
52+
Charts: []types.Chart{},
53+
CompatibilityResults: []types.CompatibilityResult{},
54+
IsHelmOnly: false,
55+
}
3656
)
3757

3858
func Response(key string) interface{} {
@@ -51,6 +71,12 @@ func Response(key string) interface{} {
5171
return &kotsclienttypes.CreateKOTSAppResponse{
5272
App: &typeKOTSAppWithChannels,
5373
}
74+
case "release-ls":
75+
return kotsclienttypes.KotsListReleasesResponse{
76+
Releases: []*types.KotsAppRelease{
77+
&typesKOTSAppRelease,
78+
},
79+
}
5480
default:
5581
panic(fmt.Sprintf("unknown integration test: %s", key))
5682
}

pkg/integration/release_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
"testing"
11+
"time"
12+
)
13+
14+
func TestRelease(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
cli string
18+
wantFormat format
19+
wantLines int
20+
wantAPIRequests []string
21+
ignoreCLIOutput bool
22+
}{
23+
{
24+
name: "release-ls",
25+
cli: "release ls",
26+
wantFormat: FormatTable,
27+
wantLines: 1,
28+
wantAPIRequests: []string{
29+
"GET:/v3/app/id/releases",
30+
},
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
args := strings.Split(tt.cli, " ")
37+
args = append(args, "--integration-test", tt.name)
38+
39+
apiCallLog, err := os.CreateTemp("", "")
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
44+
defer os.RemoveAll(apiCallLog.Name())
45+
46+
args = append(args, "--log-api-calls", apiCallLog.Name())
47+
48+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
49+
defer cancel()
50+
51+
cmd := exec.CommandContext(ctx, CLIPath(), args...)
52+
53+
if ctx.Err() == context.DeadlineExceeded {
54+
t.Fatalf("Command execution timed out")
55+
}
56+
57+
out, err := cmd.CombinedOutput()
58+
if err != nil {
59+
t.Errorf("error running cli: %v", err)
60+
}
61+
62+
fmt.Printf("out: %s\n", string(out))
63+
if !tt.ignoreCLIOutput {
64+
AssertCLIOutput(t, string(out), tt.wantFormat, tt.wantLines)
65+
}
66+
67+
AssertAPIRequests(t, tt.wantAPIRequests, apiCallLog.Name())
68+
})
69+
}
70+
}

pkg/kotsclient/release.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/pkg/errors"
1313
"github.com/replicatedhq/replicated/pkg/graphql"
14+
kotsclienttypes "github.com/replicatedhq/replicated/pkg/kotsclient/types"
1415
"github.com/replicatedhq/replicated/pkg/types"
1516
)
1617

@@ -127,14 +128,15 @@ func (c *VendorV3Client) UpdateRelease(appID string, sequence int64, multiyaml s
127128
return nil
128129
}
129130

130-
func (c *VendorV3Client) ListReleases(appID string) ([]types.ReleaseInfo, error) {
131+
func (c *VendorV3Client) ListReleases(ctx context.Context, appID string) ([]types.ReleaseInfo, error) {
131132
allReleases := []types.ReleaseInfo{}
132133
done := false
133134
page := 0
135+
134136
for !done {
135-
resp := types.KotsListReleasesResponse{}
137+
resp := kotsclienttypes.KotsListReleasesResponse{}
136138
path := fmt.Sprintf("/v3/app/%s/releases?currentPage=%d&pageSize=20", appID, page)
137-
err := c.DoJSON(context.TODO(), "GET", path, http.StatusOK, nil, &resp)
139+
err := c.DoJSON(ctx, "GET", path, http.StatusOK, nil, &resp)
138140
if err != nil {
139141
done = true
140142
continue

pkg/kotsclient/types/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ type KotsAppResponse struct {
99
type CreateKOTSAppResponse struct {
1010
App *types.KotsAppWithChannels `json:"app"`
1111
}
12+
13+
// KotsListReleasesResponse contains the JSON releases list
14+
type KotsListReleasesResponse struct {
15+
Releases []*types.KotsAppRelease `json:"releases"`
16+
}

pkg/platformclient/release.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
)
1313

1414
// ListReleases lists all releases for an app.
15-
func (c *HTTPClient) ListReleases(appID string) ([]releases.AppReleaseInfo, error) {
15+
func (c *HTTPClient) ListReleases(ctx context.Context, appID string) ([]releases.AppReleaseInfo, error) {
1616
path := fmt.Sprintf("/v1/app/%s/releases", appID)
1717
releases := make([]releases.AppReleaseInfo, 0)
18-
if err := c.DoJSON(context.TODO(), "GET", path, http.StatusOK, nil, &releases); err != nil {
18+
if err := c.DoJSON(ctx, "GET", path, http.StatusOK, nil, &releases); err != nil {
1919
return nil, fmt.Errorf("ListReleases: %w", err)
2020
}
2121
return releases, nil

0 commit comments

Comments
 (0)