github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/internal/renderer/gotemplate/driver_test.go (about)

     1  // +build experimental
     2  
     3  package gotemplate
     4  
     5  import (
     6  	"testing"
     7  
     8  	"gotest.tools/assert"
     9  	is "gotest.tools/assert/cmp"
    10  )
    11  
    12  const (
    13  	compose = `version: "3.4"
    14  services:
    15  {{if .myapp.enable}}
    16    enabledservice:
    17      image: alpine:{{.myapp.alpine_version}}
    18      command: top
    19  {{end}}
    20    other:
    21      image: nginx`
    22  	expectedCompose = `version: "3.4"
    23  services:
    24  
    25    enabledservice:
    26      image: alpine:3.7
    27      command: top
    28  
    29    other:
    30      image: nginx`
    31  )
    32  
    33  var (
    34  	settings = map[string]interface{}{
    35  		"myapp": map[string]interface{}{
    36  			"enable":         true,
    37  			"alpine_version": "3.7",
    38  		},
    39  	}
    40  )
    41  
    42  func TestDriverErrors(t *testing.T) {
    43  	testCases := []struct {
    44  		name          string
    45  		template      string
    46  		settings      map[string]interface{}
    47  		expectedError string
    48  	}{
    49  		{
    50  			name:          "invalid template",
    51  			template:      "{{}}",
    52  			expectedError: "missing value for command",
    53  		},
    54  		{
    55  			name:          "no-settings",
    56  			template:      compose,
    57  			expectedError: "map has no entry for key",
    58  		},
    59  	}
    60  	d := &Driver{}
    61  	for _, tc := range testCases {
    62  		_, err := d.Apply(tc.template, tc.settings)
    63  		assert.Check(t, err != nil)
    64  		assert.Check(t, is.ErrorContains(err, tc.expectedError))
    65  	}
    66  }
    67  
    68  func TestDriver(t *testing.T) {
    69  	d := &Driver{}
    70  	s, err := d.Apply(compose, settings)
    71  	assert.NilError(t, err)
    72  	assert.Check(t, is.Equal(s, expectedCompose))
    73  }