github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/yaml/volume_test.go (about) 1 package yaml 2 3 import ( 4 "testing" 5 6 "gopkg.in/yaml.v2" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestMarshalVolumes(t *testing.T) { 12 volumes := []struct { 13 volumes Volumes 14 expected string 15 }{ 16 { 17 volumes: Volumes{}, 18 expected: `[] 19 `, 20 }, 21 { 22 volumes: Volumes{ 23 Volumes: []*Volume{ 24 { 25 Destination: "/in/the/container", 26 }, 27 }, 28 }, 29 expected: `- /in/the/container 30 `, 31 }, 32 { 33 volumes: Volumes{ 34 Volumes: []*Volume{ 35 { 36 Source: "./a/path", 37 Destination: "/in/the/container", 38 AccessMode: "ro", 39 }, 40 }, 41 }, 42 expected: `- ./a/path:/in/the/container:ro 43 `, 44 }, 45 { 46 volumes: Volumes{ 47 Volumes: []*Volume{ 48 { 49 Source: "./a/path", 50 Destination: "/in/the/container", 51 }, 52 }, 53 }, 54 expected: `- ./a/path:/in/the/container 55 `, 56 }, 57 { 58 volumes: Volumes{ 59 Volumes: []*Volume{ 60 { 61 Source: "./a/path", 62 Destination: "/in/the/container", 63 }, 64 { 65 Source: "named", 66 Destination: "/in/the/container", 67 }, 68 }, 69 }, 70 expected: `- ./a/path:/in/the/container 71 - named:/in/the/container 72 `, 73 }, 74 } 75 for _, volume := range volumes { 76 bytes, err := yaml.Marshal(volume.volumes) 77 assert.Nil(t, err) 78 assert.Equal(t, volume.expected, string(bytes), "should be equal") 79 } 80 } 81 82 func TestUnmarshalVolumes(t *testing.T) { 83 volumes := []struct { 84 yaml string 85 expected *Volumes 86 }{ 87 { 88 yaml: `- ./a/path:/in/the/container`, 89 expected: &Volumes{ 90 Volumes: []*Volume{ 91 { 92 Source: "./a/path", 93 Destination: "/in/the/container", 94 }, 95 }, 96 }, 97 }, 98 { 99 yaml: `- /in/the/container`, 100 expected: &Volumes{ 101 Volumes: []*Volume{ 102 { 103 Destination: "/in/the/container", 104 }, 105 }, 106 }, 107 }, 108 { 109 yaml: `- /a/path:/in/the/container:ro`, 110 expected: &Volumes{ 111 Volumes: []*Volume{ 112 { 113 Source: "/a/path", 114 Destination: "/in/the/container", 115 AccessMode: "ro", 116 }, 117 }, 118 }, 119 }, 120 { 121 yaml: `- /a/path:/in/the/container 122 - named:/somewhere/in/the/container`, 123 expected: &Volumes{ 124 Volumes: []*Volume{ 125 { 126 Source: "/a/path", 127 Destination: "/in/the/container", 128 }, 129 { 130 Source: "named", 131 Destination: "/somewhere/in/the/container", 132 }, 133 }, 134 }, 135 }, 136 } 137 for _, volume := range volumes { 138 actual := &Volumes{} 139 err := yaml.Unmarshal([]byte(volume.yaml), actual) 140 assert.Nil(t, err) 141 assert.Equal(t, volume.expected, actual, "should be equal") 142 } 143 }