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

add test cases for modules route #40

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/build-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
Expand Down
38 changes: 26 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ func main() {
}

type Glob struct {
Url url.URL
Username string
Password string
Stream string
Client HTTPClient
Mode string
Url url.URL
Username string
Password string
Stream string
Client HTTPClient
Mode string
PanoramaBaseAddress string
PanoramaAdminUsername string
PanoramaAdminPassword string
}

var NewGlob = func() Glob {
Expand All @@ -43,11 +46,19 @@ var NewGlob = func() Glob {
var stream string
var mode string

var panoramaAddress string
var panoramaAdminUsername string
var panoramaAdminPassword string

flag.StringVar(&targetUrl, "url", "http://localhost:8000", "Specify url. Default is root")
flag.StringVar(&username, "user", "admin", "Specify username. Default is admin")
flag.StringVar(&password, "pass", "admin", "Specify pass. Default is admin")
flag.StringVar(&stream, "stream", "app", "Specify stream. Default is app")
flag.StringVar(&mode, "mode", "smoke", "Specify mode. Default is smoke")
flag.StringVar(&panoramaAddress, "panorama-address", "http://localhost:5000", "Specify panorama address. Default is http://localhost:5000")

flag.StringVar(&panoramaAdminUsername, "panorama-admin-username", "pano_admin", "Specify module connection username. Default is pano_admin")
flag.StringVar(&panoramaAdminPassword, "panorama-admin-password", "pano_admin", "Specify Module connection password. Default is pano_admin")

flag.Parse()

Expand All @@ -59,11 +70,14 @@ var NewGlob = func() Glob {
client := DefaultClient(*parsedTargetUrl, username, password)

return Glob{
Url: *parsedTargetUrl,
Username: username,
Password: password,
Stream: stream,
Client: client,
Mode: mode,
Url: *parsedTargetUrl,
Username: username,
Password: password,
Stream: stream,
Client: client,
Mode: mode,
PanoramaBaseAddress: panoramaAddress,
PanoramaAdminUsername: panoramaAdminUsername,
PanoramaAdminPassword: panoramaAdminPassword,
}
}()
18 changes: 11 additions & 7 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ mode=$1
endpoint=$2
username=$3
password=$4
schema_count=$5
: "${schema_count:=20}"
vus=$6
: "${vus:=10}"
duration=$7
: "${duration:="5m"}"

schema_count="${5:-20}"
vus="${6:-10}"
duration="${7:-5m}"

# todo: accept all these as named argument instead: eg: -pan_user admin
panorama_address="${8:-http:0.0.0.0:5000}"
panorama_admin_username="${9:-pano_admin}"
panorama_admin_password="${10:-pano_admin}"

stream_name=$(head /dev/urandom | tr -dc a-z | head -c10)

run () {
./quest.test -test.v -mode="$mode" -url="$endpoint" -stream="$stream_name" -user="$username" -pass="$password"
./quest.test -test.v -mode="$mode" -url="$endpoint" -stream="$stream_name" -user="$username" -pass="$password" \
-panorama-address="$panorama_address" -panorama-admin-username="$panorama_admin_username" -panorama-admin-password="$panorama_admin_password"
return $?
}

Expand Down
91 changes: 91 additions & 0 deletions modules_test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"bytes"
"testing"

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

const sample_module_config_per_stream = `
[{
"mode": "col",
"algorithm": "z-score",
"start_time": "2023-10-12T08:38:47.069Z",
"end_time": "2023-10-12T08:40:47.069Z",
"interval_window": "5s",
"column_to_watch": "api_timeToResponseInNS"
}]
`

func test_module_registration_flow(t *testing.T) error {

module_name := "panorama"
stream_name := NewGlob.Stream
sample_proxy_route_body := `
{
"stream_name": "` + stream_name + `"
}
`
println("Module Registration flow for: " + module_name)

println("Getting list of modules:")
req, _ := NewGlob.Client.NewRequest("GET", "modules", bytes.NewBufferString("{}"))
response, err := NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Updating config for stream: ", stream_name)
req, _ = NewGlob.Client.NewRequest("PUT", "modules/"+module_name+"/config/"+stream_name, bytes.NewBufferString(sample_module_config_per_stream))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Receiving config")
req, _ = NewGlob.Client.NewRequest("GET", "modules/"+module_name+"/config/"+stream_name, bytes.NewBufferString("{}"))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Testing Proxy Route")
req, _ = NewGlob.Client.NewRequest("GET", "modules/"+module_name+"/anomaly", bytes.NewBufferString(sample_proxy_route_body))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Stop anomaly watching by putting config as empty")
req, _ = NewGlob.Client.NewRequest("PUT", "modules/"+module_name+"/config/"+stream_name, bytes.NewBufferString("{}"))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Testing DeRegistering Module")
req, _ = NewGlob.Client.NewRequest("DELETE", "modules/"+module_name, bytes.NewBufferString("{}"))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

println("Testing Duplicate DeRegister ")
req, _ = NewGlob.Client.NewRequest("DELETE", "modules/"+module_name, bytes.NewBufferString("{}"))
response, err = NewGlob.Client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))

return nil
}
6 changes: 6 additions & 0 deletions quest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func TestSmokeIngestEventsToStream(t *testing.T) {
Sleep()
}

func TestSmokeModulesAPI(t *testing.T) {
CreateStream(t, NewGlob.Client, NewGlob.Stream)
test_module_registration_flow(t)
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
}

func TestSmokeLoadWithK6Stream(t *testing.T) {
CreateStream(t, NewGlob.Client, NewGlob.Stream)
cmd := exec.Command("k6",
Expand Down
Loading