Skip to content

Commit

Permalink
Tidy up unit tests and lc config
Browse files Browse the repository at this point in the history
  • Loading branch information
nolancon committed Dec 14, 2023
1 parent be374e5 commit 39a89dd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
8 changes: 6 additions & 2 deletions internal/controller/bucket/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
apisv1alpha1 "github.com/linode/provider-ceph/apis/v1alpha1"
"github.com/linode/provider-ceph/internal/backendstore"
"github.com/linode/provider-ceph/internal/backendstore/backendstorefakes"
"github.com/linode/provider-ceph/internal/controller/clienthandler"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -355,8 +356,11 @@ func TestCreate(t *testing.T) {
}

e := external{
kubeClient: cl.Build(),
backendStore: tc.fields.backendStore,
kubeClient: cl.Build(),
backendStore: tc.fields.backendStore,
clientHandler: clienthandler.NewS3ClientHandler(
clienthandler.WithAssumeRoleArn(nil),
clienthandler.WithBackendStore(tc.fields.backendStore)),
log: logging.NewNopLogger(),
operationTimeout: time.Second * 5,
}
Expand Down
29 changes: 26 additions & 3 deletions internal/controller/bucket/lifecycleconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,32 @@ type LifecycleConfigurationClient struct {
log logging.Logger
}

