forked from virtual-kubelet/azure-aci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aciMock.go
142 lines (115 loc) · 3.73 KB
/
aciMock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package azure
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/gorilla/mux"
"github.com/virtual-kubelet/azure-aci/client/aci"
)
// ACIMock implements a Azure Container Instance mock server.
type ACIMock struct {
server *httptest.Server
OnCreate func(string, string, string, *aci.ContainerGroup) (int, interface{})
OnGetContainerGroups func(string, string) (int, interface{})
OnGetContainerGroup func(string, string, string) (int, interface{})
OnGetRPManifest func() (int, interface{})
}
const (
containerGroupsRoute = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.ContainerInstance/containerGroups"
containerGroupRoute = containerGroupsRoute + "/{containerGroup}"
containerGroupLogRoute = containerGroupRoute + "/containers/{containerName}/logs"
resourceProviderRoute = "/providers/Microsoft.ContainerInstance"
)
// NewACIMock creates a new Azure Container Instance mock server.
func NewACIMock() *ACIMock {
mock := new(ACIMock)
mock.start()
return mock
}
// Start the Azure Container Instance mock service.
func (mock *ACIMock) start() {
if mock.server != nil {
return
}
router := mux.NewRouter()
router.HandleFunc(
containerGroupRoute,
func(w http.ResponseWriter, r *http.Request) {
subscription, _ := mux.Vars(r)["subscriptionId"]
resourceGroup, _ := mux.Vars(r)["resourceGroup"]
containerGroup, _ := mux.Vars(r)["containerGroup"]
var cg aci.ContainerGroup
if err := json.NewDecoder(r.Body).Decode(&cg); err != nil {
panic(err)
}
if mock.OnCreate != nil {
statusCode, response := mock.OnCreate(subscription, resourceGroup, containerGroup, &cg)
w.WriteHeader(statusCode)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(response)
w.Write(b.Bytes())
return
}
w.WriteHeader(http.StatusNotImplemented)
}).Methods("PUT")
router.HandleFunc(
containerGroupRoute,
func(w http.ResponseWriter, r *http.Request) {
subscription, _ := mux.Vars(r)["subscriptionId"]
resourceGroup, _ := mux.Vars(r)["resourceGroup"]
containerGroup, _ := mux.Vars(r)["containerGroup"]
if mock.OnGetContainerGroup != nil {
statusCode, response := mock.OnGetContainerGroup(subscription, resourceGroup, containerGroup)
w.WriteHeader(statusCode)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(response)
w.Write(b.Bytes())
return
}
w.WriteHeader(http.StatusNotImplemented)
}).Methods("GET")
router.HandleFunc(
containerGroupsRoute,
func(w http.ResponseWriter, r *http.Request) {
subscription, _ := mux.Vars(r)["subscriptionId"]
resourceGroup, _ := mux.Vars(r)["resourceGroup"]
if mock.OnGetContainerGroups != nil {
statusCode, response := mock.OnGetContainerGroups(subscription, resourceGroup)
w.WriteHeader(statusCode)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(response)
w.Write(b.Bytes())
return
}
w.WriteHeader(http.StatusNotImplemented)
}).Methods("GET")
router.HandleFunc(
resourceProviderRoute,
func(w http.ResponseWriter, r *http.Request) {
if mock.OnGetRPManifest != nil {
statusCode, response := mock.OnGetRPManifest()
w.WriteHeader(statusCode)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(response)
w.Write(b.Bytes())
return
}
w.WriteHeader(http.StatusNotImplemented)
}).Methods("GET")
mock.server = httptest.NewServer(router)
}
// GetServerURL returns the mock server URL.
func (mock *ACIMock) GetServerURL() string {
if mock.server != nil {
return mock.server.URL
}
panic("Mock server is not initialized.")
}
// Close terminates the Azure Container Instance mock server.
func (mock *ACIMock) Close() {
if mock.server != nil {
mock.server.Close()
mock.server = nil
}
}