|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/jetstack/version-checker/pkg/api" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/sirupsen/logrus" |
| 18 | +) |
| 19 | + |
| 20 | +type hostnameOverride struct { |
| 21 | + Host string |
| 22 | + RT http.RoundTripper |
| 23 | +} |
| 24 | + |
| 25 | +func (r *hostnameOverride) RoundTrip(req *http.Request) (*http.Response, error) { |
| 26 | + if req.Host != r.Host { |
| 27 | + if testing.Verbose() { |
| 28 | + fmt.Printf("Overriding URI from: %s to %s\n", req.Host, strings.TrimPrefix(r.Host, "http://")) |
| 29 | + } |
| 30 | + req.Host = strings.TrimPrefix(r.Host, "http://") |
| 31 | + req.URL.Host = strings.TrimPrefix(r.Host, "http://") |
| 32 | + req.URL.Scheme = "http" |
| 33 | + } |
| 34 | + // fmt.Printf("Req: %+v", req) |
| 35 | + return r.RT.RoundTrip(req) |
| 36 | +} |
| 37 | + |
| 38 | +func TestTags(t *testing.T) { |
| 39 | + log := logrus.NewEntry(logrus.New()) |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + t.Run("successful Tags fetch", func(t *testing.T) { |
| 43 | + |
| 44 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + require.NotEmpty(t, r.Header.Get("Authorization")) |
| 46 | + |
| 47 | + require.Equal(t, "/v2/repositories/testrepo/testimage/tags", r.URL.Path) |
| 48 | + w.WriteHeader(http.StatusOK) |
| 49 | + _ = json.NewEncoder(w).Encode(TagResponse{ |
| 50 | + Results: []Result{ |
| 51 | + { |
| 52 | + Name: "v1.0.0", |
| 53 | + Timestamp: time.Now().Add(-24 * time.Hour).Format(time.RFC3339Nano), |
| 54 | + Digest: "sha256:abcdef", |
| 55 | + Images: []Image{ |
| 56 | + {Digest: "sha256:child1", OS: "linux", Architecture: "amd64"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "v2.0.0", |
| 61 | + Timestamp: time.Now().Add(-48 * time.Hour).Format(time.RFC3339Nano), |
| 62 | + Images: []Image{ |
| 63 | + {Digest: "sha256:child2", OS: "linux", Architecture: "amd64"}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }) |
| 68 | + })) |
| 69 | + defer server.Close() |
| 70 | + |
| 71 | + client := &Client{ |
| 72 | + Client: server.Client(), |
| 73 | + log: log, |
| 74 | + Options: Options{ |
| 75 | + Token: "testtoken", |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + client.Transport = &hostnameOverride{RT: server.Client().Transport, Host: server.URL} |
| 80 | + |
| 81 | + tags, err := client.Tags(ctx, "NOT USED!", "testrepo", "testimage") |
| 82 | + require.NoError(t, err) |
| 83 | + require.Len(t, tags, 2) |
| 84 | + |
| 85 | + assert.Equal(t, "v1.0.0", tags[0].Tag) |
| 86 | + assert.Equal(t, "sha256:abcdef", tags[0].SHA) |
| 87 | + assert.Equal(t, api.OS("linux"), tags[0].Children[0].OS) |
| 88 | + assert.Equal(t, api.Architecture("amd64"), tags[0].Children[0].Architecture) |
| 89 | + |
| 90 | + assert.Equal(t, "v2.0.0", tags[1].Tag) |
| 91 | + assert.NotEmpty(t, tags[1].SHA) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("error on invalid response", func(t *testing.T) { |
| 95 | + |
| 96 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 | + w.WriteHeader(http.StatusOK) |
| 98 | + _, _ = w.Write([]byte("invalid json")) |
| 99 | + })) |
| 100 | + defer server.Close() |
| 101 | + |
| 102 | + client := &Client{ |
| 103 | + Client: server.Client(), |
| 104 | + log: log, |
| 105 | + Options: Options{ |
| 106 | + Token: "testtoken", |
| 107 | + }, |
| 108 | + } |
| 109 | + client.Transport = &hostnameOverride{RT: server.Client().Transport, Host: server.URL} |
| 110 | + |
| 111 | + tags, err := client.Tags(ctx, "NOT USED!", "testrepo", "testimage") |
| 112 | + assert.Nil(t, tags) |
| 113 | + assert.Error(t, err) |
| 114 | + assert.Contains(t, err.Error(), "unexpected image tags response") |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("error on non-200 status code", func(t *testing.T) { |
| 118 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 | + w.WriteHeader(http.StatusNotFound) |
| 120 | + _, _ = w.Write([]byte("not found")) |
| 121 | + })) |
| 122 | + defer server.Close() |
| 123 | + |
| 124 | + client := &Client{ |
| 125 | + Client: server.Client(), |
| 126 | + log: log, |
| 127 | + Options: Options{ |
| 128 | + Token: "testtoken", |
| 129 | + }, |
| 130 | + } |
| 131 | + client.Transport = &hostnameOverride{RT: server.Client().Transport, Host: server.URL} |
| 132 | + |
| 133 | + tags, err := client.Tags(ctx, "NOT USED!", "testrepo", "testimage") |
| 134 | + assert.Nil(t, tags) |
| 135 | + assert.Error(t, err) |
| 136 | + assert.Contains(t, err.Error(), "unexpected image") |
| 137 | + }) |
| 138 | +} |
0 commit comments