1
1
package com.coder.gateway.sdk
2
2
3
+ import com.sun.net.httpserver.HttpExchange
4
+ import com.sun.net.httpserver.HttpHandler
5
+ import com.sun.net.httpserver.HttpServer
3
6
import spock.lang.Requires
7
+ import spock.lang.Shared
4
8
import spock.lang.Unroll
5
9
6
10
import java.nio.file.Files
7
11
import java.nio.file.Path
8
12
9
13
@Unroll
10
14
class CoderCLIManagerTest extends spock.lang. Specification {
11
- /**
12
- * Create a CLI manager pointing to the URL in the environment or to the
13
- default URLs.
14
- *
15
- * @TODO: Implement a mock.
16
- */
17
- def createCLIManager (Boolean alternate = false ) {
18
- var url = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT" )
19
- if (url == null ) {
20
- url = " https://dev.coder.com"
21
- }
22
- if (alternate) {
23
- url = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT_ALT" )
24
- if (url == null ) {
25
- url = " https://oss.demo.coder.com"
15
+ @Shared
16
+ private Path tmpdir = Path . of(System . getProperty(" java.io.tmpdir" )). resolve(" coder-gateway-test" )
17
+ @Shared
18
+ private String deploymentURL = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT" )
19
+ @Shared
20
+ private String altDeploymentURL = System . getenv(" CODER_GATEWAY_TEST_DEPLOYMENT_ALT" )
21
+ @Shared
22
+ private def servers = []
23
+
24
+ String startMockServer () {
25
+ HttpServer srv = HttpServer . create(new InetSocketAddress (0 ), 0 )
26
+ srv. createContext(" /" , new HttpHandler () {
27
+ void handle (HttpExchange exchange ) {
28
+ int code = HttpURLConnection . HTTP_OK
29
+ String response = " not a real binary"
30
+
31
+ String [] etags = exchange. requestHeaders. get(" If-None-Match" )
32
+ if (etags != null && etags. contains(" \" cb25cf6f41bb3127d7e05b0c3c6403be9ab052bc\" " )) {
33
+ code = HttpURLConnection . HTTP_NOT_MODIFIED
34
+ response = " not modified"
35
+ }
36
+
37
+ if (! exchange. requestURI. path. startsWith(" /bin/coder-" )) {
38
+ code = HttpURLConnection . HTTP_NOT_FOUND
39
+ response = " not found"
40
+ }
41
+
42
+ byte [] body = response. getBytes()
43
+ exchange. sendResponseHeaders(code, code == HttpURLConnection . HTTP_OK ? body. length : -1 )
44
+ exchange. responseBody. write(body)
45
+ exchange. close()
26
46
}
47
+ })
48
+ servers << srv
49
+ srv. start()
50
+ return " http://localhost:" + srv. address. port
51
+ }
52
+
53
+ void setupSpec () {
54
+ // Clean up from previous runs otherwise they get cluttered since the
55
+ // port is random.
56
+ tmpdir. toFile(). deleteDir()
57
+
58
+ // Spin up mock server(s).
59
+ if (deploymentURL == null ) {
60
+ deploymentURL = startMockServer()
61
+ }
62
+ if (altDeploymentURL == null ) {
63
+ altDeploymentURL = startMockServer()
27
64
}
28
- var tmpdir = Path . of(System . getProperty(" java.io.tmpdir" )). resolve(" coder-gateway-test" )
29
- return new CoderCLIManager (new URL (url), tmpdir)
65
+ }
66
+
67
+ void cleanupSpec () {
68
+ servers. each {
69
+ it. stop(0 )
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Create a CLI manager pointing to the URLs in the environment or to mocked
75
+ * servers.
76
+ */
77
+ CoderCLIManager createCLIManager (Boolean alternate = false ) {
78
+ return new CoderCLIManager (new URL (alternate ? altDeploymentURL : deploymentURL), tmpdir)
30
79
}
31
80
32
81
def " defaults to a sub-directory in the data directory" () {
@@ -55,7 +104,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
55
104
56
105
then :
57
106
downloaded
58
- ccm. version (). contains( " Coder " )
107
+ ccm. localBinaryPath . toFile (). canExecute( )
59
108
}
60
109
61
110
def " overwrites cli if incorrect version" () {
@@ -69,7 +118,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
69
118
70
119
then :
71
120
downloaded
72
- ccm. version (). contains( " Coder " )
121
+ ccm. localBinaryPath . toFile (). canExecute( )
73
122
}
74
123
75
124
def " skips cli download if it already exists" () {
@@ -82,7 +131,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
82
131
83
132
then :
84
133
! downloaded
85
- ccm. version (). contains( " Coder " )
134
+ ccm. localBinaryPath . toFile (). canExecute( )
86
135
}
87
136
88
137
def " does not clobber other deployments" () {
@@ -98,7 +147,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
98
147
ccm1. localBinaryPath != ccm2. localBinaryPath
99
148
}
100
149
101
- def testEnv = [
150
+ Map< String , String > testEnv = [
102
151
" APPDATA" : " /tmp/coder-gateway-test/appdata" ,
103
152
" LOCALAPPDATA" : " /tmp/coder-gateway-test/localappdata" ,
104
153
" HOME" : " /tmp/coder-gateway-test/home" ,
@@ -110,7 +159,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
110
159
/**
111
160
* Get a config dir using default environment variable values.
112
161
*/
113
- def configDir (Map<String , String > env = [:]) {
162
+ Path configDir (Map<String , String > env = [:]) {
114
163
return CoderCLIManager . getConfigDir(new Environment (testEnv + env))
115
164
}
116
165
@@ -159,7 +208,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
159
208
/**
160
209
* Get a data dir using default environment variable values.
161
210
*/
162
- def dataDir (Map<String , String > env = [:]) {
211
+ Path dataDir (Map<String , String > env = [:]) {
163
212
return CoderCLIManager . getDataDir(new Environment (testEnv + env))
164
213
}
165
214
// Mostly just a sanity check to make sure the default System.getenv runs
0 commit comments