From ad1b7fb92908a57f6104f6266df30e8e9ac9050f Mon Sep 17 00:00:00 2001 From: Ayush Sharma <89914602+ayush3160@users.noreply.github.com> Date: Sat, 14 Sep 2024 10:42:41 +0530 Subject: [PATCH] Added mux-elasticsearch sample (#114) Signed-off-by: ayush3160 --- mux-elasticsearch/.gitignore | 1 + mux-elasticsearch/Dockerfile | 23 ++ mux-elasticsearch/README.md | 86 +++++ mux-elasticsearch/app.go | 243 +++++++++++++ mux-elasticsearch/go.mod | 14 + mux-elasticsearch/go.sum | 17 + mux-elasticsearch/keploy.yml | 43 +++ .../reports/test-run-0/test-set-0-report.yaml | 325 ++++++++++++++++++ .../keploy/test-set-0/mocks.yaml | 112 ++++++ .../keploy/test-set-0/tests/test-1.yaml | 55 +++ .../keploy/test-set-0/tests/test-2.yaml | 56 +++ .../keploy/test-set-0/tests/test-3.yaml | 56 +++ .../keploy/test-set-0/tests/test-4.yaml | 54 +++ mux-elasticsearch/main.go | 15 + 14 files changed, 1100 insertions(+) create mode 100644 mux-elasticsearch/.gitignore create mode 100644 mux-elasticsearch/Dockerfile create mode 100644 mux-elasticsearch/README.md create mode 100644 mux-elasticsearch/app.go create mode 100644 mux-elasticsearch/go.mod create mode 100644 mux-elasticsearch/go.sum create mode 100755 mux-elasticsearch/keploy.yml create mode 100755 mux-elasticsearch/keploy/reports/test-run-0/test-set-0-report.yaml create mode 100755 mux-elasticsearch/keploy/test-set-0/mocks.yaml create mode 100755 mux-elasticsearch/keploy/test-set-0/tests/test-1.yaml create mode 100755 mux-elasticsearch/keploy/test-set-0/tests/test-2.yaml create mode 100755 mux-elasticsearch/keploy/test-set-0/tests/test-3.yaml create mode 100755 mux-elasticsearch/keploy/test-set-0/tests/test-4.yaml create mode 100644 mux-elasticsearch/main.go diff --git a/mux-elasticsearch/.gitignore b/mux-elasticsearch/.gitignore new file mode 100644 index 00000000..9bda2e29 --- /dev/null +++ b/mux-elasticsearch/.gitignore @@ -0,0 +1 @@ +mux-elasticsearch \ No newline at end of file diff --git a/mux-elasticsearch/Dockerfile b/mux-elasticsearch/Dockerfile new file mode 100644 index 00000000..1b7163cb --- /dev/null +++ b/mux-elasticsearch/Dockerfile @@ -0,0 +1,23 @@ +# Use the Go 1.19 base image with the bullseye tag +FROM golang:1.22-bookworm + + +RUN apt-get update && \ + apt-get install -y --no-install-recommends ca-certificates && \ + rm -rf /var/lib/apt/lists/* + +# Set the working directory inside the container +WORKDIR /app + +COPY go.mod /app/ +COPY go.sum /app/ + +RUN go mod download + +# Copy the contents from the local directory to the working directory in the container +COPY . /app + +RUN go build -o app . + +# Run the application server using "go run handler.go main.go" +CMD ["./app"] \ No newline at end of file diff --git a/mux-elasticsearch/README.md b/mux-elasticsearch/README.md new file mode 100644 index 00000000..8a7b1418 --- /dev/null +++ b/mux-elasticsearch/README.md @@ -0,0 +1,86 @@ +# Introduction +This is a sample go project to show the crud operations of golang with elasticsearch and mux. + +## Installation Setup + +```bash +git clone https://github.com/keploy/samples-go.git && cd samples-go/mux-elasticsearch +go mod download +``` + +## Installation Keploy +Install keploy via one-click:- + +```sh +curl --silent -O -L https://keploy.io/install.sh && source install.sh +``` + +### Install And Run Elastic + +Using the docker we will start our elastic instance:- + +```bash +docker run --name=es01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:8.14.3 +``` + +In the above command we are passing two environment variables to run the elastic in dev mode and disabling the need to pass the password to connect to elastic database. If you want to enable it you can follow steps mentioned on this [page](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html) . Then you have to pass the password and http cert file to connect. + + +### Capture the Testcases + +Now, we will create the binary of our application:- + +```zsh +go build -cover +``` + +Once we have our binary file ready,this command will start the recording of API calls using ebpf:- + +```shell +sudo -E keploy record -c "./mux-elasticsearch" +``` + +Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks. + +#### Generate testcases + +To genereate testcases we just need to make some API calls. You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl` + +1. Post Command To Insert Document + +```bash +curl --request POST \ + --url http://localhost:8000/documents \ + --header 'Content-Type: application/json' \ + --data '{ + "title" : "somethingTitle", + "content" : "something22" + }' +``` +this will return the response which includes the id of the inserted document. +```json +{"id":"1b5wJ5EBIPW7ZBPsse8e"} +``` + +2. Fetch the Products +```bash +curl --request GET \ + --url http://localhost:8000/documents/1b5wJ5EBIPW7ZBPsse8e +``` + +we will get output: + +```json +{"content":"something22","title":"somethingTitle"} +``` + + +Now let's run the test mode (in the mux-elasticsearch directory, not the Keploy directory). + +### Run captured testcases + +```shell +sudo -E keploy test -c "./mux-elasticsearch" --delay 10 --goCoverage +``` + + diff --git a/mux-elasticsearch/app.go b/mux-elasticsearch/app.go new file mode 100644 index 00000000..d3e7853a --- /dev/null +++ b/mux-elasticsearch/app.go @@ -0,0 +1,243 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "strings" + "time" + + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/esapi" + "github.com/gorilla/mux" +) + +type App struct { + Router *mux.Router + DB *elasticsearch.Client + Server *http.Server +} + +const SearchIndex = "documents" + +func (a *App) Initialize() error { + var err error + a.DB, err = elasticsearch.NewDefaultClient() + + if err != nil { + return fmt.Errorf("error : %s", err) + } + + _, err = esapi.IndicesExistsRequest{ + Index: []string{SearchIndex}, + }.Do(context.Background(), a.DB) + + if err != nil { + fmt.Println("Indices is not present") + a.CreateIndices() + } + + a.Router = mux.NewRouter() + a.Server = &http.Server{ + Addr: ":8000", + Handler: a.Router, + } + + a.initializeRoutes() + + return nil +} + +type Document struct { + ID string `json:"id,omitempty"` + Title string `json:"title"` + Content string `json:"content"` +} + +type CreateDocumentResponse struct { + ID string `json:"_id"` +} + +func (a *App) CreateIndices() { + var err error + + _, err = a.DB.Indices.Create(SearchIndex) + + if err != nil { + fmt.Println(err) + } +} + +func (a *App) createDocument(w http.ResponseWriter, r *http.Request) { + var doc Document + err := json.NewDecoder(r.Body).Decode(&doc) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + data, err := json.Marshal(doc) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + req := esapi.IndexRequest{ + Index: "documents", + Body: strings.NewReader(string(data)), + } + res, err := req.Do(context.Background(), a.DB) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer res.Body.Close() + + if res.IsError() { + http.Error(w, res.String(), http.StatusInternalServerError) + return + } + + var createResponse CreateDocumentResponse + if err := json.NewDecoder(res.Body).Decode(&createResponse); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(map[string]string{"id": createResponse.ID}) +} + +func (a *App) getDocument(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + id := params["id"] + + req := esapi.GetRequest{ + Index: "documents", + DocumentID: id, + } + res, err := req.Do(context.Background(), a.DB) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer res.Body.Close() + + if res.IsError() { + http.Error(w, res.String(), http.StatusNotFound) + return + } + + var doc map[string]interface{} + if err := json.NewDecoder(res.Body).Decode(&doc); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + source := doc["_source"].(map[string]interface{}) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(source) +} + +func (a *App) updateDocument(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + id := params["id"] + + var doc Document + err := json.NewDecoder(r.Body).Decode(&doc) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + data, err := json.Marshal(doc) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + req := esapi.UpdateRequest{ + Index: "documents", + DocumentID: id, + Body: strings.NewReader(fmt.Sprintf(`{"doc": %s}`, string(data))), + } + res, err := req.Do(context.Background(), a.DB) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer res.Body.Close() + + if res.IsError() { + http.Error(w, res.String(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +} + +func (a *App) deleteDocument(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + id := params["id"] + + req := esapi.DeleteRequest{ + Index: "documents", + DocumentID: id, + } + res, err := req.Do(context.Background(), a.DB) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer res.Body.Close() + + if res.IsError() { + http.Error(w, res.String(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +} + +func (a *App) Run(port string) { + go func() { + if err := a.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Could not listen on %s: %v\n", port, err) + } + }() + + quit := make(chan os.Signal, 1) + signal.Notify(quit, os.Interrupt) + <-quit + + log.Println("Server is shutting down...") + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := a.Server.Shutdown(ctx); err != nil { + log.Fatalf("Server forced to shutdown: %v", err) + } + + log.Println("Server exiting") +} + +func (a *App) Hello(res http.ResponseWriter, req *http.Request) { + var result = "Hello" + res.Write([]byte(result)) +} + +func (a *App) initializeRoutes() { + a.Router.HandleFunc("/", a.Hello).Methods("GET") + a.Router.HandleFunc("/documents", a.createDocument).Methods("POST") + a.Router.HandleFunc("/documents/{id}", a.getDocument).Methods("GET") + a.Router.HandleFunc("/documents/{id}", a.updateDocument).Methods("PUT") + a.Router.HandleFunc("/documents/{id}", a.deleteDocument).Methods("DELETE") + +} diff --git a/mux-elasticsearch/go.mod b/mux-elasticsearch/go.mod new file mode 100644 index 00000000..f967e611 --- /dev/null +++ b/mux-elasticsearch/go.mod @@ -0,0 +1,14 @@ +module mux-elasticsearch + +go 1.22 + +require ( + github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/go-elasticsearch/v8 v8.14.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/gorilla/mux v1.8.1 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect +) diff --git a/mux-elasticsearch/go.sum b/mux-elasticsearch/go.sum new file mode 100644 index 00000000..60467d21 --- /dev/null +++ b/mux-elasticsearch/go.sum @@ -0,0 +1,17 @@ +github.com/elastic/elastic-transport-go/v8 v8.6.0 h1:Y2S/FBjx1LlCv5m6pWAF2kDJAHoSjSRSJCApolgfthA= +github.com/elastic/elastic-transport-go/v8 v8.6.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk= +github.com/elastic/go-elasticsearch/v8 v8.14.0 h1:1ywU8WFReLLcxE1WJqii3hTtbPUE2hc38ZK/j4mMFow= +github.com/elastic/go-elasticsearch/v8 v8.14.0/go.mod h1:WRvnlGkSuZyp83M2U8El/LGXpCjYLrvlkSgkAH4O5I4= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= diff --git a/mux-elasticsearch/keploy.yml b/mux-elasticsearch/keploy.yml new file mode 100755 index 00000000..369c7e94 --- /dev/null +++ b/mux-elasticsearch/keploy.yml @@ -0,0 +1,43 @@ +path: "" +appId: 0 +appName: "" +command: ./mux-elasticsearch +port: 0 +dnsPort: 26789 +proxyPort: 16789 +debug: false +disableTele: false +disableANSI: false +containerName: "" +networkName: "" +buildDelay: 30 +test: + selectedTests: {} + globalNoise: + global: {} + test-sets: {} + delay: 5 + apiTimeout: 5 + skipCoverage: false + coverageReportPath: "" + ignoreOrdering: true + mongoPassword: default@123 + language: "" + removeUnusedMocks: false + fallBackOnMiss: false + jacocoAgentPath: "" + basePath: "" + mocking: true + ignoredTests: {} +record: + filters: [] + recordTimer: 0s +configPath: "" +bypassRules: [] +generateGithubActions: false +keployContainer: keploy-v2 +keployNetwork: keploy-network +cmdType: native +inCi: false + +# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file. diff --git a/mux-elasticsearch/keploy/reports/test-run-0/test-set-0-report.yaml b/mux-elasticsearch/keploy/reports/test-run-0/test-set-0-report.yaml new file mode 100755 index 00000000..49a1f215 --- /dev/null +++ b/mux-elasticsearch/keploy/reports/test-run-0/test-set-0-report.yaml @@ -0,0 +1,325 @@ +version: api.keploy.io/v1beta1 +name: test-set-0-report +status: PASSED +success: 4 +failure: 0 +ignored: 0 +total: 4 +tests: + - kind: Http + name: test-set-0 + status: PASSED + started: 1722941940 + completed: 1722941940 + test_case_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0 + mock_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0/mocks + test_case_id: test-1 + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/ + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "61" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: df09f4f4-ccae-47c4-820c-794dd0c81985 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "something123", + "content" : "something" + } + timestamp: 2024-08-06T16:26:37.822148815+05:30 + resp: + status_code: 200 + header: + Content-Length: "5" + Content-Type: text/plain; charset=utf-8 + Date: Tue, 06 Aug 2024 10:59:00 GMT + body: Hello + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + noise: + header.Date: [] + result: + status_code: + normal: true + expected: 200 + actual: 200 + headers_result: + - normal: true + expected: + key: Content-Type + value: + - text/plain; charset=utf-8 + actual: + key: Content-Type + value: + - text/plain; charset=utf-8 + - normal: true + expected: + key: Date + value: + - Tue, 06 Aug 2024 10:56:37 GMT + actual: + key: Date + value: + - Tue, 06 Aug 2024 10:59:00 GMT + - normal: true + expected: + key: Content-Length + value: + - "5" + actual: + key: Content-Length + value: + - "5" + body_result: + - normal: true + type: PLAIN + expected: Hello + actual: Hello + dep_result: [] + - kind: Http + name: test-set-0 + status: PASSED + started: 1722941940 + completed: 1722941940 + test_case_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0 + mock_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0/mocks + test_case_id: test-2 + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "61" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 63a32b9b-133b-4f69-9656-476b54a65040 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "something123", + "content" : "something" + } + timestamp: 2024-08-06T16:26:50.734291385+05:30 + resp: + status_code: 200 + header: + Content-Length: "47" + Content-Type: application/json + Date: Tue, 06 Aug 2024 10:59:00 GMT + body: | + {"content":"something","title":"something123"} + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + noise: + header.Date: [] + result: + status_code: + normal: true + expected: 200 + actual: 200 + headers_result: + - normal: true + expected: + key: Content-Type + value: + - application/json + actual: + key: Content-Type + value: + - application/json + - normal: true + expected: + key: Date + value: + - Tue, 06 Aug 2024 10:56:50 GMT + actual: + key: Date + value: + - Tue, 06 Aug 2024 10:59:00 GMT + - normal: true + expected: + key: Content-Length + value: + - "47" + actual: + key: Content-Length + value: + - "47" + body_result: + - normal: true + type: JSON + expected: | + {"content":"something","title":"something123"} + actual: | + {"content":"something","title":"something123"} + dep_result: [] + - kind: Http + name: test-set-0 + status: PASSED + started: 1722941940 + completed: 1722941940 + test_case_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0 + mock_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0/mocks + test_case_id: test-3 + req: + method: POST + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "65" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 77746e28-0c1e-44c1-b665-486651d226d9 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "somethingTitle", + "content" : "something22" + } + timestamp: 2024-08-06T16:27:28.593250857+05:30 + resp: + status_code: 201 + header: + Content-Length: "30" + Content-Type: application/json + Date: Tue, 06 Aug 2024 10:59:00 GMT + body: | + {"id":"1L5WJ5EBIPW7ZBPsPO8T"} + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + noise: + header.Date: [] + result: + status_code: + normal: true + expected: 201 + actual: 201 + headers_result: + - normal: true + expected: + key: Content-Length + value: + - "30" + actual: + key: Content-Length + value: + - "30" + - normal: true + expected: + key: Content-Type + value: + - application/json + actual: + key: Content-Type + value: + - application/json + - normal: true + expected: + key: Date + value: + - Tue, 06 Aug 2024 10:57:28 GMT + actual: + key: Date + value: + - Tue, 06 Aug 2024 10:59:00 GMT + body_result: + - normal: true + type: JSON + expected: | + {"id":"1L5WJ5EBIPW7ZBPsPO8T"} + actual: | + {"id":"1L5WJ5EBIPW7ZBPsPO8T"} + dep_result: [] + - kind: Http + name: test-set-0 + status: PASSED + started: 1722941940 + completed: 1722941940 + test_case_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0 + mock_path: /home/ayush/Desktop/Open-Source/samples-go/mux-elasticsearch/keploy/test-set-0/mocks + test_case_id: test-4 + req: + method: PUT + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "65" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 207b9990-8257-4576-8202-39896f1fc02b + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "somethingTitle", + "content" : "something22" + } + timestamp: 2024-08-06T16:27:52.682428851+05:30 + resp: + status_code: 200 + header: + Content-Length: "0" + Date: Tue, 06 Aug 2024 10:59:00 GMT + body: "" + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + noise: + header.Date: [] + result: + status_code: + normal: true + expected: 200 + actual: 200 + headers_result: + - normal: true + expected: + key: Content-Length + value: + - "0" + actual: + key: Content-Length + value: + - "0" + - normal: true + expected: + key: Date + value: + - Tue, 06 Aug 2024 10:57:52 GMT + actual: + key: Date + value: + - Tue, 06 Aug 2024 10:59:00 GMT + body_result: + - normal: true + type: PLAIN + expected: "" + actual: "" + dep_result: [] +test_set: test-set-0 diff --git a/mux-elasticsearch/keploy/test-set-0/mocks.yaml b/mux-elasticsearch/keploy/test-set-0/mocks.yaml new file mode 100755 index 00000000..24f06c2f --- /dev/null +++ b/mux-elasticsearch/keploy/test-set-0/mocks.yaml @@ -0,0 +1,112 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: mock-0 +spec: + metadata: + name: Http + operation: GET + type: HTTP_CLIENT + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: /documents/_doc/075IJ5EBIPW7ZBPs5-8y + header: + Accept-Encoding: gzip + User-Agent: go-elasticsearch/8.14.0 (linux amd64; Go 1.22.5) + X-Elastic-Client-Meta: es=8.14.0,go=1.22.5,t=8.6.0,hc=1.22.5 + body: "" + timestamp: 0001-01-01T00:00:00Z + resp: + status_code: 200 + header: + Content-Encoding: gzip + Content-Length: "164" + Content-Type: application/json + X-Elastic-Product: Elasticsearch + body: '{"_index":"documents","_id":"075IJ5EBIPW7ZBPs5-8y","_version":2,"_seq_no":5,"_primary_term":1,"found":true,"_source":{"title":"something123","content":"something"}}' + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + objects: [] + created: 1722941810 + reqTimestampMock: 2024-08-06T16:26:50.741516528+05:30 + resTimestampMock: 2024-08-06T16:26:50.741516528+05:30 +--- +version: api.keploy.io/v1beta1 +kind: Http +name: mock-1 +spec: + metadata: + name: Http + operation: POST + type: HTTP_CLIENT + req: + method: POST + proto_major: 1 + proto_minor: 1 + url: /documents/_doc + header: + Accept-Encoding: gzip + Content-Length: "50" + Content-Type: application/json + User-Agent: go-elasticsearch/8.14.0 (linux amd64; Go 1.22.5) + X-Elastic-Client-Meta: es=8.14.0,go=1.22.5,t=8.6.0,hc=1.22.5 + body: '{"title":"somethingTitle","content":"something22"}' + timestamp: 0001-01-01T00:00:00Z + resp: + status_code: 201 + header: + Content-Encoding: gzip + Content-Length: "161" + Content-Type: application/json + Location: /documents/_doc/1L5WJ5EBIPW7ZBPsPO8T + X-Elastic-Product: Elasticsearch + body: '{"_index":"documents","_id":"1L5WJ5EBIPW7ZBPsPO8T","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":6,"_primary_term":1}' + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + objects: [] + created: 1722941848 + reqTimestampMock: 2024-08-06T16:27:28.815334036+05:30 + resTimestampMock: 2024-08-06T16:27:28.815334036+05:30 +--- +version: api.keploy.io/v1beta1 +kind: Http +name: mock-2 +spec: + metadata: + name: Http + operation: POST + type: HTTP_CLIENT + req: + method: POST + proto_major: 1 + proto_minor: 1 + url: /documents/_update/075IJ5EBIPW7ZBPs5-8y + header: + Accept-Encoding: gzip + Content-Length: "59" + Content-Type: application/json + User-Agent: go-elasticsearch/8.14.0 (linux amd64; Go 1.22.5) + X-Elastic-Client-Meta: es=8.14.0,go=1.22.5,t=8.6.0,hc=1.22.5 + body: '{"doc": {"title":"somethingTitle","content":"something22"}}' + timestamp: 0001-01-01T00:00:00Z + resp: + status_code: 200 + header: + Content-Encoding: gzip + Content-Length: "161" + Content-Type: application/json + X-Elastic-Product: Elasticsearch + body: '{"_index":"documents","_id":"075IJ5EBIPW7ZBPs5-8y","_version":3,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":7,"_primary_term":1}' + status_message: "" + proto_major: 0 + proto_minor: 0 + timestamp: 0001-01-01T00:00:00Z + objects: [] + created: 1722941872 + reqTimestampMock: 2024-08-06T16:27:52.778818516+05:30 + resTimestampMock: 2024-08-06T16:27:52.778818516+05:30 diff --git a/mux-elasticsearch/keploy/test-set-0/tests/test-1.yaml b/mux-elasticsearch/keploy/test-set-0/tests/test-1.yaml new file mode 100755 index 00000000..3c39e414 --- /dev/null +++ b/mux-elasticsearch/keploy/test-set-0/tests/test-1.yaml @@ -0,0 +1,55 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-1 +spec: + metadata: {} + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/ + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "61" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: df09f4f4-ccae-47c4-820c-794dd0c81985 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "something123", + "content" : "something" + } + timestamp: 2024-08-06T16:26:37.822148815+05:30 + resp: + status_code: 200 + header: + Content-Length: "5" + Content-Type: text/plain; charset=utf-8 + Date: Tue, 06 Aug 2024 10:56:37 GMT + body: Hello + status_message: OK + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-06T16:26:39.849576194+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1722941799 +curl: |- + curl --request GET \ + --url http://localhost:8000/ \ + --header 'Host: localhost:8000' \ + --header 'Accept-Encoding: gzip, deflate, br' \ + --header 'Connection: keep-alive' \ + --header 'Content-Type: application/json' \ + --header 'User-Agent: PostmanRuntime/7.40.0' \ + --header 'Accept: */*' \ + --header 'Postman-Token: df09f4f4-ccae-47c4-820c-794dd0c81985' \ + --data '{ + "title" : "something123", + "content" : "something" + }' diff --git a/mux-elasticsearch/keploy/test-set-0/tests/test-2.yaml b/mux-elasticsearch/keploy/test-set-0/tests/test-2.yaml new file mode 100755 index 00000000..1758c334 --- /dev/null +++ b/mux-elasticsearch/keploy/test-set-0/tests/test-2.yaml @@ -0,0 +1,56 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-2 +spec: + metadata: {} + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "61" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 63a32b9b-133b-4f69-9656-476b54a65040 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "something123", + "content" : "something" + } + timestamp: 2024-08-06T16:26:50.734291385+05:30 + resp: + status_code: 200 + header: + Content-Length: "47" + Content-Type: application/json + Date: Tue, 06 Aug 2024 10:56:50 GMT + body: | + {"content":"something","title":"something123"} + status_message: OK + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-06T16:26:52.807867702+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1722941812 +curl: |- + curl --request GET \ + --url http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y \ + --header 'Content-Type: application/json' \ + --header 'User-Agent: PostmanRuntime/7.40.0' \ + --header 'Accept: */*' \ + --header 'Postman-Token: 63a32b9b-133b-4f69-9656-476b54a65040' \ + --header 'Host: localhost:8000' \ + --header 'Accept-Encoding: gzip, deflate, br' \ + --header 'Connection: keep-alive' \ + --data '{ + "title" : "something123", + "content" : "something" + }' diff --git a/mux-elasticsearch/keploy/test-set-0/tests/test-3.yaml b/mux-elasticsearch/keploy/test-set-0/tests/test-3.yaml new file mode 100755 index 00000000..31423c84 --- /dev/null +++ b/mux-elasticsearch/keploy/test-set-0/tests/test-3.yaml @@ -0,0 +1,56 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-3 +spec: + metadata: {} + req: + method: POST + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "65" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 77746e28-0c1e-44c1-b665-486651d226d9 + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "somethingTitle", + "content" : "something22" + } + timestamp: 2024-08-06T16:27:28.593250857+05:30 + resp: + status_code: 201 + header: + Content-Length: "30" + Content-Type: application/json + Date: Tue, 06 Aug 2024 10:57:28 GMT + body: | + {"id":"1L5WJ5EBIPW7ZBPsPO8T"} + status_message: Created + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-06T16:27:30.871417146+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1722941850 +curl: |- + curl --request POST \ + --url http://localhost:8000/documents \ + --header 'Content-Type: application/json' \ + --header 'User-Agent: PostmanRuntime/7.40.0' \ + --header 'Accept: */*' \ + --header 'Postman-Token: 77746e28-0c1e-44c1-b665-486651d226d9' \ + --header 'Host: localhost:8000' \ + --header 'Accept-Encoding: gzip, deflate, br' \ + --header 'Connection: keep-alive' \ + --data '{ + "title" : "somethingTitle", + "content" : "something22" + }' diff --git a/mux-elasticsearch/keploy/test-set-0/tests/test-4.yaml b/mux-elasticsearch/keploy/test-set-0/tests/test-4.yaml new file mode 100755 index 00000000..54937f24 --- /dev/null +++ b/mux-elasticsearch/keploy/test-set-0/tests/test-4.yaml @@ -0,0 +1,54 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-4 +spec: + metadata: {} + req: + method: PUT + proto_major: 1 + proto_minor: 1 + url: http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y + header: + Accept: '*/*' + Accept-Encoding: gzip, deflate, br + Connection: keep-alive + Content-Length: "65" + Content-Type: application/json + Host: localhost:8000 + Postman-Token: 207b9990-8257-4576-8202-39896f1fc02b + User-Agent: PostmanRuntime/7.40.0 + body: |- + { + "title" : "somethingTitle", + "content" : "something22" + } + timestamp: 2024-08-06T16:27:52.682428851+05:30 + resp: + status_code: 200 + header: + Content-Length: "0" + Date: Tue, 06 Aug 2024 10:57:52 GMT + body: "" + status_message: OK + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-06T16:27:54.785227838+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1722941874 +curl: |- + curl --request PUT \ + --url http://localhost:8000/documents/075IJ5EBIPW7ZBPs5-8y \ + --header 'Host: localhost:8000' \ + --header 'Accept-Encoding: gzip, deflate, br' \ + --header 'Connection: keep-alive' \ + --header 'Content-Type: application/json' \ + --header 'User-Agent: PostmanRuntime/7.40.0' \ + --header 'Accept: */*' \ + --header 'Postman-Token: 207b9990-8257-4576-8202-39896f1fc02b' \ + --data '{ + "title" : "somethingTitle", + "content" : "something22" + }' diff --git a/mux-elasticsearch/main.go b/mux-elasticsearch/main.go new file mode 100644 index 00000000..060613c6 --- /dev/null +++ b/mux-elasticsearch/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" +) + +func main() { + a := &App{} + err := a.Initialize() + if err != nil { + log.Fatal("Failed to initialize app", err) + } + log.Printf("😃 Connected to 8000 port !!") + a.Run(":8000") +}