github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/internal/packager/init_test.go (about)

     1  package packager
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/app/internal"
     7  	"github.com/docker/app/types/metadata"
     8  	"gotest.tools/assert"
     9  	"gotest.tools/fs"
    10  )
    11  
    12  func TestInitFromComposeFile(t *testing.T) {
    13  	composeData := `
    14  version: '3.0'
    15  services:
    16    nginx:
    17      image: nginx:${NGINX_VERSION}
    18      command: nginx $NGINX_ARGS
    19  `
    20  	envData := "# some comment\nNGINX_VERSION=latest"
    21  
    22  	inputDir := fs.NewDir(t, "app_input_",
    23  		fs.WithFile(internal.ComposeFileName, composeData),
    24  		fs.WithFile(".env", envData),
    25  	)
    26  	defer inputDir.Remove()
    27  
    28  	appName := "my.dockerapp"
    29  	dir := fs.NewDir(t, "app_",
    30  		fs.WithDir(appName),
    31  	)
    32  	defer dir.Remove()
    33  
    34  	err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName))
    35  	assert.NilError(t, err)
    36  
    37  	manifest := fs.Expected(
    38  		t,
    39  		fs.WithMode(0755),
    40  		fs.WithFile(internal.ComposeFileName, composeData, fs.WithMode(0644)),
    41  		fs.WithFile(internal.SettingsFileName, "NGINX_ARGS: FILL ME\nNGINX_VERSION: latest\n", fs.WithMode(0644)),
    42  	)
    43  
    44  	assert.Assert(t, fs.Equal(dir.Join(appName), manifest))
    45  }
    46  
    47  func TestInitFromInvalidComposeFile(t *testing.T) {
    48  	err := initFromComposeFile("my.dockerapp", "doesnotexist")
    49  	assert.ErrorContains(t, err, "failed to read")
    50  }
    51  
    52  func TestInitFromV2ComposeFile(t *testing.T) {
    53  	composeData := `
    54  version: '2.4'
    55  services:
    56    nginx:
    57      image: nginx:${NGINX_VERSION}
    58      command: nginx $NGINX_ARGS
    59  `
    60  	inputDir := fs.NewDir(t, "app_input_",
    61  		fs.WithFile(internal.ComposeFileName, composeData),
    62  	)
    63  	defer inputDir.Remove()
    64  
    65  	appName := "my.dockerapp"
    66  	dir := fs.NewDir(t, "app_",
    67  		fs.WithDir(appName),
    68  	)
    69  	defer dir.Remove()
    70  
    71  	err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName))
    72  	assert.ErrorContains(t, err, "unsupported Compose file version")
    73  }
    74  
    75  func TestInitFromV1ComposeFile(t *testing.T) {
    76  	composeData := `
    77  nginx:
    78    image: nginx
    79  `
    80  	inputDir := fs.NewDir(t, "app_input_",
    81  		fs.WithFile(internal.ComposeFileName, composeData),
    82  	)
    83  	defer inputDir.Remove()
    84  
    85  	appName := "my.dockerapp"
    86  	dir := fs.NewDir(t, "app_",
    87  		fs.WithDir(appName),
    88  	)
    89  	defer dir.Remove()
    90  
    91  	err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName))
    92  	assert.ErrorContains(t, err, "unsupported Compose file version")
    93  }
    94  
    95  func TestWriteMetadataFile(t *testing.T) {
    96  	appName := "writemetadata_test"
    97  	tmpdir := fs.NewDir(t, appName)
    98  	defer tmpdir.Remove()
    99  
   100  	err := writeMetadataFile(appName, tmpdir.Path(), "", []string{"bearclaw:bearclaw"})
   101  	assert.NilError(t, err)
   102  
   103  	data := `# Version of the application
   104  version: 0.1.0
   105  # Name of the application
   106  name: writemetadata_test
   107  # A short description of the application
   108  description: 
   109  # Namespace to use when pushing to a registry. This is typically your Hub username.
   110  #namespace: myHubUsername
   111  # List of application maintainers with name and email for each
   112  maintainers:
   113    - name: bearclaw
   114      email: bearclaw
   115  `
   116  
   117  	manifest := fs.Expected(t,
   118  		fs.WithFile(internal.MetadataFileName, data, fs.WithMode(0644)),
   119  	)
   120  	assert.Assert(t, fs.Equal(tmpdir.Path(), manifest))
   121  }
   122  
   123  func TestParseMaintainersData(t *testing.T) {
   124  	input := []string{
   125  		"sakuya:sakuya.izayoi@touhou.jp",
   126  		"marisa.kirisame",
   127  		"Reimu Hakurei",
   128  		"Hong Meiling:kurenai.misuzu@touhou.jp",
   129  		"    :    ",
   130  		"perfect:cherry:blossom",
   131  	}
   132  
   133  	expectedOutput := []metadata.Maintainer{
   134  		{Name: "sakuya", Email: "sakuya.izayoi@touhou.jp"},
   135  		{Name: "marisa.kirisame", Email: ""},
   136  		{Name: "Reimu Hakurei", Email: ""},
   137  		{Name: "Hong Meiling", Email: "kurenai.misuzu@touhou.jp"},
   138  		{Name: "    ", Email: "    "},
   139  		{Name: "perfect", Email: "cherry:blossom"},
   140  	}
   141  	output := parseMaintainersData(input)
   142  
   143  	assert.DeepEqual(t, output, expectedOutput)
   144  }