-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from w-h-a/e2e-state
tests: e2e state
- Loading branch information
Showing
12 changed files
with
523 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang | ||
WORKDIR /service | ||
COPY service.go . | ||
RUN go mod init service | ||
RUN go mod tidy | ||
RUN CGO_ENABLED=0 go build -o /service ./ | ||
CMD ["./service"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
services: | ||
roach: | ||
container_name: roach | ||
command: start-single-node --advertise-addr 'localhost' --insecure | ||
image: cockroachdb/cockroach:latest | ||
ports: | ||
- '26257:26257' | ||
- '9000:8080' | ||
networks: | ||
- state-net | ||
networks: | ||
state-net: | ||
name: state-net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
services: | ||
############################ | ||
# state service + sidecar | ||
############################ | ||
state: | ||
container_name: state | ||
build: | ||
dockerfile: Dockerfile | ||
restart: on-failure:10 | ||
ports: | ||
- '3002:3000' | ||
networks: | ||
- state-net | ||
state-sidecar: | ||
container_name: state-sidecar | ||
command: sidecar | ||
build: | ||
context: ../../../.. | ||
dockerfile: Dockerfile | ||
restart: on-failure:10 | ||
environment: | ||
NAMESPACE: 'state' | ||
NAME: 'state-sidecar' | ||
VERSION: '0.1.0-alpha.0' | ||
HTTP_ADDRESS: ':3501' | ||
RPC_ADDRESS: ':50001' | ||
SERVICE_NAME: 'localhost' | ||
SERVICE_PORT: '3002' | ||
SERVICE_PROTOCOL: 'http' | ||
STORE: 'cockroach' | ||
STORE_ADDRESS: 'postgresql://root@roach:26257?sslmode=disable' | ||
DB: 'test' | ||
STORES: 'test' | ||
BROKER: 'memory' | ||
network_mode: 'service:state' | ||
networks: | ||
state-net: | ||
external: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
const servicePort = 3000 | ||
|
||
const stateURL = "http://localhost:3501/state" | ||
|
||
type RequestResponse struct { | ||
StartTime int `json:"start_time,omitempty"` | ||
EndTime int `json:"end_time,omitempty"` | ||
States []SidecarState `json:"states,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
} | ||
|
||
type SidecarState struct { | ||
Key string `json:"key,omitempty"` | ||
Value *ServiceState `json:"value,omitempty"` | ||
} | ||
|
||
type ServiceState struct { | ||
Data string `json:"data,omitempty"` | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
log.Println("indexHandler has been called") | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func testHandler(w http.ResponseWriter, r *http.Request) { | ||
log.Println("testHander has been called") | ||
|
||
var req RequestResponse | ||
|
||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
json.NewEncoder(w).Encode(RequestResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
statusCode := http.StatusOK | ||
|
||
uri := r.URL.RequestURI() | ||
|
||
cmd := mux.Vars(r)["command"] | ||
|
||
rsp := RequestResponse{} | ||
|
||
var err error | ||
|
||
rsp.StartTime = int(time.Now().UnixMilli()) | ||
|
||
switch cmd { | ||
case "create": | ||
err = create(req.States) | ||
case "list": | ||
rsp.States, err = list() | ||
case "delete": | ||
err = delete(req.States) | ||
default: | ||
err = fmt.Errorf("invalid URI: %s", uri) | ||
statusCode = http.StatusBadRequest | ||
rsp.Message = err.Error() | ||
} | ||
|
||
if err != nil && statusCode == http.StatusOK { | ||
statusCode = http.StatusInternalServerError | ||
rsp.Message = err.Error() | ||
} | ||
|
||
rsp.EndTime = int(time.Now().UnixMilli()) | ||
|
||
w.Header().Set("content-type", "application/json") | ||
w.WriteHeader(statusCode) | ||
json.NewEncoder(w).Encode(rsp) | ||
} | ||
|
||
func create(states []SidecarState) error { | ||
log.Printf("processing create request for %d entries", len(states)) | ||
|
||
bs, err := json.Marshal(states) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rsp, err := http.Post(fmt.Sprintf("%s/%s", stateURL, "test"), "application/json", bytes.NewBuffer(bs)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer rsp.Body.Close() | ||
|
||
return nil | ||
} | ||
|
||
func list() ([]SidecarState, error) { | ||
log.Println("processing list request") | ||
|
||
rsp, err := http.Get(fmt.Sprintf("%s/%s", stateURL, "test")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer rsp.Body.Close() | ||
|
||
body, err := io.ReadAll(rsp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var states []SidecarState | ||
|
||
if err := json.Unmarshal(body, &states); err != nil { | ||
return nil, err | ||
} | ||
|
||
return states, nil | ||
} | ||
|
||
func delete(states []SidecarState) error { | ||
log.Printf("processing delete request for %d entries", len(states)) | ||
|
||
for _, state := range states { | ||
url := fmt.Sprintf("%s/%s/%s", stateURL, "test", state.Key) | ||
|
||
req, err := http.NewRequest("DELETE", url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := &http.Client{} | ||
|
||
rsp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rsp.Body.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func appRouter() *mux.Router { | ||
router := mux.NewRouter() | ||
|
||
router.HandleFunc("/", indexHandler).Methods("GET") | ||
router.HandleFunc("/test/{command}", testHandler).Methods("POST") | ||
|
||
return router | ||
} | ||
|
||
func main() { | ||
log.Printf("state test service is listening on port %d", servicePort) | ||
|
||
if err := http.ListenAndServe(fmt.Sprintf(":%d", servicePort), appRouter()); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.