ARO-RP/pkg/frontend/admin_openshiftcluster_rede...

111 строки
3.0 KiB
Go
Исходник Постоянная ссылка Обычный вид История

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"
"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"
"github.com/Azure/ARO-RP/pkg/frontend/adminactions"
2020-04-15 08:20:14 +03:00
"github.com/Azure/ARO-RP/pkg/metrics/noop"
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"
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) {
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
})
f.AddSubscriptionDocuments(&api.SubscriptionDocument{
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) {
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) {
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)
err := ti.buildFixtures(tt.fixture)
2020-09-21 10:04:35 +03:00
if err != nil {
t.Fatal(err)
}
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) {
return a, nil
}, 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
}
})
}
}