11import { describe , it } from 'node:test' ;
2- import { deepStrictEqual , strictEqual } from 'node:assert' ;
3- import { V1Namespace } from './api.js' ;
2+ import { deepEqual , deepStrictEqual , strictEqual } from 'node:assert' ;
3+ import { V1CustomResourceDefinition , V1Namespace } from './api.js' ;
44import { dumpYaml , loadAllYaml , loadYaml } from './yaml.js' ;
55
66describe ( 'yaml' , ( ) => {
@@ -12,6 +12,43 @@ describe('yaml', () => {
1212 strictEqual ( ns . kind , 'Namespace' ) ;
1313 strictEqual ( ns . metadata ! . name , 'some-namespace' ) ;
1414 } ) ;
15+ it ( 'should load safely by mapping properties correctly' , ( ) => {
16+ const yaml = `
17+ apiVersion: apiextensions.k8s.io/v1
18+ kind: CustomResourceDefinition
19+ metadata:
20+ name: my-crd.example.com
21+ spec:
22+ group: example.com
23+ versions:
24+ - name: v1
25+ served: true
26+ storage: true
27+ schema:
28+ openAPIV3Schema:
29+ type: object
30+ properties:
31+ foobar:
32+ anyOf:
33+ - type: integer
34+ - type: string
35+ x-kubernetes-int-or-string: true
36+ ` ;
37+ const ns = loadYaml < V1CustomResourceDefinition > ( yaml ) ;
38+
39+ strictEqual ( ns . apiVersion , 'apiextensions.k8s.io/v1' ) ;
40+ strictEqual ( ns . kind , 'CustomResourceDefinition' ) ;
41+ strictEqual ( ns . metadata ! . name , 'my-crd.example.com' ) ;
42+ strictEqual (
43+ ns . spec . versions [ 0 ] ! . schema ! . openAPIV3Schema ! . properties ! [ 'foobar' ] . x_kubernetes_int_or_string ,
44+ true ,
45+ ) ;
46+ strictEqual (
47+ ns . spec . versions [ 0 ] ! . schema ! . openAPIV3Schema ! . properties ! [ 'foobar' ] [ 'x-kubernetes-int-or-string' ] ,
48+ undefined ,
49+ ) ;
50+ } ) ;
51+
1552 it ( 'should load all safely' , ( ) => {
1653 const yaml =
1754 'apiVersion: v1\n' +
@@ -23,15 +60,49 @@ describe('yaml', () => {
2360 'kind: Pod\n' +
2461 'metadata:\n' +
2562 ' name: some-pod\n' +
26- ' namespace: some-ns\n' ;
63+ ' namespace: some-ns\n' +
64+ '---\n' +
65+ 'apiVersion: apiextensions.k8s.io/v1\n' +
66+ 'kind: CustomResourceDefinition\n' +
67+ 'metadata:\n' +
68+ ' name: my-crd.example.com\n' +
69+ 'spec:\n' +
70+ ' group: example.com\n' +
71+ ' versions:\n' +
72+ ' - name: v1\n' +
73+ ' served: true\n' +
74+ ' storage: true\n' +
75+ ' schema:\n' +
76+ ' openAPIV3Schema:\n' +
77+ ' type: object\n' +
78+ ' properties:\n' +
79+ ' foobar:\n' +
80+ ' anyOf:\n' +
81+ ' - type: integer\n' +
82+ ' - type: string\n' +
83+ ' x-kubernetes-int-or-string: true\n' ;
2784 const objects = loadAllYaml ( yaml ) ;
2885
29- strictEqual ( objects . length , 2 ) ;
86+ strictEqual ( objects . length , 3 ) ;
3087 strictEqual ( objects [ 0 ] . kind , 'Namespace' ) ;
3188 strictEqual ( objects [ 1 ] . kind , 'Pod' ) ;
3289 strictEqual ( objects [ 0 ] . metadata . name , 'some-namespace' ) ;
3390 strictEqual ( objects [ 1 ] . metadata . name , 'some-pod' ) ;
3491 strictEqual ( objects [ 1 ] . metadata . namespace , 'some-ns' ) ;
92+ strictEqual ( objects [ 2 ] . apiVersion , 'apiextensions.k8s.io/v1' ) ;
93+ strictEqual ( objects [ 2 ] . kind , 'CustomResourceDefinition' ) ;
94+ strictEqual ( objects [ 2 ] . metadata ! . name , 'my-crd.example.com' ) ;
95+ strictEqual (
96+ objects [ 2 ] . spec . versions [ 0 ] ! . schema ! . openAPIV3Schema ! . properties ! [ 'foobar' ]
97+ . x_kubernetes_int_or_string ,
98+ true ,
99+ ) ;
100+ strictEqual (
101+ objects [ 2 ] . spec . versions [ 0 ] ! . schema ! . openAPIV3Schema ! . properties ! [ 'foobar' ] [
102+ 'x-kubernetes-int-or-string'
103+ ] ,
104+ undefined ,
105+ ) ;
35106 } ) ;
36107 it ( 'should round trip successfully' , ( ) => {
37108 const expected = {
@@ -43,4 +114,44 @@ describe('yaml', () => {
43114 const actual = loadYaml ( yamlString ) ;
44115 deepStrictEqual ( actual , expected ) ;
45116 } ) ;
117+
118+ it ( 'should round trip successfully with mapped properties' , ( ) => {
119+ const expected : V1CustomResourceDefinition = {
120+ apiVersion : 'apiextensions.k8s.io/v1' ,
121+ kind : 'CustomResourceDefinition' ,
122+ metadata : {
123+ name : 'my-crd.example.com' ,
124+ } ,
125+ spec : {
126+ group : 'example.com' ,
127+ names : {
128+ kind : 'MyCRD' ,
129+ plural : 'mycrds' ,
130+ } ,
131+ scope : 'Namespaced' ,
132+ versions : [
133+ {
134+ name : 'v1' ,
135+ schema : {
136+ openAPIV3Schema : {
137+ properties : {
138+ foobar : {
139+ anyOf : [ { type : 'integer' } , { type : 'string' } ] ,
140+ x_kubernetes_int_or_string : true ,
141+ } ,
142+ } ,
143+ type : 'object' ,
144+ } ,
145+ } ,
146+ served : true ,
147+ storage : true ,
148+ } ,
149+ ] ,
150+ } ,
151+ } ;
152+ const yamlString = dumpYaml ( expected ) ;
153+ const actual = loadYaml ( yamlString ) ;
154+ // not using strict equality as types are not matching
155+ deepEqual ( actual , expected ) ;
156+ } ) ;
46157} ) ;
0 commit comments