generated from codecrafters-io/tester-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-cluster-payload-interface
- Loading branch information
Showing
65 changed files
with
6,617 additions
and
3,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 CodeCrafters | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package assertions | ||
|
||
import ( | ||
"fmt" | ||
|
||
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
) | ||
|
||
type ApiVersionsResponseAssertion struct { | ||
ActualValue kafkaapi.ApiVersionsResponse | ||
ExpectedValue kafkaapi.ApiVersionsResponse | ||
} | ||
|
||
func NewApiVersionsResponseAssertion(actualValue kafkaapi.ApiVersionsResponse, expectedValue kafkaapi.ApiVersionsResponse) ApiVersionsResponseAssertion { | ||
return ApiVersionsResponseAssertion{ActualValue: actualValue, ExpectedValue: expectedValue} | ||
} | ||
|
||
var apiKeyNames = map[int16]string{ | ||
1: "FETCH", | ||
18: "API_VERSIONS", | ||
75: "DESCRIBE_TOPIC_PARTITIONS", | ||
} | ||
|
||
var errorCodes = map[int]string{ | ||
0: "NO_ERROR", | ||
3: "UNKNOWN_TOPIC_OR_PARTITION", | ||
35: "UNSUPPORTED_VERSION", | ||
100: "UNKNOWN_TOPIC_ID", | ||
} | ||
|
||
func (a ApiVersionsResponseAssertion) Evaluate(fields []string, AssertApiVersionsResponseKey bool, logger *logger.Logger) error { | ||
if Contains(fields, "ErrorCode") { | ||
if a.ActualValue.ErrorCode != a.ExpectedValue.ErrorCode { | ||
return fmt.Errorf("Expected %s to be %d, got %d", "ErrorCode", a.ExpectedValue.ErrorCode, a.ActualValue.ErrorCode) | ||
} | ||
|
||
errorCodeName, ok := errorCodes[int(a.ActualValue.ErrorCode)] | ||
if !ok { | ||
errorCodeName = "UNKNOWN" | ||
} | ||
logger.Successf("✓ Error code: %d (%s)", a.ActualValue.ErrorCode, errorCodeName) | ||
} | ||
|
||
if AssertApiVersionsResponseKey { | ||
if len(a.ActualValue.ApiKeys) < len(a.ExpectedValue.ApiKeys) { | ||
return fmt.Errorf("Expected API keys array to include atleast %d keys, got %d", len(a.ExpectedValue.ApiKeys), len(a.ActualValue.ApiKeys)) | ||
} | ||
logger.Successf("✓ API keys array length: %d", len(a.ActualValue.ApiKeys)) | ||
|
||
for _, expectedApiVersionKey := range a.ExpectedValue.ApiKeys { | ||
found := false | ||
for _, actualApiVersionKey := range a.ActualValue.ApiKeys { | ||
if actualApiVersionKey.ApiKey == expectedApiVersionKey.ApiKey { | ||
found = true | ||
if actualApiVersionKey.MinVersion > expectedApiVersionKey.MaxVersion { | ||
return fmt.Errorf("Expected min version %v to be < max version %v for %s", actualApiVersionKey.MinVersion, expectedApiVersionKey.MaxVersion, apiKeyNames[expectedApiVersionKey.ApiKey]) | ||
} | ||
|
||
// anything above or equal to expected minVersion is fine | ||
if actualApiVersionKey.MinVersion < expectedApiVersionKey.MinVersion { | ||
return fmt.Errorf("Expected API version %v to be supported for %s, got %v", expectedApiVersionKey.MaxVersion, apiKeyNames[expectedApiVersionKey.ApiKey], actualApiVersionKey.MaxVersion) | ||
} | ||
logger.Successf("✓ MinVersion for %s is <= %v & >= %v", apiKeyNames[expectedApiVersionKey.ApiKey], expectedApiVersionKey.MaxVersion, expectedApiVersionKey.MinVersion) | ||
|
||
if actualApiVersionKey.MaxVersion < expectedApiVersionKey.MaxVersion { | ||
return fmt.Errorf("Expected API version %v to be supported for %s, got %v", expectedApiVersionKey.MaxVersion, apiKeyNames[expectedApiVersionKey.ApiKey], actualApiVersionKey.MaxVersion) | ||
} | ||
logger.Successf("✓ MaxVersion for %s is >= %v", apiKeyNames[expectedApiVersionKey.ApiKey], expectedApiVersionKey.MaxVersion) | ||
} | ||
} | ||
if !found { | ||
return fmt.Errorf("Expected APIVersionsResponseKey array to include API key %d (%s)", expectedApiVersionKey.ApiKey, apiKeyNames[expectedApiVersionKey.ApiKey]) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
136 changes: 136 additions & 0 deletions
136
internal/assertions/describe_topic_partitions_response_assertion.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package assertions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/codecrafters-io/kafka-tester/protocol" | ||
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
) | ||
|
||
type DescribeTopicPartitionsResponseAssertion struct { | ||
ActualValue kafkaapi.DescribeTopicPartitionsResponse | ||
ExpectedValue kafkaapi.DescribeTopicPartitionsResponse | ||
logger *logger.Logger | ||
err error | ||
} | ||
|
||
func NewDescribeTopicPartitionsResponseAssertion(actualValue kafkaapi.DescribeTopicPartitionsResponse, expectedValue kafkaapi.DescribeTopicPartitionsResponse, logger *logger.Logger) *DescribeTopicPartitionsResponseAssertion { | ||
return &DescribeTopicPartitionsResponseAssertion{ | ||
ActualValue: actualValue, | ||
ExpectedValue: expectedValue, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (a *DescribeTopicPartitionsResponseAssertion) AssertBody(fields []string) *DescribeTopicPartitionsResponseAssertion { | ||
if a.err != nil { | ||
return a | ||
} | ||
if Contains(fields, "ThrottleTimeMs") { | ||
if a.ActualValue.ThrottleTimeMs != a.ExpectedValue.ThrottleTimeMs { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", "ThrottleTimeMs", a.ExpectedValue.ThrottleTimeMs, a.ActualValue.ThrottleTimeMs) | ||
return a | ||
} | ||
a.logger.Successf("✓ Throttle Time: %d", a.ActualValue.ThrottleTimeMs) | ||
} | ||
|
||
return a | ||
} | ||
|
||
func (a *DescribeTopicPartitionsResponseAssertion) AssertTopics(topicFields []string, partitionFields []string) *DescribeTopicPartitionsResponseAssertion { | ||
if a.err != nil { | ||
return a | ||
} | ||
|
||
if len(a.ActualValue.Topics) != len(a.ExpectedValue.Topics) { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", "topics.length", len(a.ExpectedValue.Topics), len(a.ActualValue.Topics)) | ||
return a | ||
} | ||
|
||
for i, actualTopic := range a.ActualValue.Topics { | ||
expectedTopic := a.ExpectedValue.Topics[i] | ||
if Contains(topicFields, "ErrorCode") { | ||
if actualTopic.ErrorCode != expectedTopic.ErrorCode { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", fmt.Sprintf("TopicResponse[%d] Error Code", i), expectedTopic.ErrorCode, actualTopic.ErrorCode) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 1, "✓ TopicResponse[%d] Error code: %d", i, actualTopic.ErrorCode) | ||
} | ||
|
||
if Contains(topicFields, "Name") { | ||
if actualTopic.Name != expectedTopic.Name { | ||
a.err = fmt.Errorf("Expected %s to be %s, got %s", fmt.Sprintf("TopicResponse[%d] Topic Name", i), expectedTopic.Name, actualTopic.Name) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 1, "✓ TopicResponse[%d] Topic Name: %s", i, actualTopic.Name) | ||
} | ||
|
||
if Contains(topicFields, "TopicID") { | ||
if actualTopic.TopicID != expectedTopic.TopicID { | ||
a.err = fmt.Errorf("Expected %s to be %s, got %s", fmt.Sprintf("TopicResponse[%d] Topic UUID", i), expectedTopic.TopicID, actualTopic.TopicID) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 1, "✓ TopicResponse[%d] Topic UUID: %s", i, actualTopic.TopicID) | ||
} | ||
|
||
if Contains(topicFields, "TopicAuthorizedOperations") { | ||
if actualTopic.TopicAuthorizedOperations != expectedTopic.TopicAuthorizedOperations { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", fmt.Sprintf("TopicResponse[%d] Topic Authorized Operations", i), expectedTopic.TopicAuthorizedOperations, actualTopic.TopicAuthorizedOperations) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 1, "✓ TopicResponse[%d] Topic Authorized Operations: %d", i, actualTopic.TopicAuthorizedOperations) | ||
} | ||
|
||
expectedPartitions := expectedTopic.Partitions | ||
actualPartitions := actualTopic.Partitions | ||
|
||
if (partitionFields) != nil { | ||
a.assertPartitions(expectedPartitions, actualPartitions, partitionFields) | ||
} else { | ||
if len(actualPartitions) != 0 { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", "partitions.length", 0, len(actualPartitions)) | ||
return a | ||
} | ||
} | ||
} | ||
|
||
return a | ||
} | ||
|
||
func (a *DescribeTopicPartitionsResponseAssertion) assertPartitions(expectedPartitions []kafkaapi.DescribeTopicPartitionsResponsePartition, actualPartitions []kafkaapi.DescribeTopicPartitionsResponsePartition, fields []string) *DescribeTopicPartitionsResponseAssertion { | ||
if len(actualPartitions) != len(expectedPartitions) { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", "partitions.length", len(expectedPartitions), len(actualPartitions)) | ||
return a | ||
} | ||
|
||
for j, actualPartition := range actualPartitions { | ||
expectedPartition := expectedPartitions[j] | ||
|
||
if Contains(fields, "ErrorCode") { | ||
if actualPartition.ErrorCode != expectedPartition.ErrorCode { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", fmt.Sprintf("PartitionResponse[%d] Error Code", j), expectedPartition.ErrorCode, actualPartition.ErrorCode) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 2, "✓ PartitionResponse[%d] Error code: %d", j, actualPartition.ErrorCode) | ||
} | ||
|
||
if Contains(fields, "PartitionIndex") { | ||
if actualPartition.PartitionIndex != expectedPartition.PartitionIndex { | ||
a.err = fmt.Errorf("Expected %s to be %d, got %d", fmt.Sprintf("Partition Response[%d] Partition Index", j), expectedPartition.PartitionIndex, actualPartition.PartitionIndex) | ||
return a | ||
} | ||
protocol.SuccessLogWithIndentation(a.logger, 2, "✓ PartitionResponse[%d] Partition Index: %d", j, actualPartition.PartitionIndex) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a DescribeTopicPartitionsResponseAssertion) Run() error { | ||
// firstLevelFields: ["ThrottleTimeMs"] | ||
// secondLevelFields (Topics): ["ErrorCode", "Name", "TopicID", "TopicAuthorizedOperations"] | ||
// thirdLevelFields (Partitions): ["ErrorCode, "PartitionIndex"] | ||
return a.err | ||
} |
Oops, something went wrong.