Skip to content

Commit

Permalink
test(portable): happy path fvt
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <[email protected]>
  • Loading branch information
ngjaying committed Aug 19, 2024
1 parent ba0684f commit 79be3d8
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 4 deletions.
214 changes: 214 additions & 0 deletions fvt/portable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Copyright 2024 EMQ Technologies Co., Ltd.
//
// 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 fvt

import (
"archive/zip"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"

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

type PortableTestSuite struct {
suite.Suite
}

func TestPortableTestSuite(t *testing.T) {
suite.Run(t, new(PortableTestSuite))
}

func (s *ServerTestSuite) TestLC() {
streamSql := `{"sql": "create stream pyjsonStream () WITH (TYPE=\"pyjson\", FORMAT=\"json\")"}`
ruleSql := `{
"id": "rulePort1",
"sql": "SELECT * FROM pyjsonStream",
"actions": [
{
"print": {}
}
]
}`
s.Run("delete existing plugin", func() {
resp, err := client.Delete("portable/pysam")
s.NoError(err)
if resp.StatusCode != http.StatusOK {
s.T().Log(resp.Status)
}
})
time.Sleep(ConstantInterval)
s.Run("create rule error when plugin not installed", func() {
resp, err := client.CreateStream(streamSql)
s.Require().NoError(err)
s.Require().Equal(http.StatusBadRequest, resp.StatusCode)
s.T().Log(GetResponseText(resp))

resp, err = client.CreateRule(ruleSql)
s.Require().NoError(err)
s.Require().Equal(http.StatusBadRequest, resp.StatusCode)
s.T().Log(GetResponseText(resp))
})
s.Run("install plugin and check status", func() {
// zip the plugin dir
pysamDir := filepath.Join(PWD, "sdk", "python", "example", "pysam")
pysamZipPath := "/tmp/pysam.zip"
err := zipDirectory(pysamDir, pysamZipPath)
s.Require().NoError(err)
defer func() {
os.Remove(pysamZipPath)
}()
// install the plugin
resp, err := client.Post("plugins/portables", `{"name":"pysam","file":"file:///tmp/pysam.zip"}`)
s.Require().NoError(err)
s.Require().Equal(http.StatusCreated, resp.StatusCode)
})
s.Run("check plugin status", func() {
// check the plugin status
resp, err := client.Get("plugins/portables")
s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode)
payload, err := io.ReadAll(resp.Body)
s.NoError(err)
defer resp.Body.Close()
s.Require().Equal("[{\"name\":\"mirror\",\"version\":\"v1.0.0\",\"language\":\"go\",\"executable\":\"/home/runner/work/ekuiper/ekuiper/plugins/portable/mirror/mirror\",\"sources\":[\"random\"],\"sinks\":[\"file\"],\"functions\":[\"echo\"]},{\"name\":\"pysam\",\"version\":\"v1.0.0\",\"language\":\"python\",\"executable\":\"/home/runner/work/ekuiper/ekuiper/plugins/portable/pysam/pysam.py\",\"sources\":[\"pyjson\"],\"sinks\":[\"print\"],\"functions\":[\"revert\"]}]", string(payload))
})
s.Run("test rule with plugin", func() {
resp, err := client.CreateStream(streamSql)
s.Require().NoError(err)
s.T().Log(GetResponseText(resp))
s.Require().Equal(http.StatusCreated, resp.StatusCode)

resp, err = client.CreateRule(ruleSql)
s.Require().NoError(err)
s.T().Log(GetResponseText(resp))
s.Require().Equal(http.StatusCreated, resp.StatusCode)

// Check rule status after a while
time.Sleep(ConstantInterval)
metrics, err := client.GetRulStatus("rulePort1")
s.Require().NoError(err)
s.Equal("running", metrics["status"])
sinkOut, ok := metrics["sink_print_0_0_records_out_total"]
s.True(ok)
if !ok {
s.T().Log(metrics)
}
s.True(sinkOut.(float64) > 0)
s.Equal("", metrics["source_pyjsonStream_0_last_exception"])
})
//s.Run("test rule restart", func() {
// resp, err := client.RestartRule("rulePort1")
// s.Require().NoError(err)
// s.Require().Equal(http.StatusOK, resp.StatusCode)
// // Check rule status after a while
// time.Sleep(ConstantInterval)
// metrics, err := client.GetRulStatus("rulePort1")
// s.Require().NoError(err)
// s.Equal("running", metrics["status"])
// sinkOut, ok := metrics["sink_print_0_0_records_out_total"]
// s.True(ok)
// if !ok {
// s.T().Log(metrics)
// }
// s.True(sinkOut.(float64) > 0)
// s.Equal("", metrics["source_pyjsonStream_0_last_exception"])
//})
//s.Run("test plugin update", func() {})
s.Run("clean up", func() {
resp, err := client.DeleteRule("rulePort1")
s.NoError(err)
s.Equal(200, resp.StatusCode)

resp, err = client.DeleteStream("pyjsonStream")
s.NoError(err)
s.Equal(200, resp.StatusCode)
})
s.Run("delete plugin", func() {
resp, err := client.Delete("portable/pysam")
s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
})
}