// NewLifecycleConfigurationClient creates the client for Accelerate Configuration
func NewLifecycleConfigurationClient(b *backendstore.BackendStore, c *clienthandler.S3ClientHandler, l logging.Logger) *LifecycleConfigurationClient {
return &LifecycleConfigurationClient{backendStore: b, clientHandler: c, log: l}
// NewLifecycleConfigurationClient creates the client for Lifecycle Configuration
func NewLifecycleConfigurationClient(options ...func(*LifecycleConfigurationClient)) *LifecycleConfigurationClient {
l := &LifecycleConfigurationClient{}
for _, o := range options {
o(l)
}

return l
}

func LifecycleConfigurationClientWithClientHandler(h *clienthandler.S3ClientHandler) func(*LifecycleConfigurationClient) {
return func(l *LifecycleConfigurationClient) {
l.clientHandler = h
}
}

func LifecycleConfiguraionClientWithBackendStore(s *backendstore.BackendStore) func(*LifecycleConfigurationClient) {
return func(l *LifecycleConfigurationClient) {
l.backendStore = s
}
}

func LifecycleConfiguraionClientWithLog(log logging.Logger) func(*LifecycleConfigurationClient) {
return func(l *LifecycleConfigurationClient) {
l.log = log
}
}

func (l *LifecycleConfigurationClient) Observe(ctx context.Context, bucket *v1alpha1.Bucket, backendNames []string) (ResourceStatus, error) {
Expand Down
35 changes: 25 additions & 10 deletions internal/controller/bucket/lifecycleconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/linode/provider-ceph/internal/backendstore"
"github.com/linode/provider-ceph/internal/backendstore/backendstorefakes"
ceph "github.com/linode/provider-ceph/internal/ceph"
"github.com/linode/provider-ceph/internal/controller/clienthandler"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -420,7 +421,14 @@ func TestObserveBackend(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

c := NewLifecycleConfigurationClient(tc.fields.backendStore, logging.NewNopLogger())
c := NewLifecycleConfigurationClient(
LifecycleConfiguraionClientWithBackendStore(tc.fields.backendStore),
LifecycleConfigurationClientWithClientHandler(
clienthandler.NewS3ClientHandler(
clienthandler.WithAssumeRoleArn(nil),
clienthandler.WithBackendStore(tc.fields.backendStore))),
LifecycleConfiguraionClientWithLog(logging.NewNopLogger()))

got, err := c.observeBackend(context.Background(), tc.args.bucket, tc.args.backendName)
assert.ErrorIs(t, err, tc.want.err, "unexpected error")
assert.Equal(t, tc.want.status, got, "unexpected status")
Expand Down Expand Up @@ -472,7 +480,7 @@ func TestHandle(t *testing.T) {
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -522,7 +530,7 @@ func TestHandle(t *testing.T) {
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend(beName, &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend(beName, &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -558,12 +566,12 @@ func TestHandle(t *testing.T) {
GetBucketLifecycleConfigurationStub: func(ctx context.Context, lci *s3.GetBucketLifecycleConfigurationInput, f ...func(*s3.Options)) (*s3.GetBucketLifecycleConfigurationOutput, error) {
return &s3.GetBucketLifecycleConfigurationOutput{
Rules: []s3types.LifecycleRule{},
}, &smithy.GenericAPIError{Code: s3internal.LifecycleNotFoundErrCode}
}, &smithy.GenericAPIError{Code: ceph.LifecycleNotFoundErrCode}
},
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -610,12 +618,12 @@ func TestHandle(t *testing.T) {
Filter: &s3types.LifecycleRuleFilterMemberPrefix{},
},
},
}, &smithy.GenericAPIError{Code: s3internal.LifecycleNotFoundErrCode}
}, &smithy.GenericAPIError{Code: ceph.LifecycleNotFoundErrCode}
},
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -670,14 +678,14 @@ func TestHandle(t *testing.T) {
Filter: &s3types.LifecycleRuleFilterMemberPrefix{},
},
},
}, &smithy.GenericAPIError{Code: s3internal.LifecycleNotFoundErrCode}
}, &smithy.GenericAPIError{Code: ceph.LifecycleNotFoundErrCode}
},
PutBucketLifecycleConfigurationStub: func(ctx context.Context, lci *s3.PutBucketLifecycleConfigurationInput, f ...func(*s3.Options)) (*s3.PutBucketLifecycleConfigurationOutput, error) {
return &s3.PutBucketLifecycleConfigurationOutput{}, errRandom
},
}
bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -723,7 +731,14 @@ func TestHandle(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

c := NewLifecycleConfigurationClient(tc.fields.backendStore, logging.NewNopLogger())
c := NewLifecycleConfigurationClient(
LifecycleConfiguraionClientWithBackendStore(tc.fields.backendStore),
LifecycleConfigurationClientWithClientHandler(
clienthandler.NewS3ClientHandler(
clienthandler.WithAssumeRoleArn(nil),
clienthandler.WithBackendStore(tc.fields.backendStore))),
LifecycleConfiguraionClientWithLog(logging.NewNopLogger()))

bb := newBucketBackends()
bb.setLifecycleConfigCondition(bucketName, beName, &creating)

Expand Down
5 changes: 4 additions & 1 deletion internal/controller/bucket/subresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ type SubresourceClient interface {
// NewSubresourceClients creates the array of all sub resource clients.
func NewSubresourceClients(b *backendstore.BackendStore, c *clienthandler.S3ClientHandler, l logging.Logger) []SubresourceClient {
return []SubresourceClient{
NewLifecycleConfigurationClient(b, c, l.WithValues("lifecycle-configuration-client", managed.ControllerName(v1alpha1.BucketGroupKind))),
NewLifecycleConfigurationClient(
LifecycleConfiguraionClientWithBackendStore(b),
LifecycleConfigurationClientWithClientHandler(c),
LifecycleConfiguraionClientWithLog(l.WithValues("lifecycle-configuration-client", managed.ControllerName(v1alpha1.BucketGroupKind)))),
}
}

Expand Down
16 changes: 10 additions & 6 deletions internal/controller/bucket/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
apisv1alpha1 "github.com/linode/provider-ceph/apis/v1alpha1"
"github.com/linode/provider-ceph/internal/backendstore"
"github.com/linode/provider-ceph/internal/backendstore/backendstorefakes"
"github.com/linode/provider-ceph/internal/controller/clienthandler"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -219,8 +220,8 @@ func TestUpdate(t *testing.T) {
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-2", &fake, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-2", &fake, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -271,8 +272,8 @@ func TestUpdate(t *testing.T) {
}

bs := backendstore.NewBackendStore()
bs.AddOrUpdateBackend("s3-backend-1", &fakeOK, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-2", &fakeErr, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-1", &fakeOK, nil, true, apisv1alpha1.HealthStatusHealthy)
bs.AddOrUpdateBackend("s3-backend-2", &fakeErr, nil, true, apisv1alpha1.HealthStatusHealthy)

return bs
}(),
Expand Down Expand Up @@ -393,8 +394,11 @@ func TestUpdate(t *testing.T) {
WithScheme(s).Build()

e := external{
kubeClient: cl,
backendStore: tc.fields.backendStore,
kubeClient: cl,
backendStore: tc.fields.backendStore,
clientHandler: clienthandler.NewS3ClientHandler(
clienthandler.WithAssumeRoleArn(nil),
clienthandler.WithBackendStore(tc.fields.backendStore)),
autoPauseBucket: tc.fields.autoPauseBucket,
log: logging.NewNopLogger(),
}
Expand Down

0 comments on commit 39a89dd

Please sign in to comment.