github.com/camronlevanger/libcompose@v0.4.1-0.20180423130544-6bb86d53fa21/config/marshal_config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	yamlTypes "github.com/docker/libcompose/yaml"
     7  	"github.com/stretchr/testify/assert"
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  type TestConfig struct {
    12  	SystemContainers map[string]*ServiceConfig
    13  }
    14  
    15  func newTestConfig() TestConfig {
    16  	return TestConfig{
    17  		SystemContainers: map[string]*ServiceConfig{
    18  			"udev": {
    19  				Image:       "udev",
    20  				Restart:     "always",
    21  				NetworkMode: "host",
    22  				Privileged:  true,
    23  				DNS:         []string{"8.8.8.8", "8.8.4.4"},
    24  				Environment: yamlTypes.MaporEqualSlice([]string{
    25  					"DAEMON=true",
    26  				}),
    27  				Labels: yamlTypes.SliceorMap{
    28  					"io.rancher.os.detach": "true",
    29  					"io.rancher.os.scope":  "system",
    30  				},
    31  				VolumesFrom: []string{
    32  					"system-volumes",
    33  				},
    34  				Ulimits: yamlTypes.Ulimits{
    35  					Elements: []yamlTypes.Ulimit{
    36  						yamlTypes.NewUlimit("nproc", 65557, 65557),
    37  					},
    38  				},
    39  			},
    40  			"system-volumes": {
    41  				Image:       "state",
    42  				NetworkMode: "none",
    43  				ReadOnly:    true,
    44  				Privileged:  true,
    45  				Labels: yamlTypes.SliceorMap{
    46  					"io.rancher.os.createonly": "true",
    47  					"io.rancher.os.scope":      "system",
    48  				},
    49  				Volumes: &yamlTypes.Volumes{
    50  					Volumes: []*yamlTypes.Volume{
    51  						{
    52  							Source:      "/dev",
    53  							Destination: "/host/dev",
    54  						},
    55  						{
    56  							Source:      "/var/lib/rancher/conf",
    57  							Destination: "/var/lib/rancher/conf",
    58  						},
    59  						{
    60  							Source:      "/etc/ssl/certs/ca-certificates.crt",
    61  							Destination: "/etc/ssl/certs/ca-certificates.crt.rancher",
    62  						},
    63  						{
    64  							Source:      "/lib/modules",
    65  							Destination: "lib/modules",
    66  						},
    67  						{
    68  							Source:      "/lib/firmware",
    69  							Destination: "/lib/firmware",
    70  						},
    71  						{
    72  							Source:      "/var/run",
    73  							Destination: "/var/run",
    74  						},
    75  						{
    76  							Source:      "/var/log",
    77  							Destination: "/var/log",
    78  						},
    79  					},
    80  				},
    81  				Logging: Log{
    82  					Driver: "json-file",
    83  				},
    84  			},
    85  		},
    86  	}
    87  }
    88  
    89  func TestMarshalConfig(t *testing.T) {
    90  	config := newTestConfig()
    91  	bytes, err := yaml.Marshal(config)
    92  	assert.Nil(t, err)
    93  
    94  	config2 := TestConfig{}
    95  
    96  	err = yaml.Unmarshal(bytes, &config2)
    97  	assert.Nil(t, err)
    98  
    99  	assert.Equal(t, config, config2)
   100  }
   101  
   102  func TestMarshalServiceConfig(t *testing.T) {
   103  	configPtr := newTestConfig().SystemContainers["udev"]
   104  	bytes, err := yaml.Marshal(configPtr)
   105  	assert.Nil(t, err)
   106  
   107  	configPtr2 := &ServiceConfig{}
   108  
   109  	err = yaml.Unmarshal(bytes, configPtr2)
   110  	assert.Nil(t, err)
   111  
   112  	assert.Equal(t, configPtr, configPtr2)
   113  }