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 option
17+
18+ import (
19+ "bytes"
20+ "strings"
21+ "sync"
22+ "testing"
23+
24+ "github.com/sirupsen/logrus"
25+ "oras.land/oras-go/v2/registry/remote"
26+ )
27+
28+ func TestWarningHandler_GetHandler (t * testing.T ) {
29+ tests := []struct {
30+ name string
31+ registry string
32+ warnings []remote.Warning
33+ wantLogs []string
34+ wantCount int
35+ }{
36+ {
37+ name : "single warning" ,
38+ registry : "localhost:5000" ,
39+ warnings : []remote.Warning {
40+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "First warning" }},
41+ },
42+ wantLogs : []string {"First warning" },
43+ wantCount : 1 ,
44+ },
45+ {
46+ name : "duplicate warnings same registry" ,
47+ registry : "localhost:5000" ,
48+ warnings : []remote.Warning {
49+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "First warning" }},
50+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "First warning" }},
51+ },
52+ wantLogs : []string {"First warning" },
53+ wantCount : 1 ,
54+ },
55+ {
56+ name : "different warnings same registry" ,
57+ registry : "localhost:5000" ,
58+ warnings : []remote.Warning {
59+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "First warning" }},
60+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "Second warning" }},
61+ },
62+ wantLogs : []string {"First warning" , "Second warning" },
63+ wantCount : 2 ,
64+ },
65+ {
66+ name : "empty warning value" ,
67+ registry : "localhost:5000" ,
68+ warnings : []remote.Warning {
69+ {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "Empty value warning" }},
70+ },
71+ wantLogs : []string {"Empty value warning" },
72+ wantCount : 1 ,
73+ },
74+ }
75+
76+ for _ , tt := range tests {
77+ t .Run (tt .name , func (t * testing.T ) {
78+ var buf bytes.Buffer
79+ logger := logrus .New ()
80+ logger .SetOutput (& buf )
81+ logger .SetLevel (logrus .WarnLevel )
82+
83+ wh := & WarningHandler {}
84+ handler := wh .GetHandler (tt .registry , logger )
85+
86+ for _ , warning := range tt .warnings {
87+ handler (warning )
88+ }
89+
90+ output := buf .String ()
91+ logCount := strings .Count (output , "level=warning" )
92+
93+ if logCount != tt .wantCount {
94+ t .Errorf ("Expected %d warning logs, got %d" , tt .wantCount , logCount )
95+ }
96+
97+ for _ , expectedLog := range tt .wantLogs {
98+ if ! strings .Contains (output , expectedLog ) {
99+ t .Errorf ("Expected log to contain %q, but it didn't. Output: %s" , expectedLog , output )
100+ }
101+ }
102+
103+ if ! strings .Contains (output , tt .registry ) && tt .wantCount > 0 {
104+ t .Errorf ("Expected log to contain registry %q, but it didn't. Output: %s" , tt .registry , output )
105+ }
106+ })
107+ }
108+ }
109+
110+ func TestWarningHandler_GetHandler_DifferentRegistries (t * testing.T ) {
111+ var buf bytes.Buffer
112+ logger := logrus .New ()
113+ logger .SetOutput (& buf )
114+ logger .SetLevel (logrus .WarnLevel )
115+
116+ wh := & WarningHandler {}
117+
118+ handler1 := wh .GetHandler ("registry1.example.com" , logger )
119+ handler2 := wh .GetHandler ("registry2.example.com" , logger )
120+
121+ warning := remote.Warning {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "Test warning" }}
122+
123+ handler1 (warning )
124+ handler2 (warning )
125+
126+ output := buf .String ()
127+ logCount := strings .Count (output , "level=warning" )
128+
129+ if logCount != 2 {
130+ t .Errorf ("Expected 2 warning logs for different registries, got %d" , logCount )
131+ }
132+
133+ if ! strings .Contains (output , "registry1.example.com" ) {
134+ t .Error ("Expected log to contain registry1.example.com" )
135+ }
136+ if ! strings .Contains (output , "registry2.example.com" ) {
137+ t .Error ("Expected log to contain registry2.example.com" )
138+ }
139+ }
140+
141+ func TestWarningHandler_GetHandler_Concurrency (t * testing.T ) {
142+ var buf bytes.Buffer
143+ logger := logrus .New ()
144+ logger .SetOutput (& buf )
145+ logger .SetLevel (logrus .WarnLevel )
146+
147+ wh := & WarningHandler {}
148+ handler := wh .GetHandler ("localhost:5000" , logger )
149+
150+ var wg sync.WaitGroup
151+ numGoroutines := 100
152+
153+ for i := 0 ; i < numGoroutines ; i ++ {
154+ wg .Add (1 )
155+ go func (i int ) {
156+ defer wg .Done ()
157+ warning := remote.Warning {
158+ WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "Concurrent warning" },
159+ }
160+ handler (warning )
161+ }(i )
162+ }
163+
164+ wg .Wait ()
165+
166+ output := buf .String ()
167+ logCount := strings .Count (output , "level=warning" )
168+
169+ if logCount != 1 {
170+ t .Errorf ("Expected exactly 1 warning log despite concurrent calls, got %d" , logCount )
171+ }
172+ }
173+
174+ func TestWarningHandler_GetHandler_MultipleHandlersForSameRegistry (t * testing.T ) {
175+ var buf bytes.Buffer
176+ logger := logrus .New ()
177+ logger .SetOutput (& buf )
178+ logger .SetLevel (logrus .WarnLevel )
179+
180+ wh := & WarningHandler {}
181+
182+ handler1 := wh .GetHandler ("localhost:5000" , logger )
183+ handler2 := wh .GetHandler ("localhost:5000" , logger )
184+
185+ warning := remote.Warning {WarningValue : remote.WarningValue {Code : 299 , Agent : "oras" , Text : "Test warning" }}
186+
187+ handler1 (warning )
188+ handler2 (warning )
189+
190+ output := buf .String ()
191+ logCount := strings .Count (output , "level=warning" )
192+
193+ if logCount != 1 {
194+ t .Errorf ("Expected 1 warning log for same registry with multiple handlers, got %d" , logCount )
195+ }
196+ }
0 commit comments