-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] By using setup-envtest, setup k8s environment for unit …
…test (#347) Signed-off-by: yandongxiao <[email protected]>
- Loading branch information
1 parent
a56309a
commit f82f706
Showing
11 changed files
with
295 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package pkg | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
v1 "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1" | ||
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils/fake" | ||
) | ||
|
||
func TestSetupClusterReconciler(t *testing.T) { | ||
type args struct { | ||
mgr controllerruntime.Manager | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test setup cluster reconciler", | ||
args: args{ | ||
mgr: func() controllerruntime.Manager { | ||
env := fake.NewEnvironment(fake.WithClusterCRD()) | ||
defer func() { | ||
err := env.Stop() | ||
assert.Nil(t, err) | ||
}() | ||
return fake.NewManager(env) | ||
}(), | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
v1.Register() | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := SetupClusterReconciler(tt.args.mgr); (err != nil) != tt.wantErr { | ||
t.Errorf("SetupClusterReconciler() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSetupWarehouseReconciler(t *testing.T) { | ||
env1 := fake.NewEnvironment(fake.WithClusterCRD()) | ||
env2 := fake.NewEnvironment(fake.WithClusterCRD(), fake.WithWarehouseCRD()) | ||
defer func() { | ||
err := env1.Stop() | ||
assert.Nil(t, err) | ||
err = env2.Stop() | ||
assert.Nil(t, err) | ||
}() | ||
|
||
type args struct { | ||
mgr controllerruntime.Manager | ||
ctx context.Context | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test setup warehouse reconciler with no warehouse CRD", | ||
args: args{ | ||
mgr: func() controllerruntime.Manager { | ||
env1 = fake.NewEnvironment(fake.WithClusterCRD()) | ||
return fake.NewManager(env1) | ||
}(), | ||
ctx: context.Background(), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test setup warehouse reconciler with warehouse CRD", | ||
args: args{ | ||
mgr: func() controllerruntime.Manager { | ||
return fake.NewManager(env2) | ||
}(), | ||
ctx: context.Background(), | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
v1.Register() | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := SetupWarehouseReconciler(tt.args.ctx, tt.args.mgr); (err != nil) != tt.wantErr { | ||
t.Errorf("SetupWarehouseReconciler() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type Reader struct { | ||
hasWarehouseCRD bool | ||
} | ||
|
||
func (r Reader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { | ||
return nil | ||
} | ||
|
||
func (r Reader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { | ||
if r.hasWarehouseCRD { | ||
return nil | ||
} | ||
return &meta.NoKindMatchError{} | ||
} | ||
|
||
var _ client.Reader = &Reader{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package fake | ||
|
||
import ( | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
v1 "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1" | ||
) | ||
|
||
type WithCRD func() *apiextensionsv1.CustomResourceDefinition | ||
|
||
var ( | ||
clusterCRD = &apiextensionsv1.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "starrocksclusters.starrocks.com", | ||
}, | ||
Spec: apiextensionsv1.CustomResourceDefinitionSpec{ | ||
Group: "starrocks.com", | ||
Names: apiextensionsv1.CustomResourceDefinitionNames{ | ||
Plural: "starrocksclusters", | ||
Singular: "starrockscluster", | ||
Kind: "StarRocksCluster", | ||
}, | ||
Scope: apiextensionsv1.NamespaceScoped, | ||
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ | ||
{ | ||
Name: "v1", | ||
Served: true, | ||
Storage: true, | ||
Schema: &apiextensionsv1.CustomResourceValidation{ | ||
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ | ||
Type: "object", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
warehouseCRD = &apiextensionsv1.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "starrockswarehouses.starrocks.com", | ||
}, | ||
Spec: apiextensionsv1.CustomResourceDefinitionSpec{ | ||
Group: "starrocks.com", | ||
Names: apiextensionsv1.CustomResourceDefinitionNames{ | ||
Plural: "starrockswarehouses", | ||
Singular: "starrockswarehouse", | ||
Kind: "StarRocksWarehouse", | ||
}, | ||
Scope: apiextensionsv1.NamespaceScoped, | ||
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ | ||
{ | ||
Name: "v1", | ||
Served: true, | ||
Storage: true, | ||
Schema: &apiextensionsv1.CustomResourceValidation{ | ||
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ | ||
Type: "object", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
// NewEnvironment is used to create a fake manager, code inspired from pkg/manager/internal/integration/manager_test.go | ||
// see https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/manager/internal/integration/manager_test.go | ||
// for more information | ||
func NewEnvironment(crds ...WithCRD) *envtest.Environment { | ||
env := &envtest.Environment{ | ||
Scheme: v1.Scheme, | ||
CRDInstallOptions: envtest.CRDInstallOptions{}, | ||
} | ||
for i := range crds { | ||
env.CRDInstallOptions.CRDs = append(env.CRDInstallOptions.CRDs, crds[i]()) | ||
} | ||
return env | ||
} | ||
|
||
func WithClusterCRD() WithCRD { | ||
return func() *apiextensionsv1.CustomResourceDefinition { | ||
return clusterCRD | ||
} | ||
} | ||
|
||
func WithWarehouseCRD() WithCRD { | ||
return func() *apiextensionsv1.CustomResourceDefinition { | ||
return warehouseCRD | ||
} | ||
} | ||
|
||
func NewManager(env *envtest.Environment) ctrl.Manager { | ||
_, err := env.Start() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
mgr, err := manager.New(env.Config, manager.Options{ | ||
MetricsBindAddress: "0", | ||
Scheme: v1.Scheme, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return mgr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package load | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
v1 "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1" | ||
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/common/resource_utils" | ||
) | ||
|
||
func TestSelector(t *testing.T) { | ||
type args struct { | ||
clusterName string | ||
spec v1.SpecInterface | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want resource_utils.Labels | ||
}{ | ||
{ | ||
name: "test selector", | ||
args: args{ | ||
clusterName: "kube-starrocks", | ||
spec: (*v1.StarRocksFeProxySpec)(nil), | ||
}, | ||
want: map[string]string{ | ||
"app.kubernetes.io/component": "fe-proxy", | ||
"app.starrocks.ownerreference/name": "kube-starrocks-fe-proxy", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Selector(tt.args.clusterName, tt.args.spec); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Selector() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.