-
Notifications
You must be signed in to change notification settings - Fork 59
/
mockTelemetry.go
107 lines (98 loc) · 4.22 KB
/
mockTelemetry.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package telemetry
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-telemetry/tmodel"
)
// MockIsAuthorized mocks IsAuthorized function
func MockIsAuthorized(ctx context.Context, sessionToken string, privileges, oemPrivileges []string) (response.RPC, error) {
if sessionToken == "InvalidToken" {
return common.GeneralError(http.StatusUnauthorized, response.NoValidSession, "error while trying to authenticate session", nil, nil), nil
}
return common.GeneralError(http.StatusOK, response.Success, "", nil, nil), nil
}
// MockContactClient mocks ContactClient function
func MockContactClient(ctx context.Context, url, method, token string, odataID string, body interface{}, loginCredential map[string]string) (*http.Response, error) {
if url == "https://localhost:9091/ODIM/v1/Sessions" {
body := `{"Token": "12345"}`
return &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
Header: http.Header{
"X-Auth-Token": []string{"12345"},
},
}, nil
} else if url == "https://localhost:9092/ODIM/v1/Sessions" {
body := `{"Token": ""}`
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}, nil
}
if url == "https://localhost:9091/ODIM/v1/TelemetryService/MetricReports/CPUUtilCustom1" && token == "12345" {
body := `{"@odata.id":"/redfish/v1/TelemetryService/MetricReports/CPUUtilCustom1","@odata.type":"#MetricReport.v1_0_0.MetricReport","Id":"CPUUtilCustom1","Name":"Metric report of CPU Utilization for 10 minutes with sensing interval of 20 seconds.","MetricReportDefinition":{"@odata.id":"/redfish/v1/TelemetryService/MetricReportDefinitions/CPUUtilCustom1"},"MetricValues":[{"MetricDefinition":{"@odata.id":"/redfish/v1/TelemetryService/MetricDefinitions/CPUUtil"},"MetricId":"CPUUtil","MetricValue":"0","Timestamp":"2021-06-16T07:59:43Z"},{"MetricDefinition":{"@odata.id":"/redfish/v1/TelemetryService/MetricDefinitions/CPUUtil"},"MetricId":"CPUUtil","MetricValue":"0","Timestamp":"2021-06-16T08:00:04Z"}]}`
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}, nil
}
return nil, fmt.Errorf("InvalidRequest")
}
// MockGetResource mocks GetResource function
func MockGetResource(ctx context.Context, table, key string, dbType common.DbType) (string, *errors.Error) {
if key == "error" {
return "", &errors.Error{}
}
return "body", nil
}
// MockGetAllKeysFromTable mocks GetAllKeysFromTable function
func MockGetAllKeysFromTable(ctx context.Context, table string, dbType common.DbType) ([]string, error) {
if table == "Plugin" {
return []string{"ILO", "GRF"}, nil
}
return []string{"/redfish/v1/TelemetryService/Triggers/uuid.1"}, nil
}
// GetEncryptedKey is used to encrypt device password using odimra public key for unit test
func GetEncryptedKey(t *testing.T, key []byte) []byte {
cryptedKey, err := common.EncryptWithPublicKey(key)
if err != nil {
t.Fatalf("error: failed to encrypt data: %v", err)
}
return cryptedKey
}
// MockGetPluginData mocks GetPluginData function
func MockGetPluginData(pluginID string) (tmodel.Plugin, *errors.Error) {
var t *testing.T
password := GetEncryptedKey(t, []byte("$2a$10$OgSUYvuYdI/7dLL5KkYNp.RCXISefftdj.MjbBTr6vWyNwAvht6ci"))
plugin := tmodel.Plugin{
IP: "localhost",
Port: "9091",
Username: "admin",
Password: password,
ID: pluginID,
PreferredAuthType: "XAuthToken",
PluginType: "Compute",
}
return plugin, nil
}
// MockGetExternalInterface mocks ExternalInterface for unit test
func MockGetExternalInterface() *ExternalInterface {
return &ExternalInterface{
External: External{
Auth: MockIsAuthorized,
ContactClient: MockContactClient,
GetPluginData: MockGetPluginData,
},
DB: DB{
GetAllKeysFromTable: MockGetAllKeysFromTable,
GetResource: MockGetResource,
},
}
}