Skip to content

Commit

Permalink
Refactoring to reuse MockLicenseChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Oct 7, 2021
1 parent 1b18890 commit e343bf3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 92 deletions.
32 changes: 8 additions & 24 deletions pkg/controller/autoscaling/elasticsearch/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withCapacity("frozen-tier"),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "frozen-tier",
Expand All @@ -109,7 +109,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withCapacity("ml"),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "ml",
Expand All @@ -124,7 +124,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withErrorOnDeleteAutoscalingAutoscalingPolicies(),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "min-nodes-increased-by-user",
Expand All @@ -139,7 +139,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withCapacity("empty-autoscaling-api-response"),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "empty-autoscaling-api-response",
Expand All @@ -152,7 +152,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "cluster-creation",
Expand All @@ -165,7 +165,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withCapacity("max-storage-reached"),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "max-storage-reached",
Expand All @@ -182,7 +182,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t).withCapacity("storage-scaled-horizontally"),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "storage-scaled-horizontally",
Expand All @@ -195,7 +195,7 @@ func TestReconcile(t *testing.T) {
fields: fields{
EsClient: newFakeEsClient(t),
recorder: record.NewFakeRecorder(1000),
licenseChecker: &fakeLicenceChecker{},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
},
args: args{
esManifest: "",
Expand Down Expand Up @@ -374,19 +374,3 @@ func (f *fakeEsClient) GetAutoscalingCapacity(_ context.Context) (esclient.Autos
func (f *fakeEsClient) UpdateMLNodesSettings(_ context.Context, maxLazyMLNodes int32, maxMemory string) error {
return nil
}

// - Fake licence checker

type fakeLicenceChecker struct{}

func (flc *fakeLicenceChecker) CurrentEnterpriseLicense() (*license.EnterpriseLicense, error) {
return nil, nil
}

func (flc *fakeLicenceChecker) EnterpriseFeaturesEnabled() (bool, error) {
return true, nil
}

func (flc *fakeLicenceChecker) Valid(l license.EnterpriseLicense) (bool, error) {
return true, nil
}
20 changes: 10 additions & 10 deletions pkg/controller/common/license/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,24 @@ func (lc checker) ValidOperatorLicenseKey() (OperatorLicenseType, error) {
return licType, nil
}

type MockChecker struct {
MissingLicense bool
type MockLicenseChecker struct {
EnterpriseEnabled bool
}

func (m MockChecker) CurrentEnterpriseLicense() (*EnterpriseLicense, error) {
func (m MockLicenseChecker) CurrentEnterpriseLicense() (*EnterpriseLicense, error) {
return &EnterpriseLicense{}, nil
}

func (m MockChecker) EnterpriseFeaturesEnabled() (bool, error) {
return !m.MissingLicense, nil
func (m MockLicenseChecker) EnterpriseFeaturesEnabled() (bool, error) {
return m.EnterpriseEnabled, nil
}

func (m MockChecker) Valid(l EnterpriseLicense) (bool, error) {
return !m.MissingLicense, nil
func (m MockLicenseChecker) Valid(l EnterpriseLicense) (bool, error) {
return m.EnterpriseEnabled, nil
}

func (m MockChecker) ValidOperatorLicenseKey() (OperatorLicenseType, error) {
return LicenseTypeBasic, nil
func (m MockLicenseChecker) ValidOperatorLicenseKey() (OperatorLicenseType, error) {
return LicenseTypeEnterprise, nil
}

var _ Checker = &MockChecker{}
var _ Checker = &MockLicenseChecker{}
38 changes: 11 additions & 27 deletions pkg/controller/elasticsearch/remotecluster/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ func newEsWithRemoteClusters(
}
}

type fakeLicenseChecker struct {
enterpriseFeaturesEnabled bool
}

func (fakeLicenseChecker) CurrentEnterpriseLicense() (*license.EnterpriseLicense, error) {
return nil, nil
}

func (f *fakeLicenseChecker) EnterpriseFeaturesEnabled() (bool, error) {
return f.enterpriseFeaturesEnabled, nil
}

func (fakeLicenseChecker) Valid(_ license.EnterpriseLicense) (bool, error) {
return true, nil
}

func TestUpdateSettings(t *testing.T) {
emptySettings := esclient.RemoteClustersSettings{PersistentSettings: &esclient.SettingsGroup{}}
type args struct {
Expand All @@ -144,7 +128,7 @@ func TestUpdateSettings(t *testing.T) {
name: "Nothing to create, nothing to delete",
args: args{
esClient: &fakeESClient{existingSettings: emptySettings},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand All @@ -158,7 +142,7 @@ func TestUpdateSettings(t *testing.T) {
name: "Empty annotation",
args: args{
esClient: &fakeESClient{existingSettings: emptySettings},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand All @@ -172,7 +156,7 @@ func TestUpdateSettings(t *testing.T) {
name: "Outdated annotation should be removed",
args: args{
esClient: &fakeESClient{existingSettings: emptySettings},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand All @@ -186,7 +170,7 @@ func TestUpdateSettings(t *testing.T) {
name: "Create a new remote cluster",
args: args{
esClient: &fakeESClient{existingSettings: emptySettings},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -215,7 +199,7 @@ func TestUpdateSettings(t *testing.T) {
esClient: &fakeESClient{
existingSettings: esclient.RemoteClustersSettings{PersistentSettings: &esclient.SettingsGroup{}},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -251,7 +235,7 @@ func TestUpdateSettings(t *testing.T) {
},
},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -290,7 +274,7 @@ func TestUpdateSettings(t *testing.T) {
},
},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -331,7 +315,7 @@ func TestUpdateSettings(t *testing.T) {
},
},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -374,7 +358,7 @@ func TestUpdateSettings(t *testing.T) {
},
},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down Expand Up @@ -404,7 +388,7 @@ func TestUpdateSettings(t *testing.T) {
name: "No valid license to create a new remote cluster",
args: args{
esClient: &fakeESClient{},
licenseChecker: &fakeLicenseChecker{false},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: false},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand All @@ -431,7 +415,7 @@ func TestUpdateSettings(t *testing.T) {
},
},
},
licenseChecker: &fakeLicenseChecker{true},
licenseChecker: &license.MockLicenseChecker{EnterpriseEnabled: true},
es: newEsWithRemoteClusters(
"ns1",
"es1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestReconcile(t *testing.T) {
c, stop := test.StartManager(t, func(mgr manager.Manager, p operator.Parameters) error {
r := &ReconcileLicenses{
Client: mgr.GetClient(),
checker: license.MockChecker{},
checker: license.MockLicenseChecker{},
}
c, err := common.NewController(mgr, name, r, p)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/license/license_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestReconcileLicenses_reconcileInternal(t *testing.T) {
client := k8s.NewFakeClient(tt.k8sResources...)
r := &ReconcileLicenses{
Client: client,
checker: commonlicense.MockChecker{},
checker: commonlicense.MockLicenseChecker{EnterpriseEnabled: true},
}
nsn := k8s.ExtractNamespacedName(tt.cluster)
res, err := r.reconcileInternal(reconcile.Request{NamespacedName: nsn}).Aggregate()
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/maps/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
Client: k8s.NewFakeClient(&v1alpha1.ElasticMapsServer{
ObjectMeta: metav1.ObjectMeta{Name: nsnFixture.Name, Namespace: nsnFixture.Namespace, DeletionTimestamp: &timeFixture},
}),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
dynamicWatches: watches.NewDynamicWatches(),
},
pre: func(r ReconcileMapsServer) {
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
Client: k8s.NewFakeClient(&emsFixture),
recorder: record.NewFakeRecorder(10),
dynamicWatches: watches.NewDynamicWatches(),
licenseChecker: license.MockChecker{MissingLicense: true},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: false},
Parameters: operator.Parameters{OperatorInfo: about.OperatorInfo{BuildInfo: about.BuildInfo{Version: "1.6.0"}}},
},
post: func(r ReconcileMapsServer) {
Expand All @@ -132,7 +132,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
Client: k8s.NewFakeClient(&emsFixture),
recorder: record.NewFakeRecorder(10),
dynamicWatches: watches.NewDynamicWatches(),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
Parameters: operator.Parameters{OperatorInfo: about.OperatorInfo{BuildInfo: about.BuildInfo{Version: "1.6.0"}}},
},
post: func(r ReconcileMapsServer) {
Expand All @@ -155,7 +155,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
Version: "7.10.0", // unsupported version
},
}),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
recorder: record.NewFakeRecorder(10),
},
wantErr: true,
Expand All @@ -174,7 +174,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
},
}),
dynamicWatches: watches.NewDynamicWatches(),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
recorder: record.NewFakeRecorder(10),
},
post: func(r ReconcileMapsServer) {
Expand All @@ -200,7 +200,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
},
}),
dynamicWatches: watches.NewDynamicWatches(),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
recorder: record.NewFakeRecorder(10),
},
post: func(r ReconcileMapsServer) {
Expand All @@ -215,7 +215,7 @@ func TestReconcileMapsServer_Reconcile(t *testing.T) {
Client: k8s.NewFakeClient(&emsFixture),
recorder: record.NewFakeRecorder(10),
dynamicWatches: watches.NewDynamicWatches(),
licenseChecker: license.MockChecker{},
licenseChecker: license.MockLicenseChecker{EnterpriseEnabled: true},
Parameters: operator.Parameters{OperatorInfo: about.OperatorInfo{BuildInfo: about.BuildInfo{Version: "1.6.0"}}},
},
post: func(r ReconcileMapsServer) {
Expand Down
Loading

0 comments on commit e343bf3

Please sign in to comment.