@@ -828,3 +828,100 @@ func TestUpdateMaxReplicas(t *testing.T) {
828828
829829 assert .DeepEqual (t , svc .TaskTemplate .Placement , & swarm.Placement {MaxReplicas : uint64 (2 )})
830830}
831+
832+ func TestUpdateSysCtls (t * testing.T ) {
833+ ctx := context .Background ()
834+
835+ tests := []struct {
836+ name string
837+ spec map [string ]string
838+ add []string
839+ rm []string
840+ expected map [string ]string
841+ }{
842+ {
843+ name : "from scratch" ,
844+ add : []string {"sysctl.zet=value-99" , "sysctl.alpha=value-1" },
845+ expected : map [string ]string {"sysctl.zet" : "value-99" , "sysctl.alpha" : "value-1" },
846+ },
847+ {
848+ name : "append new" ,
849+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
850+ add : []string {"new.sysctl=newvalue" },
851+ expected : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" , "new.sysctl" : "newvalue" },
852+ },
853+ {
854+ name : "append duplicate is a no-op" ,
855+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
856+ add : []string {"sysctl.one=value-1" },
857+ expected : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
858+ },
859+ {
860+ name : "remove and append existing is a no-op" ,
861+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
862+ add : []string {"sysctl.one=value-1" },
863+ rm : []string {"sysctl.one=value-1" },
864+ expected : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
865+ },
866+ {
867+ name : "remove and append new should append" ,
868+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
869+ add : []string {"new.sysctl=newvalue" },
870+ rm : []string {"new.sysctl=newvalue" },
871+ expected : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" , "new.sysctl" : "newvalue" },
872+ },
873+ {
874+ name : "update existing" ,
875+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
876+ add : []string {"sysctl.one=newvalue" },
877+ expected : map [string ]string {"sysctl.one" : "newvalue" , "sysctl.two" : "value-2" },
878+ },
879+ {
880+ name : "update existing twice" ,
881+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
882+ add : []string {"sysctl.one=newvalue" , "sysctl.one=evennewervalue" },
883+ expected : map [string ]string {"sysctl.one" : "evennewervalue" , "sysctl.two" : "value-2" },
884+ },
885+ {
886+ name : "remove all" ,
887+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
888+ rm : []string {"sysctl.one=value-1" , "sysctl.two=value-2" },
889+ expected : map [string ]string {},
890+ },
891+ {
892+ name : "remove by key" ,
893+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
894+ rm : []string {"sysctl.one" },
895+ expected : map [string ]string {"sysctl.two" : "value-2" },
896+ },
897+ {
898+ name : "remove by key and different value" ,
899+ spec : map [string ]string {"sysctl.one" : "value-1" , "sysctl.two" : "value-2" },
900+ rm : []string {"sysctl.one=anyvalueyoulike" },
901+ expected : map [string ]string {"sysctl.two" : "value-2" },
902+ },
903+ }
904+
905+ for _ , tc := range tests {
906+ t .Run (tc .name , func (t * testing.T ) {
907+ svc := swarm.ServiceSpec {
908+ TaskTemplate : swarm.TaskSpec {
909+ ContainerSpec : & swarm.ContainerSpec {Sysctls : tc .spec },
910+ },
911+ }
912+ flags := newUpdateCommand (nil ).Flags ()
913+ for _ , v := range tc .add {
914+ assert .NilError (t , flags .Set (flagSysCtlAdd , v ))
915+ }
916+ for _ , v := range tc .rm {
917+ assert .NilError (t , flags .Set (flagSysCtlRemove , v ))
918+ }
919+ err := updateService (ctx , & fakeClient {}, flags , & svc )
920+ assert .NilError (t , err )
921+ if ! assert .Check (t , is .DeepEqual (svc .TaskTemplate .ContainerSpec .Sysctls , tc .expected )) {
922+ t .Logf ("expected: %v" , tc .expected )
923+ t .Logf ("actual: %v" , svc .TaskTemplate .ContainerSpec .Sysctls )
924+ }
925+ })
926+ }
927+ }
0 commit comments