github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/fgs/v2/functiongraph_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"encoding/base64"
     5  	"strings"
     6  	"testing"
     7  
     8  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/fgs/v2/function"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  const appCode = `
    16  # -*- coding:utf-8 -*-
    17  import json
    18  def handler (event, context):
    19      return {
    20          "statusCode": 200,
    21          "isBase64Encoded": False,
    22          "body": json.dumps(event),
    23          "headers": {
    24              "Content-Type": "application/json"
    25          }
    26      }
    27  `
    28  
    29  func TestFunctionGraphLifecycle(t *testing.T) {
    30  	client, err := clients.NewFuncGraphClient()
    31  	th.AssertNoErr(t, err)
    32  
    33  	createResp, funcName := createFunctionGraph(t, client)
    34  
    35  	funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")
    36  
    37  	defer func(client *golangsdk.ServiceClient, id string) {
    38  		err = function.Delete(client, id)
    39  		th.AssertNoErr(t, err)
    40  	}(client, funcUrn)
    41  
    42  	getCodeResp, err := function.GetCode(client, funcUrn)
    43  	th.AssertNoErr(t, err)
    44  	th.AssertEquals(t, createResp.CodeURL, getCodeResp.CodeURL)
    45  
    46  	updateFuncOpts := function.UpdateFuncCodeOpts{
    47  		FuncUrn:  funcUrn,
    48  		CodeType: "inline",
    49  		FuncCode: &function.FuncCode{
    50  			File: base64.StdEncoding.EncodeToString([]byte(appCode)),
    51  		},
    52  	}
    53  
    54  	updateResp, err := function.UpdateFuncCode(client, updateFuncOpts)
    55  	th.AssertNoErr(t, err)
    56  	th.AssertEquals(t, updateResp.CodeType, "inline")
    57  	th.AssertEquals(t, updateResp.CodeFilename, "index.py")
    58  
    59  	updateMetaOpts := function.UpdateFuncMetadataOpts{
    60  		FuncUrn:    funcUrn,
    61  		Name:       funcName,
    62  		Runtime:    "Python3.6",
    63  		Timeout:    10,
    64  		Handler:    "index.py",
    65  		MemorySize: 128,
    66  	}
    67  
    68  	updateMetaResp, err := function.UpdateFuncMetadata(client, updateMetaOpts)
    69  	th.AssertNoErr(t, err)
    70  	th.AssertEquals(t, updateMetaResp.FuncName, funcName)
    71  
    72  	getMetaResp, err := function.GetMetadata(client, funcUrn)
    73  	th.AssertNoErr(t, err)
    74  	th.AssertEquals(t, getMetaResp.CPU, updateMetaResp.CPU)
    75  	th.AssertEquals(t, getMetaResp.MemorySize, updateMetaResp.MemorySize)
    76  	th.AssertEquals(t, getMetaResp.Timeout, updateMetaResp.Timeout)
    77  
    78  	updateFuncInstance, err := function.UpdateMaxInstances(client,
    79  		function.UpdateFuncInstancesOpts{
    80  			FuncUrn:        funcUrn,
    81  			MaxInstanceNum: 200,
    82  		})
    83  	th.AssertNoErr(t, err)
    84  	th.AssertEquals(t, updateFuncInstance.StrategyConfig.Concurrency, 200)
    85  
    86  	// API not registered
    87  	// err = function.UpdateStatus(client, funcUrn, "true")
    88  	// th.AssertNoErr(t, err)
    89  }
    90  
    91  func TestFunctionGraphExports(t *testing.T) {
    92  	client, err := clients.NewFuncGraphClient()
    93  	th.AssertNoErr(t, err)
    94  
    95  	createResp, _ := createFunctionGraph(t, client)
    96  
    97  	funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")
    98  
    99  	defer func(client *golangsdk.ServiceClient, id string) {
   100  		err = function.Delete(client, id)
   101  		th.AssertNoErr(t, err)
   102  	}(client, funcUrn)
   103  
   104  	t.Logf("Attempting to EXPORT FUNCGRAPH")
   105  	err = function.Export(client, funcUrn, function.ExportOpts{
   106  		Type: "code",
   107  	})
   108  	th.AssertNoErr(t, err)
   109  
   110  	// Import fails with any given filecode
   111  	// importOpts := function.ImportOpts{
   112  	// 	FuncName: createResp.FuncName,
   113  	// 	FileName: "index.zip",
   114  	// 	FileType: "zip",
   115  	// 	FileCode: base64.StdEncoding.EncodeToString([]byte("1")),
   116  	// }
   117  	//
   118  	// importResp, err := function.Import(client, importOpts)
   119  	// th.AssertNoErr(t, err)
   120  	// tools.PrintResource(t, importResp)
   121  }
   122  
   123  func TestFunctionGraphList(t *testing.T) {
   124  	client, err := clients.NewFuncGraphClient()
   125  	th.AssertNoErr(t, err)
   126  
   127  	listOpts := function.ListOpts{}
   128  	_, err = function.List(client, listOpts)
   129  	th.AssertNoErr(t, err)
   130  }
   131  
   132  func createFunctionGraph(t *testing.T, client *golangsdk.ServiceClient) (*function.FuncGraph, string) {
   133  	funcName := "funcgraph-" + tools.RandomString("acctest", 4)
   134  
   135  	createOpts := function.CreateOpts{
   136  		Name:       funcName,
   137  		Package:    "default",
   138  		Runtime:    "Python3.9",
   139  		Timeout:    200,
   140  		Handler:    "index.py",
   141  		MemorySize: 512,
   142  		CodeType:   "zip",
   143  		CodeURL:    "https://regr-func-graph.obs.eu-de.otc.t-systems.com/index.py",
   144  	}
   145  
   146  	createResp, err := function.Create(client, createOpts)
   147  	th.AssertNoErr(t, err)
   148  
   149  	return createResp, funcName
   150  }