2020-04-15 08:20:14 +03:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
// Licensed under the Apache License 2.0.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2024-09-25 01:35:34 +03:00
|
|
|
"go.uber.org/mock/gomock"
|
2020-04-15 08:20:14 +03:00
|
|
|
|
|
|
|
"github.com/Azure/ARO-RP/pkg/api"
|
|
|
|
"github.com/Azure/ARO-RP/pkg/env"
|
2020-08-10 17:36:51 +03:00
|
|
|
"github.com/Azure/ARO-RP/pkg/frontend/adminactions"
|
2020-04-15 08:20:14 +03:00
|
|
|
"github.com/Azure/ARO-RP/pkg/metrics/noop"
|
2020-08-10 17:36:51 +03:00
|
|
|
mock_adminactions "github.com/Azure/ARO-RP/pkg/util/mocks/adminactions"
|
2020-09-21 10:04:35 +03:00
|
|
|
testdatabase "github.com/Azure/ARO-RP/test/database"
|
2020-04-15 08:20:14 +03:00
|
|
|
)
|
|
|
|
|
2020-04-23 22:56:56 +03:00
|
|
|
func TestAdminRedeployVM(t *testing.T) {
|
2020-04-15 08:20:14 +03:00
|
|
|
mockSubID := "00000000-0000-0000-0000-000000000000"
|
2020-07-07 08:50:35 +03:00
|
|
|
mockTenantID := "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
2020-04-15 08:20:14 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
resourceID string
|
2020-09-21 10:04:35 +03:00
|
|
|
fixture func(*testdatabase.Fixture)
|
2020-04-15 08:20:14 +03:00
|
|
|
vmName string
|
2021-01-13 22:58:40 +03:00
|
|
|
mocks func(*test, *mock_adminactions.MockAzureActions)
|
2020-04-15 08:20:14 +03:00
|
|
|
wantStatusCode int
|
2020-09-04 20:27:03 +03:00
|
|
|
wantResponse []byte
|
2020-04-15 08:20:14 +03:00
|
|
|
wantError string
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range []*test{
|
|
|
|
{
|
|
|
|
name: "basic coverage",
|
|
|
|
vmName: "aro-worker-australiasoutheast-7tcq7",
|
2020-09-21 10:04:35 +03:00
|
|
|
resourceID: testdatabase.GetResourcePath(mockSubID, "resourceName"),
|
|
|
|
fixture: func(f *testdatabase.Fixture) {
|
2020-10-14 00:51:49 +03:00
|
|
|
f.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{
|
2020-09-21 10:04:35 +03:00
|
|
|
Key: strings.ToLower(testdatabase.GetResourcePath(mockSubID, "resourceName")),
|
2020-04-15 08:20:14 +03:00
|
|
|
OpenShiftCluster: &api.OpenShiftCluster{
|
2020-09-21 10:04:35 +03:00
|
|
|
ID: testdatabase.GetResourcePath(mockSubID, "resourceName"),
|
2020-04-15 08:20:14 +03:00
|
|
|
Properties: api.OpenShiftClusterProperties{
|
|
|
|
ClusterProfile: api.ClusterProfile{
|
|
|
|
ResourceGroupID: fmt.Sprintf("/subscriptions/%s/resourceGroups/test-cluster", mockSubID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-21 10:04:35 +03:00
|
|
|
})
|
|
|
|
|
2020-10-14 00:51:49 +03:00
|
|
|
f.AddSubscriptionDocuments(&api.SubscriptionDocument{
|
2020-07-07 08:50:35 +03:00
|
|
|
ID: mockSubID,
|
|
|
|
Subscription: &api.Subscription{
|
|
|
|
State: api.SubscriptionStateRegistered,
|
|
|
|
Properties: &api.SubscriptionProperties{
|
|
|
|
TenantID: mockTenantID,
|
|
|
|
},
|
|
|
|
},
|
2020-09-21 10:04:35 +03:00
|
|
|
})
|
|
|
|
},
|
2021-01-13 22:58:40 +03:00
|
|
|
mocks: func(tt *test, a *mock_adminactions.MockAzureActions) {
|
2020-08-10 17:36:51 +03:00
|
|
|
a.EXPECT().VMRedeployAndWait(gomock.Any(), tt.vmName).Return(nil)
|
2020-04-15 08:20:14 +03:00
|
|
|
},
|
|
|
|
wantStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-09-29 08:16:20 +03:00
|
|
|
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions()
|
2020-09-04 20:27:03 +03:00
|
|
|
defer ti.done()
|
2020-04-15 08:20:14 +03:00
|
|
|
|
2021-01-13 22:58:40 +03:00
|
|
|
a := mock_adminactions.NewMockAzureActions(ti.controller)
|
2020-09-21 10:04:35 +03:00
|
|
|
tt.mocks(tt, a)
|
|
|
|
|
2020-09-29 08:16:20 +03:00
|
|
|
err := ti.buildFixtures(tt.fixture)
|
2020-09-21 10:04:35 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-18 01:25:32 +03:00
|
|
|
f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster, *api.SubscriptionDocument) (adminactions.AzureActions, error) {
|
2020-08-10 17:36:51 +03:00
|
|
|
return a, nil
|
2024-07-15 09:32:23 +03:00
|
|
|
}, nil, nil)
|
2020-04-15 08:20:14 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go f.Run(ctx, nil, nil)
|
|
|
|
|
2020-09-04 20:27:03 +03:00
|
|
|
resp, b, err := ti.request(http.MethodPost,
|
|
|
|
fmt.Sprintf("https://server/admin%s/redeployvm?vmName=%s", tt.resourceID, tt.vmName),
|
|
|
|
nil, nil)
|
2020-09-21 10:04:35 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-04-15 08:20:14 +03:00
|
|
|
|
2020-09-04 20:27:03 +03:00
|
|
|
err = validateResponse(resp, b, tt.wantStatusCode, tt.wantError, tt.wantResponse)
|
2020-04-15 08:20:14 +03:00
|
|
|
if err != nil {
|
2020-09-04 20:27:03 +03:00
|
|
|
t.Error(err)
|
2020-04-15 08:20:14 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|