Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline #539

Merged
merged 100 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
854072c
rename filter to filters
localvar Mar 4, 2022
6993fcb
remove useless field 'rawSpec'
localvar Mar 4, 2022
fc4f3b4
fix test case compile error
localvar Mar 4, 2022
fe00b7c
rename protocol to protocols
localvar Mar 4, 2022
bebec93
rename HTTPPipeline to Pipeline
localvar Mar 7, 2022
157ecb2
refactor pipeline: remove useless code
localvar Mar 7, 2022
8450314
refactor pipeline
localvar Mar 7, 2022
d2be7b6
fix some compile errors
localvar Mar 9, 2022
f4072d7
add 'Name' to filter interface
localvar Mar 9, 2022
2bf713b
refactor pipeline: simplify reload
localvar Mar 9, 2022
f568b0e
move filter interface/struct to filters package
localvar Mar 10, 2022
3e12d31
refactor filter interface: add filters.Kind
localvar Mar 10, 2022
3b48226
refactor filter interface: add Spec, update Kind
localvar Mar 10, 2022
7a67845
sort filter results
localvar Mar 11, 2022
017dd91
refactor validator for pipeline
localvar Mar 14, 2022
e6c0b2a
rename HTTPPipeline to pipeline (WIP)
localvar Mar 17, 2022
ecdb5df
add new package util/readers/ReaderAt
localvar Mar 21, 2022
ec699d3
update filter and util
suchen-sci Mar 22, 2022
7f6958a
change httpprot Request Response from interface to struct
suchen-sci Mar 22, 2022
e8bab2d
fix some compile errors
localvar Mar 22, 2022
10361dd
put some http util into protocols/httpprot and update httpprot reques…
suchen-sci Mar 24, 2022
c823a66
add test
suchen-sci Mar 25, 2022
4718258
update filter
suchen-sci Mar 25, 2022
23ed969
fix some compile error
localvar Mar 26, 2022
dfc44c8
update payload implementation for http request.
localvar Mar 27, 2022
4bc240e
update payload implementation for http response.
localvar Mar 29, 2022
dfe4f18
add traffic matcher for http
suchen-sci Mar 29, 2022
0092613
Merge remote-tracking branch 'origin/main' into pipeline
localvar Mar 29, 2022
64b92a2
update httpserver to use new context.
localvar Mar 30, 2022
3b914e6
update context: rename default IDs to initial
localvar Mar 31, 2022
36eceb5
update server and loadbalancer
suchen-sci Apr 1, 2022
3a6f362
refactor filter interface
localvar Apr 3, 2022
40333dc
refactor matcher for HTTP
localvar Apr 4, 2022
eec0116
refactor proxy (WIP)
localvar Apr 4, 2022
f11532a
update loadbalancer
suchen-sci Apr 6, 2022
e1f129c
fix several issues in load balancer
localvar Apr 6, 2022
35b07a5
refactor proxy (WIP)
localvar Apr 6, 2022
ecca79e
add Close to load balancer
localvar Apr 7, 2022
6930252
move loadbalancer from httpprot to proxy
localvar Apr 7, 2022
a6bec09
move traffic matcher from protocols to proxy
localvar Apr 7, 2022
57c07f1
refactor pool in proxy (WIP)
localvar Apr 8, 2022
da7b154
refactor pool of proxy for compression
localvar Apr 10, 2022
b3ab105
convert Context from interface to struct
localvar Apr 11, 2022
878a49f
add resilience for backend proxy
suchen-sci Apr 13, 2022
8a840e7
fix many compile errors
localvar Apr 13, 2022
16e9d8b
refactor mux
localvar Apr 14, 2022
08c7345
add memorycache back
localvar Apr 15, 2022
8c50b9e
refactor resilience
localvar Apr 16, 2022
429ab44
fix several bugs
localvar Apr 17, 2022
0e791f0
refactor server pool to simplify the code (#585)
localvar Apr 18, 2022
d88df64
add metrics (#588)
localvar Apr 19, 2022
02972f1
Merge remote-tracking branch 'origin/main' into pipeline
localvar Apr 20, 2022
97efc21
support zipkin b3 format
localvar Apr 22, 2022
658c1a5
Merge remote-tracking branch 'origin/main' into pipeline
localvar Apr 22, 2022
78a175b
add HTTPRequestBuilder and HTTPResponseBuilder to pipeline (#591)
suchen-sci Apr 24, 2022
aecbbf9
remove useless code (#594)
localvar Apr 24, 2022
86eb98a
refactor request/response builder for flexibility (#601)
localvar Apr 29, 2022
a94da75
update filters tests and fix behaviours (#602)
suchen-sci May 2, 2022
04601db
enhance req/resp builder and fix bugs (#612)
localvar May 5, 2022
df557d2
improve proxy test coverage (#615)
localvar May 6, 2022
0f802e6
improve test coverage of http server (#616)
localvar May 7, 2022
9cb5c68
update mqtt code for new pipeline (#620)
suchen-sci May 12, 2022
80fbfd1
make traffic controller protocol independent (#621)
localvar May 13, 2022
6068e0f
rename flow node fields (#623)
localvar May 13, 2022
a609c2c
add GZipCompressReader & ByteCountReader (#624)
localvar May 16, 2022
f9da7a3
Update pipeline test (#627)
suchen-sci May 19, 2022
1c329e2
Limit req/resp size and support stream bodies (#628)
localvar May 19, 2022
5596d7c
fix proxy compress panic bug (#629)
suchen-sci May 23, 2022
96e546c
update integration test (#630)
suchen-sci May 25, 2022
21f959c
Refactor flow (#631)
localvar May 26, 2022
6cbfa18
add more test (#632)
suchen-sci May 30, 2022
e409b6e
Refactor flow (#635)
localvar Jun 1, 2022
b503f42
Cluster etcd config (#647)
suchen-sci Jun 7, 2022
7058f1b
fix bug in CopyRequest & CopyResponse (#648)
localvar Jun 9, 2022
029eae1
update dependencies (#652)
localvar Jun 9, 2022
80aed99
req/resp builder to support any protocol (#653)
localvar Jun 13, 2022
e1cb633
Mock cluster in Headerlookup and Validator unittest (#590)
Apr 24, 2022
0ad16f5
update mqtt-proxy doc (#593)
suchen-sci Apr 25, 2022
de7ebb6
[typo] fix kernel-tuning document format (#596)
jxd134 Apr 27, 2022
fc4762e
feat(etcd): support external standalone etcd (#595)
aniaan May 5, 2022
da4ba22
Support Easegress ingress rewrite target (#617)
xxx7xxxx May 10, 2022
d882747
udpate topicmapper policy route from map to array to eliminate random…
suchen-sci May 17, 2022
146490d
sidecar httpserver keepalive config by spec (#626)
sodaRyCN Jun 6, 2022
3376c03
The spec of the httpserver supports the AND-OR strategy of the header…
sodaRyCN May 10, 2022
786c7c9
remove hop-by-hop headers
localvar Jun 13, 2022
ba8ffec
fix issue #633
localvar Jun 13, 2022
84fc70a
fix issues reported by github action
localvar Jun 13, 2022
7126f58
fix: cannot start without specify initial cluster (#656)
localvar Jun 15, 2022
e7c2694
optim code (#659)
suchen-sci Jun 17, 2022
0d7a961
update doc filters.md (#657)
suchen-sci Jun 22, 2022
d3d3783
add function mergeObject for API aggregation (#664)
localvar Jun 22, 2022
2c5f8c7
update controller.md (#665)
suchen-sci Jun 24, 2022
e4a285c
Update doc (#660)
localvar Jun 24, 2022
760083d
update according to review comments (#667)
localvar Jun 24, 2022
fdce281
add more test (#666)
suchen-sci Jun 24, 2022
4661301
update according to review comments (#670)
localvar Jun 28, 2022
fa583c4
add filter migration guide (#669)
localvar Jun 28, 2022
d007fac
document limitation of stream req/resp (#671)
localvar Jun 28, 2022
f673d30
Merge remote-tracking branch 'origin/main' into pipeline
localvar Jun 28, 2022
cb2674b
update github action test yaml (#673)
suchen-sci Jun 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/code.analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- main
- pipeline
paths:
- "**/*.go"
- "go.mod"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/license-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- main
- pipeline
pull_request:
branches:
- main
- pipeline

jobs:
check-license:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
run: |
go mod verify
go mod download
go test -v ./...
go test -v ./pkg/...
integration-test-ubuntu:
needs: [test, test-win]
runs-on: ubuntu-latest
Expand All @@ -65,7 +65,7 @@ jobs:

- name: Test
run: |
make httpserver_test
make integration_test
goreleaser:
runs-on: ubuntu-latest
needs: integration-test-ubuntu
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- pipeline
paths:
- "**/*.go"
- "**/*.yaml"
Expand All @@ -15,6 +16,7 @@ on:
types: [opened, synchronize, reopened]
branches:
- main
- pipeline
paths:
- "**/*.go"
- "go.mod"
Expand Down Expand Up @@ -66,9 +68,8 @@ jobs:
run: |
go mod verify
go mod download
go test -v ./...
go test -v ./pkg/...
integration-test-ubuntu:
needs: [test, test-win]
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -83,5 +84,4 @@ jobs:

- name: Test
run: |
make httpserver_test

make integration_test
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKFILE_DIR := $(dir $(MKFILE_PATH))
RELEASE_DIR := ${MKFILE_DIR}bin
GO_PATH := $(shell go env | grep GOPATH | awk -F '"' '{print $$2}')
HTTPSERVER_TEST_PATH := build/test
INTEGRATION_TEST_PATH := build/test

# Image Name
IMAGE_NAME?=megaease/easegress
Expand Down Expand Up @@ -98,16 +98,17 @@ build_docker:

test:
cd ${MKFILE_DIR}
go mod tidy
go mod tidy -compat=1.17
git diff --exit-code go.mod go.sum
go mod verify
go test -v ./... ${TEST_FLAGS}
go test -v ${MKFILE_DIR}pkg/... ${TEST_FLAGS}

httpserver_test: build
integration_test: build
{ \
set -e ;\
cd ${HTTPSERVER_TEST_PATH} ;\
./httpserver_test.sh ;\
cd ${INTEGRATION_TEST_PATH} ;\
./test.sh ;\
./clean.sh ;\
}

clean:
Expand Down
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Easegress

[![Go Report Card](https://goreportcard.com/badge/github.com/megaease/easegress)](https://goreportcard.com/report/github.com/megaease/easegress) [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/megaease/easegress/Test/main)](https://github.com/megaease/easegress/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/megaease/easegress/branch/main/graph/badge.svg?token=5Q80B98LPI)](https://codecov.io/gh/megaease/easegress) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/megaease/easegress)](https://github.com/megaease/easegress/blob/main/go.mod) [![Join MegaEase Slack](https://img.shields.io/badge/slack-megaease-brightgreen?logo=slack)](https://join.slack.com/t/openmegaease/shared_invite/zt-upo7v306-lYPHvVwKnvwlqR0Zl2vveA)
[![Go Report Card](https://goreportcard.com/badge/github.com/megaease/easegress)](https://goreportcard.com/report/github.com/megaease/easegress)
[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/megaease/easegress/Test/main)](https://github.com/megaease/easegress/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/megaease/easegress/branch/main/graph/badge.svg?token=5Q80B98LPI)](https://codecov.io/gh/megaease/easegress)
[![Docker pulls](https://img.shields.io/docker/pulls/megaease/easegress.svg)](https://hub.docker.com/r/megaease/easegress)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/megaease/easegress)](https://github.com/megaease/easegress/blob/main/go.mod)
[![Join MegaEase Slack](https://img.shields.io/badge/slack-megaease-brightgreen?logo=slack)](https://join.slack.com/t/openmegaease/shared_invite/zt-upo7v306-lYPHvVwKnvwlqR0Zl2vveA)

<a href="https://megaease.com/easegress">
<img src="./doc/imgs/easegress.svg"
Expand Down Expand Up @@ -47,10 +53,10 @@ The architecture of Easegress:
- MQTT
- **Rich Routing Rules:** exact path, path prefix, regular expression of the path, method, headers.
- **Resilience&Fault Tolerance**
- **Circuit breaker:** temporarily blocks possible failures.
- **Rate limiter:** limits the rate of incoming requests.
- **Retryer:** repeats failed executions.
- **Time limiter:** limits the duration of execution.
- **Circuit break:** temporarily blocks possible failures.
- **Rate limit:** limits the rate of incoming requests.
- **Retry:** repeats failed executions.
- **Time limit:** limits the duration of execution.
- **Deployment Management**
- **Blue-green Strategy:** switches traffic at one time.
- **Canary Strategy:** schedules traffic slightly.
Expand Down Expand Up @@ -103,7 +109,7 @@ The architecture of Easegress:

The following examples show how to use Easegress for different scenarios.

- [API Aggregator](./doc/cookbook/api-aggregator.md) - Aggregating many APIs into a single API.
- [API Aggregation](./doc/cookbook/api-aggregation.md) - Aggregating many APIs into a single API.
- [Cluster Deployment](./doc/cookbook/multi-node-cluster.md) - How to deploy multiple Easegress cluster nodes.
- [Distributed Tracing](./doc/cookbook/distributed-tracing.md) - How to do APM tracing - Zipkin.
- [FaaS](./doc/cookbook/faas.md) - Supporting Knative FaaS integration
Expand All @@ -113,7 +119,7 @@ The following examples show how to use Easegress for different scenarios.
- [MQTTProxy](./doc/cookbook/mqtt-proxy.md) - An Example to MQTT proxy with Kafka backend.
- [Performance](./doc/cookbook/performance.md) - Performance optimization - compression, caching etc.
- [Pipeline](./doc/cookbook/pipeline.md) - How to orchestrate HTTP filters for requests/responses handling
- [Resilience and Fault Tolerance](./doc/cookbook/resilience.md) - Circuit Breaker, Rate Limiter, Retryer, Time limiter, etc. (Porting from [Java resilience4j](https://github.com/resilience4j/resilience4j))
- [Resilience and Fault Tolerance](./doc/cookbook/resilience.md) - Circuit Break, Rate Limit, Retry, Time Limit, etc. (Porting from [Java resilience4j](https://github.com/resilience4j/resilience4j))
- [Security](./doc/cookbook/security.md) - How to do authentication by Header, JWT, HMAC, OAuth2, etc.
- [Service Proxy](./doc/cookbook/service-proxy.md) - Supporting the Microservice registries - Zookeeper, Eureka, Consul, Nacos, etc.
- [WebAssembly](./doc/cookbook/wasm.md) - Using AssemblyScript to extend the Easegress
Expand Down Expand Up @@ -213,14 +219,14 @@ The rules of routers above mean that it will lead the traffic with the prefix `/
```bash
$ echo '
name: pipeline-demo
kind: HTTPPipeline
kind: Pipeline
flow:
- filter: proxy
filters:
- name: proxy
kind: Proxy
mainPool:
servers:
pools:
- servers:
- url: http://127.0.0.1:9095
- url: http://127.0.0.1:9096
- url: http://127.0.0.1:9097
Expand Down Expand Up @@ -262,7 +268,7 @@ Now we want to add more features to the pipeline, then we could add kinds of fil
```bash
$ cat pipeline-demo.yaml
name: pipeline-demo
kind: HTTPPipeline
kind: Pipeline
flow:
- filter: validator
jumpIf: { invalid: END }
Expand All @@ -282,8 +288,8 @@ filters:
X-Adapt-Key: goodplan
- name: proxy
kind: Proxy
mainPool:
servers:
pools:
- servers:
- url: http://127.0.0.1:9095
- url: http://127.0.0.1:9096
- url: http://127.0.0.1:9097
Expand Down
22 changes: 14 additions & 8 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Easegress

[![Go Report Card](https://goreportcard.com/badge/github.com/megaease/easegress)](https://goreportcard.com/report/github.com/megaease/easegress) [![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/megaease/easegress/Test/main)](https://github.com/haoel/easegress/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/megaease/easegress/branch/main/graph/badge.svg?token=5Q80B98LPI)](https://codecov.io/gh/megaease/easegress) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/megaease/easegress)](https://github.com/haoel/easegress/blob/main/go.mod) [![Join MegaEase Slack](https://img.shields.io/badge/slack-megaease-brightgreen?logo=slack)](https://join.slack.com/t/openmegaease/shared_invite/zt-upo7v306-lYPHvVwKnvwlqR0Zl2vveA)
[![Go Report Card](https://goreportcard.com/badge/github.com/megaease/easegress)](https://goreportcard.com/report/github.com/megaease/easegress)
[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/megaease/easegress/Test/main)](https://github.com/megaease/easegress/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/megaease/easegress/branch/main/graph/badge.svg?token=5Q80B98LPI)](https://codecov.io/gh/megaease/easegress)
[![Docker pulls](https://img.shields.io/docker/pulls/megaease/easegress.svg)](https://hub.docker.com/r/megaease/easegress)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/megaease/easegress)](https://github.com/megaease/easegress/blob/main/go.mod)
[![Join MegaEase Slack](https://img.shields.io/badge/slack-megaease-brightgreen?logo=slack)](https://join.slack.com/t/openmegaease/shared_invite/zt-upo7v306-lYPHvVwKnvwlqR0Zl2vveA)

<a href="https://megaease.com/easegress">
<img src="./doc/imgs/easegress.svg"
Expand Down Expand Up @@ -102,7 +108,7 @@

下面的例子展示了如何在不同场景下使用 Easegress。

- [API 聚合](./doc/cookbook/api-aggregator.md) - 将多个 API 聚合为一个。
- [API 聚合](./doc/cookbook/api-aggregation.md) - 将多个 API 聚合为一个。
- [Easegress 集群化部署](./doc/cookbook/multi-node-cluster.md) - Easegress 如何进行集群化多点部署。
- [分布式调用链](./doc/cookbook/distributed-tracing.md) - 如何使用 Zipkin 进行 APM 追踪。
- [函数即服务 FaaS](./doc/cookbook/faas.md) - 支持 Knative FaaS 集成。
Expand Down Expand Up @@ -212,14 +218,14 @@ rules:
```bash
$ echo '
name: pipeline-demo
kind: HTTPPipeline
kind: Pipeline
flow:
- filter: proxy
filters:
- name: proxy
kind: Proxy
mainPool:
servers:
pools:
- servers:
- url: http://127.0.0.1:9095
- url: http://127.0.0.1:9096
- url: http://127.0.0.1:9097
Expand Down Expand Up @@ -261,7 +267,7 @@ Body : Hello, Easegress
```bash
$ cat pipeline-demo.yaml
name: pipeline-demo
kind: HTTPPipeline
kind: Pipeline
flow:
- filter: validator
jumpIf: { invalid: END }
Expand All @@ -281,8 +287,8 @@ filters:
X-Adapt-Key: goodplan
- name: proxy
kind: Proxy
mainPool:
servers:
pools:
- servers:
- url: http://127.0.0.1:9095
- url: http://127.0.0.1:9096
- url: http://127.0.0.1:9097
Expand Down
39 changes: 39 additions & 0 deletions build/test/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# Copyright (c) 2017, MegaEase
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Test the Easegress' basic functionality which is generating
# an HTTPServer and Pipeline for testing HTTP Requests.
set -e

# path related define.
# Note: use $(dirname $(realpath ${BASH_SOURCE[0]})) to value SCRIPTPATH is OK in linux platform,
# but not in MacOS.(cause there is not `realpath` in it)
# reference: https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
pushd $SCRIPTPATH"/../../example" > /dev/null
EXAMPLEDIR="$SCRIPTPATH"/../../example

# clean cleans primary-single's data and cluster data and the `go run` process.
function clean()
{
# basic cleaning routine
bash $EXAMPLEDIR/stop_cluster.sh
bash $EXAMPLEDIR/clean_cluster.sh
}

# clean the cluster resource.
clean
101 changes: 101 additions & 0 deletions build/test/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test

import (
"fmt"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func makeURL(template string, a ...interface{}) string {
return "http://127.0.0.1:12381/apis/v1" + fmt.Sprintf(template, a...)
}

func successfulStatusCode(code int) bool {
return code >= 200 && code < 300
}

func intentString(str string) string {
return "\n\t\t" + strings.ReplaceAll(str, "\n", "\n\t\t")
}

func handleRequest(t *testing.T, method string, url string, reader io.Reader) *http.Response {
req, err := http.NewRequest(method, url, reader)
require.Nil(t, err)
resp, err := http.DefaultClient.Do(req)
require.Nil(t, err)
return resp
}

const (
objectsURL = "/objects"
objectURL = "/objects/%s"
)

func createObject(t *testing.T, yamlFile string) (ok bool, msg string) {
resp := handleRequest(t, http.MethodPost, makeURL(objectsURL), strings.NewReader(yamlFile))
defer resp.Body.Close()

ok = successfulStatusCode(resp.StatusCode)
if !ok {
data, err := io.ReadAll(resp.Body)
require.Nil(t, err)
msg = fmt.Sprintf("create object\n %v\nfailed, %v", intentString(yamlFile), intentString(string(data)))
}
return
}

func updateObject(t *testing.T, name string, yamlFile string) (ok bool, msg string) {
resp := handleRequest(t, http.MethodPut, makeURL(objectURL, name), strings.NewReader(yamlFile))
defer resp.Body.Close()

ok = successfulStatusCode(resp.StatusCode)
if !ok {
data, err := io.ReadAll(resp.Body)
require.Nil(t, err)
msg = fmt.Sprintf("update object %v\n %v\nfailed, %v", name, intentString(yamlFile), intentString(string(data)))
}
return
}

func deleteObject(t *testing.T, name string) (ok bool, msg string) {
resp := handleRequest(t, http.MethodDelete, makeURL(objectURL, name), nil)
defer resp.Body.Close()

ok = successfulStatusCode(resp.StatusCode)
if !ok {
data, err := io.ReadAll(resp.Body)
require.Nil(t, err)
msg = fmt.Sprintf("delete object %v failed, %v", name, intentString(string(data)))
}
return ok, msg
}

func listObject(t *testing.T) (ok bool, msg string) {
resp := handleRequest(t, http.MethodGet, makeURL(objectsURL), nil)
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
require.Nil(t, err)
return successfulStatusCode(resp.StatusCode), string(data)
}
Loading