github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/createacls_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	ktesting "github.com/segmentio/kafka-go/testing"
     8  )
     9  
    10  func TestClientCreateACLs(t *testing.T) {
    11  	if !ktesting.KafkaIsAtLeast("2.0.1") {
    12  		return
    13  	}
    14  
    15  	client, shutdown := newLocalClient()
    16  	defer shutdown()
    17  
    18  	topic := makeTopic()
    19  	group := makeGroupID()
    20  
    21  	createRes, err := client.CreateACLs(context.Background(), &CreateACLsRequest{
    22  		ACLs: []ACLEntry{
    23  			{
    24  				Principal:           "User:alice",
    25  				PermissionType:      ACLPermissionTypeAllow,
    26  				Operation:           ACLOperationTypeRead,
    27  				ResourceType:        ResourceTypeTopic,
    28  				ResourcePatternType: PatternTypeLiteral,
    29  				ResourceName:        topic,
    30  				Host:                "*",
    31  			},
    32  			{
    33  				Principal:           "User:bob",
    34  				PermissionType:      ACLPermissionTypeAllow,
    35  				Operation:           ACLOperationTypeRead,
    36  				ResourceType:        ResourceTypeGroup,
    37  				ResourcePatternType: PatternTypeLiteral,
    38  				ResourceName:        group,
    39  				Host:                "*",
    40  			},
    41  		},
    42  	})
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	for _, err := range createRes.Errors {
    48  		if err != nil {
    49  			t.Error(err)
    50  		}
    51  	}
    52  }
    53  
    54  func TestACLPermissionTypeMarshal(t *testing.T) {
    55  	for i := ACLPermissionTypeUnknown; i <= ACLPermissionTypeAllow; i++ {
    56  		text, err := i.MarshalText()
    57  		if err != nil {
    58  			t.Errorf("couldn't marshal %d to text: %s", i, err)
    59  		}
    60  		var got ACLPermissionType
    61  		err = got.UnmarshalText(text)
    62  		if err != nil {
    63  			t.Errorf("couldn't unmarshal %s to ACLPermissionType: %s", text, err)
    64  		}
    65  		if got != i {
    66  			t.Errorf("got %d, want %d", got, i)
    67  		}
    68  	}
    69  }
    70  
    71  func TestACLOperationTypeMarshal(t *testing.T) {
    72  	for i := ACLOperationTypeUnknown; i <= ACLOperationTypeIdempotentWrite; i++ {
    73  		text, err := i.MarshalText()
    74  		if err != nil {
    75  			t.Errorf("couldn't marshal %d to text: %s", i, err)
    76  		}
    77  		var got ACLOperationType
    78  		err = got.UnmarshalText(text)
    79  		if err != nil {
    80  			t.Errorf("couldn't unmarshal %s to ACLOperationType: %s", text, err)
    81  		}
    82  		if got != i {
    83  			t.Errorf("got %d, want %d", got, i)
    84  		}
    85  	}
    86  }