Skip to content

Commit

Permalink
test: fix failed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
choujimmy committed May 12, 2020
1 parent de3e051 commit cd9c729
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 270 deletions.
21 changes: 11 additions & 10 deletions pkg/business/registry/namespace/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package namespace

import (
"context"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestClusterLimitation(t *testing.T) {
_testNamespace.Spec.ClusterName = ClusterName

hasError := false
errors := ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors := ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), _clusterLimitErrorInfo) {
hasError = true
Expand All @@ -124,7 +125,7 @@ func TestClusterLimitation(t *testing.T) {
_testProject.Spec.Clusters = business.ClusterHard{
ClusterName: {},
}
errors = ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors = ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand All @@ -143,7 +144,7 @@ func TestQuotaLimitation(t *testing.T) {
_testNamespace.Spec.Hard = business.ResourceList{}

hasError := false
errors := ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors := ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.QuotaLimitErrorInfo) {
hasError = true
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestCreateAllocatable(t *testing.T) {
}

hasError := false
errors := ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors := ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.AllocatableErrorInfo) {
hasError = true
Expand All @@ -186,7 +187,7 @@ func TestCreateAllocatable(t *testing.T) {

quota, _ = apimachineryresource.ParseQuantity("1")
_testNamespace.Spec.Hard["requests.cpu"] = quota
errors = ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors = ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand Down Expand Up @@ -223,7 +224,7 @@ func TestUpdateAllocatable(t *testing.T) {
}

hasError := false
errors := ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors := ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.AllocatableErrorInfo) {
hasError = true
Expand All @@ -237,7 +238,7 @@ func TestUpdateAllocatable(t *testing.T) {

quota, _ = apimachineryresource.ParseQuantity("6")
_testNamespace.Spec.Hard["requests.cpu"] = quota
errors = ValidateNamespaceUpdate(&_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
errors = ValidateNamespaceUpdate(context.Background(), &_testNamespace, &_oldNamespace, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand All @@ -253,15 +254,15 @@ func newClusterGetter() validation.ClusterGetter {

type testObjectGetter struct{}

func (getter testObjectGetter) Project(name string, options apimachinerymetav1.GetOptions) (*business.Project, error) {
func (getter testObjectGetter) Project(ctx context.Context, name string, options apimachinerymetav1.GetOptions) (*business.Project, error) {
project, has := _projectMap[name]
if has {
return project, nil
}
return nil, fmt.Errorf("failed to get project by name '%s'", name)
}

func (getter testObjectGetter) Namespace(project, name string, options apimachinerymetav1.GetOptions) (*business.Namespace, error) {
func (getter testObjectGetter) Namespace(ctx context.Context, project, name string, options apimachinerymetav1.GetOptions) (*business.Namespace, error) {
namespace, has := _namespaceMap[name]
if has {
return namespace, nil
Expand All @@ -271,7 +272,7 @@ func (getter testObjectGetter) Namespace(project, name string, options apimachin

type testClusterGetter struct{}

func (getter testClusterGetter) Cluster(name string, options apimachinerymetav1.GetOptions) (*platformv1.Cluster, error) {
func (getter testClusterGetter) Cluster(ctx context.Context, name string, options apimachinerymetav1.GetOptions) (*platformv1.Cluster, error) {
cluster, has := _clusterMap[name]
if has {
return cluster, nil
Expand Down
33 changes: 17 additions & 16 deletions pkg/business/registry/project/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package project

import (
"context"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestClusterLimitation(t *testing.T) {
}

hasError := false
errors := ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), _clusterLimitErrorInfo) {
hasError = true
Expand All @@ -145,7 +146,7 @@ func TestClusterLimitation(t *testing.T) {
InvalidCluster: {},
ClusterName: {},
}
errors = ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand All @@ -166,7 +167,7 @@ func TestQuotaLimitation(t *testing.T) {
}

hasError := false
errors := ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.QuotaLimitErrorInfo) {
hasError = true
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestCreateAllocatable(t *testing.T) {
}

hasError := false
errors := ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.AllocatableErrorInfo) {
hasError = true
Expand All @@ -213,7 +214,7 @@ func TestCreateAllocatable(t *testing.T) {

quota, _ = apimachineryresource.ParseQuantity("1")
_childProject.Spec.Clusters[ClusterName].Hard["requests.cpu"] = quota
errors = ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand Down Expand Up @@ -258,7 +259,7 @@ func TestUpdateAllocatable(t *testing.T) {
}

hasError := false
errors := ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.AllocatableErrorInfo) {
hasError = true
Expand All @@ -272,7 +273,7 @@ func TestUpdateAllocatable(t *testing.T) {

quota, _ = apimachineryresource.ParseQuantity("6")
_childProject.Spec.Clusters[ClusterName].Hard["requests.cpu"] = quota
errors = ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestUpdateQuota(t *testing.T) {
}

hasError := false
errors := ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), resource.UpdateQuotaErrorInfo) {
hasError = true
Expand All @@ -314,7 +315,7 @@ func TestUpdateQuota(t *testing.T) {

quota, _ = apimachineryresource.ParseQuantity("7")
_childProject.Spec.Clusters[ClusterName].Hard["requests.cpu"] = quota
errors = ValidateProjectUpdate(&_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_childProject, &_oldChildProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand Down Expand Up @@ -344,7 +345,7 @@ func TestNewQuota(t *testing.T) {

_parentProject.Status.CalculatedChildProjects = []string{_childProject.Name}
hasError := false
errors := ValidateProjectUpdate(&_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
errors := ValidateProjectUpdate(context.Background(), &_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), _addNewQuotaErrorInfo) {
hasError = true
Expand All @@ -363,14 +364,14 @@ func TestNewQuota(t *testing.T) {
},
},
}
errors = ValidateProjectUpdate(&_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}

_parentProject.Status.CalculatedNamespaces = []string{_childNamespace.Name}
hasError = false
errors = ValidateProjectUpdate(&_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
if strings.Contains(err.Error(), _addNewQuotaErrorInfo) {
hasError = true
Expand All @@ -385,7 +386,7 @@ func TestNewQuota(t *testing.T) {
"pods": quota,
"requests.cpu": quota,
}
errors = ValidateProjectUpdate(&_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
errors = ValidateProjectUpdate(context.Background(), &_parentProject, &_parentProject, newObjectGetter(), newClusterGetter())
for _, err := range errors {
t.Errorf("Unexpected: %s", err.Error())
}
Expand All @@ -401,15 +402,15 @@ func newClusterGetter() validation.ClusterGetter {

type testObjectGetter struct{}

func (getter testObjectGetter) Project(name string, options apimachinerymetav1.GetOptions) (*business.Project, error) {
func (getter testObjectGetter) Project(ctx context.Context, name string, options apimachinerymetav1.GetOptions) (*business.Project, error) {
project, has := _projectMap[name]
if has {
return project, nil
}
return nil, fmt.Errorf("failed to get project by name '%s'", name)
}

func (getter testObjectGetter) Namespace(project, name string, options apimachinerymetav1.GetOptions) (*business.Namespace, error) {
func (getter testObjectGetter) Namespace(ctx context.Context, project, name string, options apimachinerymetav1.GetOptions) (*business.Namespace, error) {
namespace, has := _namespaceMap[name]
if has {
return namespace, nil
Expand All @@ -419,7 +420,7 @@ func (getter testObjectGetter) Namespace(project, name string, options apimachin

type testClusterGetter struct{}

func (getter testClusterGetter) Cluster(name string, options apimachinerymetav1.GetOptions) (*platformv1.Cluster, error) {
func (getter testClusterGetter) Cluster(ctx context.Context, name string, options apimachinerymetav1.GetOptions) (*platformv1.Cluster, error) {
cluster, has := _clusterMap[name]
if has {
return cluster, nil
Expand Down
21 changes: 19 additions & 2 deletions pkg/gateway/apis/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package config

import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"reflect"
"strings"
"testing"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestGatewayConfigurationPathFields(t *testing.T) {
Expand Down Expand Up @@ -153,6 +154,22 @@ var (
"Components.Notify.Passthrough.CAFile",
"Components.Platform.Passthrough.CAFile",
"Components.Registry.Passthrough.CAFile",
"Components.Audit.Address",
"Components.Audit.FrontProxy.CAFile",
"Components.Audit.FrontProxy.ClientCertFile",
"Components.Audit.FrontProxy.ClientKeyFile",
"Components.Audit.FrontProxy.ExtraPrefixHeader",
"Components.Audit.FrontProxy.GroupsHeader",
"Components.Audit.FrontProxy.UsernameHeader",
"Components.Audit.Passthrough.CAFile",
"Components.LogAgent.Address",
"Components.LogAgent.FrontProxy.CAFile",
"Components.LogAgent.FrontProxy.ClientCertFile",
"Components.LogAgent.FrontProxy.ClientKeyFile",
"Components.LogAgent.FrontProxy.ExtraPrefixHeader",
"Components.LogAgent.FrontProxy.GroupsHeader",
"Components.LogAgent.FrontProxy.UsernameHeader",
"Components.LogAgent.Passthrough.CAFile",
)

// GatewayConfiguration fields that do not contain file paths.
Expand Down
6 changes: 4 additions & 2 deletions pkg/monitor/apis/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package config

import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"reflect"
"strings"
"testing"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestMonitorConfigurationPathFields(t *testing.T) {
Expand Down Expand Up @@ -141,5 +142,6 @@ var (
"Storage.InfluxDB.Servers[*].Password",
"Storage.InfluxDB.Servers[*].TimeoutSeconds",
"Storage.InfluxDB.Servers[*].Username",
"Storage.Thanos.Servers[*].Address",
)
)
3 changes: 2 additions & 1 deletion pkg/monitor/services/alertmanager/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package alertmanager

import (
"context"
"time"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -96,7 +97,7 @@ func createProcessorServer() (kubernetes.Interface, services.RouteProcessor, str
}

util.ClusterNameToClient.Store(testClusterName, k8sClient)
_, _ = k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(configMap)
_, _ = k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(context.Background(), configMap, metav1.CreateOptions{})
// Because we have set kubernetes client, so set nil is ok
p := NewProcessor(nil)

Expand Down
Loading

0 comments on commit cd9c729

Please sign in to comment.