Add tests for setTenantMonitoring (#2608)
This commit is contained in:
2
.github/workflows/jobs.yaml
vendored
2
.github/workflows/jobs.yaml
vendored
@@ -1539,7 +1539,7 @@ jobs:
|
||||
go tool cover -func=all.out | grep total > tmp2
|
||||
result=`cat tmp2 | awk 'END {print $3}'`
|
||||
result=${result%\%}
|
||||
threshold=67.2
|
||||
threshold=67.7
|
||||
echo "Result:"
|
||||
echo "$result%"
|
||||
if (( $(echo "$result >= $threshold" |bc -l) )); then
|
||||
|
||||
@@ -2034,7 +2034,10 @@ func setTenantMonitoringResponse(session *models.Principal, params operator_api.
|
||||
if err != nil {
|
||||
return false, restapi.ErrorWithContext(ctx, err, restapi.ErrUnableToGetTenantUsage)
|
||||
}
|
||||
return setTenantMonitoring(ctx, minTenant, opClient, params)
|
||||
}
|
||||
|
||||
func setTenantMonitoring(ctx context.Context, minTenant *miniov2.Tenant, opClient OperatorClientI, params operator_api.SetTenantMonitoringParams) (bool, *models.Error) {
|
||||
if params.Data.Toggle {
|
||||
if params.Data.PrometheusEnabled {
|
||||
minTenant.Spec.Prometheus = nil
|
||||
@@ -2046,7 +2049,7 @@ func setTenantMonitoringResponse(session *models.Principal, params operator_api.
|
||||
Image: promImage,
|
||||
}
|
||||
}
|
||||
_, err = opClient.TenantUpdate(ctx, minTenant, metav1.UpdateOptions{})
|
||||
_, err := opClient.TenantUpdate(ctx, minTenant, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return false, restapi.ErrorWithContext(ctx, err)
|
||||
}
|
||||
|
||||
@@ -1179,6 +1179,99 @@ func (suite *TenantTestSuite) TestSetTenantMonitoringHandlerWithError() {
|
||||
suite.assert.True(ok)
|
||||
}
|
||||
|
||||
func (suite *TenantTestSuite) TestSetTenantMonitoringWithTogglePrometheusError() {
|
||||
opClientTenantUpdateMock = func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) {
|
||||
return nil, errors.New("mock-tenant-update-error")
|
||||
}
|
||||
params, _ := suite.initSetTenantMonitoringRequest()
|
||||
params.Data = &models.TenantMonitoringInfo{
|
||||
Toggle: true,
|
||||
}
|
||||
tenant := &miniov2.Tenant{}
|
||||
ok, err := setTenantMonitoring(context.Background(), tenant, suite.opClient, params)
|
||||
suite.assert.False(ok)
|
||||
suite.assert.NotNil(err)
|
||||
}
|
||||
|
||||
func (suite *TenantTestSuite) TestSetTenantMonitoringWithTogglePrometheusNoError() {
|
||||
opClientTenantUpdateMock = func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) {
|
||||
return nil, nil
|
||||
}
|
||||
params, _ := suite.initSetTenantMonitoringRequest()
|
||||
params.Data = &models.TenantMonitoringInfo{
|
||||
Toggle: true,
|
||||
}
|
||||
tenant := &miniov2.Tenant{}
|
||||
ok, err := setTenantMonitoring(context.Background(), tenant, suite.opClient, params)
|
||||
suite.assert.True(ok)
|
||||
suite.assert.Nil(err)
|
||||
}
|
||||
|
||||
func (suite *TenantTestSuite) TestSetTenantMonitoringWithTenantUpdateError() {
|
||||
opClientTenantUpdateMock = func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) {
|
||||
return nil, errors.New("mock-tenant-update-error")
|
||||
}
|
||||
runAsUser := "1000"
|
||||
runAsGroup := "1000"
|
||||
fsGroup := "1000"
|
||||
params, _ := suite.initSetTenantMonitoringRequest()
|
||||
params.Data = &models.TenantMonitoringInfo{
|
||||
Labels: []*models.Label{{
|
||||
Key: "mock-label",
|
||||
Value: "mock-value",
|
||||
}},
|
||||
Annotations: []*models.Annotation{{
|
||||
Key: "mock-annotation",
|
||||
Value: "mock-value",
|
||||
}},
|
||||
NodeSelector: []*models.NodeSelector{{
|
||||
Key: "mock-annotation",
|
||||
Value: "mock-value",
|
||||
}},
|
||||
MonitoringCPURequest: "1",
|
||||
MonitoringMemRequest: "1Gi",
|
||||
DiskCapacityGB: "1Gi",
|
||||
SecurityContext: &models.SecurityContext{
|
||||
RunAsUser: &runAsUser,
|
||||
RunAsGroup: &runAsGroup,
|
||||
FsGroup: fsGroup,
|
||||
},
|
||||
}
|
||||
tenant := &miniov2.Tenant{
|
||||
Spec: miniov2.TenantSpec{
|
||||
Prometheus: &miniov2.PrometheusConfig{},
|
||||
},
|
||||
}
|
||||
ok, err := setTenantMonitoring(context.Background(), tenant, suite.opClient, params)
|
||||
suite.assert.False(ok)
|
||||
suite.assert.NotNil(err)
|
||||
}
|
||||
|
||||
func (suite *TenantTestSuite) TestSetTenantMonitoringWithoutError() {
|
||||
opClientTenantUpdateMock = func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) {
|
||||
return nil, nil
|
||||
}
|
||||
runAsUser := "1000"
|
||||
runAsGroup := "1000"
|
||||
fsGroup := "1000"
|
||||
params, _ := suite.initSetTenantMonitoringRequest()
|
||||
params.Data = &models.TenantMonitoringInfo{
|
||||
SecurityContext: &models.SecurityContext{
|
||||
RunAsUser: &runAsUser,
|
||||
RunAsGroup: &runAsGroup,
|
||||
FsGroup: fsGroup,
|
||||
},
|
||||
}
|
||||
tenant := &miniov2.Tenant{
|
||||
Spec: miniov2.TenantSpec{
|
||||
Prometheus: &miniov2.PrometheusConfig{},
|
||||
},
|
||||
}
|
||||
ok, err := setTenantMonitoring(context.Background(), tenant, suite.opClient, params)
|
||||
suite.assert.True(ok)
|
||||
suite.assert.Nil(err)
|
||||
}
|
||||
|
||||
func (suite *TenantTestSuite) initSetTenantMonitoringRequest() (params operator_api.SetTenantMonitoringParams, api operations.OperatorAPI) {
|
||||
registerTenantHandlers(&api)
|
||||
params.HTTPRequest = &http.Request{}
|
||||
@@ -1362,7 +1455,9 @@ func (suite *TenantTestSuite) TestUpdateTenantPoolsWithoutError() {
|
||||
}},
|
||||
},
|
||||
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.PreferredSchedulingTerm{{
|
||||
Preference: corev1.NodeSelectorTerm{},
|
||||
Preference: corev1.NodeSelectorTerm{
|
||||
MatchFields: []corev1.NodeSelectorRequirement{{}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
PodAffinity: &corev1.PodAffinity{
|
||||
|
||||
Reference in New Issue
Block a user