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 text
17
+
18
+ import (
19
+ "bytes"
20
+ "os"
21
+ "testing"
22
+
23
+ "github.com/opencontainers/go-digest"
24
+ ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25
+ "oras.land/oras/cmd/oras/internal/display/metadata"
26
+ "oras.land/oras/cmd/oras/internal/option"
27
+ "oras.land/oras/cmd/oras/internal/output"
28
+ )
29
+
30
+ func TestNewAttachHandler (t * testing.T ) {
31
+ printer := output .NewPrinter (& bytes.Buffer {}, os .Stderr )
32
+ handler := NewAttachHandler (printer )
33
+
34
+ if handler == nil {
35
+ t .Fatal ("NewAttachHandler() returned nil" )
36
+ }
37
+
38
+ attachHandler , ok := handler .(* AttachHandler )
39
+ if ! ok {
40
+ t .Fatal ("NewAttachHandler() did not return an *AttachHandler" )
41
+ }
42
+
43
+ if attachHandler .printer != printer {
44
+ t .Error ("NewAttachHandler() did not set printer correctly" )
45
+ }
46
+ }
47
+
48
+ func TestAttachHandler_OnAttached (t * testing.T ) {
49
+ content := []byte ("test content" )
50
+ subjectDigest := digest .FromBytes (content )
51
+ rootDigest := digest .FromBytes ([]byte ("root content" ))
52
+
53
+ tests := []struct {
54
+ name string
55
+ target * option.Target
56
+ root ocispec.Descriptor
57
+ subject ocispec.Descriptor
58
+ expectedDisplayReference string
59
+ }{
60
+ {
61
+ name : "reference ends with subject digest" ,
62
+ target : & option.Target {
63
+ Type : "registry" ,
64
+ RawReference : "example.com/repo@" + subjectDigest .String (),
65
+ Path : "example.com/repo" ,
66
+ },
67
+ root : ocispec.Descriptor {
68
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
69
+ Digest : rootDigest ,
70
+ Size : 100 ,
71
+ },
72
+ subject : ocispec.Descriptor {
73
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
74
+ Digest : subjectDigest ,
75
+ Size : 50 ,
76
+ },
77
+ expectedDisplayReference : "[registry] example.com/repo@" + subjectDigest .String (),
78
+ },
79
+ {
80
+ name : "reference with tag, not digest" ,
81
+ target : & option.Target {
82
+ Type : "registry" ,
83
+ RawReference : "example.com/repo:latest" ,
84
+ Path : "example.com/repo" ,
85
+ },
86
+ root : ocispec.Descriptor {
87
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
88
+ Digest : rootDigest ,
89
+ Size : 100 ,
90
+ },
91
+ subject : ocispec.Descriptor {
92
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
93
+ Digest : subjectDigest ,
94
+ Size : 50 ,
95
+ },
96
+ expectedDisplayReference : "[registry] example.com/repo@" + subjectDigest .String (),
97
+ },
98
+ {
99
+ name : "reference with partial digest match" ,
100
+ target : & option.Target {
101
+ Type : "registry" ,
102
+ RawReference : "example.com/repo@sha256:partial" + subjectDigest .String ()[12 :],
103
+ Path : "example.com/repo" ,
104
+ },
105
+ root : ocispec.Descriptor {
106
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
107
+ Digest : rootDigest ,
108
+ Size : 100 ,
109
+ },
110
+ subject : ocispec.Descriptor {
111
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
112
+ Digest : subjectDigest ,
113
+ Size : 50 ,
114
+ },
115
+ expectedDisplayReference : "[registry] example.com/repo@" + subjectDigest .String (),
116
+ },
117
+ }
118
+
119
+ for _ , tt := range tests {
120
+ t .Run (tt .name , func (t * testing.T ) {
121
+ printer := output .NewPrinter (& bytes.Buffer {}, os .Stderr )
122
+ handler := & AttachHandler {
123
+ printer : printer ,
124
+ }
125
+
126
+ handler .OnAttached (tt .target , tt .root , tt .subject )
127
+
128
+ if handler .root .Digest != tt .root .Digest {
129
+ t .Errorf ("OnAttached() root digest = %v, want %v" , handler .root .Digest , tt .root .Digest )
130
+ }
131
+
132
+ if handler .subjectDisplayReference != tt .expectedDisplayReference {
133
+ t .Errorf ("OnAttached() subjectDisplayReference = %v, want %v" , handler .subjectDisplayReference , tt .expectedDisplayReference )
134
+ }
135
+ })
136
+ }
137
+ }
138
+
139
+ func TestAttachHandler_Render (t * testing.T ) {
140
+ content := []byte ("test content" )
141
+ rootDigest := digest .FromBytes (content )
142
+
143
+ tests := []struct {
144
+ name string
145
+ out * bytes.Buffer
146
+ errorOut bool
147
+ subjectDisplayReference string
148
+ root ocispec.Descriptor
149
+ wantErr bool
150
+ expectedOutput string
151
+ }{
152
+ {
153
+ name : "successful render" ,
154
+ out : & bytes.Buffer {},
155
+ subjectDisplayReference : "[registry] example.com/repo:latest" ,
156
+ root : ocispec.Descriptor {
157
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
158
+ Digest : rootDigest ,
159
+ Size : int64 (len (content )),
160
+ },
161
+ wantErr : false ,
162
+ expectedOutput : "Attached to [registry] example.com/repo:latest\n Digest: " + rootDigest .String () + "\n " ,
163
+ },
164
+ {
165
+ name : "error on first print" ,
166
+ out : nil ,
167
+ errorOut : true ,
168
+ subjectDisplayReference : "[registry] example.com/repo:latest" ,
169
+ root : ocispec.Descriptor {
170
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
171
+ Digest : rootDigest ,
172
+ Size : int64 (len (content )),
173
+ },
174
+ wantErr : true ,
175
+ },
176
+ }
177
+
178
+ for _ , tt := range tests {
179
+ t .Run (tt .name , func (t * testing.T ) {
180
+ var printer * output.Printer
181
+ if tt .errorOut {
182
+ printer = output .NewPrinter (& errorWriter {}, os .Stderr )
183
+ } else {
184
+ printer = output .NewPrinter (tt .out , os .Stderr )
185
+ }
186
+
187
+ handler := & AttachHandler {
188
+ printer : printer ,
189
+ subjectDisplayReference : tt .subjectDisplayReference ,
190
+ root : tt .root ,
191
+ }
192
+
193
+ err := handler .Render ()
194
+
195
+ if (err != nil ) != tt .wantErr {
196
+ t .Errorf ("AttachHandler.Render() error = %v, wantErr %v" , err , tt .wantErr )
197
+ return
198
+ }
199
+
200
+ if ! tt .wantErr && tt .out != nil {
201
+ output := tt .out .String ()
202
+ if output != tt .expectedOutput {
203
+ t .Errorf ("AttachHandler.Render() output = %q, want %q" , output , tt .expectedOutput )
204
+ }
205
+ }
206
+ })
207
+ }
208
+ }
209
+
210
+ func TestAttachHandler_Render_SuccessfulCase (t * testing.T ) {
211
+ content := []byte ("test content" )
212
+ rootDigest := digest .FromBytes (content )
213
+
214
+ buffer := & bytes.Buffer {}
215
+ printer := output .NewPrinter (buffer , os .Stderr )
216
+
217
+ handler := & AttachHandler {
218
+ printer : printer ,
219
+ subjectDisplayReference : "[registry] example.com/repo:latest" ,
220
+ root : ocispec.Descriptor {
221
+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
222
+ Digest : rootDigest ,
223
+ Size : int64 (len (content )),
224
+ },
225
+ }
226
+
227
+ err := handler .Render ()
228
+ if err != nil {
229
+ t .Errorf ("AttachHandler.Render() error = %v, want nil" , err )
230
+ }
231
+
232
+ expectedOutput := "Attached to [registry] example.com/repo:latest\n Digest: " + rootDigest .String () + "\n "
233
+ if buffer .String () != expectedOutput {
234
+ t .Errorf ("AttachHandler.Render() output = %q, want %q" , buffer .String (), expectedOutput )
235
+ }
236
+ }
237
+
238
+ func TestAttachHandler_InterfaceCompliance (t * testing.T ) {
239
+ var _ metadata.AttachHandler = (* AttachHandler )(nil )
240
+ }
0 commit comments