Skip to content

Commit

Permalink
Integration test for release ls
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Oct 25, 2024
1 parent d39df83 commit c2c8396
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 12 deletions.
20 changes: 16 additions & 4 deletions cli/cmd/release_ls.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"context"

"github.com/replicatedhq/replicated/cli/print"
"github.com/replicatedhq/replicated/pkg/integration"
"github.com/spf13/cobra"
)

Expand All @@ -10,16 +13,25 @@ func (r *runners) IniReleaseList(parent *cobra.Command) {
Use: "ls",
Short: "List all of an app's releases",
Long: "List all of an app's releases",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if integrationTest != "" {
ctx = context.WithValue(ctx, integration.IntegrationTestContextKey, integrationTest)
}
if logAPICalls != "" {
ctx = context.WithValue(ctx, integration.APICallLogContextKey, logAPICalls)
}

return r.releaseList(ctx, cmd, args)
},
}

parent.AddCommand(cmd)
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")

cmd.RunE = r.releaseList
}

func (r *runners) releaseList(cmd *cobra.Command, args []string) error {
releases, err := r.api.ListReleases(r.appID, r.appType)
func (r *runners) releaseList(ctx context.Context, cmd *cobra.Command, args []string) error {
releases, err := r.api.ListReleases(ctx, r.appID, r.appType)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions client/release.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package client

import (
"context"
"errors"

"github.com/replicatedhq/replicated/pkg/types"
)

func (c *Client) ListReleases(appID string, appType string) ([]types.ReleaseInfo, error) {
func (c *Client) ListReleases(ctx context.Context, appID string, appType string) ([]types.ReleaseInfo, error) {
if appType == "platform" {
platformReleases, err := c.PlatformClient.ListReleases(appID)
platformReleases, err := c.PlatformClient.ListReleases(ctx, appID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -41,7 +42,7 @@ func (c *Client) ListReleases(appID string, appType string) ([]types.ReleaseInfo
return releaseInfos, nil

} else if appType == "kots" {
return c.KotsClient.ListReleases(appID)
return c.KotsClient.ListReleases(ctx, appID)
}

return nil, errors.New("unknown app type")
Expand Down
26 changes: 26 additions & 0 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"fmt"
"path"
"time"

kotsclienttypes "github.com/replicatedhq/replicated/pkg/kotsclient/types"
"github.com/replicatedhq/replicated/pkg/types"
Expand Down Expand Up @@ -33,6 +34,25 @@ var (
Name: "name",
IsFoundation: true,
}

typesKOTSAppRelease = types.KotsAppRelease{
AppID: "app-id",
Sequence: 1,
CreatedAt: time.Now(),
IsArchived: false,
Spec: "spec",
ReleaseNotes: "release-notes",
IsReleaseNotEditable: false,
Channels: []*types.Channel{
{
ID: "channel-id",
Name: "channel-name",
},
},
Charts: []types.Chart{},
CompatibilityResults: []types.CompatibilityResult{},
IsHelmOnly: false,
}
)

func Response(key string) interface{} {
Expand All @@ -51,6 +71,12 @@ func Response(key string) interface{} {
return &kotsclienttypes.CreateKOTSAppResponse{
App: &typeKOTSAppWithChannels,
}
case "release-ls":
return kotsclienttypes.KotsListReleasesResponse{
Releases: []*types.KotsAppRelease{
&typesKOTSAppRelease,
},
}
default:
panic(fmt.Sprintf("unknown integration test: %s", key))
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/integration/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package integration

import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
"time"
)

func TestRelease(t *testing.T) {
tests := []struct {
name string
cli string
wantFormat format
wantLines int
wantAPIRequests []string
ignoreCLIOutput bool
}{
{
name: "release-ls",
cli: "release ls",
wantFormat: FormatTable,
wantLines: 1,
wantAPIRequests: []string{
"GET:/v3/app/id/releases",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := strings.Split(tt.cli, " ")
args = append(args, "--integration-test", tt.name)

apiCallLog, err := os.CreateTemp("", "")
if err != nil {
log.Fatal(err)
}

defer os.RemoveAll(apiCallLog.Name())

args = append(args, "--log-api-calls", apiCallLog.Name())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, CLIPath(), args...)

if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("Command execution timed out")
}

out, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("error running cli: %v", err)
}

fmt.Printf("out: %s\n", string(out))
if !tt.ignoreCLIOutput {
AssertCLIOutput(t, string(out), tt.wantFormat, tt.wantLines)
}

AssertAPIRequests(t, tt.wantAPIRequests, apiCallLog.Name())
})
}
}
8 changes: 5 additions & 3 deletions pkg/kotsclient/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/graphql"
kotsclienttypes "github.com/replicatedhq/replicated/pkg/kotsclient/types"
"github.com/replicatedhq/replicated/pkg/types"
)

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

func (c *VendorV3Client) ListReleases(appID string) ([]types.ReleaseInfo, error) {
func (c *VendorV3Client) ListReleases(ctx context.Context, appID string) ([]types.ReleaseInfo, error) {
allReleases := []types.ReleaseInfo{}
done := false
page := 0

for !done {
resp := types.KotsListReleasesResponse{}
resp := kotsclienttypes.KotsListReleasesResponse{}
path := fmt.Sprintf("/v3/app/%s/releases?currentPage=%d&pageSize=20", appID, page)
err := c.DoJSON(context.TODO(), "GET", path, http.StatusOK, nil, &resp)
err := c.DoJSON(ctx, "GET", path, http.StatusOK, nil, &resp)
if err != nil {
done = true
continue
Expand Down
5 changes: 5 additions & 0 deletions pkg/kotsclient/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ type KotsAppResponse struct {
type CreateKOTSAppResponse struct {
App *types.KotsAppWithChannels `json:"app"`
}

// KotsListReleasesResponse contains the JSON releases list
type KotsListReleasesResponse struct {
Releases []*types.KotsAppRelease `json:"releases"`
}
4 changes: 2 additions & 2 deletions pkg/platformclient/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
)

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

0 comments on commit c2c8396

Please sign in to comment.