From af56113c3e6e09e108acdc9eb6e1afaffeb06f36 Mon Sep 17 00:00:00 2001 From: Martin Levesque Date: Sat, 12 Feb 2022 20:57:42 -0500 Subject: [PATCH 1/3] test --- .github/workflows/main.yml | 63 ++++++++++++++++++++++++ Makefile | 16 +++++++ http_shaping.go | 3 +- http_shaping_test.go | 98 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/main.yml create mode 100644 Makefile create mode 100644 http_shaping_test.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..37cadb4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,63 @@ +name: Test + +on: + push: + branches: + - master + tags: + - v* + pull_request: + +jobs: + + main: + name: Test middleware + runs-on: ubuntu-latest + env: + GO_VERSION: 1.17 + GOLANGCI_LINT_VERSION: v1.42.1 + YAEGI_VERSION: v0.10.0 + CGO_ENABLED: 0 + defaults: + run: + working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }} + + steps: + + # https://github.com/marketplace/actions/setup-go-environment + - name: Set up Go ${{ env.GO_VERSION }} + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} + + # https://github.com/marketplace/actions/checkout + - name: Check out code + uses: actions/checkout@v2 + with: + path: go/src/github.com/${{ github.repository }} + fetch-depth: 0 + + # https://github.com/marketplace/actions/cache + - name: Cache Go modules + uses: actions/cache@v2 + with: + path: ${{ github.workspace }}/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Setup GOPATH + run: go env -w GOPATH=${{ github.workspace }}/go + + - name: Check and get dependencies + run: | + go mod tidy + git diff --exit-code go.mod + # git diff --exit-code go.sum + go mod download + go mod vendor + # git diff --exit-code ./vendor/ + + - name: Tests + run: make + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3359fd9 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ + +export GO111MODULE=on + +default: test + +test: + go test -v -cover ./... + +yaegi_test: + yaegi test -v . + +vendor: + go mod vendor + +clean: + rm -rf ./vendor \ No newline at end of file diff --git a/http_shaping.go b/http_shaping.go index 1db8a19..9b4b518 100644 --- a/http_shaping.go +++ b/http_shaping.go @@ -2,7 +2,6 @@ package http_shaping import ( - "bytes" "context" "fmt" "net/http" @@ -128,6 +127,8 @@ func (a *HttpShaping) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } } +// the following bytes conversion methods are taken from: +// https://github.com/cloudfoundry/bytefmt/blob/master/bytes.go const ( BYTE = 1 << (10 * iota) diff --git a/http_shaping_test.go b/http_shaping_test.go new file mode 100644 index 0000000..b09bee4 --- /dev/null +++ b/http_shaping_test.go @@ -0,0 +1,98 @@ +package http_shaping_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/martinlevesque/http_shaping" +) + +func TestHttpShapingHappyPath(t *testing.T) { + cfg := http_shaping.CreateConfig() + cfg.LoopInterval = 5 + cfg.InTrafficLimit = "1KiB" + cfg.OutTrafficLimit = "1KiB" + cfg.ConsiderLimits = true + + ctx := context.Background() + next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) + + handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") + if err != nil { + t.Fatal(err) + } + + recorder := httptest.NewRecorder() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(recorder, req) + + if recorder.Code != 200 { + t.Errorf("Invalid status code") + } +} + +func TestHttpShapingWithLowLimit(t *testing.T) { + cfg := http_shaping.CreateConfig() + cfg.LoopInterval = 5 + cfg.InTrafficLimit = "0KiB" + cfg.OutTrafficLimit = "1KiB" + cfg.ConsiderLimits = true + + ctx := context.Background() + next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) + + handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") + if err != nil { + t.Fatal(err) + } + + recorder := httptest.NewRecorder() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(recorder, req) + + if recorder.Code != 429 { + t.Errorf("Invalid status code with low limit") + } +} + +func TestHttpShapingWithLowLimitButShouldIgnore(t *testing.T) { + cfg := http_shaping.CreateConfig() + cfg.LoopInterval = 5 + cfg.InTrafficLimit = "0KiB" + cfg.OutTrafficLimit = "1KiB" + cfg.ConsiderLimits = false + + ctx := context.Background() + next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}) + + handler, err := http_shaping.New(ctx, next, cfg, "demo-plugin") + if err != nil { + t.Fatal(err) + } + + recorder := httptest.NewRecorder() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(recorder, req) + + if recorder.Code != 200 { + t.Errorf("Invalid status code") + } +} + From be60daf804daccaaf4a486212cd735c8e8501810 Mon Sep 17 00:00:00 2001 From: Martin Levesque Date: Sat, 12 Feb 2022 20:58:31 -0500 Subject: [PATCH 2/3] w --- .github/workflows/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 37cadb4..4ecc1b4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,8 +2,6 @@ name: Test on: push: - branches: - - master tags: - v* pull_request: From 77e6170097b8152ba85b07fdc90bab3f9a7b4698 Mon Sep 17 00:00:00 2001 From: Martin Levesque Date: Sat, 12 Feb 2022 20:59:02 -0500 Subject: [PATCH 3/3] test --- .github/workflows/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4ecc1b4..8c63bcc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,8 +2,6 @@ name: Test on: push: - tags: - - v* pull_request: jobs: