github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/yaml/build_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  var (
    11  	buildno       = "1"
    12  	user          = "vincent"
    13  	empty         = "\x00"
    14  	testCacheFrom = "someotherimage:latest"
    15  	target        = "intermediateimage"
    16  	network       = "buildnetwork"
    17  )
    18  
    19  func TestMarshalBuild(t *testing.T) {
    20  	builds := []struct {
    21  		build    Build
    22  		expected string
    23  	}{
    24  		{
    25  			expected: `{}
    26  `,
    27  		},
    28  		{
    29  			build: Build{
    30  				Context: ".",
    31  			},
    32  			expected: `context: .
    33  `,
    34  		},
    35  		{
    36  			build: Build{
    37  				Context:    ".",
    38  				Dockerfile: "alternate",
    39  			},
    40  			expected: `context: .
    41  dockerfile: alternate
    42  `,
    43  		},
    44  		{
    45  			build: Build{
    46  				Context:    ".",
    47  				Dockerfile: "alternate",
    48  				Args: map[string]*string{
    49  					"buildno": &buildno,
    50  					"user":    &user,
    51  				},
    52  				CacheFrom: []*string{
    53  					&testCacheFrom,
    54  				},
    55  				Labels: map[string]*string{
    56  					"buildno": &buildno,
    57  					"user":    &user,
    58  				},
    59  				Target:  target,
    60  				Network: network,
    61  			},
    62  			expected: `args:
    63    buildno: "1"
    64    user: vincent
    65  cache_from:
    66  - someotherimage:latest
    67  context: .
    68  dockerfile: alternate
    69  labels:
    70    buildno: "1"
    71    user: vincent
    72  network: buildnetwork
    73  target: intermediateimage
    74  `,
    75  		},
    76  	}
    77  	for _, build := range builds {
    78  		bytes, err := yaml.Marshal(build.build)
    79  		assert.Nil(t, err)
    80  		assert.Equal(t, build.expected, string(bytes), "should be equal")
    81  	}
    82  }
    83  
    84  func TestUnmarshalBuild(t *testing.T) {
    85  	builds := []struct {
    86  		yaml     string
    87  		expected *Build
    88  	}{
    89  		{
    90  			yaml: `.`,
    91  			expected: &Build{
    92  				Context: ".",
    93  			},
    94  		},
    95  		{
    96  			yaml: `context: .`,
    97  			expected: &Build{
    98  				Context: ".",
    99  			},
   100  		},
   101  		{
   102  			yaml: `context: .
   103  dockerfile: alternate`,
   104  			expected: &Build{
   105  				Context:    ".",
   106  				Dockerfile: "alternate",
   107  			},
   108  		},
   109  		{
   110  			yaml: `context: .
   111  dockerfile: alternate
   112  args:
   113    buildno: 1
   114    user: vincent
   115  cache_from:
   116    - someotherimage:latest
   117  labels:
   118    buildno: "1"
   119    user: vincent
   120  target: intermediateimage
   121  network: buildnetwork
   122  `,
   123  			expected: &Build{
   124  				Context:    ".",
   125  				Dockerfile: "alternate",
   126  				Args: map[string]*string{
   127  					"buildno": &buildno,
   128  					"user":    &user,
   129  				},
   130  				CacheFrom: []*string{
   131  					&testCacheFrom,
   132  				},
   133  				Labels: map[string]*string{
   134  					"buildno": &buildno,
   135  					"user":    &user,
   136  				},
   137  				Target:  target,
   138  				Network: network,
   139  			},
   140  		},
   141  		{
   142  			yaml: `context: .
   143  args:
   144    - buildno
   145    - user`,
   146  			expected: &Build{
   147  				Context: ".",
   148  				Args: map[string]*string{
   149  					"buildno": &empty,
   150  					"user":    &empty,
   151  				},
   152  			},
   153  		},
   154  		{
   155  			yaml: `context: .
   156  args:
   157    - buildno=1
   158    - user=vincent`,
   159  			expected: &Build{
   160  				Context: ".",
   161  				Args: map[string]*string{
   162  					"buildno": &buildno,
   163  					"user":    &user,
   164  				},
   165  			},
   166  		},
   167  	}
   168  	for _, build := range builds {
   169  		actual := &Build{}
   170  		err := yaml.Unmarshal([]byte(build.yaml), actual)
   171  		assert.Nil(t, err)
   172  		assert.Equal(t, build.expected, actual, "should be equal")
   173  	}
   174  }