github.com/mvo5/godd@v0.0.0-20190117182126-c6433560a7fe/godd_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 . "gopkg.in/check.v1" 11 ) 12 13 func Test(t *testing.T) { TestingT(t) } 14 15 type GoddTestSuite struct { 16 cwd string 17 } 18 19 var _ = Suite(&GoddTestSuite{}) 20 21 func (g *GoddTestSuite) SetUpTest(c *C) { 22 var err error 23 g.cwd, err = os.Getwd() 24 c.Assert(err, IsNil) 25 tempdir := c.MkDir() 26 os.Chdir(tempdir) 27 } 28 29 func (g *GoddTestSuite) TearDownTest(c *C) { 30 os.Chdir(g.cwd) 31 32 Stdout = os.Stdout 33 Stdin = os.Stdin 34 } 35 36 func (g *GoddTestSuite) TestSimple(c *C) { 37 // ensure we test with tiny buffer 38 defaultBufSize = 2 39 40 canary := []byte("foo bar") 41 err := ioutil.WriteFile("src", canary, 0644) 42 c.Assert(err, IsNil) 43 44 dd := &ddOpts{ 45 src: "src", 46 dst: "dst", 47 bs: 0, 48 } 49 err = dd.Run() 50 c.Assert(err, IsNil) 51 52 read, err := ioutil.ReadFile("dst") 53 c.Assert(err, IsNil) 54 c.Assert(read, DeepEquals, canary) 55 } 56 57 func (g *GoddTestSuite) TestParseTrivial(c *C) { 58 opts, err := parseArgs([]string{"src", "dst"}) 59 c.Assert(err, IsNil) 60 c.Check(opts.src, Equals, "src") 61 c.Check(opts.dst, Equals, "dst") 62 } 63 64 func (g *GoddTestSuite) TestParseIfOf(c *C) { 65 opts, err := parseArgs([]string{"if=src", "of=dst"}) 66 c.Assert(err, IsNil) 67 c.Check(opts.src, Equals, "src") 68 c.Check(opts.dst, Equals, "dst") 69 } 70 71 func (g *GoddTestSuite) TestParseComp(c *C) { 72 type tT struct { 73 arg string 74 comp compType 75 } 76 for _, t := range []tT{ 77 {arg: "comp=gz", comp: compGzip}, 78 {arg: "comp=gzip", comp: compGzip}, 79 {arg: "comp=bz2", comp: compBzip2}, 80 {arg: "comp=bzip2", comp: compBzip2}, 81 {arg: "comp=xz", comp: compXz}, 82 {arg: "comp=none", comp: compNone}, 83 {arg: "comp=auto", comp: compAuto}, 84 } { 85 opts, err := parseArgs([]string{"if=src", "of=dst", t.arg}) 86 c.Assert(err, IsNil, Commentf(t.arg)) 87 c.Check(opts.comp, Equals, t.comp, Commentf(t.arg)) 88 } 89 } 90 91 func (g *GoddTestSuite) TestGuessComp(c *C) { 92 type tT struct { 93 arg string 94 comp compType 95 } 96 for _, t := range []tT{ 97 {arg: "foo.gz", comp: compGzip}, 98 {arg: "foo.bz2", comp: compBzip2}, 99 {arg: "foo.xz", comp: compXz}, 100 {arg: "foo.txt", comp: compNone}, 101 } { 102 c.Check(guessComp(t.arg), Equals, t.comp) 103 } 104 } 105 106 func (g *GoddTestSuite) TestParseError(c *C) { 107 opts, err := parseArgs([]string{"if=src", "invalid=command"}) 108 c.Assert(err, ErrorMatches, `unknown argument "invalid=command"`) 109 c.Assert(opts, IsNil) 110 } 111 112 func (g *GoddTestSuite) TestParseBs(c *C) { 113 opts, err := parseArgs([]string{"if=src", "of=dst", "bs=5"}) 114 c.Assert(err, IsNil) 115 c.Assert(opts, DeepEquals, &ddOpts{ 116 src: "src", 117 dst: "dst", 118 bs: 5, 119 }) 120 } 121 122 func (g *GoddTestSuite) TestParseBsWithString(c *C) { 123 opts, err := parseArgs([]string{"if=src", "of=dst", "bs=5M"}) 124 c.Assert(err, IsNil) 125 c.Assert(opts, DeepEquals, &ddOpts{ 126 src: "src", 127 dst: "dst", 128 bs: int64(5 * 1024 * 1024), 129 }) 130 } 131 132 func (g *GoddTestSuite) TestParseDD(c *C) { 133 n, err := ddAtoi("5M") 134 c.Assert(err, IsNil) 135 c.Assert(n, Equals, int64(5*1024*1024)) 136 } 137 138 func (g *GoddTestSuite) TestParseDDTwo(c *C) { 139 n, err := ddAtoi("5kB") 140 c.Assert(err, IsNil) 141 c.Assert(n, Equals, int64(5*1000)) 142 } 143 144 func makeMountInfo(c *C, mountSrc, mountPath string) { 145 // write a example mountinfo 146 cwd, err := os.Getwd() 147 c.Assert(err, IsNil) 148 mountinfoPath = filepath.Join(cwd, "mountinfo") 149 err = ioutil.WriteFile(mountinfoPath, []byte(fmt.Sprintf(`425 22 8:50 / %s rw,nosuid,nodev,relatime shared:442 - ext4 %s rw,data=ordered`, mountPath, mountSrc)), 0644) 150 c.Assert(err, IsNil) 151 } 152 153 func (g *GoddTestSuite) TestSanityCheckDstOk(c *C) { 154 makeMountInfo(c, "/dev/sdd2", "/media/ubuntu/a") 155 err := sanityCheckDst("/some/path") 156 c.Assert(err, IsNil) 157 } 158 159 func (g *GoddTestSuite) TestSanityCheckDstMounted(c *C) { 160 makeMountInfo(c, "/dev/sdd2", "/media/ubuntu/a") 161 err := sanityCheckDst("/dev/sdd2") 162 c.Assert(err, ErrorMatches, "/dev/sdd2 is mounted on /media/ubuntu/a") 163 } 164 165 func (g *GoddTestSuite) TestSanityCheckDstParentMounted(c *C) { 166 makeMountInfo(c, "/dev/sdd2", "/media/ubuntu/a") 167 err := sanityCheckDst("/dev/sdd") 168 c.Assert(err, ErrorMatches, "/dev/sdd2 is mounted on /media/ubuntu/a") 169 } 170 171 func (g *GoddTestSuite) TestSanityCheckDst(c *C) { 172 makeMountInfo(c, "/dev/sdd2", "/media/ubuntu/a") 173 err := sanityCheckDst("/dev/sdd1") 174 c.Assert(err, IsNil) 175 } 176 177 func (g *GoddTestSuite) TestOutputToStdout(c *C) { 178 mockStdout, err := os.Create(filepath.Join(c.MkDir(), "stdout")) 179 c.Assert(err, IsNil) 180 Stdout = mockStdout 181 182 canary := []byte("foo bar") 183 err = ioutil.WriteFile("src", canary, 0644) 184 c.Assert(err, IsNil) 185 186 dd, err := parseArgs([]string{"src", "-"}) 187 c.Assert(err, IsNil) 188 189 err = dd.Run() 190 c.Assert(err, IsNil) 191 192 got, err := ioutil.ReadFile(mockStdout.Name()) 193 c.Assert(err, IsNil) 194 c.Check(got, DeepEquals, canary) 195 }