Skip to content

Commit

Permalink
Merge pull request #121 from staebler/cincy-query-params
Browse files Browse the repository at this point in the history
pkg/cincinnati: add required parameters to requests
  • Loading branch information
openshift-merge-robot authored Feb 19, 2019
2 parents 93d4eff + 06c7f80 commit 70c0232
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 72 deletions.
14 changes: 13 additions & 1 deletion pkg/cincinnati/cincinnati.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/blang/semver"
"github.com/google/uuid"
Expand Down Expand Up @@ -38,8 +39,19 @@ type Update node
// the current version and their payloads indicate from where the actual update
// image can be downloaded.
func (c Client) GetUpdates(upstream string, channel string, version semver.Version) ([]Update, error) {
// Prepare parametrized cincinnati query.
cincinnatiURL, err := url.Parse(upstream)
if err != nil {
return nil, fmt.Errorf("failed to parse upstream URL: %s", err)
}
queryParams := cincinnatiURL.Query()
queryParams.Add("channel", channel)
queryParams.Add("id", c.id.String())
queryParams.Add("version", version.String())
cincinnatiURL.RawQuery = queryParams.Encode()

// Download the update graph.
req, err := http.NewRequest("GET", upstream, nil)
req, err := http.NewRequest("GET", cincinnatiURL.String(), nil)
if err != nil {
return nil, err
}
Expand Down
179 changes: 108 additions & 71 deletions pkg/cincinnati/cincinnati_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"

Expand All @@ -14,97 +15,115 @@ import (
)

func TestGetUpdates(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

mtype := r.Header.Get("Accept")
if mtype != GraphMediaType {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}

_, err := w.Write([]byte(`{
"nodes": [
{
"version": "4.0.0-4",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-4",
"metadata": {}
},
{
"version": "4.0.0-5",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-5",
"metadata": {}
},
{
"version": "4.0.0-6",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-6",
"metadata": {}
},
{
"version": "4.0.0-6+2",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-6+2",
"metadata": {}
},
{
"version": "4.0.0-0.okd-0",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.okd-0",
"metadata": {}
},
{
"version": "4.0.0-0.2",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.2",
"metadata": {}
},
{
"version": "4.0.0-0.3",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.3",
"metadata": {}
}
],
"edges": [[0,1],[1,2],[1,3],[5,6]]
}`))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

clientID := uuid.Must(uuid.Parse("01234567-0123-0123-0123-0123456789ab"))
channelName := "test-channel"
tests := []struct {
name string
version string

available []Update
err string
expectedQuery string
available []Update
err string
}{{
name: "one update available",
version: "4.0.0-4",
name: "one update available",
version: "4.0.0-4",
expectedQuery: "channel=test-channel&id=01234567-0123-0123-0123-0123456789ab&version=4.0.0-4",
available: []Update{
{semver.MustParse("4.0.0-5"), "quay.io/openshift-release-dev/ocp-release:4.0.0-5"},
},
}, {
name: "two updates available",
version: "4.0.0-5",
name: "two updates available",
version: "4.0.0-5",
expectedQuery: "channel=test-channel&id=01234567-0123-0123-0123-0123456789ab&version=4.0.0-5",
available: []Update{
{semver.MustParse("4.0.0-6"), "quay.io/openshift-release-dev/ocp-release:4.0.0-6"},
{semver.MustParse("4.0.0-6+2"), "quay.io/openshift-release-dev/ocp-release:4.0.0-6+2"},
},
}, {
name: "no updates available",
version: "4.0.0-0.okd-0",
name: "no updates available",
version: "4.0.0-0.okd-0",
expectedQuery: "channel=test-channel&id=01234567-0123-0123-0123-0123456789ab&version=4.0.0-0.okd-0",
}, {
name: "unknown version",
version: "4.0.0-3",
err: "unknown version 4.0.0-3",
name: "unknown version",
version: "4.0.0-3",
expectedQuery: "channel=test-channel&id=01234567-0123-0123-0123-0123456789ab&version=4.0.0-3",
err: "unknown version 4.0.0-3",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
requestQuery := make(chan string, 1)
defer close(requestQuery)

handler := func(w http.ResponseWriter, r *http.Request) {
select {
case requestQuery <- r.URL.RawQuery:
default:
t.Fatalf("received multiple requests at upstream URL")
}

if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

mtype := r.Header.Get("Accept")
if mtype != GraphMediaType {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}

_, err := w.Write([]byte(`{
"nodes": [
{
"version": "4.0.0-4",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-4",
"metadata": {}
},
{
"version": "4.0.0-5",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-5",
"metadata": {}
},
{
"version": "4.0.0-6",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-6",
"metadata": {}
},
{
"version": "4.0.0-6+2",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-6+2",
"metadata": {}
},
{
"version": "4.0.0-0.okd-0",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.okd-0",
"metadata": {}
},
{
"version": "4.0.0-0.2",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.2",
"metadata": {}
},
{
"version": "4.0.0-0.3",
"payload": "quay.io/openshift-release-dev/ocp-release:4.0.0-0.3",
"metadata": {}
}
],
"edges": [[0,1],[1,2],[1,3],[5,6]]
}`))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

ts := httptest.NewServer(http.HandlerFunc(handler))
c := NewClient(uuid.New())
defer ts.Close()

updates, err := c.GetUpdates(ts.URL, "", semver.MustParse(test.version))
c := NewClient(clientID)

updates, err := c.GetUpdates(ts.URL, channelName, semver.MustParse(test.version))
if test.err == "" {
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
Expand All @@ -117,6 +136,24 @@ func TestGetUpdates(t *testing.T) {
t.Fatalf("expected err to be %s, got: %v", test.err, err)
}
}

actualQuery := ""
select {
case actualQuery = <-requestQuery:
default:
t.Fatal("no request received at upstream URL")
}
expectedQueryValues, err := url.ParseQuery(test.expectedQuery)
if err != nil {
t.Fatalf("could not parse expected query: %v", err)
}
actualQueryValues, err := url.ParseQuery(actualQuery)
if err != nil {
t.Fatalf("could not parse acutal query: %v", err)
}
if e, a := expectedQueryValues, actualQueryValues; !reflect.DeepEqual(e, a) {
t.Errorf("expected query to be %q, got: %q", e, a)
}
})
}
}
Expand Down
Loading

0 comments on commit 70c0232

Please sign in to comment.