Skip to content

Commit 2f88579

Browse files
committed
Added test file and changes to deployment flow
1 parent ef5217a commit 2f88579

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

azurefunctions_setup/shared_azure_workload/azureworkload.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Global variable for hostname
1212
hostname = socket.gethostname()
1313

14-
# Placeholder for `execute_function`
14+
# Placeholder for `execute_function`, will be updated according to server/trace-func-py/trace_func.py
1515
def execute_function(input, runTime, totalMem):
1616
startTime = process_time_ns()
1717

pkg/driver/deployment/azure_functions.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func newAzureFunctionsDeployer() *azureFunctionsDeployer {
3434

3535
func (afd *azureFunctionsDeployer) Deploy(cfg *config.Configuration) {
3636
afd.functions = cfg.Functions
37-
deployAzureFunctions(afd.functions)
37+
DeployAzureFunctions(afd.functions)
3838
}
3939

4040
func (afd *azureFunctionsDeployer) Clean() {
41-
cleanAzureFunctions()
41+
CleanAzureFunctions()
4242
}
4343

44-
func deployAzureFunctions(functions []*common.Function) {
44+
func DeployAzureFunctions(functions []*common.Function) {
4545
// 1. Run script to extract workload from trace_func.py
4646
// 2. Initialize resources required for Azure Functions deployment
4747
// 3. Create function folders
@@ -83,7 +83,7 @@ func deployAzureFunctions(functions []*common.Function) {
8383
}
8484
}
8585

86-
func cleanAzureFunctions() {
86+
func CleanAzureFunctions() {
8787
// Load azurefunctionsconfig yaml file
8888
config, err := LoadConfig("azurefunctions_setup/azurefunctionsconfig.yaml")
8989
if err != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package deployment_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"os"
9+
"os/exec"
10+
"testing"
11+
"time"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/vhive-serverless/loader/pkg/common"
15+
"github.com/vhive-serverless/loader/pkg/driver/deployment"
16+
)
17+
18+
func TestDeploymentWithAzureFunctions(t *testing.T) {
19+
20+
/* Test Setup */
21+
22+
// Mock functions for deployment
23+
functions := []*common.Function{
24+
{Name: "function0"},
25+
{Name: "function1"},
26+
}
27+
28+
// Change the working directory to the project root
29+
err := os.Chdir("../../../")
30+
if err != nil {
31+
t.Fatalf("Failed to change working directory: %s", err)
32+
}
33+
34+
// Log current working directory for debugging
35+
wd, err := os.Getwd()
36+
assert.NoError(t, err, "Failed to get current working directory")
37+
t.Logf("Current working directory: %s", wd)
38+
39+
/* Deployment Tests */
40+
41+
// Deploy Azure Functions
42+
t.Log("Testing `DeployAzureFunctions`...")
43+
deployment.DeployAzureFunctions(functions)
44+
45+
// Verify the Function App exists (name and resource group from azurefunctionsconfig.yaml)
46+
t.Log("Verifying Function App deployment...")
47+
cmd := exec.Command("az", "functionapp", "show", "--name", "invitrofunctionapp", "--resource-group", "ExperimentResourceGroup")
48+
err = cmd.Run()
49+
assert.NoError(t, err, "Function App does not exist. Deployment failed.")
50+
51+
// Verify deployed functions and POST requests
52+
t.Log("Verifying deployed functions...")
53+
for i, function := range functions {
54+
// Construct function URL
55+
functionURL := fmt.Sprintf("https://invitrofunctionapp.azurewebsites.net/api/function%d", i)
56+
57+
// Prepare JSON payload
58+
payload := map[string]interface{}{
59+
"RuntimeInMilliSec": 2000,
60+
"MemoryInMebiBytes": 256,
61+
}
62+
payloadBytes, _ := json.Marshal(payload)
63+
64+
// Create POST request
65+
req, err := http.NewRequest("POST", functionURL, bytes.NewBuffer(payloadBytes))
66+
assert.NoError(t, err, "Failed to create POST request for function: "+function.Name)
67+
req.Header.Set("Content-Type", "application/json")
68+
69+
// Send POST request
70+
client := &http.Client{}
71+
resp, err := client.Do(req)
72+
73+
// Retry if not immediately accessible
74+
if err != nil || resp.StatusCode != http.StatusOK {
75+
t.Logf("Function %s is not immediately accessible. Retrying...", function.Name)
76+
time.Sleep(5 * time.Second)
77+
resp, err = client.Do(req)
78+
}
79+
80+
// Validate response
81+
assert.NoError(t, err, "Failed to access deployed function: "+function.Name)
82+
assert.Equal(t, http.StatusOK, resp.StatusCode, "Deployed function is not accessible: "+function.Name)
83+
84+
// Parse and validate response body
85+
var response map[string]interface{}
86+
err = json.NewDecoder(resp.Body).Decode(&response)
87+
assert.NoError(t, err, "Failed to decode JSON response from function: "+function.Name)
88+
t.Logf("Response from function %s: %v", function.Name, response)
89+
90+
// Check response fields
91+
assert.Equal(t, "Success", response["Status"])
92+
assert.Contains(t, response, "DurationInMicroSec")
93+
assert.Contains(t, response, "ExecutionTime")
94+
95+
resp.Body.Close()
96+
}
97+
98+
/* Cleanup Tests */
99+
100+
// Cleanup resources
101+
t.Log("Testing `CleanAzureFunctions`...")
102+
deployment.CleanAzureFunctions()
103+
104+
// Verify Cleanup
105+
t.Log("Verifying cleanup...")
106+
// Check that temporary files and folders are removed
107+
_, err = os.Stat("azure_functions_for_zip")
108+
assert.True(t, os.IsNotExist(err), "Temp directory should be deleted")
109+
_, err = os.Stat("azurefunctions.zip")
110+
assert.True(t, os.IsNotExist(err), "Temp zip file should be deleted")
111+
112+
// Verify that the Azure resource group no longer exists
113+
cmd = exec.Command("az", "group", "exists", "--name", "ExperimentResourceGroup")
114+
output, err := cmd.Output()
115+
assert.NoError(t, err, "Failed to check Azure resource group existence")
116+
assert.Equal(t, "false\n", string(output), "Resource group should be deleted")
117+
118+
t.Log("End-to-End test and cleanup completed successfully.")
119+
}

0 commit comments

Comments
 (0)