Ai
1 Star 0 Fork 1

mysnapcore/mysnapd

forked from tupelo-shen/mysnapd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mountprofile_linux_test.go 5.49 KB
一键复制 编辑 原始数据 按行查看 历史
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package osutil_test
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
. "gopkg.in/check.v1"
"gitee.com/mysnapcore/mysnapd/osutil"
"gitee.com/mysnapcore/mysnapd/testutil"
)
type profileSuite struct{}
var _ = Suite(&profileSuite{})
// Test that loading a profile from inexisting file returns an empty profile.
func (s *profileSuite) TestLoadMountProfile1(c *C) {
dir := c.MkDir()
p, err := osutil.LoadMountProfile(filepath.Join(dir, "missing"))
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 0)
}
// Test that loading profile from a file works as expected.
func (s *profileSuite) TestLoadMountProfile2(c *C) {
dir := c.MkDir()
fname := filepath.Join(dir, "existing")
err := ioutil.WriteFile(fname, []byte("name-1 dir-1 type-1 options-1 1 1 # 1st entry"), 0644)
c.Assert(err, IsNil)
p, err := osutil.LoadMountProfile(fname)
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 1)
c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{
{Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1},
})
}
// Test that loading profile with various comments works as expected.
func (s *profileSuite) TestLoadMountProfile3(c *C) {
dir := c.MkDir()
fname := filepath.Join(dir, "existing")
err := ioutil.WriteFile(fname, []byte(`
# comment with leading spaces
name#-1 dir#-1 type#-1 options#-1 1 1 # inline comment
# comment without leading spaces
`), 0644)
c.Assert(err, IsNil)
p, err := osutil.LoadMountProfile(fname)
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 1)
c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{
{Name: "name#-1", Dir: "dir#-1", Type: "type#-1", Options: []string{"options#-1"}, DumpFrequency: 1, CheckPassNumber: 1},
})
}
func (s *profileSuite) TestLoadMountProfileText(c *C) {
p1, err := osutil.LoadMountProfileText("tmpfs /tmp tmpfs defaults 0 0")
c.Assert(err, IsNil)
c.Assert(p1.Entries, DeepEquals, []osutil.MountEntry{
{Name: "tmpfs", Dir: "/tmp", Type: "tmpfs", Options: []string{"defaults"}},
})
p2, err := osutil.LoadMountProfileText(
"tmpfs /tmp tmpfs defaults 0 0\n" +
"/tmp /var/tmp none bind 0 0\n")
c.Assert(err, IsNil)
c.Assert(p2.Entries, DeepEquals, []osutil.MountEntry{
{Name: "tmpfs", Dir: "/tmp", Type: "tmpfs", Options: []string{"defaults"}},
{Name: "/tmp", Dir: "/var/tmp", Type: "none", Options: []string{"bind"}},
})
}
// Test that saving a profile to a file works correctly.
func (s *profileSuite) TestSaveMountProfile1(c *C) {
dir := c.MkDir()
fname := filepath.Join(dir, "profile")
p := &osutil.MountProfile{
Entries: []osutil.MountEntry{
{Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1},
},
}
err := p.Save(fname)
c.Assert(err, IsNil)
stat, err := os.Stat(fname)
c.Assert(err, IsNil)
c.Assert(stat.Mode().Perm(), Equals, os.FileMode(0644))
c.Assert(fname, testutil.FileEquals, "name-1 dir-1 type-1 options-1 1 1\n")
}
// Test that empty fstab is parsed without errors
func (s *profileSuite) TestReadMountProfile1(c *C) {
p, err := osutil.ReadMountProfile(strings.NewReader(""))
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 0)
}
// Test that '#'-comments are skipped
func (s *profileSuite) TestReadMountProfile2(c *C) {
p, err := osutil.ReadMountProfile(strings.NewReader("# comment"))
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 0)
}
// Test that simple profile can be loaded correctly.
func (s *profileSuite) TestReadMountProfile3(c *C) {
p, err := osutil.ReadMountProfile(strings.NewReader(`
name-1 dir-1 type-1 options-1 1 1 # 1st entry
name-2 dir-2 type-2 options-2 2 2 # 2nd entry`))
c.Assert(err, IsNil)
c.Assert(p.Entries, HasLen, 2)
c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{
{Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1},
{Name: "name-2", Dir: "dir-2", Type: "type-2", Options: []string{"options-2"}, DumpFrequency: 2, CheckPassNumber: 2},
})
}
// Test that writing an empty fstab file works correctly.
func (s *profileSuite) TestWriteTo1(c *C) {
p := &osutil.MountProfile{}
var buf bytes.Buffer
n, err := p.WriteTo(&buf)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(0))
c.Assert(buf.String(), Equals, "")
}
// Test that writing an trivial fstab file works correctly.
func (s *profileSuite) TestWriteTo2(c *C) {
p := &osutil.MountProfile{
Entries: []osutil.MountEntry{
{Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1},
{Name: "name-2", Dir: "dir-2", Type: "type-2", Options: []string{"options-2"}, DumpFrequency: 2, CheckPassNumber: 2},
},
}
var buf bytes.Buffer
n, err := p.WriteTo(&buf)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(68))
c.Assert(buf.String(), Equals, ("" +
"name-1 dir-1 type-1 options-1 1 1\n" +
"name-2 dir-2 type-2 options-2 2 2\n"))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.1.0

搜索帮助