@@ -20,6 +20,8 @@ import (
2020 "path"
2121 "reflect"
2222 "runtime"
23+ "strconv"
24+ "strings"
2325 "testing"
2426
2527 specs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -578,6 +580,20 @@ func TestParseSelectorInvalid(t *testing.T) {
578580 }
579581}
580582
583+ func TestFormatAllSkipsEmptyOSFeatures (t * testing.T ) {
584+ p := specs.Platform {
585+ OS : "linux" ,
586+ Architecture : "amd64" ,
587+ OSFeatures : []string {"" , "gpu" , "" , "simd" },
588+ }
589+
590+ formatted := FormatAll (p )
591+ expected := "linux(+gpu+simd)/amd64"
592+ if formatted != expected {
593+ t .Fatalf ("unexpected format: %q != %q" , formatted , expected )
594+ }
595+ }
596+
581597func FuzzPlatformsParse (f * testing.F ) {
582598 f .Add ("linux/amd64" )
583599 f .Fuzz (func (t * testing.T , s string ) {
@@ -587,3 +603,131 @@ func FuzzPlatformsParse(f *testing.F) {
587603 }
588604 })
589605}
606+
607+ func BenchmarkParseOSOptions (b * testing.B ) {
608+ maxFeatures := 16
609+
610+ benchmarks := []struct {
611+ doc string
612+ input string
613+ }{
614+ {
615+ doc : "valid windows version and feature" ,
616+ input : "windows(10.0.17763+win32k)/amd64" ,
617+ },
618+ {
619+ doc : "valid but lengthy features" ,
620+ input : "linux(+" + strings .Repeat ("+feature" , maxFeatures ) + ")/amd64" ,
621+ },
622+ {
623+ doc : "exploding plus chain" ,
624+ input : "linux(" + strings .Repeat ("+" , 64 * 1024 ) + ")/amd64" ,
625+ },
626+ {
627+ doc : "kernel config feature blob" ,
628+ input : "linux(+CONFIG_" + strings .Repeat ("FOO=y_" , 16 * 1024 ) + "BAR)/amd64" ,
629+ },
630+ }
631+
632+ b .ReportAllocs ()
633+ b .ResetTimer ()
634+ for _ , bm := range benchmarks {
635+ b .Run (bm .doc , func (b * testing.B ) {
636+ for i := 0 ; i < b .N ; i ++ {
637+ _ , _ = Parse (bm .input )
638+ }
639+ })
640+ }
641+ }
642+
643+ func BenchmarkFormatAllOSFeatures (b * testing.B ) {
644+ maxFeatures := 16
645+
646+ benchmarks := []struct {
647+ doc string
648+ platform specs.Platform
649+ }{
650+ {
651+ doc : "plain linux amd64" ,
652+ platform : specs.Platform {
653+ OS : "linux" ,
654+ Architecture : "amd64" ,
655+ },
656+ },
657+ {
658+ doc : "windows version and feature" ,
659+ platform : specs.Platform {
660+ OS : "windows" ,
661+ OSVersion : "10.0.17763" ,
662+ OSFeatures : []string {"win32k" },
663+ Architecture : "amd64" ,
664+ },
665+ },
666+ {
667+ doc : "valid but lengthy features" ,
668+ platform : specs.Platform {
669+ OS : "linux" ,
670+ OSFeatures : func () (out []string ) {
671+ for i := 0 ; i <= maxFeatures ; i ++ {
672+ out = append (out , "feature" )
673+ }
674+ return out
675+ }(),
676+ Architecture : "amd64" ,
677+ },
678+ },
679+ {
680+ doc : "skips empty features" ,
681+ platform : specs.Platform {
682+ OS : "linux" ,
683+ OSFeatures : []string {"" , "gpu" , "" , "simd" },
684+ Architecture : "amd64" ,
685+ },
686+ },
687+ {
688+ doc : "kernel config feature blob" ,
689+ platform : specs.Platform {
690+ OS : "linux" ,
691+ OSFeatures : []string {"CONFIG_" + strings .Repeat ("FOO_" , 16 * 1024 ) + "BAR" },
692+ Architecture : "amd64" ,
693+ },
694+ },
695+ {
696+ doc : "many kernel config features with empties" ,
697+ platform : specs.Platform {
698+ OS : "linux" ,
699+ OSFeatures : func () []string {
700+ n := 1024
701+ out := make ([]string , n )
702+ for i := range out {
703+ if i % 10 == 0 {
704+ out [i ] = "" // simulate bad data
705+ } else {
706+ out [i ] = "CONFIG_FOO_" + strconv .Itoa (i )
707+ }
708+ }
709+ return out
710+ }(),
711+ Architecture : "amd64" ,
712+ },
713+ },
714+ {
715+ doc : "too many features" ,
716+ platform : specs.Platform {
717+ OS : "linux" ,
718+ OSFeatures : make ([]string , maxFeatures + 1 ),
719+ Architecture : "amd64" ,
720+ },
721+ },
722+ }
723+
724+ b .ReportAllocs ()
725+ b .ResetTimer ()
726+ for _ , bm := range benchmarks {
727+ b .Run (bm .doc , func (b * testing.B ) {
728+ for i := 0 ; i < b .N ; i ++ {
729+ _ = FormatAll (bm .platform )
730+ }
731+ })
732+ }
733+ }
0 commit comments