|
1 | 1 | package supportbundle |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "reflect" |
| 6 | + "strings" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
|
92 | 94 | }) |
93 | 95 | } |
94 | 96 | } |
| 97 | + |
| 98 | +func TestLoadRedactorSpec(t *testing.T) { |
| 99 | + origLoadFromSecret := loadFromSecret |
| 100 | + defer func() { loadFromSecret = origLoadFromSecret }() |
| 101 | + |
| 102 | + loadFromSecret = func(namespace, secretName, key string) ([]byte, error) { |
| 103 | + return []byte(fmt.Sprintf("namespace=%s,secret=%s,key=%s", namespace, secretName, key)), nil |
| 104 | + } |
| 105 | + |
| 106 | + tests := []struct { |
| 107 | + name string |
| 108 | + uri string |
| 109 | + wantContent string |
| 110 | + wantErr string |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "secret URI with default key", |
| 114 | + uri: "secret/default/my-redactor", |
| 115 | + wantContent: "namespace=default,secret=my-redactor,key=redactor-spec", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "secret URI with custom key", |
| 119 | + uri: "secret/default/my-redactor/custom-key", |
| 120 | + wantContent: "namespace=default,secret=my-redactor,key=custom-key", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "secret URI with too few components", |
| 124 | + uri: "secret/default", |
| 125 | + wantErr: "must have at least 3 components", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "secret URI with too many components", |
| 129 | + uri: "secret/default/my-redactor/custom-key/extra", |
| 130 | + wantErr: "must have at most 4 components", |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + for _, tt := range tests { |
| 135 | + t.Run(tt.name, func(t *testing.T) { |
| 136 | + got, err := LoadRedactorSpec(tt.uri) |
| 137 | + if tt.wantErr != "" { |
| 138 | + if err == nil { |
| 139 | + t.Fatalf("LoadRedactorSpec() expected error, got nil") |
| 140 | + } |
| 141 | + if !strings.Contains(err.Error(), tt.wantErr) { |
| 142 | + t.Errorf("LoadRedactorSpec() error = %q, want containing %q", err.Error(), tt.wantErr) |
| 143 | + } |
| 144 | + return |
| 145 | + } |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("LoadRedactorSpec() unexpected error = %v", err) |
| 148 | + } |
| 149 | + if string(got) != tt.wantContent { |
| 150 | + t.Errorf("LoadRedactorSpec() = %q, want %q", string(got), tt.wantContent) |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments