github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/stage1/init/common/mount_test.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/kr/pretty"
    22  
    23  	"github.com/appc/spec/schema"
    24  	"github.com/appc/spec/schema/types"
    25  )
    26  
    27  func TestGenerateMounts(t *testing.T) {
    28  	tests := []struct {
    29  		ra         *schema.RuntimeApp
    30  		vols       []types.Volume
    31  		fromDocker bool
    32  		hasErr     bool
    33  		expected   []Mount
    34  	}{
    35  		{ // Test matching ra.mount to volume via name w/o/ mountpoint
    36  			ra: &schema.RuntimeApp{
    37  				Mounts: []schema.Mount{
    38  					{
    39  						Volume: *types.MustACName("foo-mount"),
    40  						Path:   "/app/foo",
    41  					},
    42  				},
    43  				App: &types.App{
    44  					MountPoints: nil,
    45  				},
    46  			},
    47  			vols: []types.Volume{
    48  				{
    49  					Name:     *types.MustACName("foo-mount"),
    50  					Kind:     "host",
    51  					Source:   "/host/foo",
    52  					ReadOnly: &falseVar,
    53  				},
    54  			},
    55  			fromDocker: false,
    56  			hasErr:     false,
    57  			expected: []Mount{
    58  				{
    59  					Mount: schema.Mount{
    60  						Volume: *types.MustACName("foo-mount"),
    61  						Path:   "/app/foo",
    62  					},
    63  					Volume: types.Volume{
    64  						Name:     *types.MustACName("foo-mount"),
    65  						Kind:     "host",
    66  						Source:   "/host/foo",
    67  						ReadOnly: &falseVar,
    68  					},
    69  					DockerImplicit: false,
    70  					ReadOnly:       false,
    71  				},
    72  			},
    73  		},
    74  		{ // Test matching app's mountpoint to a volume w/o a mount
    75  			ra: &schema.RuntimeApp{
    76  				Mounts: nil,
    77  				App: &types.App{
    78  					MountPoints: []types.MountPoint{
    79  						{
    80  							Name:     *types.MustACName("foo-mp"),
    81  							Path:     "/app/foo-mp",
    82  							ReadOnly: false,
    83  						},
    84  					},
    85  				},
    86  			},
    87  			vols: []types.Volume{
    88  				{
    89  					Name:     *types.MustACName("foo-mount"),
    90  					Kind:     "host",
    91  					Source:   "/host/foo",
    92  					ReadOnly: &falseVar,
    93  				},
    94  				{
    95  					Name:     *types.MustACName("foo-mp"),
    96  					Kind:     "host",
    97  					Source:   "/host/bar",
    98  					ReadOnly: &falseVar,
    99  				},
   100  			},
   101  			fromDocker: false,
   102  			hasErr:     false,
   103  			expected: []Mount{
   104  				{
   105  					Mount: schema.Mount{
   106  						Volume: *types.MustACName("foo-mp"),
   107  						Path:   "/app/foo-mp",
   108  					},
   109  					Volume: types.Volume{
   110  						Name:     *types.MustACName("foo-mp"),
   111  						Kind:     "host",
   112  						Source:   "/host/bar",
   113  						ReadOnly: &falseVar,
   114  					},
   115  					DockerImplicit: false,
   116  					ReadOnly:       false,
   117  				},
   118  			},
   119  		},
   120  		{ // Test that app's Mount can override the volume
   121  			ra: &schema.RuntimeApp{
   122  				Mounts: []schema.Mount{
   123  					{
   124  						Volume: *types.MustACName("foo-mount"),
   125  						Path:   "/app/foo",
   126  						AppVolume: &types.Volume{
   127  							Name:     *types.MustACName("foo-mount"),
   128  							Kind:     "host",
   129  							Source:   "/host/overridden",
   130  							ReadOnly: nil,
   131  						},
   132  					},
   133  				},
   134  
   135  				App: &types.App{
   136  					MountPoints: nil,
   137  				},
   138  			},
   139  			vols: []types.Volume{
   140  				{
   141  					Name:     *types.MustACName("foo-mount"),
   142  					Kind:     "host",
   143  					Source:   "/host/foo",
   144  					ReadOnly: &falseVar,
   145  				},
   146  				{
   147  					Name:     *types.MustACName("foo-mp"),
   148  					Kind:     "host",
   149  					Source:   "/host/bar",
   150  					ReadOnly: &falseVar,
   151  				},
   152  			},
   153  			fromDocker: false,
   154  			hasErr:     false,
   155  			expected: []Mount{
   156  				{
   157  					Mount: schema.Mount{
   158  						Volume: *types.MustACName("foo-mount"),
   159  						Path:   "/app/foo",
   160  						AppVolume: &types.Volume{
   161  							Name:     *types.MustACName("foo-mount"),
   162  							Kind:     "host",
   163  							Source:   "/host/overridden",
   164  							ReadOnly: nil,
   165  						},
   166  					},
   167  					Volume: types.Volume{
   168  						Name:     *types.MustACName("foo-mount"),
   169  						Kind:     "host",
   170  						Source:   "/host/overridden",
   171  						ReadOnly: nil,
   172  					},
   173  					DockerImplicit: false,
   174  					ReadOnly:       false,
   175  				},
   176  			},
   177  		},
   178  	}
   179  
   180  	for i, tt := range tests {
   181  		result, err := GenerateMounts(tt.ra, tt.vols, tt.fromDocker)
   182  		if (err != nil) != tt.hasErr {
   183  			t.Errorf("test %d expected error status %t, didn't get it", i, tt.hasErr)
   184  		}
   185  		if !reflect.DeepEqual(result, tt.expected) {
   186  			t.Errorf("test %d, result != expected, %+v", i, pretty.Diff(tt.expected, result))
   187  		}
   188  	}
   189  }