|
| 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