@@ -46,9 +46,9 @@ public void ReturnsDefaultEndpointWhenDockerContextIsDefault()
4646 public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromPropertiesFile ( )
4747 {
4848 // Given
49- using var context = new ConfigMetaFile ( "custom" , "tcp://127.0.0.1:2375/" ) ;
49+ using var context = new ConfigMetaFile ( "custom" , new Uri ( "tcp://127.0.0.1:2375/" ) ) ;
5050
51- ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=custom" , context . GetDockerConfig ( ) } ) ;
51+ ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=custom" , $ "docker.config= { context . DockerConfigDirectoryPath } " } ) ;
5252 var dockerConfig = new DockerConfig ( customConfiguration ) ;
5353
5454 // When
@@ -62,10 +62,10 @@ public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromPropertiesFile
6262 public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromConfigFile ( )
6363 {
6464 // Given
65- using var context = new ConfigMetaFile ( "custom" , "tcp://127.0.0.1:2375/" ) ;
65+ using var context = new ConfigMetaFile ( "custom" , new Uri ( "tcp://127.0.0.1:2375/" ) ) ;
6666
6767 // This test reads the current context JSON node from the Docker config file.
68- ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { context . GetDockerConfig ( ) } ) ;
68+ ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { $ "docker.config= { context . DockerConfigDirectoryPath } " } ) ;
6969 var dockerConfig = new DockerConfig ( customConfiguration ) ;
7070
7171 // When
@@ -83,17 +83,37 @@ public void ReturnsActiveEndpointWhenDockerContextIsUnset()
8383 }
8484
8585 [ Fact ]
86- public void ReturnsNullWhenDockerContextNotFound ( )
86+ public void ThrowsWhenDockerContextNotFound ( )
8787 {
8888 // Given
8989 ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=missing" } ) ;
9090 var dockerConfig = new DockerConfig ( customConfiguration ) ;
9191
9292 // When
93- var currentEndpoint = dockerConfig . GetCurrentEndpoint ( ) ;
93+ var exception = Assert . Throws < DockerConfigurationException > ( ( ) => dockerConfig . GetCurrentEndpoint ( ) ) ;
9494
9595 // Then
96- Assert . Null ( currentEndpoint ) ;
96+ Assert . Equal ( "The Docker context 'missing' does not exist" , exception . Message ) ;
97+ Assert . IsType < DirectoryNotFoundException > ( exception . InnerException ) ;
98+ }
99+
100+ [ Fact ]
101+ public void ThrowsWhenDockerConfigEndpointNotFound ( )
102+ {
103+ // Given
104+ using var context = new ConfigMetaFile ( "custom" ) ;
105+
106+ ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=custom" , $ "docker.config={ context . DockerConfigDirectoryPath } " } ) ;
107+ var dockerConfig = new DockerConfig ( customConfiguration ) ;
108+
109+ // When
110+ var exception = Assert . Throws < DockerConfigurationException > ( ( ) => dockerConfig . GetCurrentEndpoint ( ) ) ;
111+
112+ // Then
113+ Assert . StartsWith ( "The Docker host is null in " , exception . Message ) ;
114+ Assert . Contains ( context . DockerConfigDirectoryPath , exception . Message ) ;
115+ Assert . EndsWith ( " (JSONPath: Endpoints.docker.Host)" , exception . Message ) ;
116+ Assert . Null ( exception . InnerException ) ;
97117 }
98118 }
99119
@@ -117,9 +137,9 @@ public void ReturnsActiveEndpointWhenDockerHostIsEmpty()
117137 public void ReturnsConfiguredEndpointWhenDockerHostIsSet ( )
118138 {
119139 // Given
120- using var context = new ConfigMetaFile ( "custom" , "" ) ;
140+ using var context = new ConfigMetaFile ( "custom" ) ;
121141
122- ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.host=tcp://127.0.0.1:2375/" , context . GetDockerConfig ( ) } ) ;
142+ ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.host=tcp://127.0.0.1:2375/" , $ "docker.config= { context . DockerConfigDirectoryPath } " } ) ;
123143 var dockerConfig = new DockerConfig ( customConfiguration ) ;
124144
125145 // When
@@ -147,26 +167,32 @@ private sealed class ConfigMetaFile : IDisposable
147167
148168 private const string MetaFileJson = "{{\" Name\" :\" {0}\" ,\" Metadata\" :{{}},\" Endpoints\" :{{\" docker\" :{{\" Host\" :\" {1}\" ,\" SkipTLSVerify\" :false}}}}}}" ;
149169
150- private readonly string _dockerConfigDirectoryPath ;
170+ public string DockerConfigDirectoryPath { get ; }
151171
152- public ConfigMetaFile ( string context , string endpoint , [ CallerMemberName ] string caller = "" )
172+ public ConfigMetaFile ( string context , [ CallerMemberName ] string caller = "" )
153173 {
154- _dockerConfigDirectoryPath = Path . Combine ( TestSession . TempDirectoryPath , caller ) ;
155- var dockerContextHash = Convert . ToHexString ( SHA256 . HashData ( Encoding . Default . GetBytes ( context ) ) ) . ToLowerInvariant ( ) ;
156- var dockerContextMetaDirectoryPath = Path . Combine ( _dockerConfigDirectoryPath , "contexts" , "meta" , dockerContextHash ) ;
157- _ = Directory . CreateDirectory ( dockerContextMetaDirectoryPath ) ;
158- File . WriteAllText ( Path . Combine ( _dockerConfigDirectoryPath , "config.json" ) , string . Format ( ConfigFileJson , context ) ) ;
159- File . WriteAllText ( Path . Combine ( dockerContextMetaDirectoryPath , "meta.json" ) , string . Format ( MetaFileJson , context , endpoint ) ) ;
174+ DockerConfigDirectoryPath = InitializeContext ( context , null , caller ) ;
160175 }
161176
162- public string GetDockerConfig ( )
177+ public ConfigMetaFile ( string context , Uri endpoint , [ CallerMemberName ] string caller = "" )
163178 {
164- return "docker.config=" + _dockerConfigDirectoryPath ;
179+ DockerConfigDirectoryPath = InitializeContext ( context , endpoint , caller ) ;
180+ }
181+
182+ private static string InitializeContext ( string context , Uri endpoint , [ CallerMemberName ] string caller = "" )
183+ {
184+ var dockerConfigDirectoryPath = Path . Combine ( TestSession . TempDirectoryPath , caller ) ;
185+ var dockerContextHash = Convert . ToHexString ( SHA256 . HashData ( Encoding . Default . GetBytes ( context ) ) ) . ToLowerInvariant ( ) ;
186+ var dockerContextMetaDirectoryPath = Path . Combine ( dockerConfigDirectoryPath , "contexts" , "meta" , dockerContextHash ) ;
187+ _ = Directory . CreateDirectory ( dockerContextMetaDirectoryPath ) ;
188+ File . WriteAllText ( Path . Combine ( dockerConfigDirectoryPath , "config.json" ) , string . Format ( ConfigFileJson , context ) ) ;
189+ File . WriteAllText ( Path . Combine ( dockerContextMetaDirectoryPath , "meta.json" ) , endpoint == null ? "{}" : string . Format ( MetaFileJson , context , endpoint . AbsoluteUri ) ) ;
190+ return dockerConfigDirectoryPath ;
165191 }
166192
167193 public void Dispose ( )
168194 {
169- Directory . Delete ( _dockerConfigDirectoryPath , true ) ;
195+ Directory . Delete ( DockerConfigDirectoryPath , true ) ;
170196 }
171197 }
172198 }
0 commit comments