|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mount |
| 18 | + |
| 19 | +import "k8s.io/utils/exec" |
| 20 | + |
| 21 | +// NewOSExec returns a new Exec interface implementation based on exec() |
| 22 | +func NewOSExec() Exec { |
| 23 | + return &osExec{} |
| 24 | +} |
| 25 | + |
| 26 | +// Real implementation of Exec interface that uses simple utils.Exec |
| 27 | +type osExec struct{} |
| 28 | + |
| 29 | +var _ Exec = &osExec{} |
| 30 | + |
| 31 | +func (e *osExec) Run(cmd string, args ...string) ([]byte, error) { |
| 32 | + exe := exec.New() |
| 33 | + return exe.Command(cmd, args...).CombinedOutput() |
| 34 | +} |
| 35 | + |
| 36 | +// NewFakeExec returns a new FakeExec |
| 37 | +func NewFakeExec(run runHook) *FakeExec { |
| 38 | + return &FakeExec{runHook: run} |
| 39 | +} |
| 40 | + |
| 41 | +// FakeExec for testing. |
| 42 | +type FakeExec struct { |
| 43 | + runHook runHook |
| 44 | +} |
| 45 | +type runHook func(cmd string, args ...string) ([]byte, error) |
| 46 | + |
| 47 | +// Run executes the command using the optional runhook, if given |
| 48 | +func (f *FakeExec) Run(cmd string, args ...string) ([]byte, error) { |
| 49 | + if f.runHook != nil { |
| 50 | + return f.runHook(cmd, args...) |
| 51 | + } |
| 52 | + return nil, nil |
| 53 | +} |
0 commit comments