github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/render/render_test.go (about)

     1  package render
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/docker/app/types"
     8  	composetypes "github.com/docker/cli/cli/compose/types"
     9  	yaml "gopkg.in/yaml.v2"
    10  	"gotest.tools/assert"
    11  	is "gotest.tools/assert/cmp"
    12  )
    13  
    14  const (
    15  	validMeta = `version: "0.1"
    16  name: my-app`
    17  )
    18  
    19  func TestRenderMissingValue(t *testing.T) {
    20  	configFiles := []composetypes.ConfigFile{
    21  		{
    22  			Config: map[string]interface{}{
    23  				"version": "3",
    24  				"services": map[string]interface{}{
    25  					"foo": map[string]interface{}{
    26  						"image": "${imageName}:${version}",
    27  					},
    28  				},
    29  			},
    30  		},
    31  	}
    32  	finalEnv := map[string]string{
    33  		"imageName": "foo",
    34  	}
    35  	_, err := render(configFiles, finalEnv)
    36  	assert.Check(t, err != nil)
    37  	assert.Check(t, is.ErrorContains(err, "required variable"))
    38  }
    39  
    40  func TestRender(t *testing.T) {
    41  	configFiles := []composetypes.ConfigFile{
    42  		{
    43  			Config: map[string]interface{}{
    44  				"version": "3",
    45  				"services": map[string]interface{}{
    46  					"foo": map[string]interface{}{
    47  						"image":   "busybox:${version}",
    48  						"command": []interface{}{"-text", "${foo.bar}"},
    49  					},
    50  				},
    51  			},
    52  		},
    53  	}
    54  	finalEnv := map[string]string{
    55  		"version": "latest",
    56  		"foo.bar": "baz",
    57  	}
    58  	c, err := render(configFiles, finalEnv)
    59  	assert.NilError(t, err)
    60  	assert.Check(t, is.Len(c.Services, 1))
    61  	assert.Check(t, is.Equal(c.Services[0].Image, "busybox:latest"))
    62  	assert.Check(t, is.DeepEqual([]string(c.Services[0].Command), []string{"-text", "baz"}))
    63  }
    64  
    65  func TestRenderEnabledFalse(t *testing.T) {
    66  	for _, tc := range []interface{}{false, "false", "! ${myapp.debug}"} {
    67  		configs := []composetypes.ConfigFile{
    68  			{
    69  				Config: map[string]interface{}{
    70  					"version": "3.7",
    71  					"services": map[string]interface{}{
    72  						"foo": map[string]interface{}{
    73  							"image":     "busybox",
    74  							"command":   []interface{}{"-text", "foo"},
    75  							"x-enabled": tc,
    76  						},
    77  					},
    78  				},
    79  			},
    80  		}
    81  		c, err := render(configs, map[string]string{
    82  			"myapp.debug": "true",
    83  		})
    84  		assert.NilError(t, err)
    85  		assert.Check(t, is.Len(c.Services, 0))
    86  	}
    87  }
    88  
    89  func TestRenderUserSettings(t *testing.T) {
    90  	metadata := strings.NewReader(validMeta)
    91  	composeFile := strings.NewReader(`
    92  version: "3.6"
    93  services:
    94    front:
    95      image: ${front.image}
    96      ports:
    97       - "${front.port}:80"
    98    back:
    99      image: ${back.image}
   100  `)
   101  	settings := strings.NewReader(`
   102  front:
   103    image: wrong
   104    port: 8484
   105  back:
   106    image: wrong
   107  `)
   108  	app := &types.App{Path: "my-app"}
   109  	assert.NilError(t, types.Metadata(metadata)(app))
   110  	assert.NilError(t, types.WithComposes(composeFile)(app))
   111  	assert.NilError(t, types.WithSettings(settings)(app))
   112  	userSettings := map[string]string{
   113  		"front.image": "nginx",
   114  		"front.port":  "4242",
   115  		"back.image":  "myapp",
   116  	}
   117  	c, err := Render(app, userSettings)
   118  	assert.NilError(t, err)
   119  	s, err := yaml.Marshal(c)
   120  	assert.NilError(t, err)
   121  	assert.Equal(t, string(s), `version: "3.6"
   122  services:
   123    back:
   124      image: myapp
   125    front:
   126      image: nginx
   127      ports:
   128      - mode: ingress
   129        target: 80
   130        published: 4242
   131        protocol: tcp
   132  `)
   133  }
   134  
   135  func TestRenderWithoutDefaultSettings(t *testing.T) {
   136  	metadata := strings.NewReader(validMeta)
   137  	composeFile := strings.NewReader(`
   138  version: "3.6"
   139  services:
   140    front:
   141      image: ${front.image}
   142  `)
   143  	settings := strings.NewReader("")
   144  	app := &types.App{Path: "my-app"}
   145  	assert.NilError(t, types.Metadata(metadata)(app))
   146  	assert.NilError(t, types.WithComposes(composeFile)(app))
   147  	assert.NilError(t, types.WithSettings(settings)(app))
   148  	userSettings := map[string]string{
   149  		"front.image": "nginx",
   150  	}
   151  	c, err := Render(app, userSettings)
   152  	assert.NilError(t, err)
   153  	s, err := yaml.Marshal(c)
   154  	assert.NilError(t, err)
   155  	assert.Equal(t, string(s), `version: "3.6"
   156  services:
   157    front:
   158      image: nginx
   159  `)
   160  }
   161  
   162  func TestValidateBrokenComposeFile(t *testing.T) {
   163  	metadata := strings.NewReader(validMeta)
   164  	brokenComposeFile := strings.NewReader(`version: "3.6"
   165  unknown-property: value`)
   166  	app := &types.App{Path: "my-app"}
   167  	err := types.Metadata(metadata)(app)
   168  	assert.NilError(t, err)
   169  	err = types.WithComposes(brokenComposeFile)(app)
   170  	assert.NilError(t, err)
   171  	c, err := Render(app, nil)
   172  	assert.Assert(t, is.Nil(c))
   173  	assert.Error(t, err, "failed to load Compose file: unknown-property Additional property unknown-property is not allowed")
   174  }
   175  
   176  func TestValidateRenderedApplication(t *testing.T) {
   177  	metadata := strings.NewReader(validMeta)
   178  	composeFile := strings.NewReader(`
   179  version: "3.6"
   180  services:
   181      hello:
   182          image: ${image}`)
   183  	settings := strings.NewReader(`image: hashicorp/http-echo`)
   184  	app := &types.App{Path: "my-app"}
   185  	err := types.Metadata(metadata)(app)
   186  	assert.NilError(t, err)
   187  	err = types.WithComposes(composeFile)(app)
   188  	assert.NilError(t, err)
   189  	err = types.WithSettings(settings)(app)
   190  	assert.NilError(t, err)
   191  	c, err := Render(app, nil)
   192  	assert.Assert(t, c != nil)
   193  	assert.NilError(t, err)
   194  }