|
| 1 | +/* |
| 2 | +Copyright 2021 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 config |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "k8s.io/minikube/pkg/minikube/out" |
| 25 | +) |
| 26 | + |
| 27 | +func TestAddonsList(t *testing.T) { |
| 28 | + t.Run("NonExistingClusterTable", func(t *testing.T) { |
| 29 | + b := make([]byte, 167) |
| 30 | + r, w, err := os.Pipe() |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("failed to create pipe: %v", err) |
| 33 | + } |
| 34 | + old := os.Stdout |
| 35 | + defer func() { os.Stdout = old }() |
| 36 | + os.Stdout = w |
| 37 | + printAddonsList(nil) |
| 38 | + if err := w.Close(); err != nil { |
| 39 | + t.Fatalf("failed to close pipe: %v", err) |
| 40 | + } |
| 41 | + if _, err := r.Read(b); err != nil { |
| 42 | + t.Fatalf("failed to read bytes: %v", err) |
| 43 | + } |
| 44 | + got := string(b) |
| 45 | + expected := `|-----------------------------|-----------------------| |
| 46 | +| ADDON NAME | MAINTAINER | |
| 47 | +|-----------------------------|-----------------------|` |
| 48 | + if got != expected { |
| 49 | + t.Errorf("Expected header to be: %q; got = %q", expected, got) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("NonExistingClusterJSON", func(t *testing.T) { |
| 54 | + type addons struct { |
| 55 | + Ambassador *interface{} `json:"ambassador"` |
| 56 | + } |
| 57 | + |
| 58 | + b := make([]byte, 534) |
| 59 | + r, w, err := os.Pipe() |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("failed to create pipe: %v", err) |
| 62 | + } |
| 63 | + old := os.Stdout |
| 64 | + defer func() { |
| 65 | + os.Stdout = old |
| 66 | + out.SetOutFile(os.Stdout) |
| 67 | + }() |
| 68 | + os.Stdout = w |
| 69 | + out.SetOutFile(os.Stdout) |
| 70 | + printAddonsJSON(nil) |
| 71 | + if err := w.Close(); err != nil { |
| 72 | + t.Fatalf("failed to close pipe: %v", err) |
| 73 | + } |
| 74 | + if _, err := r.Read(b); err != nil { |
| 75 | + t.Fatalf("failed to read bytes: %v", err) |
| 76 | + } |
| 77 | + got := addons{} |
| 78 | + if err := json.Unmarshal(b, &got); err != nil { |
| 79 | + t.Fatalf("failed to unmarshal output; output: %q; err: %v", string(b), err) |
| 80 | + } |
| 81 | + if got.Ambassador == nil { |
| 82 | + t.Errorf("expected `ambassador` field to not be nil, but was") |
| 83 | + } |
| 84 | + }) |
| 85 | +} |
0 commit comments