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 c0345ad
Show file tree
Hide file tree
Showing 3 changed files with 4,613 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
126 changes: 126 additions & 0 deletions pkg/openapiclient/local_crds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package openapiclient

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

"k8s.io/apimachinery/pkg/util/sets"
"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]sets.Set[string]
wantErr bool
}{{
name: "fs nil and dir empty",
}, {
name: "only dir",
dir: "../../testcases/crds",
want: map[string]sets.Set[string]{
"apis/batch.x-k8s.io/v1alpha1": sets.New(
"batch.x-k8s.io/v1alpha1.JobSet",
),
"apis/stable.example.com/v1": sets.New(
"stable.example.com/v1.CELBasic",
),
"apis/cert-manager.io/v1": sets.New(
"cert-manager.io/v1.CertificateRequest",
),
},
}, {
name: "only fs",
fs: os.DirFS("../../testcases/crds"),
}, {
name: "both fs and dir",
fs: os.DirFS("../../testcases"),
dir: "crds",
want: map[string]sets.Set[string]{
"apis/batch.x-k8s.io/v1alpha1": sets.New(
"batch.x-k8s.io/v1alpha1.JobSet",
),
"apis/stable.example.com/v1": sets.New(
"stable.example.com/v1.CELBasic",
),
"apis/cert-manager.io/v1": sets.New(
"cert-manager.io/v1.CertificateRequest",
),
},
}, {
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
}
var got map[string]sets.Set[string]
if paths != nil {
got = map[string]sets.Set[string]{}
for key, value := range paths {
got[key] = sets.New[string]()
for component := range value.(inmemoryGroupVersion).Components.Schemas {
got[key] = got[key].Insert(component)
}
}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("localCRDsClient.Paths() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit c0345ad

Please sign in to comment.