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