...

Source file src/gomodules.xyz/jsonpatch/v2/jsonpatch_test.go

Documentation: gomodules.xyz/jsonpatch/v2

     1  package jsonpatch_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	jp "github.com/evanphx/json-patch"
     8  	"github.com/stretchr/testify/assert"
     9  	"gomodules.xyz/jsonpatch/v2"
    10  )
    11  
    12  var simpleA = `{"a":100, "b":200, "c":"hello"}`
    13  var simpleB = `{"a":100, "b":200, "c":"goodbye"}`
    14  var simpleC = `{"a":100, "b":100, "c":"hello"}`
    15  var simpleD = `{"a":100, "b":200, "c":"hello", "d":"foo"}`
    16  var simpleE = `{"a":100, "b":200}`
    17  var simplef = `{"a":100, "b":100, "d":"foo"}`
    18  var simpleG = `{"a":100, "b":null, "d":"foo"}`
    19  var empty = `{}`
    20  
    21  var arraySrc = `
    22  {
    23    "spec": {
    24      "loadBalancerSourceRanges": [
    25        "192.101.0.0/16",
    26        "192.0.0.0/24"
    27      ]
    28    }
    29  }
    30  `
    31  
    32  var arrayDst = `
    33  {
    34    "spec": {
    35      "loadBalancerSourceRanges": [
    36        "192.101.0.0/24"
    37      ]
    38    }
    39  }
    40  `
    41  
    42  var complexBase = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
    43  var complexA = `{"a":100, "b":[{"c1":"goodbye", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
    44  var complexB = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":100, "g":"h", "i":"j"}}`
    45  var complexC = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}, "k":[{"l":"m"}, {"l":"o"}]}`
    46  var complexD = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"}, {"c3":"hello3", "d3":"foo3"} ], "e":{"f":200, "g":"h", "i":"j"}}`
    47  var complexE = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
    48  
    49  var point = `{"type":"Point", "coordinates":[0.0, 1.0]}`
    50  var lineString = `{"type":"LineString", "coordinates":[[0.0, 1.0], [2.0, 3.0]]}`
    51  
    52  var hyperComplexBase = `
    53  {
    54  	"goods": [
    55  	{
    56  		"id": "0001",
    57  		"type": "donut",
    58  		"name": "Cake",
    59  		"ppu": 0.55,
    60  		"batters":
    61  			{
    62  				"batter":
    63  					[
    64  						{ "id": "1001", "type": "Regular" },
    65  						{ "id": "1002", "type": "Chocolate" },
    66  						{ "id": "1003", "type": "Blueberry" },
    67  						{ "id": "1004", "type": "Devil's Food" }
    68  					]
    69  			},
    70  		"topping":
    71  			[
    72  				{ "id": "5001", "type": "None" },
    73  				{ "id": "5002", "type": "Glazed" },
    74  				{ "id": "5005", "type": "Sugar" },
    75  				{ "id": "5007", "type": "Powdered Sugar" },
    76  				{ "id": "5006", "type": "Chocolate with Sprinkles" },
    77  				{ "id": "5003", "type": "Chocolate" },
    78  				{ "id": "5004", "type": "Maple" }
    79  			]
    80  	},
    81  	{
    82  		"id": "0002",
    83  		"type": "donut",
    84  		"name": "Raised",
    85  		"ppu": 0.55,
    86  		"batters":
    87  			{
    88  				"batter":
    89  					[
    90  						{ "id": "1001", "type": "Regular" }
    91  					]
    92  			},
    93  		"topping":
    94  			[
    95  				{ "id": "5001", "type": "None" },
    96  				{ "id": "5002", "type": "Glazed" },
    97  				{ "id": "5005", "type": "Sugar" },
    98  				{ "id": "5003", "type": "Chocolate" },
    99  				{ "id": "5004", "type": "Maple" }
   100  			]
   101  	},
   102  	{
   103  		"id": "0003",
   104  		"type": "donut",
   105  		"name": "Old Fashioned",
   106  		"ppu": 0.55,
   107  		"batters":
   108  			{
   109  				"batter":
   110  					[
   111  						{ "id": "1001", "type": "Regular" },
   112  						{ "id": "1002", "type": "Chocolate" }
   113  					]
   114  			},
   115  		"topping":
   116  			[
   117  				{ "id": "5001", "type": "None" },
   118  				{ "id": "5002", "type": "Glazed" },
   119  				{ "id": "5003", "type": "Chocolate" },
   120  				{ "id": "5004", "type": "Maple" }
   121  			]
   122  	}
   123  ]
   124  }`
   125  
   126  var hyperComplexA = `
   127  {
   128  	"goods": [
   129  	{
   130  		"id": "0001",
   131  		"type": "donut",
   132  		"name": "Cake",
   133  		"ppu": 0.55,
   134  		"batters":
   135  			{
   136  				"batter":
   137  					[
   138  						{ "id": "1001", "type": "Regular" },
   139  						{ "id": "1002", "type": "Chocolate" },
   140  						{ "id": "1003", "type": "Strawberry" },
   141  						{ "id": "1004", "type": "Devil's Food" }
   142  					]
   143  			},
   144  		"topping":
   145  			[
   146  				{ "id": "5001", "type": "None" },
   147  				{ "id": "5002", "type": "Glazed" },
   148  				{ "id": "5005", "type": "Sugar" },
   149  				{ "id": "5007", "type": "Powdered Sugar" },
   150  				{ "id": "5006", "type": "Chocolate with Sprinkles" },
   151  				{ "id": "5003", "type": "Chocolate" },
   152  				{ "id": "5004", "type": "Maple" }
   153  			]
   154  	},
   155  	{
   156  		"id": "0002",
   157  		"type": "donut",
   158  		"name": "Raised",
   159  		"ppu": 0.55,
   160  		"batters":
   161  			{
   162  				"batter":
   163  					[
   164  						{ "id": "1001", "type": "Regular" }
   165  					]
   166  			},
   167  		"topping":
   168  			[
   169  				{ "id": "5001", "type": "None" },
   170  				{ "id": "5002", "type": "Glazed" },
   171  				{ "id": "5005", "type": "Sugar" },
   172  				{ "id": "5003", "type": "Chocolate" },
   173  				{ "id": "5004", "type": "Maple" }
   174  			]
   175  	},
   176  	{
   177  		"id": "0003",
   178  		"type": "donut",
   179  		"name": "Old Fashioned",
   180  		"ppu": 0.55,
   181  		"batters":
   182  			{
   183  				"batter":
   184  					[
   185  						{ "id": "1001", "type": "Regular" },
   186  						{ "id": "1002", "type": "Chocolate" },
   187  						{ "id": "1003", "type": "Vanilla" }
   188  					]
   189  			},
   190  		"topping":
   191  			[
   192  				{ "id": "5001", "type": "None" },
   193  				{ "id": "5002", "type": "Glazed" },
   194  				{ "id": "5004", "type": "Maple" }
   195  			]
   196  	}
   197  ]
   198  }`
   199  
   200  var superComplexBase = `
   201  {
   202  	"annotations": {
   203  		"annotation": [
   204  			{
   205  				"name": "version",
   206  				"value": "8"
   207  			},
   208  			{
   209  				"name": "versionTag",
   210  				"value": "Published on May 13, 2015 at 8:48pm (MST)"
   211  			}
   212  		]
   213  	},
   214  	"attributes": {
   215  		"attribute-key": [
   216  			{
   217  				"id": "3b05c943-d81a-436f-b242-8b519e7a6f30",
   218  				"properties": {
   219  					"visible": true
   220  				}
   221  			},
   222  			{
   223  				"id": "d794c7ee-2a4b-4da4-bba7-e8b973d50c4b",
   224  				"properties": {
   225  					"visible": true
   226  				}
   227  			},
   228  			{
   229  				"id": "a0259458-517c-480f-9f04-9b54b1b2af1f",
   230  				"properties": {
   231  					"visible": true
   232  				}
   233  			},
   234  			{
   235  				"id": "9415f39d-c396-4458-9019-fc076c847964",
   236  				"properties": {
   237  					"visible": true
   238  				}
   239  			},
   240  			{
   241  				"id": "0a2e49a9-8989-42fb-97da-cc66334f828b",
   242  				"properties": {
   243  					"visible": true
   244  				}
   245  			},
   246  			{
   247  				"id": "27f5f14a-ea97-4feb-b22a-6ff754a31212",
   248  				"properties": {
   249  					"visible": true
   250  				}
   251  			},
   252  			{
   253  				"id": "6f810508-4615-4fd0-9e87-80f9c94f9ad8",
   254  				"properties": {
   255  					"visible": true
   256  				}
   257  			},
   258  			{
   259  				"id": "3451b1b2-7365-455c-8bb1-0b464d4d3ba1",
   260  				"properties": {
   261  					"visible": true
   262  				}
   263  			},
   264  			{
   265  				"id": "a82ec957-8c26-41ea-8af6-6dd75c384801",
   266  				"properties": {
   267  					"visible": true
   268  				}
   269  			},
   270  			{
   271  				"id": "736c5496-9a6e-4a82-aa00-456725796432",
   272  				"properties": {
   273  					"visible": true
   274  				}
   275  			},
   276  			{
   277  				"id": "2d428b3c-9d3b-4ec1-bf98-e00673599d60",
   278  				"properties": {
   279  					"visible": true
   280  				}
   281  			},
   282  			{
   283  				"id": "68566ebb-811d-4337-aba9-a8a8baf90e4b",
   284  				"properties": {
   285  					"visible": true
   286  				}
   287  			},
   288  			{
   289  				"id": "ca88bab1-a1ea-40cc-8f96-96d1e9f1217d",
   290  				"properties": {
   291  					"visible": true
   292  				}
   293  			},
   294  			{
   295  				"id": "c63a12c8-542d-47f3-bee1-30b5fe2b0690",
   296  				"properties": {
   297  					"visible": true
   298  				}
   299  			},
   300  			{
   301  				"id": "cbd9e3bc-6a49-432a-a906-b1674c1de24c",
   302  				"properties": {
   303  					"visible": true
   304  				}
   305  			},
   306  			{
   307  				"id": "03262f07-8a15-416d-a3f5-e2bf561c78f9",
   308  				"properties": {
   309  					"visible": true
   310  				}
   311  			},
   312  			{
   313  				"id": "e5c93b87-83fc-45b6-b4d5-bf1e3f523075",
   314  				"properties": {
   315  					"visible": true
   316  				}
   317  			},
   318  			{
   319  				"id": "72260ac5-3d51-49d7-bb31-f794dd129f1c",
   320  				"properties": {
   321  					"visible": true
   322  				}
   323  			},
   324  			{
   325  				"id": "d856bde1-1b42-4935-9bee-c37e886c9ecf",
   326  				"properties": {
   327  					"visible": true
   328  				}
   329  			},
   330  			{
   331  				"id": "62380509-bedf-4134-95c3-77ff377a4a6a",
   332  				"properties": {
   333  					"visible": true
   334  				}
   335  			},
   336  			{
   337  				"id": "f4ed5ac9-b386-49a6-a0a0-6f3341ce9021",
   338  				"properties": {
   339  					"visible": true
   340  				}
   341  			},
   342  			{
   343  				"id": "528d2bd2-87fe-4a49-954a-c93a03256929",
   344  				"properties": {
   345  					"visible": true
   346  				}
   347  			},
   348  			{
   349  				"id": "ff8951f1-61a7-416b-9223-fac4bb6dac50",
   350  				"properties": {
   351  					"visible": true
   352  				}
   353  			},
   354  			{
   355  				"id": "95c2b011-d782-4042-8a07-6aa4a5765c2e",
   356  				"properties": {
   357  					"visible": true
   358  				}
   359  			},
   360  			{
   361  				"id": "dbe5837b-0624-4a05-91f3-67b5bd9b812a",
   362  				"properties": {
   363  					"visible": true
   364  				}
   365  			},
   366  			{
   367  				"id": "13f198ed-82ab-4e51-8144-bfaa5bf77fd5",
   368  				"properties": {
   369  					"visible": true
   370  				}
   371  			},
   372  			{
   373  				"id": "025312eb-12b6-47e6-9750-0fb31ddc2111",
   374  				"properties": {
   375  					"visible": true
   376  				}
   377  			},
   378  			{
   379  				"id": "24292d58-db66-4ef3-8f4f-005d7b719433",
   380  				"properties": {
   381  					"visible": true
   382  				}
   383  			},
   384  			{
   385  				"id": "22e5b5c4-821c-413a-a5b1-ab866d9a03bb",
   386  				"properties": {
   387  					"visible": true
   388  				}
   389  			},
   390  			{
   391  				"id": "2fde0aac-df89-403d-998e-854b949c7b57",
   392  				"properties": {
   393  					"visible": true
   394  				}
   395  			},
   396  			{
   397  				"id": "8b576876-5c16-4178-805e-24984c24fac3",
   398  				"properties": {
   399  					"visible": true
   400  				}
   401  			},
   402  			{
   403  				"id": "415b7d2a-b362-4f1e-b83a-927802328ecb",
   404  				"properties": {
   405  					"visible": true
   406  				}
   407  			},
   408  			{
   409  				"id": "8ef24fc2-ab25-4f22-9d9f-61902b49dc01",
   410  				"properties": {
   411  					"visible": true
   412  				}
   413  			},
   414  			{
   415  				"id": "2299b09e-9f8e-4b79-a55c-a7edacde2c85",
   416  				"properties": {
   417  					"visible": true
   418  				}
   419  			},
   420  			{
   421  				"id": "bf506538-f438-425c-be85-5aa2f9b075b8",
   422  				"properties": {
   423  					"visible": true
   424  				}
   425  			},
   426  			{
   427  				"id": "2b501dc6-799d-4675-9144-fac77c50c57c",
   428  				"properties": {
   429  					"visible": true
   430  				}
   431  			},
   432  			{
   433  				"id": "c0446da1-e069-417e-bd5a-34edcd028edc",
   434  				"properties": {
   435  					"visible": true
   436  				}
   437  			}
   438  		]
   439  	}
   440  }`
   441  
   442  var superComplexA = `
   443  {
   444  	"annotations": {
   445  		"annotation": [
   446  			{
   447  				"name": "version",
   448  				"value": "8"
   449  			},
   450  			{
   451  				"name": "versionTag",
   452  				"value": "Published on May 13, 2015 at 8:48pm (MST)"
   453  			}
   454  		]
   455  	},
   456  	"attributes": {
   457  		"attribute-key": [
   458  			{
   459  				"id": "3b05c943-d81a-436f-b242-8b519e7a6f30",
   460  				"properties": {
   461  					"visible": true
   462  				}
   463  			},
   464  			{
   465  				"id": "d794c7ee-2a4b-4da4-bba7-e8b973d50c4b",
   466  				"properties": {
   467  					"visible": true
   468  				}
   469  			},
   470  			{
   471  				"id": "a0259458-517c-480f-9f04-9b54b1b2af1f",
   472  				"properties": {
   473  					"visible": true
   474  				}
   475  			},
   476  			{
   477  				"id": "9415f39d-c396-4458-9019-fc076c847964",
   478  				"properties": {
   479  					"visible": true
   480  				}
   481  			},
   482  			{
   483  				"id": "0a2e49a9-8989-42fb-97da-cc66334f828b",
   484  				"properties": {
   485  					"visible": true
   486  				}
   487  			},
   488  			{
   489  				"id": "27f5f14a-ea97-4feb-b22a-6ff754a31212",
   490  				"properties": {
   491  					"visible": true
   492  				}
   493  			},
   494  			{
   495  				"id": "6f810508-4615-4fd0-9e87-80f9c94f9ad8",
   496  				"properties": {
   497  					"visible": true
   498  				}
   499  			},
   500  			{
   501  				"id": "3451b1b2-7365-455c-8bb1-0b464d4d3ba1",
   502  				"properties": {
   503  					"visible": true
   504  				}
   505  			},
   506  			{
   507  				"id": "a82ec957-8c26-41ea-8af6-6dd75c384801",
   508  				"properties": {
   509  					"visible": true
   510  				}
   511  			},
   512  			{
   513  				"id": "736c5496-9a6e-4a82-aa00-456725796432",
   514  				"properties": {
   515  					"visible": true
   516  				}
   517  			},
   518  			{
   519  				"id": "2d428b3c-9d3b-4ec1-bf98-e00673599d60",
   520  				"properties": {
   521  					"visible": true
   522  				}
   523  			},
   524  			{
   525  				"id": "68566ebb-811d-4337-aba9-a8a8baf90e4b",
   526  				"properties": {
   527  					"visible": true
   528  				}
   529  			},
   530  			{
   531  				"id": "ca88bab1-a1ea-40cc-8f96-96d1e9f1217d",
   532  				"properties": {
   533  					"visible": true
   534  				}
   535  			},
   536  			{
   537  				"id": "c63a12c8-542d-47f3-bee1-30b5fe2b0690",
   538  				"properties": {
   539  					"visible": true
   540  				}
   541  			},
   542  			{
   543  				"id": "cbd9e3bc-6a49-432a-a906-b1674c1de24c",
   544  				"properties": {
   545  					"visible": true
   546  				}
   547  			},
   548  			{
   549  				"id": "03262f07-8a15-416d-a3f5-e2bf561c78f9",
   550  				"properties": {
   551  					"visible": true
   552  				}
   553  			},
   554  			{
   555  				"id": "e5c93b87-83fc-45b6-b4d5-bf1e3f523075",
   556  				"properties": {
   557  					"visible": true
   558  				}
   559  			},
   560  			{
   561  				"id": "72260ac5-3d51-49d7-bb31-f794dd129f1c",
   562  				"properties": {
   563  					"visible": true
   564  				}
   565  			},
   566  			{
   567  				"id": "d856bde1-1b42-4935-9bee-c37e886c9ecf",
   568  				"properties": {
   569  					"visible": true
   570  				}
   571  			},
   572  			{
   573  				"id": "62380509-bedf-4134-95c3-77ff377a4a6a",
   574  				"properties": {
   575  					"visible": true
   576  				}
   577  			},
   578  			{
   579  				"id": "f4ed5ac9-b386-49a6-a0a0-6f3341ce9021",
   580  				"properties": {
   581  					"visible": true
   582  				}
   583  			},
   584  			{
   585  				"id": "528d2bd2-87fe-4a49-954a-c93a03256929",
   586  				"properties": {
   587  					"visible": true
   588  				}
   589  			},
   590  			{
   591  				"id": "ff8951f1-61a7-416b-9223-fac4bb6dac50",
   592  				"properties": {
   593  					"visible": true
   594  				}
   595  			},
   596  			{
   597  				"id": "95c2b011-d782-4042-8a07-6aa4a5765c2e",
   598  				"properties": {
   599  					"visible": true
   600  				}
   601  			},
   602  			{
   603  				"id": "dbe5837b-0624-4a05-91f3-67b5bd9b812a",
   604  				"properties": {
   605  					"visible": true
   606  				}
   607  			},
   608  			{
   609  				"id": "13f198ed-82ab-4e51-8144-bfaa5bf77fd5",
   610  				"properties": {
   611  					"visible": true
   612  				}
   613  			},
   614  			{
   615  				"id": "025312eb-12b6-47e6-9750-0fb31ddc2111",
   616  				"properties": {
   617  					"visible": true
   618  				}
   619  			},
   620  			{
   621  				"id": "24292d58-db66-4ef3-8f4f-005d7b719433",
   622  				"properties": {
   623  					"visible": true
   624  				}
   625  			},
   626  			{
   627  				"id": "22e5b5c4-821c-413a-a5b1-ab866d9a03bb",
   628  				"properties": {
   629  					"visible": true
   630  				}
   631  			},
   632  			{
   633  				"id": "2fde0aac-df89-403d-998e-854b949c7b57",
   634  				"properties": {
   635  					"visible": true
   636  				}
   637  			},
   638  			{
   639  				"id": "8b576876-5c16-4178-805e-24984c24fac3",
   640  				"properties": {
   641  					"visible": true
   642  				}
   643  			},
   644  			{
   645  				"id": "415b7d2a-b362-4f1e-b83a-927802328ecb",
   646  				"properties": {
   647  					"visible": true
   648  				}
   649  			},
   650  			{
   651  				"id": "8ef24fc2-ab25-4f22-9d9f-61902b49dc01",
   652  				"properties": {
   653  					"visible": true
   654  				}
   655  			},
   656  			{
   657  				"id": "2299b09e-9f8e-4b79-a55c-a7edacde2c85",
   658  				"properties": {
   659  					"visible": true
   660  				}
   661  			},
   662  			{
   663  				"id": "bf506538-f438-425c-be85-5aa2f9b075b8",
   664  				"properties": {
   665  					"visible": true
   666  				}
   667  			},
   668  			{
   669  				"id": "2b501dc6-799d-4675-9144-fac77c50c57c",
   670  				"properties": {
   671  					"visible": true
   672  				}
   673  			},
   674  			{
   675  				"id": "c0446da1-e069-417e-bd5a-34edcd028edc",
   676  				"properties": {
   677  					"visible": false
   678  				}
   679  			}
   680  		]
   681  	}
   682  }`
   683  
   684  var (
   685  	oldDeployment = `{
   686    "apiVersion": "apps/v1beta1",
   687    "kind": "Deployment",
   688    "metadata": {
   689      "annotations": {
   690        "k8s.io/app": "busy-dep"
   691      }
   692    }
   693  }`
   694  
   695  	newDeployment = `{
   696    "apiVersion": "apps/v1beta1",
   697    "kind": "Deployment",
   698    "metadata": {
   699      "annotations": {
   700        "k8s.io/app": "busy-dep",
   701        "docker.com/commit": "github.com/myrepo#xyz"
   702      }
   703    }
   704  }`
   705  )
   706  
   707  var (
   708  	oldNestedObj = `{
   709    "apiVersion": "kubedb.com/v1alpha1",
   710    "kind": "Elasticsearch",
   711    "metadata": {
   712      "name": "quick-elasticsearch",
   713      "namespace": "demo"
   714    },
   715    "spec": {
   716      "doNotPause": true,
   717      "version": "5.6"
   718    }
   719  }`
   720  
   721  	newNestedObj = `{
   722    "apiVersion": "kubedb.com/v1alpha1",
   723    "kind": "Elasticsearch",
   724    "metadata": {
   725      "name": "quick-elasticsearch",
   726      "namespace": "demo"
   727    },
   728    "spec": {
   729      "doNotPause": true,
   730      "version": "5.6",
   731      "storageType": "Durable",
   732      "updateStrategy": {
   733        "type": "RollingUpdate"
   734      },
   735      "terminationPolicy": "Pause"
   736    }
   737  }`
   738  )
   739  
   740  var (
   741  	oldArray = `{
   742    "apiVersion": "kubedb.com/v1alpha1",
   743    "kind": "Elasticsearch",
   744    "metadata": {
   745      "name": "quick-elasticsearch",
   746      "namespace": "demo"
   747    },
   748    "spec": {
   749      "tolerations": [
   750        {
   751            "key": "node.kubernetes.io/key1",
   752            "operator": "Equal",
   753            "value": "value1",
   754            "effect": "NoSchedule"
   755        },
   756        {
   757            "key": "node.kubernetes.io/key2",
   758            "operator": "Equal",
   759            "value": "value2",
   760            "effect": "NoSchedule"
   761        },
   762        {
   763            "key": "node.kubernetes.io/not-ready",
   764            "operator": "Exists",
   765            "effect": "NoExecute",
   766            "tolerationSeconds": 300
   767        },
   768        {
   769            "key": "node.kubernetes.io/unreachable",
   770            "operator": "Exists",
   771            "effect": "NoExecute",
   772            "tolerationSeconds": 300
   773        }
   774      ]
   775    }
   776  }`
   777  
   778  	newArray = `{
   779    "apiVersion": "kubedb.com/v1alpha1",
   780    "kind": "Elasticsearch",
   781    "metadata": {
   782      "name": "quick-elasticsearch",
   783      "namespace": "demo"
   784    },
   785    "spec": {
   786      "tolerations": [
   787        {
   788            "key": "node.kubernetes.io/key2",
   789            "operator": "Equal",
   790            "value": "value2",
   791            "effect": "NoSchedule"
   792        },
   793        {
   794            "key": "node.kubernetes.io/key1",
   795            "operator": "Equal",
   796            "value": "value1",
   797            "effect": "NoSchedule"
   798        }
   799      ]
   800    }
   801  }`
   802  )
   803  
   804  var (
   805  	nullKeyA = `{
   806    "apiVersion": "cert-manager.io/v1",
   807    "kind": "CertificateRequest",
   808    "metadata": {
   809      "creationTimestamp": null,
   810      "name": "test-cr",
   811      "namespace": "default-unit-test-ns"
   812    },
   813    "spec": {
   814      "issuerRef": {
   815        "name": ""
   816      },
   817      "request": null
   818    },
   819    "status": {}
   820  }`
   821  	nullKeyB = `{
   822    "apiVersion": "cert-manager.io/v1",
   823    "kind": "CertificateRequest",
   824    "metadata": {
   825      "creationTimestamp": null,
   826      "name": "test-cr",
   827      "namespace": "default-unit-test-ns"
   828    },
   829    "spec": {
   830      "issuerRef": {
   831        "name": ""
   832      },
   833      "request": "bXV0YXRpb24gY2FsbGVk"
   834    },
   835    "status": {}
   836  }`
   837  )
   838  
   839  var (
   840  	emptyKeyA = `{"":[0]}`
   841  	emptyKeyB = `{"":[]}`
   842  )
   843  
   844  var (
   845  	specialChars = string([]byte{123, 34, 92, 98, 34, 58, 91, 93, 125})
   846  )
   847  
   848  func TestCreatePatch(t *testing.T) {
   849  	cases := []struct {
   850  		name string
   851  		src  string
   852  		dst  string
   853  	}{
   854  		// simple
   855  		{"Simple:OneNullReplace", simplef, simpleG},
   856  		{"Simple:Same", simpleA, simpleA},
   857  		{"Simple:OneStringReplace", simpleA, simpleB},
   858  		{"Simple:OneIntReplace", simpleA, simpleC},
   859  		{"Simple:OneAdd", simpleA, simpleD},
   860  		{"Simple:OneRemove", simpleA, simpleE},
   861  		{"Simple:VsEmpty", simpleA, empty},
   862  		// array types
   863  		{"Array:Same", arraySrc, arraySrc},
   864  		{"Array:BoolReplace", arraySrc, arrayDst},
   865  		{"Array:AlmostSame", `{"Lines":[1,2,3,4,5,6,7,8,9,10]}`, `{"Lines":[2,3,4,5,6,7,8,9,10,11]}`},
   866  		{"Array:Remove", `{"x":["A", "B", "C"]}`, `{"x":["D"]}`},
   867  		{"Array:EditDistance", `{"letters":["A","B","C","D","E","F","G","H","I","J","K"]}`, `{"letters":["L","M","N"]}`},
   868  		// complex types
   869  		{"Complex:Same", complexBase, complexBase},
   870  		{"Complex:OneStringReplaceInArray", complexBase, complexA},
   871  		{"Complex:OneIntReplace", complexBase, complexB},
   872  		{"Complex:OneAdd", complexBase, complexC},
   873  		{"Complex:OneAddToArray", complexBase, complexC},
   874  		{"Complex:VsEmpty", complexBase, empty},
   875  		// geojson
   876  		{"GeoJson:PointLineStringReplace", point, lineString},
   877  		{"GeoJson:LineStringPointReplace", lineString, point},
   878  		// HyperComplex
   879  		{"HyperComplex:Same", hyperComplexBase, hyperComplexBase},
   880  		{"HyperComplex:BoolReplace", hyperComplexBase, hyperComplexA},
   881  		// SuperComplex
   882  		{"SuperComplex:Same", superComplexBase, superComplexBase},
   883  		{"SuperComplex:BoolReplace", superComplexBase, superComplexA},
   884  		// map
   885  		{"Kubernetes:Annotations", oldDeployment, newDeployment},
   886  		// crd with nested object
   887  		{"Nested Member Object", oldNestedObj, newNestedObj},
   888  		// array with different order
   889  		{"Different Array", oldArray, newArray},
   890  		{"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
   891  		{"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
   892  		{"Null Key uses replace operation", nullKeyA, nullKeyB},
   893  		// empty key
   894  		{"Empty key", emptyKeyA, emptyKeyB},
   895  		// special chars
   896  		{"Special chars", empty, specialChars},
   897  	}
   898  
   899  	for _, c := range cases {
   900  		t.Run(c.name+"[src->dst]", func(t *testing.T) {
   901  			check(t, c.src, c.dst)
   902  		})
   903  		t.Run(c.name+"[dst->src]", func(t *testing.T) {
   904  			check(t, c.dst, c.src)
   905  		})
   906  	}
   907  }
   908  
   909  func check(t *testing.T, src, dst string) {
   910  	patch, err := jsonpatch.CreatePatch([]byte(src), []byte(dst))
   911  	assert.Nil(t, err)
   912  
   913  	data, err := json.Marshal(patch)
   914  	assert.Nil(t, err)
   915  
   916  	p2, err := jp.DecodePatch(data)
   917  	assert.Nil(t, err)
   918  
   919  	d2, err := p2.Apply([]byte(src))
   920  	assert.Nil(t, err)
   921  
   922  	assert.JSONEq(t, dst, string(d2))
   923  }
   924  

View as plain text