@@ -155,7 +155,7 @@ configMapGenerator:
155
155
}
156
156
}
157
157
158
- func Test_ObjectAddLabels (t * testing.T ) {
158
+ func Test_AddLabels (t * testing.T ) {
159
159
inputManifest := `---
160
160
apiVersion: apps/v1
161
161
kind: Deployment
@@ -841,3 +841,144 @@ spec:
841
841
})
842
842
}
843
843
}
844
+
845
+ func Test_Sort (t * testing.T ) {
846
+ deployment1 := & Object {
847
+ object : & unstructured.Unstructured {
848
+ Object : map [string ]interface {}{
849
+ "apiVersion" : "apps/v1" ,
850
+ "kind" : "Deployment" ,
851
+ "metadata" : map [string ]interface {}{
852
+ "name" : "frontend111" ,
853
+ },
854
+ },
855
+ },
856
+ Name : "frontend111" ,
857
+ Kind : "Deployment" ,
858
+ Group : "apps" ,
859
+ }
860
+ deployment2 := & Object {
861
+ object : & unstructured.Unstructured {
862
+ Object : map [string ]interface {}{
863
+ "apiVersion" : "apps/v1" ,
864
+ "kind" : "Deployment" ,
865
+ "metadata" : map [string ]interface {}{
866
+ "name" : "frontend22222" ,
867
+ },
868
+ },
869
+ },
870
+ Name : "frontend22222" ,
871
+ Kind : "Deployment" ,
872
+ Group : "apps" ,
873
+ }
874
+ service := & Object {
875
+ object : & unstructured.Unstructured {
876
+ Object : map [string ]interface {}{
877
+ "apiVersion" : "v1" ,
878
+ "kind" : "Service" ,
879
+ "metadata" : map [string ]interface {}{
880
+ "name" : "frontend-service" ,
881
+ },
882
+ },
883
+ },
884
+ Name : "frontend-service" ,
885
+ Kind : "Service" ,
886
+ Group : "" ,
887
+ }
888
+ serviceAccount := & Object {
889
+ object : & unstructured.Unstructured {
890
+ Object : map [string ]interface {}{
891
+ "apiVersion" : "v1" ,
892
+ "kind" : "ServiceAccount" ,
893
+ "metadata" : map [string ]interface {}{
894
+ "name" : "serviceaccount" ,
895
+ },
896
+ },
897
+ },
898
+ Name : "serviceaccount" ,
899
+ Kind : "ServiceAccount" ,
900
+ Group : "" ,
901
+ }
902
+ tests := []struct {
903
+ name string
904
+ inputObjects * Objects
905
+ expectedObjects * Objects
906
+ error bool
907
+ scoreFunc func (* Object ) int
908
+ }{
909
+ {
910
+ name : "sort with score function's result" ,
911
+ inputObjects : & Objects {
912
+ Items : []* Object {
913
+ deployment2 ,
914
+ deployment1 ,
915
+ },
916
+ },
917
+ expectedObjects : & Objects {
918
+ Items : []* Object {
919
+ deployment1 ,
920
+ deployment2 ,
921
+ },
922
+ },
923
+ error : false ,
924
+ scoreFunc : func (o * Object ) int { return len (o .Name ) },
925
+ },
926
+ {
927
+ name : "sort with Group" ,
928
+ inputObjects : & Objects {
929
+ Items : []* Object {
930
+ deployment1 ,
931
+ service ,
932
+ },
933
+ },
934
+ expectedObjects : & Objects {
935
+ Items : []* Object {
936
+ service ,
937
+ deployment1 ,
938
+ },
939
+ },
940
+ error : false ,
941
+ scoreFunc : func (o * Object ) int { return 0 },
942
+ },
943
+ {
944
+ name : "sort with Kind" ,
945
+ inputObjects : & Objects {
946
+ Items : []* Object {
947
+ serviceAccount ,
948
+ service ,
949
+ },
950
+ },
951
+ expectedObjects : & Objects {
952
+ Items : []* Object {
953
+ service ,
954
+ serviceAccount ,
955
+ },
956
+ },
957
+ error : false ,
958
+ scoreFunc : func (o * Object ) int { return 0 },
959
+ },
960
+ {
961
+ name : "sort with Name" ,
962
+ inputObjects : & Objects {
963
+ Items : []* Object {
964
+ deployment2 ,
965
+ deployment1 ,
966
+ },
967
+ },
968
+ expectedObjects : & Objects {
969
+ Items : []* Object {
970
+ deployment1 ,
971
+ deployment2 ,
972
+ },
973
+ },
974
+ error : false ,
975
+ scoreFunc : func (o * Object ) int { return 0 },
976
+ },
977
+ }
978
+ for _ , tt := range tests {
979
+ t .Run (tt .name , func (t * testing.T ) {
980
+ tt .inputObjects .Sort (tt .scoreFunc )
981
+ assert .Equal (t , tt .expectedObjects , tt .inputObjects )
982
+ })
983
+ }
984
+ }
0 commit comments