1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading ;
4+ using System . Threading . Tasks ;
5+ using Agent . Sdk ;
6+ using Agent . Sdk . Knob ;
7+ using BuildXL . Cache . ContentStore . Hashing ;
8+ using Microsoft . VisualStudio . Services . Agent . Blob ;
9+ using Microsoft . VisualStudio . Services . Agent . Worker ;
10+ using Microsoft . VisualStudio . Services . BlobStore . Common ;
11+ using Microsoft . VisualStudio . Services . BlobStore . WebApi ;
12+ using Microsoft . VisualStudio . Services . BlobStore . WebApi . Contracts ;
13+ using Microsoft . VisualStudio . Services . Common ;
14+ using Microsoft . VisualStudio . Services . Content . Common ;
15+ using Microsoft . VisualStudio . Services . Content . Common . Tracing ;
16+ using Microsoft . VisualStudio . Services . WebApi ;
17+ using Moq ;
18+ using Xunit ;
19+
20+ namespace Microsoft . VisualStudio . Services . Agent . Tests
21+ {
22+
23+ public class BlobstoreClientSettingsL0
24+ {
25+ private const string OverrideChunkSize = "OVERRIDE_PIPELINE_ARTIFACT_CHUNKSIZE" ;
26+ private const string EnablePipelineArtifactLargeChunkSize = "AGENT_ENABLE_PIPELINEARTIFACT_LARGE_CHUNK_SIZE" ;
27+ [ Fact ]
28+ public void GetDefaultDomainId_ReturnsDefault_WhenNoSettings ( )
29+ {
30+ // Arrange
31+ var tracer = new Mock < IAppTraceSource > ( ) ;
32+ var settings = new BlobstoreClientSettings ( null , tracer . Object ) ;
33+
34+ // Act
35+ var result = settings . GetDefaultDomainId ( ) ;
36+
37+ // Assert
38+ Assert . Equal ( WellKnownDomainIds . DefaultDomainId , result ) ;
39+ }
40+
41+ [ Fact ]
42+ public void GetDefaultDomainId_ReturnsDomainId_WhenSettingsPresent ( )
43+ {
44+ // Arrange
45+ var tracer = new Mock < IAppTraceSource > ( ) ;
46+ var domainId = Guid . NewGuid ( ) . ToString ( ) ;
47+ var clientSettings = new ClientSettingsInfo
48+ {
49+ Properties = new Dictionary < string , string >
50+ {
51+ { ClientSettingsConstants . DefaultDomainId , domainId }
52+ }
53+ } ;
54+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
55+
56+ // Act
57+ var result = settings . GetDefaultDomainId ( ) ;
58+
59+ // Assert
60+ Assert . NotNull ( result ) ;
61+ }
62+
63+ [ Fact ]
64+ public void GetClientHashType_EnablePipelineArtifactLargeChunkSize_EnablesOrDisablesChunkSizing ( )
65+ {
66+ // Arrange
67+ var tracer = new Mock < IAppTraceSource > ( ) ;
68+ var clientSettings = new ClientSettingsInfo { Properties = new Dictionary < string , string > ( )
69+ {
70+ { ClientSettingsConstants . ChunkSize , HashType . Dedup1024K . ToString ( ) }
71+ }
72+ } ;
73+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
74+
75+ var environment = new LocalEnvironment ( ) ;
76+ var context = new Mock < AgentTaskPluginExecutionContext > ( ) ;
77+
78+ context . As < IKnobValueContext > ( )
79+ . Setup ( x => x . GetScopedEnvironment ( ) )
80+ . Returns ( environment ) ;
81+
82+ context . As < IKnobValueContext > ( )
83+ . Setup ( x => x . GetVariableValueOrDefault ( EnablePipelineArtifactLargeChunkSize ) )
84+ . Returns ( "false" ) ;
85+ environment . SetEnvironmentVariable ( EnablePipelineArtifactLargeChunkSize , "false" ) ;
86+
87+ // Act
88+ var result = settings . GetClientHashType ( context . Object ) ;
89+
90+ // make sure if we enable it, it uses the client settings
91+ Assert . Equal ( ChunkerHelper . DefaultChunkHashType , result ) ;
92+ context . As < IKnobValueContext > ( )
93+ . Setup ( x => x . GetVariableValueOrDefault ( EnablePipelineArtifactLargeChunkSize ) )
94+ . Returns ( "true" ) ;
95+ environment . SetEnvironmentVariable ( EnablePipelineArtifactLargeChunkSize , "true" ) ;
96+
97+ // Act
98+ result = settings . GetClientHashType ( context . Object ) ;
99+
100+ // Assert
101+ Assert . Equal ( HashType . Dedup1024K , result ) ;
102+ }
103+
104+ [ Fact ]
105+ public void GetClientHashType_PipelineOverride ( )
106+ {
107+ // Arrange
108+ var tracer = new Mock < IAppTraceSource > ( ) ;
109+ var clientSettings = new ClientSettingsInfo
110+ {
111+ Properties = new Dictionary < string , string > ( )
112+ {
113+ { ClientSettingsConstants . ChunkSize , HashType . Dedup64K . ToString ( ) }
114+ }
115+ } ;
116+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
117+
118+ var environment = new LocalEnvironment ( ) ;
119+ var context = new Mock < AgentTaskPluginExecutionContext > ( ) ;
120+
121+ context . As < IKnobValueContext > ( )
122+ . Setup ( x => x . GetScopedEnvironment ( ) )
123+ . Returns ( environment ) ;
124+
125+ context . As < IKnobValueContext > ( )
126+ . Setup ( x => x . GetVariableValueOrDefault ( EnablePipelineArtifactLargeChunkSize ) )
127+ . Returns ( "true" ) ;
128+ environment . SetEnvironmentVariable ( EnablePipelineArtifactLargeChunkSize , "true" ) ;
129+
130+ context . As < IKnobValueContext > ( )
131+ . Setup ( x => x . GetVariableValueOrDefault ( OverrideChunkSize ) )
132+ . Returns ( HashType . Dedup1024K . ToString ( ) ) ;
133+ environment . SetEnvironmentVariable ( OverrideChunkSize , HashType . Dedup1024K . ToString ( ) ) ;
134+
135+ // Act
136+ var result = settings . GetClientHashType ( context . Object ) ;
137+
138+ // we should successfully override the chunk size in the client settings:
139+ Assert . Equal ( HashType . Dedup1024K , result ) ;
140+
141+ // now let's setup a bad override and make sure it falls back to the client settings:
142+ clientSettings . Properties [ ClientSettingsConstants . ChunkSize ] = HashType . Dedup1024K . ToString ( ) ;
143+ context . As < IKnobValueContext > ( )
144+ . Setup ( x => x . GetVariableValueOrDefault ( OverrideChunkSize ) )
145+ . Returns ( "nonsense" ) ;
146+ environment . SetEnvironmentVariable ( OverrideChunkSize , "nonsense" ) ;
147+
148+ // Act
149+ result = settings . GetClientHashType ( context . Object ) ;
150+
151+ // Assert
152+ Assert . Equal ( HashType . Dedup1024K , result ) ;
153+ }
154+
155+ [ Fact ]
156+ public void GetRedirectTimeout_ReturnsNull_WhenNotPresent ( )
157+ {
158+ // Arrange
159+ var tracer = new Mock < IAppTraceSource > ( ) ;
160+ var clientSettings = new ClientSettingsInfo { Properties = new Dictionary < string , string > ( ) } ;
161+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
162+
163+ // Act
164+ var result = settings . GetRedirectTimeout ( ) ;
165+
166+ // Assert
167+ Assert . Null ( result ) ;
168+ }
169+
170+ [ Fact ]
171+ public void GetRedirectTimeout_ReturnsValue_WhenPresent ( )
172+ {
173+ // Arrange
174+ var tracer = new Mock < IAppTraceSource > ( ) ;
175+ var clientSettings = new ClientSettingsInfo
176+ {
177+ Properties = new Dictionary < string , string >
178+ {
179+ { ClientSettingsConstants . RedirectTimeout , "42" }
180+ }
181+ } ;
182+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
183+
184+ // Act
185+ var result = settings . GetRedirectTimeout ( ) ;
186+
187+ // Assert
188+ Assert . Equal ( 42 , result ) ;
189+ }
190+
191+ [ Fact ]
192+ public void GetMaxParallelism_ReturnsNull_WhenNotPresent ( )
193+ {
194+ // Arrange
195+ var tracer = new Mock < IAppTraceSource > ( ) ;
196+ var clientSettings = new ClientSettingsInfo { Properties = new Dictionary < string , string > ( ) } ;
197+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
198+
199+ // Act
200+ var result = settings . GetMaxParallelism ( ) ;
201+
202+ // Assert
203+ Assert . Null ( result ) ;
204+ }
205+
206+ [ Fact ]
207+ public void GetMaxParallelism_ReturnsValue_WhenPresent ( )
208+ {
209+ // Arrange
210+ var tracer = new Mock < IAppTraceSource > ( ) ;
211+ var clientSettings = new ClientSettingsInfo
212+ {
213+ Properties = new Dictionary < string , string >
214+ {
215+ { "MaxParallelism" , "8" }
216+ }
217+ } ;
218+ var settings = new BlobstoreClientSettings ( clientSettings , tracer . Object ) ;
219+
220+ // Act
221+ var result = settings . GetMaxParallelism ( ) ;
222+
223+ // Assert
224+ Assert . Equal ( 8 , result ) ;
225+ }
226+ }
227+ }
0 commit comments