Skip to content

Commit

Permalink
test: add unit test for local crds
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed May 25, 2023
1 parent 454fc9a commit 8640c1b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/openapiclient/local_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewLocalCRDFiles(fs fs.FS, dirPath string) openapi.Client {
}

func (k *localCRDsClient) Paths() (map[string]openapi.GroupVersion, error) {
if len(k.dir) == 0 && k.fs == nil {
if len(k.dir) == 0 {
return nil, nil
}
files, err := readDir(k.fs, k.dir)
Expand Down
111 changes: 111 additions & 0 deletions pkg/openapiclient/local_crds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package openapiclient

import (
"io/fs"
"os"
"reflect"
"testing"

"k8s.io/client-go/openapi"
)

func TestNewLocalCRDFiles(t *testing.T) {
tests := []struct {
name string
fs fs.FS
dirPath string
want openapi.Client
}{{
name: "fs nil and dir empty",
want: &localCRDsClient{},
}, {
name: "only dir",
dirPath: "test",
want: &localCRDsClient{
dir: "test",
},
}, {
name: "only fs",
fs: os.DirFS("."),
want: &localCRDsClient{
fs: os.DirFS("."),
},
}, {
name: "both fs and dir",
fs: os.DirFS("."),
dirPath: "test",
want: &localCRDsClient{
fs: os.DirFS("."),
dir: "test",
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewLocalCRDFiles(tt.fs, tt.dirPath); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewLocalCRDFiles() = %v, want %v", got, tt.want)
}
})
}
}

func Test_localCRDsClient_Paths(t *testing.T) {
tests := []struct {
name string
fs fs.FS
dir string
want map[string]struct{}
wantErr bool
}{{
name: "fs nil and dir empty",
}, {
name: "only dir",
dir: "../../testcases/crds",
want: map[string]struct{}{
"apis/batch.x-k8s.io/v1alpha1": {},
"apis/stable.example.com/v1": {},
},
}, {
name: "only fs",
fs: os.DirFS("../../testcases/crds"),
}, {
name: "both fs and dir",
fs: os.DirFS("../../testcases"),
dir: "crds",
want: map[string]struct{}{
"apis/batch.x-k8s.io/v1alpha1": {},
"apis/stable.example.com/v1": {},
},
}, {
name: "invalid dir",
dir: "invalid",
wantErr: true,
}, {
name: "invalid fs",
fs: os.DirFS("../../invalid"),
dir: ".",
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := NewLocalCRDFiles(tt.fs, tt.dir)
paths, err := k.Paths()
if (err != nil) != tt.wantErr {
t.Errorf("localCRDsClient.Paths() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.want == nil && paths != nil || tt.want != nil && paths == nil {
t.Errorf("localCRDsClient.Paths() = %v, want %v", paths, tt.want)
return
}
if tt.want != nil && paths != nil {
got := map[string]struct{}{}
for key := range paths {
got[key] = struct{}{}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("localCRDsClient.Paths() = %v, want %v", got, tt.want)
}
}
})
}
}

0 comments on commit 8640c1b

Please sign in to comment.