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

[FGS]: quotas & invocation #640

Merged
merged 1 commit into from
Apr 10, 2024
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
39 changes: 25 additions & 14 deletions acceptance/openstack/fgs/v2/functiongraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,8 @@ def handler (event, context):
func TestFunctionGraphLifecycle(t *testing.T) {
client, err := clients.NewFuncGraphClient()
th.AssertNoErr(t, err)
funcName := "funcgraph-" + tools.RandomString("acctest", 4)

createOpts := function.CreateOpts{
Name: funcName,
Package: "default",
Runtime: "Python3.9",
Timeout: 200,
Handler: "index.py",
MemorySize: 512,
CodeType: "zip",
CodeURL: "https://regr-func-graph.obs.eu-de.otc.t-systems.com/index.py",
}

createResp, err := function.Create(client, createOpts)
th.AssertNoErr(t, err)
createResp, funcName := createFunctionGraph(t, client)

funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")

Expand Down Expand Up @@ -95,6 +82,10 @@ func TestFunctionGraphLifecycle(t *testing.T) {
})
th.AssertNoErr(t, err)
th.AssertEquals(t, updateFuncInstance.StrategyConfig.Concurrency, 200)

// API not registered
// err = function.UpdateStatus(client, funcUrn, "true")
// th.AssertNoErr(t, err)
}

func TestFunctionGraphList(t *testing.T) {
Expand All @@ -105,3 +96,23 @@ func TestFunctionGraphList(t *testing.T) {
_, err = function.List(client, listOpts)
th.AssertNoErr(t, err)
}

func createFunctionGraph(t *testing.T, client *golangsdk.ServiceClient) (*function.FuncGraph, string) {
funcName := "funcgraph-" + tools.RandomString("acctest", 4)

createOpts := function.CreateOpts{
Name: funcName,
Package: "default",
Runtime: "Python3.9",
Timeout: 200,
Handler: "index.py",
MemorySize: 512,
CodeType: "zip",
CodeURL: "https://regr-func-graph.obs.eu-de.otc.t-systems.com/index.py",
}

createResp, err := function.Create(client, createOpts)
th.AssertNoErr(t, err)

return createResp, funcName
}
63 changes: 63 additions & 0 deletions acceptance/openstack/fgs/v2/invocation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package v2

import (
"os"
"strings"
"testing"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/fgs/v2/function"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/fgs/v2/invoke"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestFunctionGraphSync(t *testing.T) {
if funcGraph := os.Getenv("FUNCGRAPH_TEST"); funcGraph == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think you can run this test in zuul, no need to skip

Copy link
Member Author

@artem-lifshits artem-lifshits Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't work because of some events in functiongraph that I haven't figured out yet :)

t.Skip("`FUNCGRAPH_TEST`needs to be defined to run this test")
}
client, err := clients.NewFuncGraphClient()
th.AssertNoErr(t, err)

createResp, _ := createFunctionGraph(t, client)

funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")

defer func(client *golangsdk.ServiceClient, id string) {
err = function.Delete(client, id)
th.AssertNoErr(t, err)
}(client, funcUrn)

syncResp, err := invoke.LaunchSync(client, funcUrn)
th.AssertNoErr(t, err)

tools.PrintResource(t, syncResp)
}

func TestFunctionGraphAsync(t *testing.T) {
if funcGraph := os.Getenv("FUNCGRAPH_TEST"); funcGraph == "" {
t.Skip("`FUNCGRAPH_TEST`needs to be defined to run this test")
}
client, err := clients.NewFuncGraphClient()
th.AssertNoErr(t, err)

createResp, _ := createFunctionGraph(t, client)

funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")

defer func(client *golangsdk.ServiceClient, id string) {
err = function.Delete(client, id)
th.AssertNoErr(t, err)
}(client, funcUrn)

asyncOpts := map[string]string{
"k": "v",
"test": "start",
}

syncResp, err := invoke.LaunchAsync(client, funcUrn, asyncOpts)
th.AssertNoErr(t, err)

tools.PrintResource(t, syncResp)
}
19 changes: 19 additions & 0 deletions acceptance/openstack/fgs/v2/quota_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package v2

import (
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/fgs/v2/quotas"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestFunctionGraphQuotas(t *testing.T) {
client, err := clients.NewFuncGraphClient()
th.AssertNoErr(t, err)

listQuotasResp, err := quotas.ListQuotas(client)
th.AssertNoErr(t, err)
tools.PrintResource(t, listQuotasResp)
}
12 changes: 12 additions & 0 deletions openstack/fgs/v2/function/UpdateStatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package function

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
)

func UpdateStatus(client *golangsdk.ServiceClient, funcUrn, state string) (err error) {
_, err = client.Put(client.ServiceURL("fgs", "functions", funcUrn, "collect", state), nil, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
28 changes: 28 additions & 0 deletions openstack/fgs/v2/invoke/InvokeAsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package invoke

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func LaunchAsync(client *golangsdk.ServiceClient, funcUrn string, opts map[string]string) (*LaunchAsyncResp, error) {
b, err := build.RequestBody(opts, "body")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("fgs", "functions", funcUrn, "invocations-async"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
if err != nil {
return nil, err
}

var res LaunchAsyncResp
return &res, extract.Into(raw.Body, &res)
}

type LaunchAsyncResp struct {
RequestID string `json:"request_id"`
}
25 changes: 25 additions & 0 deletions openstack/fgs/v2/invoke/InvokeSync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package invoke

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func LaunchSync(client *golangsdk.ServiceClient, funcUrn string) (*LaunchSyncResp, error) {
raw, err := client.Post(client.ServiceURL("fgs", "functions", funcUrn, "invocations"), nil, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res LaunchSyncResp
return &res, extract.Into(raw.Body, &res)
}

type LaunchSyncResp struct {
RequestID string `json:"request_id"`
Result string `json:"result"`
Log string `json:"log"`
Status string `json:"status"`
}
28 changes: 28 additions & 0 deletions openstack/fgs/v2/quotas/ListQuotas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package quotas

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func ListQuotas(client *golangsdk.ServiceClient) (*ListQuotasResults, error) {
raw, err := client.Get(client.ServiceURL("fgs", "quotas"), nil, nil)
if err != nil {
return nil, err
}

var res ListQuotasResults
err = extract.IntoStructPtr(raw.Body, &res, "quotas")
return &res, err
}

type ListQuotasResults struct {
Resources []Resources `json:"resources"`
}

type Resources struct {
Quota int `json:"quota"`
Used int `json:"used"`
Type string `json:"type"`
Unit string `json:"unit"`
}