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

feat: add fault-injection plugin for benchmark #46

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
96 changes: 96 additions & 0 deletions cmd/go-runner/plugins/fault_injection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 plugins

import (
"encoding/json"
"errors"
"math/rand"
"net/http"

pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
"github.com/apache/apisix-go-plugin-runner/pkg/log"
"github.com/apache/apisix-go-plugin-runner/pkg/plugin"
)

const (
plugin_name = "fault-injection"
)

func init() {
err := plugin.RegisterPlugin(&FaultInjection{})
if err != nil {
log.Fatalf("failed to register plugin %s: %s", plugin_name, err)
}
}

// FaultInjection is used in the benchmark
type FaultInjection struct {
}

type FaultInjectionConf struct {
Body string `json:"body"`
HttpStatus int `json:"http_status"`
Percentage int `json:"percentage"`
}

func (p *FaultInjection) Name() string {
return plugin_name
}

func (p *FaultInjection) ParseConf(in []byte) (interface{}, error) {
conf := FaultInjectionConf{Percentage: -1}
err := json.Unmarshal(in, &conf)
if err != nil {
return nil, err
}

// schema check
if conf.HttpStatus < 200 {
return nil, errors.New("bad http_status")
}
if conf.Percentage == -1 {
conf.Percentage = 100
} else if conf.Percentage < 0 || conf.Percentage > 100 {
return nil, errors.New("bad percentage")
}

return conf, err
}

func sampleHit(percentage int) bool {
return rand.Intn(100) < percentage
}

func (p *FaultInjection) Filter(conf interface{}, w http.ResponseWriter, r pkgHTTP.Request) {
fc := conf.(FaultInjectionConf)
if !sampleHit(fc.Percentage) {
return
}

w.WriteHeader(fc.HttpStatus)
body := fc.Body
if len(body) == 0 {
return
}

_, err := w.Write([]byte(body))
if err != nil {
log.Errorf("failed to write: %s", err)
}
}
52 changes: 52 additions & 0 deletions cmd/go-runner/plugins/fault_injection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 plugins

import (
"io/ioutil"
"net/http/httptest"
"testing"

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

func TestFaultInjection(t *testing.T) {
in := []byte(`{"http_status":400, "body":"hello"}`)
fi := &FaultInjection{}
conf, err := fi.ParseConf(in)
assert.Nil(t, err)

w := httptest.NewRecorder()
fi.Filter(conf, w, nil)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, "hello", string(body))
}

func TestFaultInjection_Percentage(t *testing.T) {
in := []byte(`{"http_status":400, "percentage":0}`)
fi := &FaultInjection{}
conf, err := fi.ParseConf(in)
assert.Nil(t, err)

w := httptest.NewRecorder()
fi.Filter(conf, w, nil)
resp := w.Result()
assert.Equal(t, 200, resp.StatusCode)
}