Newer
Older
"code.fbi.h-da.de/cocsn/gosdn/mocks"
"errors"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
"net/http"
pnd, err = NewPND("test", "test pnd", defaultPndID, sbi)
if err != nil {
log.Fatal(err)
}
d = mockDevice()
tr := d.Transport.(*mocks.Transport)
mockError := errors.New("mock error")
tr.On("Get", mockContext, "/system/config/hostname").Return(mock.Anything, nil)
tr.On("Get", mockContext, "error").Return(mock.Anything, mockError)
tr.On("Set", mockContext, mock.Anything).Return(mock.Anything, nil)
tr.On("ProcessResponse", mock.Anything, mock.Anything, mock.Anything).Return(nil)
if err := pnd.AddDevice(&d); err != nil {
log.Fatal(err)
}
args = "&uuid=" + mdid.String() + "&pnd=" + defaultPndID.String() + "&sbi=" + defaultSbiID.String()
argsNotFound = "&uuid=" + uuid.New().String() + "&pnd=" + defaultPndID.String() + "&sbi=" + defaultSbiID.String()
if err := c.sbic.add(sbi); err != nil {
log.Fatal(err)
}
if err := c.pndc.add(pnd); err != nil {
log.Fatal(err)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{
name: "liveliness indicator",
request: apiEndpoint + "/livez",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "readyness indicator",
request: apiEndpoint + "/readyz",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "init",
request: apiEndpoint + "/api?q=init",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "get-ids",
request: apiEndpoint + "/api?q=getIDs",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "add-device",
request: apiEndpoint + "/api?q=addDevice" + args,
want: &http.Response{StatusCode: http.StatusCreated},
wantErr: false,
},
{
name: "request",
request: apiEndpoint + "/api?q=request" + args + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "request not found",
request: apiEndpoint + "/api?q=request" + argsNotFound + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusNotFound},
wantErr: false,
},
{
name: "request internal server error",
request: apiEndpoint + "/api?q=request" + args + "&path=error",
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
{
name: "request-all",
request: apiEndpoint + "/api?q=requestAll" + args + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "request-all internal server error",
request: apiEndpoint + "/api?q=requestAll" + args + "&path=error",
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
{
name: "get-device",
request: apiEndpoint + "/api?q=getDevice" + args,
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "get-device not found",
request: apiEndpoint + "/api?q=getDevice" + argsNotFound,
want: &http.Response{StatusCode: http.StatusNotFound},
wantErr: false,
},
{
name: "set",
request: apiEndpoint + "/api?q=update" + args + "&path=/system/config/hostname&value=ceos3000",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "replace",
request: apiEndpoint + "/api?q=replace" + args + "&path=/system/config/hostname&value=ceos3000",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "delete",
request: apiEndpoint + "/api?q=delete" + args + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "change list",
request: apiEndpoint + "/api?q=change-list" + args + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "change list pending",
request: apiEndpoint + "/api?q=change-list-pending" + args + "&path=/system/config/hostname",
want: &http.Response{StatusCode: http.StatusOK},
wantErr: false,
},
{
name: "change commit",
request: apiEndpoint + "/api?q=change-commit" + args + "&cuid=" + cuid.String(),
// TODO: Mock Change for testing
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
{
name: "change confirm",
request: apiEndpoint + "/api?q=change-confirm" + args + "&cuid=" + cuid.String(),
// TODO: Mock Change for testing
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
{
name: "bad request",
request: apiEndpoint + "/api?q=bad-request" + args,
want: &http.Response{StatusCode: http.StatusBadRequest},
wantErr: false,
},
{
name: "internal server errror: wrong pnd",
request: apiEndpoint + "/api?pnd=" + uuid.New().String(),
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
{
name: "internal server errror: wrong sbi",
request: apiEndpoint + "/api?sbi=" + uuid.New().String(),
want: &http.Response{StatusCode: http.StatusInternalServerError},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := http.Get(tt.request)
if (err != nil) != tt.wantErr {
t.Errorf("httpApi() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.StatusCode != tt.want.StatusCode {
t.Errorf("httpApi() got: %v, want %v", got.StatusCode, tt.want.StatusCode)
}
if tt.name == "add-device" {
for k := range pnd.(*pndImplementation).devices.store {
if k != mdid {
if err := pnd.RemoveDevice(k); err != nil {
t.Error(err)
return
}
}
}