1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Threading . Tasks ;
7
+ using Microsoft . Azure . WebJobs . Script . WebHost ;
8
+ using Moq ;
9
+ using Xunit ;
10
+
11
+ namespace Microsoft . Azure . WebJobs . Script . Tests
12
+ {
13
+ public class KubernetesSecretsRepositoryTests
14
+ {
15
+ [ Fact ]
16
+ public async Task Read_Write_Functions_Keys ( )
17
+ {
18
+ var environment = new TestEnvironment ( ) ;
19
+ environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebJobsKubernetesSecretName , "test" ) ;
20
+ environment . SetEnvironmentVariable ( EnvironmentSettingNames . KubernetesServiceHost , "127.0.0.1" ) ;
21
+ environment . SetEnvironmentVariable ( EnvironmentSettingNames . KubernetesServiceHttpsPort , "443" ) ;
22
+ IDictionary < string , string > configMapData = new Dictionary < string , string > ( ) ;
23
+ var clientMock = new Mock < IKubernetesClient > ( MockBehavior . Strict ) ;
24
+ clientMock . Setup ( c => c . GetSecrets ( ) ) . ReturnsAsync ( configMapData ) ;
25
+ clientMock . SetupGet ( c => c . IsWritable ) . Returns ( true ) ;
26
+ clientMock . Setup ( c => c . OnSecretChange ( It . IsAny < Action > ( ) ) ) ;
27
+ clientMock . Setup ( c => c . UpdateSecrets ( It . IsAny < IDictionary < string , string > > ( ) ) ) . Returns < IDictionary < string , string > > ( a =>
28
+ {
29
+ foreach ( var k in a )
30
+ {
31
+ configMapData [ k . Key ] = k . Value ;
32
+ }
33
+ return Task . CompletedTask ;
34
+ } ) ;
35
+
36
+ var repo = new KubernetesSecretsRepository ( environment , clientMock . Object ) ;
37
+
38
+ await repo . WriteAsync ( ScriptSecretsType . Function , "FUNCTION1" , new FunctionSecrets
39
+ {
40
+ Keys = new List < Key >
41
+ {
42
+ new Key { Name = "Key1" , Value = "value" } ,
43
+ new Key { Name = "key2" , Value = "value" }
44
+ }
45
+ } ) ;
46
+ await repo . WriteAsync ( ScriptSecretsType . Function , "function2" , new FunctionSecrets
47
+ {
48
+ Keys = new List < Key >
49
+ {
50
+ new Key { Name = "Key1" , Value = "value" } ,
51
+ new Key { Name = "key2" , Value = "value" }
52
+ }
53
+ } ) ;
54
+
55
+ var result = await repo . ReadAsync ( ScriptSecretsType . Function , "function1" ) ;
56
+ Assert . NotNull ( result ) ;
57
+ Assert . Equal ( "value" , result . GetFunctionKey ( "Key1" , "function1" ) . Value ) ;
58
+ Assert . Equal ( "value" , result . GetFunctionKey ( "key2" , "function1" ) . Value ) ;
59
+
60
+ result = await repo . ReadAsync ( ScriptSecretsType . Function , "function2" ) ;
61
+ Assert . NotNull ( result ) ;
62
+ Assert . Equal ( "value" , result . GetFunctionKey ( "Key1" , "function2" ) . Value ) ;
63
+ Assert . Equal ( "value" , result . GetFunctionKey ( "key2" , "function2" ) . Value ) ;
64
+ }
65
+ }
66
+ }
0 commit comments