github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/resolver/projection/properties_test.go (about) 1 package projection_test 2 3 import ( 4 "testing" 5 6 "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/projection" 7 "github.com/operator-framework/operator-registry/pkg/api" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestPropertiesAnnotationFromPropertyList(t *testing.T) { 12 for _, tc := range []struct { 13 name string 14 properties []*api.Property 15 expected string 16 error bool 17 }{ 18 { 19 name: "nil property slice", 20 properties: nil, 21 expected: "{}", 22 }, 23 { 24 name: "empty property slice", 25 properties: []*api.Property{}, 26 expected: "{}", 27 }, 28 { 29 name: "invalid property value", 30 properties: []*api.Property{{ 31 Type: "bad", 32 Value: `]`, 33 }}, 34 error: true, 35 }, 36 { 37 name: "nonempty property slice", 38 properties: []*api.Property{ 39 { 40 Type: "string", 41 Value: `"hello"`, 42 }, 43 { 44 Type: "number", 45 Value: `5`, 46 }, 47 { 48 Type: "array", 49 Value: `[1,"two",3,"four"]`, 50 }, { 51 Type: "object", 52 Value: `{"hello":{"worl":"d"}}`, 53 }, 54 }, 55 expected: `{"properties":[{"type":"string","value":"hello"},{"type":"number","value":5},{"type":"array","value":[1,"two",3,"four"]},{"type":"object","value":{"hello":{"worl":"d"}}}]}`, 56 }, 57 { 58 name: "unquoted string", 59 properties: []*api.Property{ 60 { 61 Type: "version", 62 Value: "4.8", 63 }, 64 }, 65 expected: `{"properties":[{"type":"version","value":4.8}]}`, 66 }, 67 } { 68 t.Run(tc.name, func(t *testing.T) { 69 actual, err := projection.PropertiesAnnotationFromPropertyList(tc.properties) 70 assert := assert.New(t) 71 assert.Equal(tc.expected, actual) 72 if tc.error { 73 assert.Error(err) 74 } else { 75 assert.NoError(err) 76 } 77 }) 78 } 79 } 80 81 func TestPropertyListFromPropertiesAnnotation(t *testing.T) { 82 for _, tc := range []struct { 83 name string 84 annotation string 85 expected []*api.Property 86 error bool 87 }{ 88 { 89 name: "empty", 90 annotation: "", 91 error: true, 92 }, 93 { 94 name: "invalid json", 95 annotation: "]", 96 error: true, 97 }, 98 { 99 name: "no properties key", 100 annotation: "{}", 101 expected: nil, 102 }, 103 { 104 name: "properties value not an array or null", 105 annotation: `{"properties":5}`, 106 error: true, 107 }, 108 { 109 name: "property element not an object", 110 annotation: `{"properties":[42]}`, 111 error: true, 112 }, 113 { 114 name: "no properties", 115 annotation: `{"properties":[]}`, 116 expected: nil, 117 }, 118 { 119 name: "several properties", 120 annotation: `{"properties":[{"type":"string","value":"hello"},{"type":"number","value":5},{"type":"array","value":[1,"two",3,"four"]},{"type":"object","value":{"hello":{"worl":"d"}}}]}`, 121 expected: []*api.Property{ 122 { 123 Type: "string", 124 Value: `"hello"`, 125 }, 126 { 127 Type: "number", 128 Value: `5`, 129 }, 130 { 131 Type: "array", 132 Value: `[1,"two",3,"four"]`, 133 }, 134 { 135 Type: "object", 136 Value: `{"hello":{"worl":"d"}}`, 137 }, 138 }, 139 }, 140 { 141 name: "unquoted string values", 142 annotation: `{"properties":[{"type": "version","value": 4.8}]}`, 143 expected: []*api.Property{ 144 { 145 Type: "version", 146 Value: "4.8", 147 }, 148 }, 149 }, 150 } { 151 t.Run(tc.name, func(t *testing.T) { 152 actual, err := projection.PropertyListFromPropertiesAnnotation(tc.annotation) 153 assert := assert.New(t) 154 assert.Equal(tc.expected, actual) 155 if tc.error { 156 assert.Error(err) 157 } else { 158 assert.NoError(err) 159 } 160 }) 161 } 162 }