-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
62 lines (48 loc) · 1.57 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
)
type mockCloudWatchClient struct {
cloudwatchiface.CloudWatchAPI
}
func (m *mockCloudWatchClient) PutMetricData(input *cloudwatch.PutMetricDataInput) (*cloudwatch.PutMetricDataOutput, error) {
return &cloudwatch.PutMetricDataOutput{}, nil
}
func TestExportToCloudwatch(t *testing.T) {
mockSvc := &mockCloudWatchClient{}
mockPHPFPMStatus := PHPFPMStatus{
ListenQueue: 10,
ActiveProcesses: 15,
SlowRequests: 20,
}
_, err := ExportToCloudwatch(mockSvc, mockPHPFPMStatus, "test-service")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestGetContainerServiceName(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metadataResponse := MetadataResponse{
Cluster: "arn:aws:ecs:us-east-1:123456789012:cluster/test-cluster",
ServiceName: "mock-service",
TaskARN: "arn:aws:ecs:us-east-1:123456789012:task/test-cluster/6d0b6d6d-5f5d-4c6d-8f5d-6d0b6d6d5f5d",
}
json.NewEncoder(w).Encode(metadataResponse)
}))
defer mockServer.Close()
os.Setenv("ECS_CONTAINER_METADATA_URI_V4", mockServer.URL)
serviceName, err := GetContainerServiceName()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectedServiceName := "mock-service"
if serviceName != expectedServiceName {
t.Errorf("Expected service name: %s, but got: %s", expectedServiceName, serviceName)
}
}