@@ -796,3 +796,84 @@ func TestKustomizeStaticPod(t *testing.T) {
796
796
t .Error ("Kustomize did not apply patches corresponding to the resource" )
797
797
}
798
798
}
799
+
800
+ func TestPatchStaticPod (t * testing.T ) {
801
+ type file struct {
802
+ name string
803
+ data string
804
+ }
805
+
806
+ tests := []struct {
807
+ name string
808
+ files []* file
809
+ pod * v1.Pod
810
+ expectedPod * v1.Pod
811
+ expectedError bool
812
+ }{
813
+ {
814
+ name : "valid: patch a kube-apiserver target using a couple of ordered patches" ,
815
+ pod : & v1.Pod {
816
+ ObjectMeta : metav1.ObjectMeta {
817
+ Name : "kube-apiserver" ,
818
+ Namespace : "foo" ,
819
+ },
820
+ },
821
+ expectedPod : & v1.Pod {
822
+ ObjectMeta : metav1.ObjectMeta {
823
+ Name : "kube-apiserver" ,
824
+ Namespace : "bar2" ,
825
+ },
826
+ },
827
+ files : []* file {
828
+ {
829
+ name : "kube-apiserver1+merge.json" ,
830
+ data : `{"metadata":{"namespace":"bar2"}}` ,
831
+ },
832
+ {
833
+ name : "kube-apiserver0+json.json" ,
834
+ data : `[{"op": "replace", "path": "/metadata/namespace", "value": "bar1"}]` ,
835
+ },
836
+ },
837
+ },
838
+ {
839
+ name : "invalid: unknown patch target name" ,
840
+ pod : & v1.Pod {
841
+ ObjectMeta : metav1.ObjectMeta {
842
+ Name : "foo" ,
843
+ Namespace : "bar" ,
844
+ },
845
+ },
846
+ expectedError : true ,
847
+ },
848
+ }
849
+
850
+ for _ , tc := range tests {
851
+ t .Run (tc .name , func (t * testing.T ) {
852
+ tempDir , err := ioutil .TempDir ("" , "patch-files" )
853
+ if err != nil {
854
+ t .Fatal (err )
855
+ }
856
+ defer os .RemoveAll (tempDir )
857
+
858
+ for _ , file := range tc .files {
859
+ filePath := filepath .Join (tempDir , file .name )
860
+ err := ioutil .WriteFile (filePath , []byte (file .data ), 0644 )
861
+ if err != nil {
862
+ t .Fatalf ("could not write temporary file %q" , filePath )
863
+ }
864
+ }
865
+
866
+ pod , err := PatchStaticPod (tc .pod , tempDir , ioutil .Discard )
867
+ if (err != nil ) != tc .expectedError {
868
+ t .Fatalf ("expected error: %v, got: %v, error: %v" , tc .expectedError , (err != nil ), err )
869
+ }
870
+ if err != nil {
871
+ return
872
+ }
873
+
874
+ if tc .expectedPod .String () != pod .String () {
875
+ t .Fatalf ("expected object:\n %s\n got:\n %s" , tc .expectedPod .String (), pod .String ())
876
+ }
877
+ })
878
+ }
879
+ }
0 commit comments