| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
// Package registry_test gives examples code of functions in the registry package. |
| 17 |
package registry_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"net/url" |
| 26 |
"os" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"oras.land/oras-go/v2/registry" |
| 30 |
. "oras.land/oras-go/v2/registry/internal/doc" |
| 31 |
"oras.land/oras-go/v2/registry/remote" |
| 32 |
) |
| 33 |
|
| 34 |
const _ = ExampleUnplayable |
| 35 |
|
| 36 |
const exampleRepositoryName = "example" |
| 37 |
|
| 38 |
var host string |
| 39 |
|
| 40 |
func TestMain(m *testing.M) { |
| 41 |
// Setup a local HTTPS registry |
| 42 |
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 |
p := r.URL.Path |
| 44 |
m := r.Method |
| 45 |
switch { |
| 46 |
case p == "/v2/_catalog" && m == "GET": |
| 47 |
result := struct { |
| 48 |
Repositories []string `json:"repositories"` |
| 49 |
}{ |
| 50 |
Repositories: []string{"public/repo1", "public/repo2", "internal/repo3"}, |
| 51 |
} |
| 52 |
json.NewEncoder(w).Encode(result) |
| 53 |
case p == fmt.Sprintf("/v2/%s/tags/list", exampleRepositoryName) && m == "GET": |
| 54 |
result := struct { |
| 55 |
Tags []string `json:"tags"` |
| 56 |
}{ |
| 57 |
Tags: []string{"tag1", "tag2"}, |
| 58 |
} |
| 59 |
json.NewEncoder(w).Encode(result) |
| 60 |
} |
| 61 |
})) |
| 62 |
defer ts.Close() |
| 63 |
u, err := url.Parse(ts.URL) |
| 64 |
if err != nil { |
| 65 |
panic(err) |
| 66 |
} |
| 67 |
host = u.Host |
| 68 |
http.DefaultTransport = ts.Client().Transport |
| 69 |
|
| 70 |
os.Exit(m.Run()) |
| 71 |
} |
| 72 |
|
| 73 |
// ExampleRepositories gives example snippets for listing repositories in the registry without pagination. |
| 74 |
func ExampleRepositories() { |
| 75 |
reg, err := remote.NewRegistry(host) |
| 76 |
if err != nil { |
| 77 |
panic(err) // Handle error |
| 78 |
} |
| 79 |
|
| 80 |
ctx := context.Background() |
| 81 |
repos, err := registry.Repositories(ctx, reg) |
| 82 |
if err != nil { |
| 83 |
panic(err) // Handle error |
| 84 |
} |
| 85 |
for _, repo := range repos { |
| 86 |
fmt.Println(repo) |
| 87 |
} |
| 88 |
// Output: |
| 89 |
// public/repo1 |
| 90 |
// public/repo2 |
| 91 |
// internal/repo3 |
| 92 |
} |
| 93 |
|
| 94 |
// ExampleTags gives example snippets for listing tags in the repository without pagination. |
| 95 |
func ExampleTags() { |
| 96 |
repo, err := remote.NewRepository(fmt.Sprintf("%s/%s", host, exampleRepositoryName)) |
| 97 |
if err != nil { |
| 98 |
panic(err) // Handle error |
| 99 |
} |
| 100 |
|
| 101 |
ctx := context.Background() |
| 102 |
tags, err := registry.Tags(ctx, repo) |
| 103 |
if err != nil { |
| 104 |
panic(err) // Handle error |
| 105 |
} |
| 106 |
for _, tag := range tags { |
| 107 |
fmt.Println(tag) |
| 108 |
} |
| 109 |
// Output: |
| 110 |
// tag1 |
| 111 |
// tag2 |
| 112 |
} |