@@ -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 ThrowsWhenDockerHostNotFound ( )
102+ {
103+ // Given
104+ using var context = new ConfigMetaFile ( "custom" , null ) ;
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 or empty 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" , null ) ;
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,21 @@ 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 ;
151-
152- public ConfigMetaFile ( string context , string endpoint , [ CallerMemberName ] string caller = "" )
170+ public ConfigMetaFile ( string context , Uri endpoint , [ CallerMemberName ] string caller = "" )
153171 {
154- _dockerConfigDirectoryPath = Path . Combine ( TestSession . TempDirectoryPath , caller ) ;
172+ DockerConfigDirectoryPath = Path . Combine ( TestSession . TempDirectoryPath , caller ) ;
155173 var dockerContextHash = Convert . ToHexString ( SHA256 . HashData ( Encoding . Default . GetBytes ( context ) ) ) . ToLowerInvariant ( ) ;
156- var dockerContextMetaDirectoryPath = Path . Combine ( _dockerConfigDirectoryPath , "contexts" , "meta" , dockerContextHash ) ;
174+ var dockerContextMetaDirectoryPath = Path . Combine ( DockerConfigDirectoryPath , "contexts" , "meta" , dockerContextHash ) ;
157175 _ = 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 ) ) ;
176+ File . WriteAllText ( Path . Combine ( DockerConfigDirectoryPath , "config.json" ) , string . Format ( ConfigFileJson , context ) ) ;
177+ File . WriteAllText ( Path . Combine ( dockerContextMetaDirectoryPath , "meta.json" ) , endpoint == null ? "{}" : string . Format ( MetaFileJson , context , endpoint . AbsoluteUri ) ) ;
160178 }
161179
162- public string GetDockerConfig ( )
163- {
164- return "docker.config=" + _dockerConfigDirectoryPath ;
165- }
180+ public string DockerConfigDirectoryPath { get ; }
166181
167182 public void Dispose ( )
168183 {
169- Directory . Delete ( _dockerConfigDirectoryPath , true ) ;
184+ Directory . Delete ( DockerConfigDirectoryPath , true ) ;
170185 }
171186 }
172187 }
0 commit comments