]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/filetest/filetest.go
tests: add end-to-end test with configuration without any changes
[safcm/safcm.git] / cmd / safcm-remote / sync / filetest / filetest.go
1 // Utility functions useful for file-related tests
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 package filetest
19
20 import (
21         "io/fs"
22         "os"
23         "os/user"
24         "path/filepath"
25         "strconv"
26         "syscall"
27 )
28
29 type File struct {
30         Path string
31         Mode fs.FileMode
32         Data []byte
33 }
34
35 func WalkDir(basePath string) ([]File, error) {
36         var res []File
37         err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry, err error) error {
38                 if err != nil {
39                         return err
40                 }
41                 info, err := d.Info()
42                 if err != nil {
43                         return err
44                 }
45                 rel, err := filepath.Rel(basePath, path)
46                 if err != nil {
47                         return err
48                 }
49
50                 f := File{
51                         Path: rel,
52                         Mode: info.Mode(),
53                 }
54                 if f.Mode.Type() == 0 {
55                         x, err := os.ReadFile(path)
56                         if err != nil {
57                                 return err
58                         }
59                         f.Data = x
60                 } else if f.Mode.Type() == fs.ModeSymlink {
61                         x, err := os.Readlink(path)
62                         if err != nil {
63                                 return err
64                         }
65                         f.Data = []byte(x)
66                         f.Mode |= 0777 // see sync/files.go
67                 }
68                 res = append(res, f)
69                 return nil
70         })
71         if err != nil {
72                 return nil, err
73         }
74         return res, nil
75 }
76
77 func CurrentUserAndGroup() (string, int, string, int) {
78         u, err := user.Current()
79         if err != nil {
80                 panic(err)
81         }
82         g, err := user.LookupGroupId(u.Gid)
83         if err != nil {
84                 panic(err)
85         }
86         uid, err := strconv.Atoi(u.Uid)
87         if err != nil {
88                 panic(err)
89         }
90         gid, err := strconv.Atoi(g.Gid)
91         if err != nil {
92                 panic(err)
93         }
94         return u.Username, uid, g.Name, gid
95 }
96
97 func CreateFile(path string, data string, mode fs.FileMode) {
98         err := os.WriteFile(path, []byte(data), 0644)
99         if err != nil {
100                 panic(err)
101         }
102         err = os.Chmod(path, mode)
103         if err != nil {
104                 panic(err)
105         }
106 }
107
108 func CreateSymlink(path string, data string) {
109         err := os.Symlink(data, path)
110         if err != nil {
111                 panic(err)
112         }
113 }
114
115 func CreateDirectory(path string, mode fs.FileMode) {
116         err := os.Mkdir(path, 0700)
117         if err != nil {
118                 panic(err)
119         }
120         err = os.Chmod(path, mode)
121         if err != nil {
122                 panic(err)
123         }
124 }
125
126 func CreateDirectoryExists(path string, mode fs.FileMode) {
127         err := os.Mkdir(path, 0700)
128         if err != nil && !os.IsExist(err) {
129                 panic(err)
130         }
131         err = os.Chmod(path, mode)
132         if err != nil {
133                 panic(err)
134         }
135 }
136
137 func CreateFifo(path string, mode fs.FileMode) {
138         err := syscall.Mkfifo(path, 0600)
139         if err != nil {
140                 panic(err)
141         }
142         err = os.Chmod(path, mode)
143         if err != nil {
144                 panic(err)
145         }
146 }