|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors All rights reserved. |
| 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 command |
| 18 | + |
| 19 | +import ( |
| 20 | + "os/exec" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/minikube/pkg/minikube/assets" |
| 24 | +) |
| 25 | + |
| 26 | +func TestFakeRunnerFile(t *testing.T) { |
| 27 | + fakeCommandRunner := NewFakeCommandRunner() |
| 28 | + cmdArg := "test" |
| 29 | + cmdToOutput := make(map[string]string) |
| 30 | + cmdToOutput[cmdArg] = "123" |
| 31 | + fakeCommandRunner.SetCommandToOutput(cmdToOutput) |
| 32 | + |
| 33 | + t.Run("SetGetFileContents", func(t *testing.T) { |
| 34 | + fileToContentsMap := make(map[string]string) |
| 35 | + fileName := "fileName" |
| 36 | + expectedFileContents := "fileContents" |
| 37 | + fileToContentsMap[fileName] = expectedFileContents |
| 38 | + |
| 39 | + fakeCommandRunner.SetFileToContents(fileToContentsMap) |
| 40 | + |
| 41 | + retrievedFileContents, err := fakeCommandRunner.GetFileToContents(fileName) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + |
| 46 | + if expectedFileContents != retrievedFileContents { |
| 47 | + t.Errorf("expected %q, retrieved %q", expectedFileContents, retrievedFileContents) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("CopyRemoveFile", func(t *testing.T) { |
| 52 | + expectedFileContents := "test contents" |
| 53 | + fileName := "memory" |
| 54 | + file := assets.NewMemoryAssetTarget([]byte(expectedFileContents), "", "") |
| 55 | + |
| 56 | + if err := fakeCommandRunner.Copy(file); err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + retrievedFileContents, err := fakeCommandRunner.GetFileToContents(fileName) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + if expectedFileContents != retrievedFileContents { |
| 66 | + t.Errorf("expected %q, retrieved %q", expectedFileContents, retrievedFileContents) |
| 67 | + } |
| 68 | + |
| 69 | + if err := fakeCommandRunner.Remove(file); err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + if _, err := fakeCommandRunner.GetFileToContents(fileName); err == nil { |
| 74 | + t.Errorf("file was not removed") |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("RunCmd", func(t *testing.T) { |
| 79 | + expectedOutput := "123" |
| 80 | + command := &exec.Cmd{Args: []string{cmdArg}} |
| 81 | + |
| 82 | + rr, err := fakeCommandRunner.RunCmd(command) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + retrievedOutput := rr.Stdout.String() |
| 88 | + if expectedOutput != retrievedOutput { |
| 89 | + t.Errorf("expected %q, retrieved %q", expectedOutput, retrievedOutput) |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("StartWaitCmd", func(t *testing.T) { |
| 94 | + expectedOutput := "123" |
| 95 | + command := &exec.Cmd{Args: []string{cmdArg}} |
| 96 | + |
| 97 | + sc, err := fakeCommandRunner.StartCmd(command) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + retrievedOutput := sc.rr.Stdout.String() |
| 103 | + if expectedOutput != retrievedOutput { |
| 104 | + t.Errorf("expected %q, retrieved %q", expectedOutput, retrievedOutput) |
| 105 | + } |
| 106 | + |
| 107 | + rr, err := fakeCommandRunner.WaitCmd(sc) |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + |
| 112 | + retrievedOutput = rr.Stdout.String() |
| 113 | + if expectedOutput != retrievedOutput { |
| 114 | + t.Errorf("expected %q, retrieved %q", expectedOutput, retrievedOutput) |
| 115 | + } |
| 116 | + |
| 117 | + }) |
| 118 | +} |
0 commit comments