@@ -46,9 +46,9 @@ public void ReturnsDefaultEndpointWhenDockerContextIsDefault()
46
46
public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromPropertiesFile ( )
47
47
{
48
48
// 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/" ) ) ;
50
50
51
- ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=custom" , context . GetDockerConfig ( ) } ) ;
51
+ ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=custom" , $ "docker.config= { context . DockerConfigDirectoryPath } " } ) ;
52
52
var dockerConfig = new DockerConfig ( customConfiguration ) ;
53
53
54
54
// When
@@ -62,10 +62,10 @@ public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromPropertiesFile
62
62
public void ReturnsConfiguredEndpointWhenDockerContextIsCustomFromConfigFile ( )
63
63
{
64
64
// 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/" ) ) ;
66
66
67
67
// 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 } " } ) ;
69
69
var dockerConfig = new DockerConfig ( customConfiguration ) ;
70
70
71
71
// When
@@ -83,17 +83,37 @@ public void ReturnsActiveEndpointWhenDockerContextIsUnset()
83
83
}
84
84
85
85
[ Fact ]
86
- public void ReturnsNullWhenDockerContextNotFound ( )
86
+ public void ThrowsWhenDockerContextNotFound ( )
87
87
{
88
88
// Given
89
89
ICustomConfiguration customConfiguration = new PropertiesFileConfiguration ( new [ ] { "docker.context=missing" } ) ;
90
90
var dockerConfig = new DockerConfig ( customConfiguration ) ;
91
91
92
92
// When
93
- var currentEndpoint = dockerConfig . GetCurrentEndpoint ( ) ;
93
+ var exception = Assert . Throws < DockerConfigurationException > ( ( ) => dockerConfig . GetCurrentEndpoint ( ) ) ;
94
94
95
95
// 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 ) ;
97
117
}
98
118
}
99
119
@@ -117,9 +137,9 @@ public void ReturnsActiveEndpointWhenDockerHostIsEmpty()
117
137
public void ReturnsConfiguredEndpointWhenDockerHostIsSet ( )
118
138
{
119
139
// Given
120
- using var context = new ConfigMetaFile ( "custom" , "" ) ;
140
+ using var context = new ConfigMetaFile ( "custom" ) ;
121
141
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 } " } ) ;
123
143
var dockerConfig = new DockerConfig ( customConfiguration ) ;
124
144
125
145
// When
@@ -147,26 +167,32 @@ private sealed class ConfigMetaFile : IDisposable
147
167
148
168
private const string MetaFileJson = "{{\" Name\" :\" {0}\" ,\" Metadata\" :{{}},\" Endpoints\" :{{\" docker\" :{{\" Host\" :\" {1}\" ,\" SkipTLSVerify\" :false}}}}}}" ;
149
169
150
- private readonly string _dockerConfigDirectoryPath ;
170
+ public string DockerConfigDirectoryPath { get ; }
151
171
152
- public ConfigMetaFile ( string context , string endpoint , [ CallerMemberName ] string caller = "" )
172
+ public ConfigMetaFile ( string context , [ CallerMemberName ] string caller = "" )
153
173
{
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 ) ;
160
175
}
161
176
162
- public string GetDockerConfig ( )
177
+ public ConfigMetaFile ( string context , Uri endpoint , [ CallerMemberName ] string caller = "" )
163
178
{
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 ;
165
191
}
166
192
167
193
public void Dispose ( )
168
194
{
169
- Directory . Delete ( _dockerConfigDirectoryPath , true ) ;
195
+ Directory . Delete ( DockerConfigDirectoryPath , true ) ;
170
196
}
171
197
}
172
198
}
0 commit comments