-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4aad0cc
commit eaafe5e
Showing
7 changed files
with
159 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of K9s | ||
|
||
package dao_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/derailed/k9s/internal/dao" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncodedSecretDescribe(t *testing.T) { | ||
s := dao.Secret{} | ||
s.Init(makeFactory(), client.NewGVR("v1/secrets")) | ||
|
||
ctx := context.Background() | ||
oo, err := s.List(ctx, "kube-system") | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(oo)) | ||
|
||
encodedString := | ||
` | ||
Name: bootstrap-token-abcdef | ||
Namespace: kube-system | ||
Labels: <none> | ||
Annotations: <none> | ||
Type: generic | ||
Data | ||
==== | ||
token-secret: 24 bytes` | ||
|
||
expected := "\nName: bootstrap-token-abcdef\n" + | ||
"Namespace: kube-system\n" + | ||
"Labels: <none>\n" + | ||
"Annotations: <none>\n" + | ||
"\n" + | ||
"Type: generic\n" + | ||
"\n" + | ||
"Data\n" + | ||
"====\n" + | ||
"token-secret:\t0123456789abcdef" | ||
|
||
decodedDescription, _ := s.Decode(encodedString, "kube-system/bootstrap-token-abcdef") | ||
assert.Equal(t, expected, decodedDescription) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"apiVersion": "v1", | ||
"data": { | ||
"token-secret": "MDEyMzQ1Njc4OWFiY2RlZg==" | ||
}, | ||
"kind": "Secret", | ||
"metadata": { | ||
"creationTimestamp": "2024-01-15T18:19:00Z", | ||
"name": "bootstrap-token-abcdef", | ||
"namespace": "kube-system", | ||
"resourceVersion": "243", | ||
"uid": "6f5695d4-c0f4-4b65-890a-b1115ffd1f3b" | ||
}, | ||
"type": "bootstrap.kubernetes.io/token" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dao_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/derailed/k9s/internal/dao" | ||
"github.com/derailed/k9s/internal/watch" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/informers" | ||
) | ||
|
||
type testFactory struct { | ||
inventory map[string]map[string][]runtime.Object | ||
} | ||
|
||
func makeFactory() dao.Factory { | ||
return &testFactory{ | ||
inventory: map[string]map[string][]runtime.Object{ | ||
"kube-system": { | ||
"v1/secrets": { | ||
load("secret"), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var _ dao.Factory = &testFactory{} | ||
|
||
func (f *testFactory) Client() client.Connection { | ||
return nil | ||
} | ||
func (f *testFactory) Get(gvr, fqn string, wait bool, sel labels.Selector) (runtime.Object, error) { | ||
ns, po := path.Split(fqn) | ||
ns = strings.Trim(ns, "/") | ||
|
||
for _, o := range f.inventory[ns][gvr] { | ||
if o.(*unstructured.Unstructured).GetName() == po { | ||
return o, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
func (f *testFactory) List(gvr, ns string, wait bool, sel labels.Selector) ([]runtime.Object, error) { | ||
return f.inventory[ns][gvr], nil | ||
} | ||
|
||
func (f *testFactory) ForResource(ns, gvr string) (informers.GenericInformer, error) { | ||
return nil, nil | ||
} | ||
func (f *testFactory) CanForResource(ns, gvr string, verbs []string) (informers.GenericInformer, error) { | ||
return nil, nil | ||
} | ||
func (f *testFactory) WaitForCacheSync() {} | ||
func (f *testFactory) Forwarders() watch.Forwarders { | ||
return nil | ||
} | ||
func (f *testFactory) DeleteForwarder(string) {} | ||
|
||
type testResource struct{} | ||
|
||
func load(n string) *unstructured.Unstructured { | ||
raw, _ := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) | ||
|
||
var o unstructured.Unstructured | ||
json.Unmarshal(raw, &o) | ||
|
||
return &o | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters