Skip to content

Commit

Permalink
Fix wrench build on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-samfira committed Aug 15, 2014
1 parent e78ac60 commit 4baf8b2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 39 deletions.
23 changes: 8 additions & 15 deletions wrench/wrench.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"bufio"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"

"github.com/juju/loggo"

Expand Down Expand Up @@ -132,20 +132,13 @@ func checkWrenchFile(category, feature, fileName string) bool {
"- ignoring %s", category, feature, fileName)
return false
}
if fileinfo.Mode()&0022 != 0 {
logger.Errorf("wrench file for %s/%s should only be writable by "+
"owner - ignoring %s", category, feature, fileName)
return false
// Windows is not fully POSIX compliant
if runtime.GOOS != "windows" {
if fileinfo.Mode()&0022 != 0 {
logger.Errorf("wrench file for %s/%s should only be writable by "+
"owner - ignoring %s", category, feature, fileName)
return false
}
}
return true
}

func isOwnedByJujuUser(fi os.FileInfo) bool {
statStruct, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
// Uid check is not supported on this platform so assume
// the owner is ok.
return true
}
return int(statStruct.Uid) == jujuUid
}
20 changes: 20 additions & 0 deletions wrench/wrench_nix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build !windows

package wrench

import (
"os"
"syscall"
)

func isOwnedByJujuUser(fi os.FileInfo) bool {
statStruct, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
// Uid check is not supported on this platform so assume
// the owner is ok.
return true
}
return int(statStruct.Uid) == jujuUid
}
35 changes: 35 additions & 0 deletions wrench/wrench_nix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build !windows

package wrench_test

import (
"os"
"syscall"

gc "launchpad.net/gocheck"

"github.com/juju/juju/wrench"
)

const fileNotFound = `stat .+: no such file or directory`

// Patch out the os.Stat call used by wrench so that a particular file
// appears to be owned by a UID that isn't Juju's UID.
func (s *wrenchSuite) tweakOwner(c *gc.C, targetPath string) {
s.PatchValue(wrench.Stat, func(path string) (fi os.FileInfo, err error) {
fi, err = os.Stat(path)
if err != nil {
return
}
if path == targetPath {
statStruct, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
c.Skip("this test only supports POSIX systems")
}
statStruct.Uid = notJujuUid
}
return
})
}
30 changes: 6 additions & 24 deletions wrench/wrench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"runtime"
stdtesting "testing"

gc "launchpad.net/gocheck"
Expand Down Expand Up @@ -127,6 +127,9 @@ func (s *wrenchSuite) TestFileNotOwnedByJujuUser(c *gc.C) {
}

func (s *wrenchSuite) TestFilePermsTooLoose(c *gc.C) {
if runtime.GOOS == "windows" {
c.Skip("Windows is not fully POSIX compliant")
}
s.createWrenchDir(c)
filename := s.createWrenchFile(c, "foo", "bar")
err := os.Chmod(filename, 0666)
Expand Down Expand Up @@ -173,25 +176,6 @@ func (s *wrenchSuite) TestSetEnabled(c *gc.C) {

var notJujuUid = uint32(os.Getuid() + 1)

// Patch out the os.Stat call used by wrench so that a particular file
// appears to be owned by a UID that isn't Juju's UID.
func (s *wrenchSuite) tweakOwner(c *gc.C, targetPath string) {
s.PatchValue(wrench.Stat, func(path string) (fi os.FileInfo, err error) {
fi, err = os.Stat(path)
if err != nil {
return
}
if path == targetPath {
statStruct, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
c.Skip("this test only support on POSIX systems")
}
statStruct.Uid = notJujuUid
}
return
})
}

func (s *wrenchSuite) AssertActivationLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.WARNING, `wrench for foo/bar is active`}})
Expand All @@ -203,12 +187,10 @@ func (s *wrenchSuite) AssertNothingLogged(c *gc.C) {

func (s *wrenchSuite) AssertFileErrorLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.DEBUG, `no wrench data for foo/bar \(ignored\): ` +
`stat .+: no such file or directory`}})
{loggo.DEBUG, `no wrench data for foo/bar \(ignored\): ` + fileNotFound}})
}

func (s *wrenchSuite) AssertDirErrorLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.DEBUG, `couldn't read wrench directory: ` +
`stat .+: no such file or directory`}})
{loggo.DEBUG, `couldn't read wrench directory: ` + fileNotFound}})
}
12 changes: 12 additions & 0 deletions wrench/wrench_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package wrench

import "os"

// Windows is not fully POSIX compliant
// there is no syscall.Stat_t on Windows
func isOwnedByJujuUser(fi os.FileInfo) bool {
return true
}
16 changes: 16 additions & 0 deletions wrench/wrench_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package wrench_test

import (
gc "launchpad.net/gocheck"
)

const fileNotFound = "GetFileAttributesEx.*: The system cannot find the (file|path) specified."

// Patch out the os.Stat call used by wrench so that a particular file
// appears to be owned by a UID that isn't Juju's UID.
func (s *wrenchSuite) tweakOwner(c *gc.C, targetPath string) {
c.Skip("this test only supports POSIX systems")
}

0 comments on commit 4baf8b2

Please sign in to comment.