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

     1  package v2
     2  
     3  import (
     4  	"os"
     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  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/fgs/v2/trigger"
    13  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/lts/v2/groups"
    14  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/lts/v2/streams"
    15  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    16  )
    17  
    18  func TestFunctionTriggerLifecycle(t *testing.T) {
    19  	agency := os.Getenv("AGENCY")
    20  	if agency == "" {
    21  		t.Skip("`AGENCY`needs to be defined to run this test")
    22  	}
    23  
    24  	client, err := clients.NewFuncGraphClient()
    25  	th.AssertNoErr(t, err)
    26  
    27  	clientLts, err := clients.NewLtsV2Client()
    28  	th.AssertNoErr(t, err)
    29  
    30  	createResp, _ := createFunctionGraphAgency(t, client, agency)
    31  
    32  	funcUrn := strings.TrimSuffix(createResp.FuncURN, ":latest")
    33  
    34  	defer func(client *golangsdk.ServiceClient, id string) {
    35  		err = function.Delete(client, id)
    36  		th.AssertNoErr(t, err)
    37  	}(client, funcUrn)
    38  
    39  	name := tools.RandomString("test-group-", 3)
    40  	createOpts := groups.CreateOpts{
    41  		LogGroupName: name,
    42  		TTLInDays:    7,
    43  	}
    44  
    45  	logId, err := groups.CreateLogGroup(clientLts, createOpts)
    46  	th.AssertNoErr(t, err)
    47  
    48  	defer func(client *golangsdk.ServiceClient, id string) {
    49  		err = groups.DeleteLogGroup(clientLts, logId)
    50  		th.AssertNoErr(t, err)
    51  	}(client, funcUrn)
    52  
    53  	streamId, err := streams.CreateLogStream(clientLts, streams.CreateOpts{
    54  		GroupId:       logId,
    55  		LogStreamName: tools.RandomString("test-stream-", 3),
    56  	})
    57  	th.AssertNoErr(t, err)
    58  
    59  	defer func(client *golangsdk.ServiceClient, id string) {
    60  		err = streams.DeleteLogStream(clientLts, streams.DeleteOpts{
    61  			GroupId:  logId,
    62  			StreamId: streamId,
    63  		})
    64  		th.AssertNoErr(t, err)
    65  	}(client, funcUrn)
    66  
    67  	createTriggerOpts := trigger.CreateOpts{
    68  		FuncUrn:         funcUrn,
    69  		TriggerTypeCode: "LTS",
    70  		TriggerStatus:   "ACTIVE",
    71  		EventData: &trigger.EventData{
    72  			LogGroupID: logId,
    73  			LogTopicID: streamId,
    74  		},
    75  	}
    76  
    77  	t.Logf("Attempting to CREATE FUNCGRAPH TRIGGER")
    78  	createTriggerResp, err := trigger.Create(client, createTriggerOpts)
    79  	th.AssertNoErr(t, err)
    80  	tools.PrintResource(t, createTriggerResp)
    81  
    82  	defer func(client *golangsdk.ServiceClient, urn, triggerType, id string) {
    83  		t.Logf("Attempting to DELETE FUNCGRAPH TRIGGER")
    84  		err = trigger.Delete(client, urn, triggerType, id)
    85  		th.AssertNoErr(t, err)
    86  	}(client, funcUrn, "LTS", createTriggerResp.TriggerId)
    87  
    88  	t.Logf("Attempting to GET FUNCGRAPH TRIGGER")
    89  	getTriggerResp, err := trigger.Get(client, funcUrn, "LTS", createTriggerResp.TriggerId)
    90  	th.AssertNoErr(t, err)
    91  	th.AssertEquals(t, createTriggerResp.TriggerId, getTriggerResp.TriggerId)
    92  	th.AssertEquals(t, createTriggerResp.TriggerStatus, getTriggerResp.TriggerStatus)
    93  	th.AssertEquals(t, createTriggerResp.CreatedTime, getTriggerResp.CreatedTime)
    94  
    95  	updateTriggerOpts := trigger.UpdateOpts{
    96  		FuncUrn:         funcUrn,
    97  		TriggerId:       getTriggerResp.TriggerId,
    98  		TriggerTypeCode: getTriggerResp.TriggerTypeCode,
    99  		TriggerStatus:   "DISABLED",
   100  	}
   101  
   102  	t.Logf("Attempting to UPDATE FUNCGRAPH TRIGGER")
   103  	updateTriggerResp, err := trigger.Update(client, updateTriggerOpts)
   104  	th.AssertNoErr(t, err)
   105  	th.AssertEquals(t, updateTriggerResp.TriggerStatus, "DISABLED")
   106  
   107  }
   108  
   109  func createFunctionGraphAgency(t *testing.T, client *golangsdk.ServiceClient, agency string) (*function.FuncGraph, string) {
   110  	funcName := "funcgraph-" + tools.RandomString("acctest", 4)
   111  
   112  	createOpts := function.CreateOpts{
   113  		Name:       funcName,
   114  		Package:    "default",
   115  		Runtime:    "Python3.9",
   116  		Timeout:    200,
   117  		Handler:    "index.py",
   118  		MemorySize: 512,
   119  		CodeType:   "zip",
   120  		Xrole:      agency,
   121  		CodeURL:    "https://regr-func-graph.obs.eu-de.otc.t-systems.com/index.py",
   122  	}
   123  
   124  	createResp, err := function.Create(client, createOpts)
   125  	th.AssertNoErr(t, err)
   126  
   127  	return createResp, funcName
   128  }