|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package volumes |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "google.golang.org/grpc/codes" |
| 12 | + |
| 13 | + "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator" |
| 14 | +) |
| 15 | + |
| 16 | +func TestUpdatePath(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + s, tmpdir, volumeInfo := setupTestService(t) |
| 20 | + |
| 21 | + t.Run("update mode only", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + filename := "update-mode.txt" |
| 25 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 26 | + |
| 27 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 28 | + Volume: volumeInfo, |
| 29 | + Path: filename, |
| 30 | + Mode: new(uint32(0o600)), |
| 31 | + }) |
| 32 | + require.NoError(t, err) |
| 33 | + require.Equal(t, uint32(0o600), resp.GetEntry().GetMode()) |
| 34 | + |
| 35 | + info, err := os.Stat(filepath.Join(tmpdir, filename)) |
| 36 | + require.NoError(t, err) |
| 37 | + require.Equal(t, os.FileMode(0o600), info.Mode().Perm()) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("update uid and gid", func(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + filename := "update-owner.txt" |
| 44 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 45 | + |
| 46 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 47 | + Volume: volumeInfo, |
| 48 | + Path: filename, |
| 49 | + Uid: new(uint32(1234)), |
| 50 | + Gid: new(uint32(5678)), |
| 51 | + }) |
| 52 | + require.NoError(t, err) |
| 53 | + require.Equal(t, uint32(1234), resp.GetEntry().GetUid()) |
| 54 | + require.Equal(t, uint32(5678), resp.GetEntry().GetGid()) |
| 55 | + |
| 56 | + fs, _, errResponse := s.getFilesystemAndPath(t.Context(), &orchestrator.UpdatePathRequest{Volume: volumeInfo}) |
| 57 | + require.Nil(t, errResponse) |
| 58 | + assertDir(t, fs, "/"+filename, 1234, 5678, 0o644) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("update uid only leaves gid unchanged", func(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + filename := "update-uid-only.txt" |
| 65 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 66 | + require.NoError(t, os.Chown(filepath.Join(tmpdir, filename), 1000, 2000)) |
| 67 | + |
| 68 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 69 | + Volume: volumeInfo, |
| 70 | + Path: filename, |
| 71 | + Uid: new(uint32(4321)), |
| 72 | + }) |
| 73 | + require.NoError(t, err) |
| 74 | + require.Equal(t, uint32(4321), resp.GetEntry().GetUid()) |
| 75 | + require.Equal(t, uint32(2000), resp.GetEntry().GetGid()) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("update gid only leaves uid unchanged", func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + filename := "update-gid-only.txt" |
| 82 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 83 | + require.NoError(t, os.Chown(filepath.Join(tmpdir, filename), 1000, 2000)) |
| 84 | + |
| 85 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 86 | + Volume: volumeInfo, |
| 87 | + Path: filename, |
| 88 | + Gid: new(uint32(8765)), |
| 89 | + }) |
| 90 | + require.NoError(t, err) |
| 91 | + require.Equal(t, uint32(1000), resp.GetEntry().GetUid()) |
| 92 | + require.Equal(t, uint32(8765), resp.GetEntry().GetGid()) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("update mode, uid, and gid together", func(t *testing.T) { |
| 96 | + t.Parallel() |
| 97 | + |
| 98 | + filename := "update-all.txt" |
| 99 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 100 | + |
| 101 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 102 | + Volume: volumeInfo, |
| 103 | + Path: filename, |
| 104 | + Mode: new(uint32(0o640)), |
| 105 | + Uid: new(uint32(111)), |
| 106 | + Gid: new(uint32(222)), |
| 107 | + }) |
| 108 | + require.NoError(t, err) |
| 109 | + require.Equal(t, uint32(0o640), resp.GetEntry().GetMode()) |
| 110 | + require.Equal(t, uint32(111), resp.GetEntry().GetUid()) |
| 111 | + require.Equal(t, uint32(222), resp.GetEntry().GetGid()) |
| 112 | + |
| 113 | + fs, _, errResponse := s.getFilesystemAndPath(t.Context(), &orchestrator.UpdatePathRequest{Volume: volumeInfo}) |
| 114 | + require.Nil(t, errResponse) |
| 115 | + assertDir(t, fs, "/"+filename, 111, 222, 0o640) |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("update directory", func(t *testing.T) { |
| 119 | + t.Parallel() |
| 120 | + |
| 121 | + dirname := "update-dir" |
| 122 | + require.NoError(t, os.Mkdir(filepath.Join(tmpdir, dirname), 0o755)) |
| 123 | + |
| 124 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 125 | + Volume: volumeInfo, |
| 126 | + Path: dirname, |
| 127 | + Mode: new(uint32(0o700)), |
| 128 | + }) |
| 129 | + require.NoError(t, err) |
| 130 | + require.Equal(t, orchestrator.FileType_FILE_TYPE_DIRECTORY, resp.GetEntry().GetType()) |
| 131 | + require.Equal(t, uint32(0o700), resp.GetEntry().GetMode()) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("no fields set only stats", func(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + |
| 137 | + filename := "update-noop.txt" |
| 138 | + require.NoError(t, os.WriteFile(filepath.Join(tmpdir, filename), []byte("test"), 0o644)) |
| 139 | + |
| 140 | + resp, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 141 | + Volume: volumeInfo, |
| 142 | + Path: filename, |
| 143 | + }) |
| 144 | + require.NoError(t, err) |
| 145 | + require.Equal(t, "/"+filename, resp.GetEntry().GetPath()) |
| 146 | + require.Equal(t, uint32(0o644), resp.GetEntry().GetMode()) |
| 147 | + }) |
| 148 | + |
| 149 | + t.Run("chmod non-existent path", func(t *testing.T) { |
| 150 | + t.Parallel() |
| 151 | + |
| 152 | + _, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 153 | + Volume: volumeInfo, |
| 154 | + Path: "does-not-exist-chmod", |
| 155 | + Mode: new(uint32(0o600)), |
| 156 | + }) |
| 157 | + requireGRPCError(t, err, codes.NotFound, orchestrator.UserErrorCode_PATH_NOT_FOUND) |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("chown non-existent path", func(t *testing.T) { |
| 161 | + t.Parallel() |
| 162 | + |
| 163 | + _, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 164 | + Volume: volumeInfo, |
| 165 | + Path: "does-not-exist-chown", |
| 166 | + Uid: new(uint32(111)), |
| 167 | + }) |
| 168 | + requireGRPCError(t, err, codes.NotFound, orchestrator.UserErrorCode_PATH_NOT_FOUND) |
| 169 | + }) |
| 170 | + |
| 171 | + t.Run("stat non-existent path", func(t *testing.T) { |
| 172 | + t.Parallel() |
| 173 | + |
| 174 | + _, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 175 | + Volume: volumeInfo, |
| 176 | + Path: "does-not-exist-stat", |
| 177 | + }) |
| 178 | + requireGRPCError(t, err, codes.NotFound, orchestrator.UserErrorCode_PATH_NOT_FOUND) |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("invalid team id", func(t *testing.T) { |
| 182 | + t.Parallel() |
| 183 | + |
| 184 | + _, err := s.UpdatePath(t.Context(), &orchestrator.UpdatePathRequest{ |
| 185 | + Volume: &orchestrator.VolumeInfo{ |
| 186 | + VolumeType: volumeType, |
| 187 | + TeamId: "not-a-uuid", |
| 188 | + VolumeId: volumeInfo.GetVolumeId(), |
| 189 | + }, |
| 190 | + Path: "whatever.txt", |
| 191 | + Mode: new(uint32(0o600)), |
| 192 | + }) |
| 193 | + requireGRPCError(t, err, codes.InvalidArgument, orchestrator.UserErrorCode_INVALID_REQUEST) |
| 194 | + }) |
| 195 | +} |
0 commit comments