Skip to content

Commit 7624d0d

Browse files
committed
refactor: convert runtime tests from external to internal package tests
Change test files from `runtime_test` to `runtime` package to enable testing of package private functions. Remove ackrt import alias as tests now have direct access to runtime package symbols.
1 parent 2ab09c6 commit 7624d0d

File tree

6 files changed

+31
-38
lines changed

6 files changed

+31
-38
lines changed

pkg/runtime/adoption_reconciler_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"context"
@@ -33,7 +33,6 @@ import (
3333
ackmocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types"
3434
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
3535
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
36-
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
3736
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
3837
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
3938
)
@@ -61,7 +60,7 @@ func mockAdoptionReconciler() (acktypes.AdoptedResourceReconciler, *ctrlrtclient
6160
sc.On("GetResourceManagerFactories").Return(rmFactoryMap)
6261
kc := &ctrlrtclientmock.Client{}
6362
apiReader := &ctrlrtclientmock.Reader{}
64-
return ackrt.NewAdoptionReconcilerWithClient(
63+
return NewAdoptionReconcilerWithClient(
6564
sc,
6665
fakeLogger,
6766
cfg,

pkg/runtime/field_export_reconciler_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"bytes"
@@ -35,7 +35,6 @@ import (
3535
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
3636
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
3737
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
38-
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
3938
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
4039
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
4140

@@ -81,7 +80,7 @@ func mockFieldExportReconcilerWithResourceDescriptor(rd *mocks.AWSResourceDescri
8180
sc.On("GetResourceManagerFactories").Return(rmFactoryMap)
8281
kc := &ctrlrtclientmock.Client{}
8382
apiReader := &ctrlrtclientmock.Reader{}
84-
return ackrt.NewFieldExportReconcilerWithClient(
83+
return NewFieldExportReconcilerWithClient(
8584
sc,
8685
fakeLogger,
8786
cfg,
@@ -230,7 +229,7 @@ func setupMockUnstructuredConverter() {
230229
}, nil,
231230
)
232231
// Update the package variable
233-
ackrt.UnstructuredConverter = conv
232+
UnstructuredConverter = conv
234233
}
235234

236235
func mockSourceResource() (
@@ -594,7 +593,7 @@ func assertPatchedSecretWithKey(expected bool, t *testing.T, ctx context.Context
594593
return bytes.Equal(val, []byte("test-book-name"))
595594
})
596595
if expected {
597-
kc.AssertCalled(t, "Patch", withoutCancelContextMatcher, dataMatcher, mock.Anything)
596+
kc.AssertCalled(t, "Patch", withoutCancelContextMatcher, dataMatcher, mock.Anything)
598597
} else {
599598
kc.AssertNotCalled(t, "Patch", withoutCancelContextMatcher, dataMatcher, mock.Anything)
600599
}

pkg/runtime/reconciler_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"context"
@@ -29,7 +29,6 @@ import (
2929
corev1 "k8s.io/api/core/v1"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32-
"k8s.io/apimachinery/pkg/runtime/schema"
3332
k8srtschema "k8s.io/apimachinery/pkg/runtime/schema"
3433
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
3534

@@ -41,7 +40,6 @@ import (
4140
"github.com/aws-controllers-k8s/runtime/pkg/featuregate"
4241
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
4342
"github.com/aws-controllers-k8s/runtime/pkg/requeue"
44-
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
4543
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
4644
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
4745

@@ -125,7 +123,7 @@ func reconcilerMocks(
125123
sc.On("GetMetadata").Return(scmd)
126124
kc := &ctrlrtclientmock.Client{}
127125

128-
return ackrt.NewReconcilerWithClient(
126+
return NewReconcilerWithClient(
129127
sc, kc, rmf, fakeLogger, cfg, metrics, ackrtcache.Caches{},
130128
), kc, scmd
131129
}
@@ -150,7 +148,7 @@ func managerFactoryMocks(
150148
) {
151149
rd := &ackmocks.AWSResourceDescriptor{}
152150
rd.On("GroupVersionKind").Return(
153-
schema.GroupVersionKind{
151+
k8srtschema.GroupVersionKind{
154152
Group: "bookstore.services.k8s.aws",
155153
Kind: "fakeBook",
156154
},
@@ -164,7 +162,7 @@ func managerFactoryMocks(
164162
rmf.On("ResourceDescriptor").Return(rd)
165163
rmf.On("RequeueOnSuccessSeconds").Return(0)
166164

167-
reg := ackrt.NewRegistry()
165+
reg := NewRegistry()
168166
reg.RegisterResourceManagerFactory(rmf)
169167
return rmf, rd
170168
}

pkg/runtime/service_controller_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"context"
@@ -42,7 +42,6 @@ import (
4242
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
4343
mocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types"
4444
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
45-
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
4645
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
4746
)
4847

@@ -150,7 +149,7 @@ func TestServiceController(t *testing.T) {
150149
rmf.On("ResourceDescriptor").Return(rd)
151150
rmf.On("RequeueOnSuccessSeconds").Return(0)
152151

153-
reg := ackrt.NewRegistry()
152+
reg := NewRegistry()
154153
reg.RegisterResourceManagerFactory(rmf)
155154

156155
vi := acktypes.VersionInfo{
@@ -159,7 +158,7 @@ func TestServiceController(t *testing.T) {
159158
BuildDate: "now",
160159
}
161160

162-
sc := ackrt.NewServiceController("bookstore", "bookstore.services.k8s.aws", vi)
161+
sc := NewServiceController("bookstore", "bookstore.services.k8s.aws", vi)
163162
require.NotNil(sc)
164163
zapOptions := ctrlrtzap.Options{
165164
Development: true,

pkg/runtime/tags_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"fmt"
@@ -21,7 +21,6 @@ import (
2121

2222
mocks "github.com/aws-controllers-k8s/runtime/mocks/controller-runtime/pkg/client"
2323
"github.com/aws-controllers-k8s/runtime/pkg/config"
24-
"github.com/aws-controllers-k8s/runtime/pkg/runtime"
2524
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
2625
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
2726
)
@@ -42,27 +41,27 @@ func TestGetDefaultTags(t *testing.T) {
4241
}
4342

4443
// nil config
45-
assert.Empty(runtime.GetDefaultTags(nil, &obj, md))
44+
assert.Empty(GetDefaultTags(nil, &obj, md))
4645

4746
// nil object
48-
assert.Empty(runtime.GetDefaultTags(&cfg, nil, md))
47+
assert.Empty(GetDefaultTags(&cfg, nil, md))
4948

5049
// no resource tags
51-
assert.Empty(runtime.GetDefaultTags(&cfg, &obj, md))
50+
assert.Empty(GetDefaultTags(&cfg, &obj, md))
5251

5352
// ill formed tags
5453
cfg.ResourceTags = []string{"foobar"}
55-
expandedTags := runtime.GetDefaultTags(&cfg, &obj, md)
54+
expandedTags := GetDefaultTags(&cfg, &obj, md)
5655
assert.Empty(expandedTags)
5756

5857
// ill formed tags
5958
cfg.ResourceTags = []string{"foo=bar=baz"}
60-
expandedTags = runtime.GetDefaultTags(&cfg, &obj, md)
59+
expandedTags = GetDefaultTags(&cfg, &obj, md)
6160
assert.Empty(expandedTags)
6261

6362
// tags without any ack resource tag format
6463
cfg.ResourceTags = []string{"foo=bar"}
65-
expandedTags = runtime.GetDefaultTags(&cfg, &obj, md)
64+
expandedTags = GetDefaultTags(&cfg, &obj, md)
6665
assert.Equal(1, len(expandedTags))
6766
assert.Equal("bar", expandedTags["foo"])
6867

@@ -80,7 +79,7 @@ func TestGetDefaultTags(t *testing.T) {
8079
acktags.ResourceNameTagFormat,
8180
),
8281
}
83-
expandedTags = runtime.GetDefaultTags(&cfg, &obj, md)
82+
expandedTags = GetDefaultTags(&cfg, &obj, md)
8483
assert.Equal(4, len(expandedTags))
8584
assert.Equal("bar", expandedTags["foo"])
8685
assert.Equal("s3-v0.0.10", expandedTags["services.k8s.aws/controller-version"])

pkg/runtime/util_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// express or implied. See the License for the specific language governing
1212
// permissions and limitations under the License.
1313

14-
package runtime_test
14+
package runtime
1515

1616
import (
1717
"testing"
@@ -21,7 +21,6 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222

2323
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
24-
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
2524

2625
mocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types"
2726
)
@@ -35,11 +34,11 @@ func TestIsAdopted(t *testing.T) {
3534
ackv1alpha1.AnnotationAdopted: "true",
3635
},
3736
})
38-
require.True(ackrt.IsAdopted(res))
37+
require.True(IsAdopted(res))
3938

4039
res = &mocks.AWSResource{}
4140
res.On("MetaObject").Return(&metav1.ObjectMeta{})
42-
require.False(ackrt.IsAdopted(res))
41+
require.False(IsAdopted(res))
4342
}
4443

4544
func TestIsSynced(t *testing.T) {
@@ -52,7 +51,7 @@ func TestIsSynced(t *testing.T) {
5251
Status: corev1.ConditionTrue,
5352
},
5453
})
55-
require.True(ackrt.IsSynced(res))
54+
require.True(IsSynced(res))
5655

5756
res = &mocks.AWSResource{}
5857
res.On("Conditions").Return([]*ackv1alpha1.Condition{
@@ -65,7 +64,7 @@ func TestIsSynced(t *testing.T) {
6564
Status: corev1.ConditionFalse,
6665
},
6766
})
68-
require.False(ackrt.IsSynced(res))
67+
require.False(IsSynced(res))
6968
}
7069

7170
func TestIsForcedAdoption(t *testing.T) {
@@ -78,7 +77,7 @@ func TestIsForcedAdoption(t *testing.T) {
7877
ackv1alpha1.AnnotationAdopted: "false",
7978
},
8079
})
81-
require.True(ackrt.NeedAdoption(res))
80+
require.True(NeedAdoption(res))
8281

8382
res = &mocks.AWSResource{}
8483
res.On("MetaObject").Return(&metav1.ObjectMeta{
@@ -87,15 +86,15 @@ func TestIsForcedAdoption(t *testing.T) {
8786
ackv1alpha1.AnnotationAdopted: "true",
8887
},
8988
})
90-
require.False(ackrt.NeedAdoption(res))
89+
require.False(NeedAdoption(res))
9190

9291
res = &mocks.AWSResource{}
9392
res.On("MetaObject").Return(&metav1.ObjectMeta{
9493
Annotations: map[string]string{
95-
ackv1alpha1.AnnotationAdopted: "true",
94+
ackv1alpha1.AnnotationAdopted: "true",
9695
},
9796
})
98-
require.False(ackrt.NeedAdoption(res))
97+
require.False(NeedAdoption(res))
9998
}
10099

101100
func TestExtractAdoptionFields(t *testing.T) {
@@ -115,7 +114,7 @@ func TestExtractAdoptionFields(t *testing.T) {
115114
"clusterName": "my-cluster",
116115
"name": "ng-1234",
117116
}
118-
actual, err := ackrt.ExtractAdoptionFields(res)
117+
actual, err := ExtractAdoptionFields(res)
119118
require.NoError(err)
120119
require.Equal(expected, actual)
121120
}

0 commit comments

Comments
 (0)