...

Source file src/k8s.io/kubernetes/pkg/volume/flexvolume/common_test.go

Documentation: k8s.io/kubernetes/pkg/volume/flexvolume

     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 flexvolume
    18  
    19  import (
    20  	"encoding/json"
    21  	goruntime "runtime"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/kubernetes/pkg/volume"
    25  	volumetesting "k8s.io/kubernetes/pkg/volume/testing"
    26  	"k8s.io/kubernetes/test/utils/harness"
    27  	"k8s.io/utils/exec"
    28  	exectesting "k8s.io/utils/exec/testing"
    29  )
    30  
    31  func testPlugin(h *harness.Harness) (*flexVolumeAttachablePlugin, string) {
    32  	rootDir := h.TempDir("", "flexvolume_test")
    33  	return &flexVolumeAttachablePlugin{
    34  		flexVolumePlugin: &flexVolumePlugin{
    35  			driverName:          "test",
    36  			execPath:            "/plugin",
    37  			host:                volumetesting.NewFakeVolumeHost(h.T, rootDir, nil, nil),
    38  			unsupportedCommands: []string{},
    39  		},
    40  	}, rootDir
    41  }
    42  
    43  func assertDriverCall(t *harness.Harness, output exectesting.FakeAction, expectedCommand string, expectedArgs ...string) exectesting.FakeCommandAction {
    44  	return func(cmd string, args ...string) exec.Cmd {
    45  		executable := "/plugin/test"
    46  		if goruntime.GOOS == "windows" {
    47  			executable = "c:\\plugin\\test"
    48  		}
    49  		if cmd != executable {
    50  			t.Errorf("Wrong executable called: got %v, expected %v", cmd, "/plugin/test")
    51  		}
    52  		if args[0] != expectedCommand {
    53  			t.Errorf("Wrong command called: got %v, expected %v", args[0], expectedCommand)
    54  		}
    55  		cmdArgs := args[1:]
    56  		if !sameArgs(cmdArgs, expectedArgs) {
    57  			t.Errorf("Wrong args for %s: got %v, expected %v", args[0], cmdArgs, expectedArgs)
    58  		}
    59  		return &exectesting.FakeCmd{
    60  			Argv:                 args,
    61  			CombinedOutputScript: []exectesting.FakeAction{output},
    62  		}
    63  	}
    64  }
    65  
    66  func fakeRunner(fakeCommands ...exectesting.FakeCommandAction) exec.Interface {
    67  	return &exectesting.FakeExec{
    68  		CommandScript: fakeCommands,
    69  	}
    70  }
    71  
    72  func fakeResultOutput(result interface{}) exectesting.FakeAction {
    73  	return func() ([]byte, []byte, error) {
    74  		bytes, err := json.Marshal(result)
    75  		if err != nil {
    76  			panic("Unable to marshal result: " + err.Error())
    77  		}
    78  		return bytes, nil, nil
    79  	}
    80  }
    81  
    82  func successOutput() exectesting.FakeAction {
    83  	return fakeResultOutput(&DriverStatus{StatusSuccess, "", "", "", true, nil, 0})
    84  }
    85  
    86  func notSupportedOutput() exectesting.FakeAction {
    87  	return fakeResultOutput(&DriverStatus{StatusNotSupported, "", "", "", false, nil, 0})
    88  }
    89  
    90  func sameArgs(args, expectedArgs []string) bool {
    91  	if len(args) != len(expectedArgs) {
    92  		return false
    93  	}
    94  	for i, v := range args {
    95  		if v != expectedArgs[i] {
    96  			return false
    97  		}
    98  	}
    99  	return true
   100  }
   101  
   102  func fakeVolumeSpec() *volume.Spec {
   103  	vol := &v1.Volume{
   104  		Name: "vol1",
   105  		VolumeSource: v1.VolumeSource{
   106  			FlexVolume: &v1.FlexVolumeSource{
   107  				Driver:   "kubernetes.io/fakeAttacher",
   108  				ReadOnly: false,
   109  			},
   110  		},
   111  	}
   112  	return volume.NewSpecFromVolume(vol)
   113  }
   114  
   115  func specJSON(plugin *flexVolumeAttachablePlugin, spec *volume.Spec, extraOptions map[string]string) string {
   116  	o, err := NewOptionsForDriver(spec, plugin.host, extraOptions)
   117  	if err != nil {
   118  		panic("Failed to convert spec: " + err.Error())
   119  	}
   120  	bytes, err := json.Marshal(o)
   121  	if err != nil {
   122  		panic("Unable to marshal result: " + err.Error())
   123  	}
   124  	return string(bytes)
   125  }
   126  

View as plain text