Skip to content

Commit

Permalink
update integration test (#630)
Browse files Browse the repository at this point in the history
* update integration test

* chmod test file

* update test-pipeline.yml

* add more integration test

* remove filter inherit close previous generation since pipline inherit will close previous filters, double close may cause problem
  • Loading branch information
suchen-sci committed May 25, 2022
1 parent 5596d7c commit 96e546c
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 118 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ jobs:
uses: codecov/[email protected]
with:
file: ./coverage.txt
integration-test-ubuntu:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Set up Go 1.x.y
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}

- name: Checkout codebase
uses: actions/checkout@v3

- name: Test
run: |
make integration_test
11 changes: 6 additions & 5 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 @@ -101,13 +101,14 @@ test:
go mod tidy
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
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:https://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:https://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:https://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

0 comments on commit 96e546c

Please sign in to comment.