github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/testing/version.go (about) 1 package testing 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 ) 8 9 type semver []int 10 11 func (v semver) atLeast(other semver) bool { 12 for i := range v { 13 if i >= len(other) { 14 break 15 } 16 if v[i] < other[i] { 17 return false 18 } 19 if v[i] > other[i] { 20 return true 21 } 22 } 23 for i := len(v); i < len(other); i++ { 24 if other[i] > 0 { 25 return false 26 } 27 } 28 return true 29 } 30 31 // kafkaVersion is set in the circle config. It can also be provided on the 32 // command line in order to target a particular kafka version. 33 var kafkaVersion = parseVersion(os.Getenv("KAFKA_VERSION")) 34 35 // KafkaIsAtLeast returns true when the test broker is running a protocol 36 // version that is semver or newer. It determines the broker's version using 37 // the `KAFKA_VERSION` environment variable. If the var is unset, then this 38 // function will return true. 39 func KafkaIsAtLeast(semver string) bool { 40 return kafkaVersion.atLeast(parseVersion(semver)) 41 } 42 43 func parseVersion(semver string) semver { 44 if semver == "" { 45 return nil 46 } 47 parts := strings.Split(semver, ".") 48 version := make([]int, len(parts)) 49 for i := range version { 50 v, err := strconv.Atoi(parts[i]) 51 if err != nil { 52 // panic-ing because tests should be using hard-coded version values 53 panic("invalid version string: " + semver) 54 } 55 version[i] = v 56 } 57 return version 58 }