~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to environs/local/config_test.go

  • Committer: Tarmac
  • Author(s): Tim Penhey
  • Date: 2013-07-16 22:25:44 UTC
  • mfrom: (1408.2.10 local-sudo-caller)
  • Revision ID: tarmac-20130716222544-fng1zvjpfgb46i2o
[r=thumper] Add some extra tests around the sudo checks.

Encapsulate the sudo checks, and make a function to
get the uid and gid for the user.

https://codereview.appspot.com/11321043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
}
84
84
 
85
85
func (s *configSuite) TestNamespaceRootNoSudo(c *gc.C) {
 
86
        restore := local.SetRootCheckFunction(func() bool { return true })
 
87
        defer restore()
86
88
        err := os.Setenv("USER", "root")
87
89
        c.Assert(err, gc.IsNil)
88
90
        testConfig := minimalConfig(c)
90
92
}
91
93
 
92
94
func (s *configSuite) TestNamespaceRootWithSudo(c *gc.C) {
 
95
        restore := local.SetRootCheckFunction(func() bool { return true })
 
96
        defer restore()
93
97
        err := os.Setenv("USER", "root")
94
98
        c.Assert(err, gc.IsNil)
95
99
        err = os.Setenv("SUDO_USER", "tester")
99
103
        c.Assert(local.ConfigNamespace(testConfig), gc.Equals, "tester-test")
100
104
}
101
105
 
 
106
func (s *configSuite) TestSudoCallerIds(c *gc.C) {
 
107
        defer os.Setenv("SUDO_UID", os.Getenv("SUDO_UID"))
 
108
        defer os.Setenv("SUDO_GID", os.Getenv("SUDO_GID"))
 
109
        for _, test := range []struct {
 
110
                uid         string
 
111
                gid         string
 
112
                errString   string
 
113
                expectedUid int
 
114
                expectedGid int
 
115
        }{{
 
116
                uid: "",
 
117
                gid: "",
 
118
        }, {
 
119
                uid:         "1001",
 
120
                gid:         "1002",
 
121
                expectedUid: 1001,
 
122
                expectedGid: 1002,
 
123
        }, {
 
124
                uid:       "1001",
 
125
                gid:       "foo",
 
126
                errString: `invalid value "foo" for SUDO_GID`,
 
127
        }, {
 
128
                uid:       "foo",
 
129
                gid:       "bar",
 
130
                errString: `invalid value "foo" for SUDO_UID`,
 
131
        }} {
 
132
                os.Setenv("SUDO_UID", test.uid)
 
133
                os.Setenv("SUDO_GID", test.gid)
 
134
                uid, gid, err := local.SudoCallerIds()
 
135
                if test.errString == "" {
 
136
                        c.Assert(err, gc.IsNil)
 
137
                        c.Assert(uid, gc.Equals, test.expectedUid)
 
138
                        c.Assert(gid, gc.Equals, test.expectedGid)
 
139
                } else {
 
140
                        c.Assert(err, gc.ErrorMatches, test.errString)
 
141
                        c.Assert(uid, gc.Equals, 0)
 
142
                        c.Assert(gid, gc.Equals, 0)
 
143
                }
 
144
        }
 
145
}
 
146
 
102
147
type configRootSuite struct {
103
148
        baseProviderSuite
104
149
}