Skip to content

Commit

Permalink
Use frameworks V2 API (#747)
Browse files Browse the repository at this point in the history
* Move to frameworks V2 API
* List only framework names not objects
  • Loading branch information
FriggaHel authored Apr 18, 2023
1 parent 68b7034 commit 6e30210
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 59 deletions.
7 changes: 1 addition & 6 deletions internal/cmd/ini/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,11 @@ func (ini *initializer) checkCredentials(region string) error {
}

func (ini *initializer) askFramework() (string, error) {
values, err := ini.infoReader.Frameworks(context.Background())
frameworks, err := ini.infoReader.Frameworks(context.Background())
if err != nil {
return "", err
}

var frameworks []string
for _, f := range values {
frameworks = append(frameworks, f.Name)
}

p := &survey.Select{
Message: "Select framework:",
Options: frameworks,
Expand Down
42 changes: 20 additions & 22 deletions internal/cmd/ini/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ type questionTest struct {

func TestAskFramework(t *testing.T) {
ir := &mocks.FakeFrameworkInfoReader{
FrameworksFn: func(ctx context.Context) ([]framework.Framework, error) {
return []framework.Framework{{Name: cypress.Kind}, {Name: espresso.Kind}, {Name: playwright.Kind}}, nil
FrameworksFn: func(ctx context.Context) ([]string, error) {
return []string{cypress.Kind, espresso.Kind, playwright.Kind}, nil
},
}
testCases := []questionTest{
Expand Down Expand Up @@ -716,8 +716,8 @@ func TestConfigure(t *testing.T) {
VersionsFn: func(ctx context.Context, frameworkName string) ([]framework.Metadata, error) {
return frameworkVersions, nil
},
FrameworksFn: func(ctx context.Context) ([]framework.Framework, error) {
return []framework.Framework{{Name: cypress.Kind}, {Name: espresso.Kind}}, nil
FrameworksFn: func(ctx context.Context) ([]string, error) {
return []string{cypress.Kind, espresso.Kind}, nil
},
}
dr := &mocks.FakeDevicesReader{
Expand Down Expand Up @@ -1028,15 +1028,15 @@ func Test_initializers(t *testing.T) {
VersionsFn: func(ctx context.Context, frameworkName string) ([]framework.Metadata, error) {
return frameworkVersions[frameworkName], nil
},
FrameworksFn: func(ctx context.Context) ([]framework.Framework, error) {
return []framework.Framework{
{Name: cypress.Kind},
{Name: espresso.Kind},
{Name: imagerunner.Kind},
{Name: playwright.Kind},
{Name: "puppeteer"},
{Name: testcafe.Kind},
{Name: xcuitest.Kind},
FrameworksFn: func(ctx context.Context) ([]string, error) {
return []string{
cypress.Kind,
espresso.Kind,
imagerunner.Kind,
playwright.Kind,
"puppeteer",
testcafe.Kind,
xcuitest.Kind,
}, nil
},
}
Expand Down Expand Up @@ -1717,30 +1717,28 @@ func Test_metaToBrowsers(t *testing.T) {
func Test_checkCredentials(t *testing.T) {
tests := []struct {
name string
frameworkFn func(ctx context.Context) ([]framework.Framework, error)
frameworkFn func(ctx context.Context) ([]string, error)
wantErr error
}{
{
name: "Success",
frameworkFn: func(ctx context.Context) ([]framework.Framework, error) {
return []framework.Framework{
{Name: cypress.Kind},
}, nil
frameworkFn: func(ctx context.Context) ([]string, error) {
return []string{cypress.Kind}, nil
},
wantErr: nil,
},
{
name: "Invalid credentials",
frameworkFn: func(ctx context.Context) ([]framework.Framework, error) {
frameworkFn: func(ctx context.Context) ([]string, error) {
errMsg := "unexpected status '401' from test-composer: Unauthorized\n"
return []framework.Framework{}, fmt.Errorf(errMsg)
return []string{}, fmt.Errorf(errMsg)
},
wantErr: errors.New("invalid credentials provided"),
},
{
name: "Other error",
frameworkFn: func(ctx context.Context) ([]framework.Framework, error) {
return []framework.Framework{}, errors.New("other error")
frameworkFn: func(ctx context.Context) ([]string, error) {
return []string{}, errors.New("other error")
},
wantErr: errors.New("other error"),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Framework struct {

// MetadataService represents an interface for retrieving framework metadata.
type MetadataService interface {
Frameworks(ctx context.Context) ([]Framework, error)
Frameworks(ctx context.Context) ([]string, error)
Versions(ctx context.Context, frameworkName string) ([]Metadata, error)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/framework/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

type MockMetadataService struct {
MockFrameworks func(ctx context.Context) ([]Framework, error)
MockFrameworks func(ctx context.Context) ([]string, error)
MockVersions func(ctx context.Context, frameworkName string) ([]Metadata, error)
}

func (m *MockMetadataService) Frameworks(ctx context.Context) ([]Framework, error) {
func (m *MockMetadataService) Frameworks(ctx context.Context) ([]string, error) {
if m.MockFrameworks != nil {
return m.MockFrameworks(ctx)
}
Expand Down
27 changes: 21 additions & 6 deletions internal/http/testcomposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,25 @@ func (c *TestComposer) UploadAsset(jobID string, realDevice bool, fileName strin
}

// Frameworks returns the list of available frameworks.
func (c *TestComposer) Frameworks(ctx context.Context) ([]framework.Framework, error) {
url := fmt.Sprintf("%s/v1/testcomposer/frameworks", c.URL)
func (c *TestComposer) Frameworks(ctx context.Context) ([]string, error) {
url := fmt.Sprintf("%s/v2/testcomposer/frameworks", c.URL)

req, err := NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return []framework.Framework{}, err
return []string{}, err
}
req.SetBasicAuth(c.Credentials.Username, c.Credentials.AccessKey)

var resp []framework.Framework
if err = c.doJSONResponse(req, 200, &resp); err != nil {
return []framework.Framework{}, err
return []string{}, err
}
return resp, nil
return uniqFrameworkNameSet(resp), nil
}

// Versions return the list of available versions for a specific framework and region.
func (c *TestComposer) Versions(ctx context.Context, frameworkName string) ([]framework.Metadata, error) {
url := fmt.Sprintf("%s/v1/testcomposer/frameworks/%s/versions", c.URL, frameworkName)
url := fmt.Sprintf("%s/v2/testcomposer/frameworks?frameworkName=%s", c.URL, frameworkName)

req, err := NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
Expand Down Expand Up @@ -205,3 +205,18 @@ func (c *TestComposer) Versions(ctx context.Context, frameworkName string) ([]fr
}
return frameworks, nil
}

func uniqFrameworkNameSet(frameworks []framework.Framework) []string {
var fws []string
mp := map[string]bool{}

for _, fw := range frameworks {
_, present := mp[fw.Name]

if !present {
mp[fw.Name] = true
fws = append(fws, fw.Name)
}
}
return fws
}
38 changes: 18 additions & 20 deletions internal/http/testcomposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,27 @@ func TestTestComposer_Frameworks(t *testing.T) {
name string
body string
httpCode int
want []framework.Framework
want []string
wantErr bool
}{
{
name: "HTTP - 200",
body: `[{"name": "cypress"},{"name":"playwright"},{"name":"puppeteer"},{"name":"testcafe"},{"name":"espresso"},{"name":"xcuitest"}]`,
body: `[{"name":"cypress","version":"12.6.0"},{"name":"cypress","version":"12.3.0"},{"name":"playwright","version":"1.31.1"},{"name":"playwright","version":"1.29.2"},{"name":"puppeteer","version":"10.4.0"},{"name":"puppeteer","version":"10.2.0"},{"name":"puppeteer-replay","version":"0.8.0"},{"name":"puppeteer-replay","version":"0.7.0"},{"name":"testcafe","version":"2.1.0"},{"name":"testcafe","version":"2.0.1"}]`,
httpCode: 200,
want: []framework.Framework{
{Name: "cypress"},
{Name: "playwright"},
{Name: "puppeteer"},
{Name: "testcafe"},
{Name: "espresso"},
{Name: "xcuitest"},
want: []string{
"cypress",
"playwright",
"puppeteer",
"puppeteer-replay",
"testcafe",
},
wantErr: false,
},
{
name: "HTTP - 500",
body: ``,
httpCode: 500,
want: []framework.Framework{},
want: []string{},
wantErr: true,
},
}
Expand All @@ -205,7 +204,7 @@ func TestTestComposer_Frameworks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
switch r.URL.Path {
case "/v1/testcomposer/frameworks":
case "/v2/testcomposer/frameworks":
w.WriteHeader(tt.httpCode)
_, err = w.Write([]byte(tt.body))
default:
Expand Down Expand Up @@ -236,12 +235,13 @@ func TestTestComposer_Frameworks(t *testing.T) {
func TestTestComposer_Versions(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
switch r.URL.Path {
case "/v1/testcomposer/frameworks/cypress/versions":
switch r.RequestURI {
case "/v2/testcomposer/frameworks?frameworkName=cypress":
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`[{"name":"cypress","version":"7.3.0","eolDate":"2023-01-01T00:00:00Z","removalDate":"2023-04-01T00:00:00Z","runner":{"cloudRunnerVersion":"","dockerImage":"saucelabs/stt-cypress-mocha-node:v7.1.1","gitRelease":"saucelabs/sauce-cypress-runner:v7.1.1"},"platforms":[{"name":"windows 10","browsers":["googlechrome","firefox","microsoftedge"]}]},{"name":"cypress","version":"7.1.0","eolDate":"2023-01-01T00:00:00Z","removalDate":"2023-04-01T00:00:00Z","runner":{"cloudRunnerVersion":"","dockerImage":"saucelabs/stt-cypress-mocha-node:v7.0.6","gitRelease":"saucelabs/sauce-cypress-runner:v7.0.6"},"platforms":[{"name":"windows 10","browsers":["googlechrome","firefox","microsoftedge"]}]},{"name":"cypress","version":"6.6.0","eolDate":"2023-01-01T00:00:00Z","removalDate":"2023-04-01T00:00:00Z","runner":{"cloudRunnerVersion":"","dockerImage":"saucelabs/stt-cypress-mocha-node:v6.0.2","gitRelease":"saucelabs/sauce-cypress-runner:v6.0.2"},"platforms":[{"name":"windows 10","browsers":["googlechrome","firefox","microsoftedge"]}]}]`))
case "/v1/testcomposer/frameworks/non-existent/versions":
w.WriteHeader(http.StatusNotFound)
case "/v2/testcomposer/frameworks?frameworkName=non-existent":
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`[]`))
default:
w.WriteHeader(http.StatusInternalServerError)
}
Expand Down Expand Up @@ -321,12 +321,11 @@ func TestTestComposer_Versions(t *testing.T) {
want: []framework.Metadata{},
wantErr: true,
}, {
name: "HTTP - 404",
name: "HTTP - Non-existent framework",
args: args{
frameworkName: "Non-existent",
frameworkName: "non-existent",
},
want: []framework.Metadata{},
wantErr: true,
wantErr: false,
},
}
for _, tt := range tests {
Expand All @@ -339,7 +338,6 @@ func TestTestComposer_Versions(t *testing.T) {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Versions() got = %v, want %v", got, tt.want)
}
ts.Close()
})
}
}
4 changes: 2 additions & 2 deletions internal/mocks/frameworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

// FakeFrameworkInfoReader is a mock for the interface framework.MetadataService.
type FakeFrameworkInfoReader struct {
FrameworksFn func(ctx context.Context) ([]framework.Framework, error)
FrameworksFn func(ctx context.Context) ([]string, error)
VersionsFn func(ctx context.Context, frameworkName string) ([]framework.Metadata, error)
}

// Frameworks is a wrapper around FrameworksFn.
func (fir *FakeFrameworkInfoReader) Frameworks(ctx context.Context) ([]framework.Framework, error) {
func (fir *FakeFrameworkInfoReader) Frameworks(ctx context.Context) ([]string, error) {
return fir.FrameworksFn(ctx)
}

Expand Down

0 comments on commit 6e30210

Please sign in to comment.