@@ -22,6 +22,7 @@ import (
2222 "github.com/stretchr/testify/require"
2323
2424 "github.com/aws-controllers-k8s/dynamodb-controller/apis/v1alpha1"
25+ svcapitypes "github.com/aws-controllers-k8s/dynamodb-controller/apis/v1alpha1"
2526)
2627
2728var (
@@ -505,3 +506,98 @@ func Test_newResourceDelta_customDeltaFunction_AttributeDefinitions(t *testing.T
505506 })
506507 }
507508}
509+
510+ func Test_compareProvisionedThroughput (t * testing.T ) {
511+ type managementType int
512+ const (
513+ managedByDefault managementType = iota
514+ managedByACKController
515+ managedByExternalAutoscaler
516+ )
517+
518+ // Helper to create table with given throughput parameters
519+ createTable := func (rpu , wpu * int64 ) * v1alpha1.Table {
520+ if rpu == nil && wpu == nil {
521+ return & v1alpha1.Table {
522+ Spec : v1alpha1.TableSpec {
523+ ProvisionedThroughput : nil ,
524+ },
525+ }
526+ }
527+ return & v1alpha1.Table {
528+ Spec : v1alpha1.TableSpec {
529+ ProvisionedThroughput : & v1alpha1.ProvisionedThroughput {
530+ ReadCapacityUnits : rpu ,
531+ WriteCapacityUnits : wpu ,
532+ },
533+ },
534+ }
535+ }
536+
537+ // Helper function to apply management type to a table
538+ applyManagement := func (table * v1alpha1.Table , mgmt managementType ) * v1alpha1.Table {
539+ switch mgmt {
540+ case managedByACKController :
541+ if table .Annotations == nil {
542+ table .Annotations = make (map [string ]string )
543+ }
544+ table .Annotations [svcapitypes .TableProvisionedThroughputManagedByAnnotation ] = svcapitypes .TableProvisionedThroughputManagedByACKController
545+ return table
546+ case managedByExternalAutoscaler :
547+ if table .Annotations == nil {
548+ table .Annotations = make (map [string ]string )
549+ }
550+ table .Annotations [svcapitypes .TableProvisionedThroughputManagedByAnnotation ] = svcapitypes .TableProvisionedThroughputManagedByExternalAutoscaler
551+ return table
552+ default :
553+ return table // managedByDefault
554+ }
555+ }
556+
557+ tests := []struct {
558+ name string
559+ mgmt managementType
560+ aRpu , aWpu * int64 // Table A provisioned throughput (nil means no throughput spec)
561+ bRpu , bWpu * int64 // Table B provisioned throughput (nil means no throughput spec)
562+ expectDelta bool
563+ }{
564+ // ManagedByDefault scenarios
565+ {"default: both nil ProvisionedThroughput" , managedByDefault , nil , nil , nil , nil , false },
566+ {"default: (a) nil ProvisionedThroughput" , managedByDefault , nil , nil , aws .Int64 (5 ), aws .Int64 (5 ), true },
567+ {"default: (b) nil ProvisionedThroughput" , managedByDefault , aws .Int64 (5 ), aws .Int64 (5 ), nil , nil , true },
568+ {"default: equal ProvisionedThroughput" , managedByDefault , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), false },
569+ {"default: different ProvisionedThroughput" , managedByDefault , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (10 ), aws .Int64 (5 ), true },
570+
571+ // ManagedByACKController scenarios
572+ {"ack: both nil ProvisionedThroughput" , managedByACKController , nil , nil , nil , nil , false },
573+ {"ack: (a) nil ProvisionedThroughput" , managedByACKController , nil , nil , aws .Int64 (5 ), aws .Int64 (5 ), true },
574+ {"ack: (b) nil ProvisionedThroughput" , managedByACKController , aws .Int64 (5 ), aws .Int64 (5 ), nil , nil , true },
575+ {"ack: equal ProvisionedThroughput" , managedByACKController , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), false },
576+ {"ack: different ProvisionedThroughput" , managedByACKController , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (10 ), aws .Int64 (5 ), true },
577+
578+ // ManagedByExternalAutoscaler scenarios (delta should be false for changes)
579+ {"external: both nil ProvisionedThroughput" , managedByExternalAutoscaler , nil , nil , nil , nil , false },
580+ {"external: (a) nil ProvisionedThroughput" , managedByExternalAutoscaler , nil , nil , aws .Int64 (5 ), aws .Int64 (5 ), false },
581+ {"external: (b) nil ProvisionedThroughput" , managedByExternalAutoscaler , aws .Int64 (5 ), aws .Int64 (5 ), nil , nil , false },
582+ {"external: equal ProvisionedThroughput" , managedByExternalAutoscaler , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (5 ), false },
583+ {"external: different ProvisionedThroughput" , managedByExternalAutoscaler , aws .Int64 (5 ), aws .Int64 (5 ), aws .Int64 (10 ), aws .Int64 (5 ), false },
584+ }
585+
586+ for _ , tt := range tests {
587+ t .Run (tt .name , func (t * testing.T ) {
588+ // Create tables
589+ tableA := createTable (tt .aRpu , tt .aWpu )
590+ tableB := createTable (tt .bRpu , tt .bWpu )
591+
592+ // Apply management type
593+ tableA = applyManagement (tableA , tt .mgmt )
594+ tableB = applyManagement (tableB , tt .mgmt )
595+
596+ // Test comparison
597+ delta := newResourceDelta (& resource {tableA }, & resource {tableB })
598+ if tt .expectDelta != delta .DifferentAt ("Spec.ProvisionedThroughput" ) {
599+ t .Errorf ("customPostCompare() has delta at ProvisionedThroughput = %v, want %v" , delta .DifferentAt ("Spec.ProvisionedThroughput" ), tt .expectDelta )
600+ }
601+ })
602+ }
603+ }
0 commit comments