Skip to content

Commit

Permalink
feat(1765): update tests, move unit test to proper file, improve remo…
Browse files Browse the repository at this point in the history
…veEmptyInterfaces algo

Signed-off-by: Martin Jirku <[email protected]>
  • Loading branch information
martinjirku committed Jan 1, 2024
1 parent 3a818f4 commit f80ad97
Show file tree
Hide file tree
Showing 56 changed files with 83 additions and 692 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ tags

.DS_Store

# VSCode specific
.vscode

# Client Test generated files
client/testdata/generated
18 changes: 15 additions & 3 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,30 @@ func removeEmptyInterfaces(obj interface{}) interface{} {
v[i] = removeEmptyInterfaces(val)
}
}
return v
case map[string]interface{}:
for k, val := range v {
if valMap, ok := val.(map[string]interface{}); (ok && len(valMap) == 0) || val == nil {
if valMap, ok := val.(map[string]interface{}); ok {
// It is always map[string]interface{} when passed the map[string]interface{}
valMap := removeEmptyInterfaces(valMap).(map[string]interface{})
if len(valMap) == 0 {
delete(v, k)
}
} else if val == nil {
delete(v, k)
} else {
v[k] = removeEmptyInterfaces(val)
processedInterface := removeEmptyInterfaces(val)
if valSlice, ok := processedInterface.([]interface{}); ok && len(valSlice) == 0 {
delete(v, k)
} else {
v[k] = processedInterface
}
}
}
return v
default:
return v
}
return obj
}

// Convert JSON to YAML.
Expand Down
25 changes: 25 additions & 0 deletions pkg/transformer/kubernetes/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package kubernetes

import (
"fmt"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -704,3 +705,27 @@ func TestFormatEnvName(t *testing.T) {
})
}
}

// Test empty interfaces removal
func TestRemoveEmptyInterfaces(t *testing.T) {
type Obj = map[string]interface{}
var testCases = []struct {
input interface{}
output interface{}
}{
{Obj{"useless": Obj{}}, Obj{}},
{Obj{"usefull": Obj{"usefull": "usefull"}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{Obj{"usefull": Obj{"usefull": "usefull", "uselessdeep": Obj{}, "uselessnil": nil}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{Obj{"uselessdeep": Obj{"uselessdeep": Obj{}, "uselessnil": nil}}, Obj{}},
{Obj{"uselessempty": []interface{}{nil}}, Obj{}},
{"test", "test"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Test removeEmptyInterfaces(%s)", tc.input), func(t *testing.T) {
result := removeEmptyInterfaces(tc.input)
if !reflect.DeepEqual(result, tc.output) {
t.Errorf("Expected %v, got %v", tc.output, result)
}
})
}
}
22 changes: 0 additions & 22 deletions pkg/transformer/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,25 +1145,3 @@ func TestNamespaceGenerationBlank(t *testing.T) {
}
}
}

// Test empty interfaces removal
func TestRemoveEmptyInterfaces(t *testing.T) {
type Obj = map[string]interface{}
var testCases = []struct {
input interface{}
output interface{}
}{
{Obj{"useless": Obj{}}, Obj{}},
{Obj{"usefull": Obj{"usefull": "usefull"}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{Obj{"usefull": Obj{"usefull": "usefull", "uselessdeep": Obj{}, "uselessnil": nil}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{"test", "test"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Test removeEmptyInterfaces(%s)", tc.input), func(t *testing.T) {
result := removeEmptyInterfaces(tc.input)
if !reflect.DeepEqual(result, tc.output) {
t.Errorf("Expected %v, got %v", tc.output, result)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -19,7 +18,6 @@ spec:
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -35,7 +33,6 @@ spec:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -44,25 +41,21 @@ spec:
selector:
matchLabels:
io.kompose.service: redis
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: redis
spec:
containers:
- image: redis
name: redis
resources: {}
restartPolicy: Always

---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -75,7 +68,6 @@ spec:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: web
Expand All @@ -90,12 +82,10 @@ spec:
- containerPort: 5000
hostPort: 5000
protocol: TCP
resources: {}
volumeMounts:
- mountPath: /code
name: code-volume
restartPolicy: Always
volumes:
- emptyDir: {}
name: code-volume
- name: code-volume

12 changes: 1 addition & 11 deletions script/test/fixtures/change-in-volume/output-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -19,7 +18,6 @@ spec:
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -35,7 +33,6 @@ spec:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -44,25 +41,21 @@ spec:
selector:
matchLabels:
io.kompose.service: redis
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: redis
spec:
containers:
- image: redis
name: redis
resources: {}
restartPolicy: Always

---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -75,7 +68,6 @@ spec:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: web
Expand All @@ -90,12 +82,10 @@ spec:
- containerPort: 5000
hostPort: 5000
protocol: TCP
resources: {}
volumeMounts:
- mountPath: /code
name: code-volume
restartPolicy: Always
volumes:
- emptyDir: {}
name: code-volume
- name: code-volume

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -19,7 +18,6 @@ spec:
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -35,27 +33,22 @@ spec:
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
replicas: 1
selector:
io.kompose.service: redis
strategy:
resources: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: redis
spec:
containers:
- image: ' '
name: redis
resources: {}
restartPolicy: Always
test: false
triggers:
Expand All @@ -73,20 +66,16 @@ spec:
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
lookupPolicy:
local: false
tags:
- annotations: null
from:
- from:
kind: DockerImage
name: redis
generation: null
importPolicy: {}
name: latest
referencePolicy:
type: ""
Expand All @@ -95,7 +84,6 @@ spec:
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
Expand All @@ -104,11 +92,9 @@ spec:
selector:
io.kompose.service: web
strategy:
resources: {}
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/change-in-volume-default: "true"
io.kompose.service: web
Expand All @@ -123,14 +109,12 @@ spec:
- containerPort: 5000
hostPort: 5000
protocol: TCP
resources: {}
volumeMounts:
- mountPath: /code
name: code-volume
restartPolicy: Always
volumes:
- emptyDir: {}
name: code-volume
- name: code-volume
test: false
triggers:
- type: ConfigChange
Expand All @@ -147,20 +131,16 @@ spec:
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
spec:
lookupPolicy:
local: false
tags:
- annotations: null
from:
- from:
kind: DockerImage
name: flask_web
generation: null
importPolicy: {}
name: latest
referencePolicy:
type: ""
Expand Down
Loading

0 comments on commit f80ad97

Please sign in to comment.