summaryrefslogtreecommitdiff
path: root/cmd/podman/common/create_test.go
blob: 80e6cbf54aaefd1be1754cabdcd102332ada8627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package common_test

import (
	"reflect"
	"strings"
	"testing"

	"github.com/containers/podman/v4/cmd/podman/common"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/stretchr/testify/assert"
)

func TestPodOptions(t *testing.T) {
	entry := "/test1"
	exampleOptions := entities.ContainerCreateOptions{CPUS: 5.5, CPUSetCPUs: "0-4", Entrypoint: &entry, Hostname: "foo", Name: "testing123", Volume: []string{"/fakeVol1", "/fakeVol2"}, Net: &entities.NetOptions{DNSSearch: []string{"search"}}, PID: "ns:/proc/self/ns"}

	podOptions := entities.PodCreateOptions{}
	err := common.ContainerToPodOptions(&exampleOptions, &podOptions)
	assert.Nil(t, err)

	cc := reflect.ValueOf(&exampleOptions).Elem()
	pc := reflect.ValueOf(&podOptions).Elem()

	pcType := reflect.TypeOf(podOptions)
	for i := 0; i < pc.NumField(); i++ {
		podField := pc.FieldByIndex([]int{i})
		podType := pcType.Field(i)
		for j := 0; j < cc.NumField(); j++ {
			containerField := cc.FieldByIndex([]int{j})
			containerType := reflect.TypeOf(exampleOptions).Field(j)
			tagPod := strings.Split(podType.Tag.Get("json"), ",")[0]
			tagContainer := strings.Split(containerType.Tag.Get("json"), ",")[0]
			if tagPod == tagContainer && (tagPod != "" && tagContainer != "") {
				areEqual := true
				if containerField.Kind() == podField.Kind() {
					switch containerField.Kind() {
					case reflect.Slice:
						for i, w := range containerField.Interface().([]string) {
							areEqual = podField.Interface().([]string)[i] == w
						}
					case reflect.String:
						areEqual = (podField.String() == containerField.String())
					case reflect.Bool:
						areEqual = (podField.Bool() == containerField.Bool())
					case reflect.Ptr:
						areEqual = (reflect.DeepEqual(podField.Elem().Interface(), containerField.Elem().Interface()))
					}
				}
				assert.True(t, areEqual)
			}
		}
	}
}