github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/validator/rules/relativepath_test.go (about)

     1  package rules
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/assert"
     7  )
     8  
     9  func TestRelativePathRule(t *testing.T) {
    10  	s := NewRelativePathRule()
    11  
    12  	t.Run("should accept only volume paths", func(t *testing.T) {
    13  		assert.Equal(t, s.Accept("services", "test"), false)
    14  		assert.Equal(t, s.Accept("services.test.volumes", "my_volume"), true)
    15  		assert.Equal(t, s.Accept("services.test", "volumes"), true)
    16  	})
    17  
    18  	t.Run("should validate named volume paths", func(t *testing.T) {
    19  		input := map[string]string{
    20  			"toto": "tata",
    21  		}
    22  		errs := s.Validate(input)
    23  		assert.Equal(t, len(errs), 0)
    24  	})
    25  
    26  	t.Run("should return error if short syntax volume path is relative", func(t *testing.T) {
    27  		input := []interface{}{
    28  			"./foo:/data",
    29  		}
    30  		errs := s.Validate(input)
    31  		assert.Equal(t, len(errs), 1)
    32  
    33  		assert.ErrorContains(t, errs[0], `can't use relative path as volume source ("./foo:/data") in service "test"`)
    34  	})
    35  
    36  	t.Run("should return error if the volume definition is invalid", func(t *testing.T) {
    37  		input := []interface{}{
    38  			"foo",
    39  		}
    40  		errs := s.Validate(input)
    41  		assert.Equal(t, len(errs), 1)
    42  
    43  		assert.ErrorContains(t, errs[0], `invalid volume definition ("foo") in service "test"`)
    44  	})
    45  
    46  	t.Run("should return all volume errors", func(t *testing.T) {
    47  		input := []interface{}{
    48  			"./foo:/data1",
    49  			"./bar:/data2",
    50  		}
    51  		errs := s.Validate(input)
    52  		assert.Equal(t, len(errs), 2)
    53  
    54  		assert.ErrorContains(t, errs[0], `can't use relative path as volume source ("./foo:/data1") in service "test"`)
    55  		assert.ErrorContains(t, errs[1], `can't use relative path as volume source ("./bar:/data2") in service "test"`)
    56  	})
    57  
    58  	// When a volume is in short syntax, the list of volumes must be strings
    59  	t.Run("shoud return error if volume list is invalid", func(t *testing.T) {
    60  		input := []interface{}{
    61  			1,
    62  		}
    63  		errs := s.Validate(input)
    64  		assert.Equal(t, len(errs), 1)
    65  
    66  		assert.ErrorContains(t, errs[0], `invalid volume in service "test"`)
    67  	})
    68  
    69  	t.Run("should return error if long syntax volume path is relative", func(t *testing.T) {
    70  		input := map[string]interface{}{
    71  			"source": "./foo",
    72  		}
    73  		errs := s.Validate(input)
    74  		assert.Equal(t, len(errs), 1)
    75  
    76  		assert.ErrorContains(t, errs[0], `can't use relative path as volume source ("./foo") in service "test"`)
    77  	})
    78  
    79  	t.Run("shoud return error if volume map is invalid", func(t *testing.T) {
    80  		input := map[string]interface{}{}
    81  		errs := s.Validate(input)
    82  		assert.Equal(t, len(errs), 1)
    83  
    84  		assert.ErrorContains(t, errs[0], `invalid volume in service "test"`)
    85  	})
    86  }