// zipDirectory zips the contents of the specified source directory into the target zip file
func zipDirectory(source string, target string) error {
// Create the zip file
zipFile, err := os.Create(target)
if err != nil {
return err
}
defer zipFile.Close()

// Create a new zip writer
writer := zip.NewWriter(zipFile)
defer writer.Close()

// Walk through the source directory and add files to the zip
return filepath.Walk(source, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip the source directory itself
if file == source {
return nil
}

// Create a header for the file
header, err := zip.FileInfoHeader(fi)
if err != nil {
return err
}

// Set the header name to the relative path
relPath, err := filepath.Rel(source, file)
if err != nil {
return err
}
header.Name = relPath

// If it's a directory, append a "/" to the name
if fi.IsDir() {
header.Name += "/"
}

// Create the writer for the file header
writer, err := writer.CreateHeader(header)
if err != nil {
return err
}

// If it's a file, write its content to the zip
if !fi.IsDir() {
fileReader, err := os.Open(file)
if err != nil {
return err
}
defer fileReader.Close()

// Copy the file content to the zip writer
_, err = io.Copy(writer, fileReader)
if err != nil {
return err
}
}

return nil
})
}
33 changes: 30 additions & 3 deletions fvt/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func (sdk *SDK) Get(command string) (resp *http.Response, err error) {
return http.Get(sdk.baseUrl.JoinPath(command).String())
}

func (sdk *SDK) Post(command string, body string) (resp *http.Response, err error) {
return http.Post(sdk.baseUrl.JoinPath(command).String(), ContentTypeJson, bytes.NewBufferString(body))
}

func (sdk *SDK) Delete(command string) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodDelete, sdk.baseUrl.JoinPath(command).String(), nil)
if err != nil {
fmt.Println(err)
return
}
return sdk.httpClient.Do(req)
}

func (sdk *SDK) CreateStream(streamJson string) (resp *http.Response, err error) {
return http.Post(sdk.baseUrl.JoinPath("streams").String(), ContentTypeJson, bytes.NewBufferString(streamJson))
}
Expand All @@ -59,6 +72,10 @@ func (sdk *SDK) CreateRule(ruleJson string) (resp *http.Response, err error) {
return http.Post(sdk.baseUrl.JoinPath("rules").String(), ContentTypeJson, bytes.NewBufferString(ruleJson))
}

func (sdk *SDK) RestartRule(ruleId string) (resp *http.Response, err error) {
return http.Post(sdk.baseUrl.JoinPath("rules", ruleId, "restart").String(), ContentTypeJson, nil)
}

func (sdk *SDK) DeleteRule(name string) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodDelete, sdk.baseUrl.JoinPath("rules", name).String(), nil)
if err != nil {
Expand All @@ -68,19 +85,29 @@ func (sdk *SDK) DeleteRule(name string) (resp *http.Response, err error) {
return sdk.httpClient.Do(req)
}

func (sdk *SDK) GetRulStatus(name string) (metrics map[string]any, err error) {
func (sdk *SDK) GetRulStatus(name string) (map[string]any, error) {
resp, err := http.Get(sdk.baseUrl.JoinPath("rules", name, "status").String())
if err != nil {
fmt.Println(err)
return
return nil, err
}
return GetResponseResultMap(resp)
}

func GetResponseText(resp *http.Response) (string, error) {
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
return string(b), err
}

func GetResponseResultMap(resp *http.Response) (result map[string]any, err error) {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(body, &metrics)
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println(err)
return
Expand Down
3 changes: 2 additions & 1 deletion internal/plugin/portable/manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -281,6 +281,7 @@ func (m *Manager) install(name, src string, shellParas []string) (resultErr erro
if err != nil {
return err
}
break
}
}
if pi == nil {
Expand Down

0 comments on commit 79be3d8

Please sign in to comment.